hurl/bin/spec/options/generate_all.py

72 lines
2.2 KiB
Python
Raw Normal View History

2024-01-22 21:02:42 +03:00
#!/usr/bin/env python3
from typing import List
import glob
from option import Option
import generate_source
2024-01-24 10:28:38 +03:00
import generate_man
import sys
import re
2024-01-22 21:02:42 +03:00
def get_option_files(dir) -> List[str]:
return sorted(glob.glob(dir + "/*.option"))
def format_option_file(option_files: List[str]):
for option_file in option_files:
option = Option.parse_file(option_file)
2024-01-24 10:28:38 +03:00
sys.stderr.write("Format " + option_file + "\n")
open(option_file, "w").write(str(option) + "\n")
2024-01-22 21:02:42 +03:00
def generate_source_file(option_files: List[str], output_file: str):
options = sorted(
[Option.parse_file(option_file) for option_file in option_files],
key=lambda option: option.name,
)
src = generate_source.generate_source(options)
2024-01-24 10:28:38 +03:00
sys.stderr.write("Generate " + output_file + "\n")
2024-01-22 21:02:42 +03:00
open(output_file, "w").write(src + "\n")
2024-01-24 10:28:38 +03:00
def update_man(option_files: List[str], output_file):
sys.stderr.write("Update " + output_file + "\n")
options = sorted(
[Option.parse_file(option_file) for option_file in option_files],
key=lambda option: option.long,
)
current_man = open(output_file).read()
result = re.search(
r"## OPTIONS[^#]*(###.*)### -h, --help", current_man, re.MULTILINE | re.DOTALL
)
if result is None:
raise Exception("Options can not been found in current man " + output_file)
existing_options_str = result.group(1)
new_options_str = generate_man.generate_man(options)
new_man = current_man.replace(existing_options_str, new_options_str)
open(output_file, "w").write(new_man)
2024-01-22 21:02:42 +03:00
def main():
option_files_hurl = get_option_files("docs/spec/options/hurl")
option_files_hurlfmt = get_option_files("docs/spec/options/hurlfmt")
# Format option files
format_option_file(option_files_hurl)
format_option_file(option_files_hurlfmt)
# Generate Source files
generate_source_file(option_files_hurl, "packages/hurl/src/cli/options/commands.rs")
generate_source_file(
option_files_hurlfmt, "packages/hurlfmt/src/cli/options/commands.rs"
)
2024-01-24 10:28:38 +03:00
# Update Man
update_man(option_files_hurl, "docs/manual/hurl.md")
2024-02-01 11:18:21 +03:00
update_man(option_files_hurlfmt, "docs/manual/hurlfmt.md")
2024-01-24 10:28:38 +03:00
2024-01-22 21:02:42 +03:00
if __name__ == "__main__":
main()