forked from artynova/hexdummy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnoxfile.py
171 lines (119 loc) · 3.49 KB
/
noxfile.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import platform
import shutil
import stat
from pathlib import Path
from typing import Any
import nox
CI_GIT_CONFIGS = {
"user.name": "GitHub Actions",
"user.email": "41898282+github-actions[bot]@users.noreply.github.com",
"init.defaultBranch": "main",
}
MAPPINGS_NAMES = [
"mojmap",
"yarn",
]
JAVA_HOME_FILE = Path(".java_home.env")
CTT_DIR = Path(".ctt")
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True
nox.options.stop_on_first_error = True
nox.options.sessions = [
"ctt",
"setup",
"gradle_build",
"gradle_genSources",
"hexdoc",
]
def parametrize_output_dir():
return nox.parametrize(
"output_dir",
[CTT_DIR / name for name in MAPPINGS_NAMES],
ids=MAPPINGS_NAMES,
)
# setup
@nox.session(tags=["setup", "mojmap", "yarn"])
def ctt(session: nox.Session):
session.install("copier-template-tester")
for git_dir in CTT_DIR.glob("*/.git"):
try_rmtree(session, git_dir)
session.run("ctt", silent=not is_ci())
@nox.session(tags=["setup"])
@parametrize_output_dir()
def setup(session: nox.Session, output_dir: Path):
session.chdir(output_dir)
session.install("copier")
if is_ci():
for key, value in CI_GIT_CONFIGS.items():
session.run("git", "config", "--global", key, value, external=True)
session.run("git", "init", external=True)
session.run(
"git",
"commit",
"--allow-empty",
"-m",
"Initial commit",
external=True,
)
session.run(
"copier",
"copy",
"gh:hexdoc-dev/hexdoc-hexcasting-template",
".",
"--answers-file",
".hexdoc-template-inputs.yml",
"--skip",
".gitignore",
"--defaults",
"--overwrite",
)
# build
@nox.session(tags=["build"], python=False)
@parametrize_output_dir()
def gradle_build(session: nox.Session, output_dir: Path):
env = gradle_env()
session.chdir(output_dir)
session.run(*gradle(), "build", external=True, env=env)
@nox.session(tags=["build"])
@parametrize_output_dir()
def hexdoc(session: nox.Session, output_dir: Path):
session.chdir(output_dir)
session.install(".")
session.run("hexdoc", "build")
session.run("hexdoc", "merge")
# genSources
@nox.session(tags=["genSources"], python=False)
@parametrize_output_dir()
def gradle_genSources(session: nox.Session, output_dir: Path):
env = gradle_env()
session.chdir(output_dir)
session.run(*gradle(), "genSources", external=True, env=env)
# other sessions
@nox.session(python=False)
def clean(session: nox.Session):
try_rmtree(session, CTT_DIR)
# helper functions
def try_rmtree(session: nox.Session, path: Path):
if not path.is_dir():
return
session.log(f"Removing directory: {path}")
shutil.rmtree(path, onerror=on_rm_error)
def on_rm_error(func: Any, path: str, exc_info: Any):
# from: https://stackoverflow.com/questions/4829043/how-to-remove-read-only-attrib-directory-with-python-in-windows
path_ = Path(path)
path_.chmod(stat.S_IWRITE)
path_.unlink()
def is_ci():
return os.getenv("CI") == "true"
def gradle() -> list[str]:
match platform.system():
case "Windows":
return [".\\gradlew.bat"]
case _:
return ["sh", "./gradlew"]
def gradle_env():
env = dict[str, str]()
if JAVA_HOME_FILE.is_file():
env["JAVA_HOME"] = JAVA_HOME_FILE.read_text("utf-8").strip()
return env