Added format scripts and rules to zbuild.

This commit is contained in:
Filipe Rodrigues 2023-12-08 21:26:31 +00:00
parent 9d77097082
commit 90b83e334f
5 changed files with 133 additions and 1 deletions

10
.prettierignore Normal file
View File

@ -0,0 +1,10 @@
# TODO: Remove this from here.
# We have a `.gitignore` with `*` inside `build.bak`
# and `resources`, but prettier seems to ignore those and
# only cares about any top-level `.gitignore` and `.pretiterignore`.
build.bak/
resources/
tools/target/
tools/__pycache__
rust/target/
remake-rust/target/

View File

@ -89,6 +89,7 @@
"Rosemon",
"rposition",
"Sakuyamon",
"scandir",
"seeked",
"seps",
"seqs",
@ -105,6 +106,7 @@
"Stingmon",
"submode",
"Taomon",
"taplo",
"thiserror",
"tims",
"unrlen",

56
tools/format_all.py Normal file
View File

@ -0,0 +1,56 @@
#!/bin/env python3
"""
Generates dependencies for the format rule.
"""
# Imports
import argparse
import subprocess
from typing import Iterator
def get_dependencies() -> Iterator[str]:
"Gets all dependencies"
exts = [".toml", ".yaml", ".json", ".py"]
deps = subprocess.check_output([
"git",
"ls-tree",
"-r",
"--name-only",
"HEAD"
])
deps = deps.decode("utf-8")
deps = map(str.strip, deps.split("\n"))
deps = filter(lambda dep: len(dep) != 0, deps)
deps = filter(lambda dep: any(dep.endswith(ext) for ext in exts), deps)
return deps
def main(_args):
"""
Main function
"""
# Get all dependencies and partition them
deps = list(get_dependencies())
taplo_deps = [dep for dep in deps if dep.endswith(".toml")]
prettier_deps = [dep for dep in deps if dep.endswith(".yaml") or dep.endswith(".json")]
autopep8_deps = [dep for dep in deps if dep.endswith(".py")]
# Then format all
# TODO: Do these in parallel?
print(f"Taplo ({len(taplo_deps)} files)")
subprocess.run(["taplo", "format"] + taplo_deps, check=True)
print(f"Prettier ({len(prettier_deps)} files)")
subprocess.run(["prettier", "--write"] + prettier_deps, check=True)
print(f"Autopep8 ({len(autopep8_deps)} files)")
subprocess.run(["autopep8", "--in-place"] + autopep8_deps, check=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
args = parser.parse_args()
main(args)

View File

@ -0,0 +1,30 @@
#!/bin/env python3
"""
Generates dependencies for the format rule.
"""
# Imports
import argparse
import format_all
def main(args):
"""
Main function
"""
# Build the dependencies
deps = format_all.get_dependencies()
deps = ' '.join(deps)
# Then write them
with open(args.deps_file, "w", encoding="utf-8") as deps_file:
deps_file.write(f"{args.format_output}: {deps}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--format-output", dest="format_output", type=str, required=True)
parser.add_argument("--deps-file", dest="deps_file", type=str, required=True)
args = parser.parse_args()
main(args)

View File

@ -1,4 +1,6 @@
---
# TODO: When running a python script, get all the dependencies, since we do a few imports around.
alias:
# External tools
# prettier-ignore
@ -21,11 +23,13 @@ alias:
# Other tools
generate_psx_iso_deps : tools/generate_psx_iso_deps.py
generate_compare_deps : tools/generate_compare_deps.py
generate_format_deps : tools/generate_format_deps.py
mkraw_exe_from_toml : tools/mkraw_exe_from_toml.py
mkraw_exe_from_toml_deps: tools/mkraw_exe_from_toml_deps.py
ld_from_toml : tools/ld_from_toml.py
ld_from_toml_deps : tools/ld_from_toml_deps.py
process_path : tools/process_path.py
format_all : tools/format_all.py
# Cargo tools
mkpsexe : $(build_rust_tool_dir)/mkpsexe
@ -72,6 +76,10 @@ alias:
compare_ok : $(build_dir)/compare.ok
compare_deps : $(build_dir)/compare.d
# Format
format_ok : $(build_dir)/format.ok
format_deps: $(build_dir)/format.d
# Rust tool
rust_tool : $(build_rust_tool_dir)/^(name::non_empty)
rust_tool_deps: $(build_rust_tool_dir)/^(name).d
@ -154,10 +162,36 @@ default:
- $(dw2003_remake_psx_iso)
rules:
# Format
format:
out: [$(format_ok)]
deps: [$(format_all), deps_file: $(format_deps)]
exec:
- - $(python3)
- $(format_all)
# Format dependencies
format-deps:
out: [$(format_deps)]
deps:
- $(format_all) # Note: `generate_format_deps` depends on this
- $(generate_format_deps)
- static: $(format_deps::dir_name)/
exec:
- - $(python3)
- $(generate_format_deps)
- --format-output
- $(format_ok)
- --deps-file
- $(format_deps)
# Compare
compare:
out: [$(compare_ok)]
deps: [$(compare_checksums), deps_file: $(compare_deps)]
deps:
- $(compare_checksums)
- deps_file: $(compare_deps)
- static: $(compare_ok::dir_name)/
exec:
- [$(sha256sum), --check, --quiet, $(compare_checksums)]
- [$(touch), $(compare_ok)]