|
3 | 3 | import os
|
4 | 4 | import sys
|
5 | 5 | import stat
|
| 6 | +import glob |
6 | 7 | import argparse
|
| 8 | +import datetime |
| 9 | + |
7 | 10 |
|
8 | 11 | parser = argparse.ArgumentParser()
|
9 | 12 |
|
10 | 13 | parser.add_argument("--hook", action="store_true")
|
11 | 14 | parser.add_argument("--unhook", action="store_true")
|
12 | 15 | parser.add_argument("--check", action="store_true")
|
13 | 16 | parser.add_argument("--fix", action="store_true")
|
| 17 | +parser.add_argument("--yamato", action="store_true") |
| 18 | + |
| 19 | +parser.add_argument("--tool-path", default="dotnet-format") |
| 20 | +parser.add_argument("--project-path", default="testproject") |
| 21 | +parser.add_argument("--project-glob", default="Unity.Multiplayer.MLAPI*.csproj") |
14 | 22 |
|
15 | 23 | if len(sys.argv) == 1:
|
16 | 24 | parser.print_help(sys.stderr)
|
|
23 | 31 | hook_exec = f"python3 {os.path.basename(sys.argv[0])} --check"
|
24 | 32 |
|
25 | 33 | if args.hook:
|
26 |
| - print("execute hook") |
| 34 | + print("hook: execute") |
27 | 35 |
|
28 | 36 | if os.path.exists(hook_path):
|
29 |
| - exit( |
30 |
| - f"fail: git pre-push hook file already exists: `{hook_path}`\n" |
31 |
| - "please make sure to backup and delete the existing pre-push hook file" |
32 |
| - ) |
| 37 | + print(f"hook: git pre-push hook file already exists: `{hook_path}`") |
| 38 | + print("hook: please make sure to backup and delete the existing pre-push hook file") |
| 39 | + exit("hook: failed") |
33 | 40 |
|
34 |
| - print("write git pre-push hook file contents") |
| 41 | + print("hook: write git pre-push hook file contents") |
35 | 42 | hook_file = open(hook_path, "w")
|
36 | 43 | hook_file.write(f"#!/bin/sh\n\n{hook_exec}\n")
|
37 | 44 | hook_file.close()
|
38 | 45 |
|
39 |
| - print("make git pre-push hook file executable") |
| 46 | + print("hook: make git pre-push hook file executable") |
40 | 47 | hook_stat = os.stat(hook_path)
|
41 | 48 | os.chmod(hook_path, hook_stat.st_mode | stat.S_IEXEC)
|
42 | 49 |
|
43 |
| - print("succeed: git pre-push hook created!") |
| 50 | + print("hook: succeeded") |
44 | 51 |
|
45 | 52 |
|
46 | 53 | if args.unhook:
|
47 |
| - print("execute unhook") |
| 54 | + print("unhook: execute") |
48 | 55 |
|
49 | 56 | hook_path = "./.git/hooks/pre-push"
|
50 | 57 | if os.path.isfile(hook_path):
|
51 |
| - print(f"found `{hook_path}`") |
| 58 | + print(f"unhook: found file -> `{hook_path}`") |
52 | 59 | delete = False
|
53 | 60 | hook_file = open(hook_path, "r")
|
54 | 61 | if hook_exec in hook_file.read():
|
55 | 62 | delete = True
|
56 | 63 | else:
|
57 |
| - exit(f"fail: existing git pre-push hook file was not created by this script") |
| 64 | + print("unhook: existing git pre-push hook file was not created by this script") |
| 65 | + exit("unhook: failed") |
58 | 66 | hook_file.close()
|
59 | 67 | if delete:
|
60 | 68 | os.remove(hook_path)
|
61 |
| - print(f"delete file: `{hook_path}`") |
| 69 | + print(f"unhook: delete file -> `{hook_path}`") |
| 70 | + |
| 71 | + print("unhook: succeeded") |
| 72 | + |
62 | 73 |
|
63 |
| - print("succeed: git pre-push hook removed!") |
| 74 | +if args.check or args.fix or args.yamato: |
| 75 | + glob_match = os.path.join(args.project_path, args.project_glob) |
| 76 | + glob_files = glob.glob(glob_match) |
| 77 | + print(f"glob: found {len(glob_files)} files matching -> {glob_match}") |
| 78 | + |
| 79 | + if len(glob_files) == 0: |
| 80 | + print("glob: no project files found!") |
| 81 | + print("glob: \tdid you forget to generate your solution and project files in Unity?") |
| 82 | + print("glob: \tdid you double-check --project-path and/or --project-glob arguments?") |
| 83 | + exit(f"glob: failed") |
| 84 | + |
| 85 | + any_old = False |
| 86 | + for project_file in glob_files: |
| 87 | + file_stat = os.stat(project_file) |
| 88 | + check_days = 7 |
| 89 | + modified_time = datetime.datetime.fromtimestamp(file_stat.st_mtime) |
| 90 | + days_ago_time = datetime.datetime.now() - datetime.timedelta(days=check_days) |
| 91 | + if modified_time < days_ago_time: |
| 92 | + any_old = True |
| 93 | + print(f"glob: last modified more than {check_days} days ago -> {project_file}") |
| 94 | + if any_old: |
| 95 | + print(f"glob: some project files are not modified for more than {check_days} days ago") |
| 96 | + print("glob: please consider regenerating project files in Unity") |
64 | 97 |
|
65 | 98 |
|
66 | 99 | if args.check:
|
67 |
| - print("execute check") |
| 100 | + print("check: execute") |
| 101 | + |
| 102 | + any_error = False |
| 103 | + for project_file in glob_files: |
| 104 | + print(f"check: project -> {project_file}") |
| 105 | + any_error = 0 != os.system(f"{args.tool_path} {project_file} --fix-whitespace --fix-style error --check") |
68 | 106 |
|
69 |
| - check_exec = os.system("dotnet-format ./testproject/testproject.sln --fix-whitespace --fix-style error --check") |
70 |
| - if check_exec != 0: |
71 |
| - exit(f"check_exec failed, exit code: {check_exec}") |
| 107 | + if any_error: |
| 108 | + exit(f"check: failed") |
| 109 | + |
| 110 | + print("check: succeeded") |
72 | 111 |
|
73 | 112 |
|
74 | 113 | if args.fix:
|
75 |
| - print("execute fix") |
| 114 | + print("fix: execute") |
| 115 | + |
| 116 | + any_error = False |
| 117 | + for project_file in glob_files: |
| 118 | + print(f"fix: project -> {project_file}") |
| 119 | + any_error = 0 != os.system(f"{args.tool_path} {project_file} --fix-whitespace --fix-style error") |
| 120 | + |
| 121 | + if any_error: |
| 122 | + exit(f"fix: failed") |
| 123 | + |
| 124 | + print("fix: succeeded") |
| 125 | + |
| 126 | +if args.yamato: |
| 127 | + print("yamato: execute") |
| 128 | + |
| 129 | + for project_file in glob_files: |
| 130 | + print(f"yamato: project -> {project_file}") |
| 131 | + yamato_exec = os.system(f"{args.tool_path} {project_file} --fix-style error --check") |
| 132 | + if yamato_exec != 0: |
| 133 | + exit(f"yamato: failed, exit code -> {yamato_exec}") |
76 | 134 |
|
77 |
| - fix_exec = os.system("dotnet-format ./testproject/testproject.sln --fix-whitespace --fix-style error") |
78 |
| - if fix_exec != 0: |
79 |
| - exit(f"fix_exec failed, exit code: {fix_exec}") |
| 135 | + print("yamato: succeeded") |
0 commit comments