From 5ed85d5b63ae432745d4f6683c0844051ef24841 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Mon, 22 Jan 2024 19:02:42 +0100 Subject: [PATCH] Generate/Check files from option files --- .github/workflows/check.yml | 4 ++ bin/check/generated.sh | 18 +++++++++ bin/spec/options/generate_all.py | 43 +++++++++++++++++++++ docs/spec/options/hurlfmt/standalone.option | 1 - 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 bin/check/generated.sh create mode 100755 bin/spec/options/generate_all.py diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 80134d608..13a6b0e14 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -58,6 +58,10 @@ jobs: if: always() run: bin/check/rust_version.py 7 + - name: Generated + if: always() + run: bin/check/generated.sh + - name: Coverage if: always() run: bin/check/coverage.sh diff --git a/bin/check/generated.sh b/bin/check/generated.sh new file mode 100755 index 000000000..eaec53929 --- /dev/null +++ b/bin/check/generated.sh @@ -0,0 +1,18 @@ +#!/bin/bash +#!/bin/bash +set -Eeuo pipefail + +cmds=( + bin/spec/options/generate_all.py +) +for cmd in "${cmds[@]}"; do + echo "$cmd" + "$cmd" + git diff --exit-code && exit_code=0 || exit_code=$? + if [ "$exit_code" -ne 0 ]; then + git --no-pager diff + echo -e "You must regenerate files by running '$cmd'" + echo "and commit them" + exit 1 + fi +done diff --git a/bin/spec/options/generate_all.py b/bin/spec/options/generate_all.py new file mode 100755 index 000000000..fbdb5a471 --- /dev/null +++ b/bin/spec/options/generate_all.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +from typing import List +import glob +from option import Option +import generate_source + + +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) + open(option_file, "w").write(str(option) + "\n") + + +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) + open(output_file, "w").write(src + "\n") + + +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" + ) + + +if __name__ == "__main__": + main() diff --git a/docs/spec/options/hurlfmt/standalone.option b/docs/spec/options/hurlfmt/standalone.option index 0b9ef7b35..4a4ce662c 100644 --- a/docs/spec/options/hurlfmt/standalone.option +++ b/docs/spec/options/hurlfmt/standalone.option @@ -6,4 +6,3 @@ conflict: no_color Output full html file with css instead of html fragment (default). This can be used only with html output. -