|
| 1 | +"""https://github.com/fastapi-mvc/cookiecutter/issues/17""" |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | + |
| 7 | + |
| 8 | +trigger_patch = """\ |
| 9 | +on: |
| 10 | + # This trigger is required by fastapi-mvc automation to dispatch this concrete workflow |
| 11 | + # from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml), |
| 12 | + # and await its result. NOTE! This is not included in the template. |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + distinct_id: |
| 16 | + required: true |
| 17 | + description: "Input required by codex-/return-dispatch@v1"\ |
| 18 | +""" |
| 19 | +echo_patch = """\ |
| 20 | +jobs: |
| 21 | + meta: |
| 22 | + runs-on: ubuntu-latest |
| 23 | +
|
| 24 | + steps: |
| 25 | + # This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow. |
| 26 | + # NOTE! This is not included in the template. |
| 27 | + - name: echo distinct ID ${{ github.event.inputs.distinct_id }} |
| 28 | + run: echo ${{ github.event.inputs.distinct_id }}\ |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +def apply_patch(file): |
| 33 | + if not os.path.exists(file): |
| 34 | + print(f"Path: '{file}' does not exist.") |
| 35 | + sys.exit(1) |
| 36 | + elif not os.path.isfile(file): |
| 37 | + print(f"Path: '{file}' is not a file.") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | + with open(file, "r+") as f: |
| 41 | + content = f.read() |
| 42 | + patched = content.replace("on:", trigger_patch, 1) |
| 43 | + patched = patched.replace("jobs:", echo_patch, 1) |
| 44 | + f.seek(0) |
| 45 | + f.write(patched) |
| 46 | + f.truncate() |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + parser.add_argument( |
| 52 | + "files", |
| 53 | + metavar="FILE", |
| 54 | + type=str, |
| 55 | + nargs="+", |
| 56 | + help="File to apply monkeypatch", |
| 57 | + ) |
| 58 | + args = parser.parse_args() |
| 59 | + |
| 60 | + for file in args.files: |
| 61 | + print(f"Patching: '{file}'") |
| 62 | + apply_patch(file) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments