diff --git a/Sources/CodeGen/generate.py b/Sources/CodeGen/generate.py index 15a58dde..48018c7d 100644 --- a/Sources/CodeGen/generate.py +++ b/Sources/CodeGen/generate.py @@ -5,8 +5,12 @@ from parse import process_json_files from parsing_types.command import Command -from git_utils import make_sure_remote_repo_is_downloaded -from utils import clean_out_directory, get_todays_date, THIS_DIR +from utils import ( + make_sure_remote_repo_is_downloaded, + clean_out_directory, + get_todays_date, + THIS_DIR, +) from swift_format import format_files from templates import render diff --git a/Sources/CodeGen/git_utils.py b/Sources/CodeGen/git_utils.py deleted file mode 100644 index 5e83879c..00000000 --- a/Sources/CodeGen/git_utils.py +++ /dev/null @@ -1,31 +0,0 @@ -from git.repo import Repo -from git import RemoteProgress -from tqdm import tqdm -from utils import THIS_DIR - - -# https://stackoverflow.com/a/65576165 -class ProgressHandler(RemoteProgress): - def __init__(self, desc): - super().__init__() - self.pbar = tqdm() - self.pbar.set_description_str(desc) - - def update(self, op_code, cur_count, max_count=None, message=""): - self.pbar.total = max_count - self.pbar.n = cur_count - self.pbar.refresh() - - -def make_sure_remote_repo_is_downloaded( - name: str, remote_url: str, branch: str = None -) -> Repo: - repo = Repo.init(f"{THIS_DIR}/{name}", mkdir=True) - try: - remote = repo.remote(name) - except: - remote = repo.create_remote(name, remote_url) - remote.pull( - branch, progress=ProgressHandler(desc=f"Downloading {name} ({remote_url})") - ) - return repo diff --git a/Sources/CodeGen/parsing_types/argument.py b/Sources/CodeGen/parsing_types/argument.py index 0d299ae0..fe07dd5d 100644 --- a/Sources/CodeGen/parsing_types/argument.py +++ b/Sources/CodeGen/parsing_types/argument.py @@ -1,10 +1,8 @@ -import os.path from collections import Counter from typing import List, Dict -from jinja2 import Template from templates import render -from utils import camel_case, snake_case, THIS_DIR +from utils import camel_case, snake_case from config import names_to_substitute, token_to_substitute, arguments_to_ignore ARG_TYPES = { diff --git a/Sources/CodeGen/parsing_types/complex_argument.py b/Sources/CodeGen/parsing_types/complex_argument.py index 214b6860..4d27a324 100644 --- a/Sources/CodeGen/parsing_types/complex_argument.py +++ b/Sources/CodeGen/parsing_types/complex_argument.py @@ -1,11 +1,14 @@ from utils import pascal_case from parsing_types.argument import Argument + class ComplexArgument(Argument): - def __init__(self, parent_name, desc, is_sub_arg = False): + def __init__(self, parent_name, desc, is_sub_arg=False): super().__init__(desc) self.parent_name: str = parent_name - self.type: str = pascal_case(self.name) if is_sub_arg else pascal_case(self.fullname()) + self.type: str = ( + pascal_case(self.name) if is_sub_arg else pascal_case(self.fullname()) + ) def fullname(self): - return f"{self.parent_name} {self.name}" \ No newline at end of file + return f"{self.parent_name} {self.name}" diff --git a/Sources/CodeGen/pyrightconfig.json b/Sources/CodeGen/pyrightconfig.json index 26281ab8..3073861e 100644 --- a/Sources/CodeGen/pyrightconfig.json +++ b/Sources/CodeGen/pyrightconfig.json @@ -1,5 +1,5 @@ { - "exclude": ["**/__pycache__"], + "exclude": ["**/__pycache__", "redis", "swift-format"], "venv": "swifty-redis-codegen-YKd2kwY0-py3.9", "venvPath": "/Users/michaelvanstraten/Library/Caches/pypoetry/virtualenvs" } diff --git a/Sources/CodeGen/swift_format.py b/Sources/CodeGen/swift_format.py index d250203d..42a21cf7 100644 --- a/Sources/CodeGen/swift_format.py +++ b/Sources/CodeGen/swift_format.py @@ -3,7 +3,7 @@ import sys from os.path import exists -from git_utils import make_sure_remote_repo_is_downloaded +from utils import make_sure_remote_repo_is_downloaded from config import formating_config @@ -32,7 +32,7 @@ def make_sure_swift_format_is_compiled(): compile_swift_format() -def compile_swift_format(*files: str): +def compile_swift_format(): print("Compiling swift-format...") try: subprocess.check_call( diff --git a/Sources/CodeGen/utils.py b/Sources/CodeGen/utils.py index c466313f..095908e3 100644 --- a/Sources/CodeGen/utils.py +++ b/Sources/CodeGen/utils.py @@ -1,51 +1,67 @@ -import glob import os -from re import sub +import glob +import re from datetime import date +import click +from git.repo import Repo +from git import RemoteProgress +from tqdm import tqdm +# Get the current directory of the script THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +class ProgressHandler(RemoteProgress): + def __init__(self, desc): + super().__init__() + self.pbar = tqdm() + self.pbar.set_description_str(desc) + + def update(self, _, cur_count, max_count=None): + self.pbar.total = max_count + self.pbar.n = cur_count + self.pbar.refresh() + + +def make_sure_remote_repo_is_downloaded( + name: str, remote_url: str, branch: str = "main" +) -> Repo: + repo_path = os.path.join(THIS_DIR, name) + repo = Repo.init(repo_path, mkdir=True) + try: + remote = repo.remote(name) + except: + remote = repo.create_remote(name, remote_url) + remote.pull( + branch, progress=ProgressHandler(desc=f"Downloading {name} ({remote_url})") + ) + return repo + + def get_optional_desc_string(desc, field, force_uppercase=False): - v = desc.get(field, None) - if v and force_uppercase: - v = v.upper() - ret = '"%s"' % v if v else "NULL" - return ret.replace("\n", "\\n") + value = desc.get(field, None) + if value and force_uppercase: + value = value.upper() + return f'"{value}"' if value else "NULL" def camel_case(s): - s = pascal_case(s) - return "".join([s[0].lower(), s[1:]]) + return pascal_case(s)[0].lower() + pascal_case(s)[1:] def pascal_case(s): - return sub("[\_\-\/\.\:]", " ", s).title().replace(" ", "") + s = re.sub(r"[\_\-\/\.\:]", " ", s) + return "".join(s.title().split()) -# https://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-97.php def snake_case(s): - return "_".join( - sub( - "([A-Z][a-z]+)", - r" \1", - sub( - "([A-Z]+)", - r" \1", - s.replace("-", " ").replace("_", " ").replace(":", " "), - ), - ).split() - ).lower() + s = re.sub(r"[\_\-\/\.\:]", " ", s) + return "_".join(s.split()).lower() def kebab_case(s): - return "-".join( - sub( - "([A-Z][a-z]+)", - r" \1", - sub("([A-Z]+)", r" \1", s.replace("-", " ").replace("_", " ")), - ).split() - ).lower() + s = re.sub(r"[\_\-\/\.\:]", " ", s) + return "-".join(s.split()).lower() def sanitize(s): @@ -53,12 +69,10 @@ def sanitize(s): def clean_out_directory(out_dir): - print("Cleaning out directory...") - if not os.path.exists(out_dir): - os.makedirs(out_dir) - else: - for f in glob.glob(f"{out_dir}/*"): - os.remove(f) + click.echo("Cleaning out directory...") + os.makedirs(out_dir, exist_ok=True) + for file_path in glob.glob(os.path.join(out_dir, "*")): + os.remove(file_path) def get_todays_date(): diff --git a/Sources/SwiftyRedis/CodeGen/Commands/acl.swift b/Sources/SwiftyRedis/CodeGen/Commands/acl.swift index 3e1cc229..5111ed61 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/acl.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/acl.swift @@ -2,7 +2,7 @@ // acl.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/client.swift b/Sources/SwiftyRedis/CodeGen/Commands/client.swift index f85dbd1c..b529cec9 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/client.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/client.swift @@ -2,7 +2,7 @@ // client.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/cluster.swift b/Sources/SwiftyRedis/CodeGen/Commands/cluster.swift index eeb8405a..b8d0124f 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/cluster.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/cluster.swift @@ -2,7 +2,7 @@ // cluster.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/command.swift b/Sources/SwiftyRedis/CodeGen/Commands/command.swift index 5eae6719..3676e4ac 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/command.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/command.swift @@ -2,7 +2,7 @@ // command.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/config.swift b/Sources/SwiftyRedis/CodeGen/Commands/config.swift index db9d570a..4992171a 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/config.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/config.swift @@ -2,7 +2,7 @@ // config.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/containerless.swift b/Sources/SwiftyRedis/CodeGen/Commands/containerless.swift index d2f459f6..57c0ba35 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/containerless.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/containerless.swift @@ -2,7 +2,7 @@ // containerless.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/function.swift b/Sources/SwiftyRedis/CodeGen/Commands/function.swift index dde524a1..050ab76a 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/function.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/function.swift @@ -2,7 +2,7 @@ // function.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/latency.swift b/Sources/SwiftyRedis/CodeGen/Commands/latency.swift index c56ee740..788cc514 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/latency.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/latency.swift @@ -2,7 +2,7 @@ // latency.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/memory.swift b/Sources/SwiftyRedis/CodeGen/Commands/memory.swift index 7770ace3..2dac0e36 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/memory.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/memory.swift @@ -2,7 +2,7 @@ // memory.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/module.swift b/Sources/SwiftyRedis/CodeGen/Commands/module.swift index 8dbe57c7..a6d05052 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/module.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/module.swift @@ -2,7 +2,7 @@ // module.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/object.swift b/Sources/SwiftyRedis/CodeGen/Commands/object.swift index 0349e8a6..3cf69a24 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/object.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/object.swift @@ -2,7 +2,7 @@ // object.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/pubsub.swift b/Sources/SwiftyRedis/CodeGen/Commands/pubsub.swift index 3cc22070..b5cc7db4 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/pubsub.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/pubsub.swift @@ -2,7 +2,7 @@ // pubsub.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/script.swift b/Sources/SwiftyRedis/CodeGen/Commands/script.swift index 1d30ba39..a11dcce9 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/script.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/script.swift @@ -2,7 +2,7 @@ // script.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/sentinel.swift b/Sources/SwiftyRedis/CodeGen/Commands/sentinel.swift index eddc4a7d..fe3d8df8 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/sentinel.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/sentinel.swift @@ -2,7 +2,7 @@ // sentinel.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/slowlog.swift b/Sources/SwiftyRedis/CodeGen/Commands/slowlog.swift index a273adf6..3eb0b3eb 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/slowlog.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/slowlog.swift @@ -2,7 +2,7 @@ // slowlog.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/xgroup.swift b/Sources/SwiftyRedis/CodeGen/Commands/xgroup.swift index 930ea1a1..4c720f1e 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/xgroup.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/xgroup.swift @@ -2,7 +2,7 @@ // xgroup.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection { diff --git a/Sources/SwiftyRedis/CodeGen/Commands/xinfo.swift b/Sources/SwiftyRedis/CodeGen/Commands/xinfo.swift index e0a88a8c..3dd2db09 100644 --- a/Sources/SwiftyRedis/CodeGen/Commands/xinfo.swift +++ b/Sources/SwiftyRedis/CodeGen/Commands/xinfo.swift @@ -2,7 +2,7 @@ // xinfo.swift // // -// Created by CodeGen on 08.09.23. +// Created by CodeGen on 14.09.23. // import Foundation extension RedisConnection {