Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions x.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@

def target(msg: str = None, dependencies: set[str] = frozenset()):
def decorator(func):
def wrapper():
def wrapper(*args, **kwargs):
if func.__name__ in already_run:
return
for dep in dependencies:
dep()
if msg:
print(f"\n>\t{msg}\n")
func()
result = func(*args, **kwargs)
already_run.add(func.__name__)
return result

targets[func.__name__] = wrapper
return wrapper
Expand All @@ -71,21 +72,45 @@ def run(args: list[str], **kwargs):
print(f"Command {e.filename} does not exist!")
sys.exit(1)


def replace_line(path: str, pattern: str, replacement: str):
with open(path, "r", encoding="utf-8") as file:
content = file.read()

new_content, replaced = re.subn(pattern, replacement, content, flags=re.MULTILINE)
if replaced != 1:
print(f"Expected to replace exactly one line in {path}, replaced {replaced} lines instead.")
sys.exit(1)

with open(path, "w", encoding="utf-8") as file:
file.write(new_content)

@target("Generating version from Git")
def version():
ver = json.loads(run(["dotnet-gitversion"], capture_output=True, text=True).stdout)
ver = json.loads(run(["dotnet", "tool", "exec", "GitVersion.Tool"], capture_output=True, text=True).stdout)
if ver['PreReleaseLabel']:
prerelease = f".{ver['PreReleaseLabel']}{ver['PreReleaseNumber']}"
else:
prerelease = ""
python_ver = f"{ver['MajorMinorPatch']}{prerelease}"
with open("../compost_rpc/compost_rpc.py", "r") as file:
content = file.read()
content = re.sub(r'^__version__\s*=.*$', f"__version__ = \"{python_ver}\"", content, flags=re.MULTILINE)
with open("../compost_rpc/compost_rpc.py", "w") as file:
file.write(content)
replace_line("../compost_rpc/compost_rpc.py", r'^__version__\s*=.*$', f'__version__ = "{python_ver}"')
run(["uv", "version", python_ver])
print(f"Detected version {python_ver} from Git repository.")
return python_ver


@target("Creating release commit")
def release(release_type: str):
run(["uv", "version", "--bump", release_type])
new_version = run(["uv", "version", "--short"], capture_output=True, text=True).stdout.strip()

replace_line("../compost_rpc/compost_rpc.py", r'^__version__\s*=.*$', f'__version__ = "{new_version}"')

run(["git", "-C", "..", "add", "compost_rpc/compost_rpc.py", "pyproject.toml"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird on multiple levels - you use -C and give a relative path and then the path is ... Why?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is weird because the script sets the test folder globally as a root. If we extend this script to do more than just tests, it should be changed. Didn't do it yet though, hence this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change it then, but even if we didn't I don't see the reason to do this.

run(["git", "-C", "..", "commit", "-m", f"chore: Release version {new_version}"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the "chore: " prefix

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be removed.

run(["git", "-C", "..", "tag", f"v{new_version}"])

print(f"Created release commit for version {new_version}.")

@target("Generating code")
def codegen():
Expand Down Expand Up @@ -181,13 +206,24 @@ def test_powerpc():


if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="test.py", description="Compost test runner")
parser.add_argument("target", nargs="?", default="test", choices=targets.keys(), help="Target to run")
parser = argparse.ArgumentParser(prog="x.py", description="Compost development script")
subparsers = parser.add_subparsers(dest="target")
parser.set_defaults(target="test")

for target_name in targets:
target_parser = subparsers.add_parser(target_name, help=f"Run '{target_name}' target")
if target_name == "release":
target_parser.add_argument("release_type", choices=("major", "minor", "patch"), help="Release bump type")

args = parser.parse_args()

target_kwargs = {}
if args.target == "release":
target_kwargs["release_type"] = args.release_type

# Change current working directory to the script directory
os.chdir(sys.path[0] + "/test")

targets[args.target]()
targets[args.target](**target_kwargs)

print("Finished successfully")