forked from maltzj/google-style-precommit-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
49 lines (35 loc) · 1.41 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import subprocess
from argparse import ArgumentParser
import requests
DEFAULT_VERSION = "1.18.1"
DOWNLOAD_URL_TEMPLATE = "https://github.com/google/google-java-format/releases/download/v{version}/google-java-format-{version}-all-deps.jar"
def main(version: str, java_files: list[str]):
os.makedirs(".cache", exist_ok=True)
os.chdir(".cache")
if version.startswith("v"):
version = version[1:]
jar_file_path = f"google-java-format-{version}-all-deps.jar"
if not os.path.isfile(jar_file_path):
url = DOWNLOAD_URL_TEMPLATE.format(version=version)
response = requests.get(url)
with open(jar_file_path, "wb") as jar_file:
jar_file.write(response.content)
print(f"Downloaded {url}")
os.chdir("..")
subprocess.run(["java", "-jar", f".cache/{jar_file_path}", "--replace"] + java_files)
def main_with_args():
parser = ArgumentParser(description="Download and run the google java formatter")
parser.add_argument(
"--version",
dest="version",
help=f"The version of the google java formatter. (Defaults to '{DEFAULT_VERSION}')",
type=str,
required=False,
default=DEFAULT_VERSION,
)
parser.add_argument("files", help="The list of filest to check", type=str, nargs="+")
args = parser.parse_args()
exit(main(args.version, args.files))
if __name__ == "__main__":
main_with_args()