9
9
from pathlib import Path
10
10
import re
11
11
import tomllib
12
+ from datetime import datetime , timezone
12
13
13
14
from e3 .main import Main
14
15
from e3 .os .process import Run
@@ -46,7 +47,22 @@ def main() -> None:
46
47
default = "pyproject.toml" ,
47
48
help = "Path to a Python project or pyproject.toml file" ,
48
49
)
49
- parser .add_argument ("--dry-run" , action = "store_true" , help = "Do not build the wheel" )
50
+ parser .add_argument (
51
+ "--template" , default = "{major}.{minor}.{patch}" , help = "Version number template"
52
+ )
53
+ parser .add_argument (
54
+ "--dry-run" ,
55
+ action = "store_true" ,
56
+ help = "Do not change the version file nor build the wheel" ,
57
+ )
58
+ parser .add_argument (
59
+ "--no-build" , action = "store_true" , help = "Do not build the wheel"
60
+ )
61
+ parser .add_argument (
62
+ "--no-restore" ,
63
+ action = "store_true" ,
64
+ help = "Keep the modified version file with the computed build version" ,
65
+ )
50
66
51
67
main .parse_args ()
52
68
assert main .args
@@ -131,7 +147,17 @@ def main() -> None:
131
147
["git" , "rev-list" , f"{ previous_commit_sha } ..HEAD" , "--count" ],
132
148
cwd = root_dir ,
133
149
)
134
- build_version = f"{ version_major } .{ version_minor } .{ output .strip ()} "
150
+
151
+ # Get the build version from custom version template
152
+ date = datetime .now (tz = timezone .utc )
153
+ build_version = main .args .template .format (
154
+ major = version_major ,
155
+ minor = version_minor ,
156
+ patch = output .strip (),
157
+ year = f"{ date .year :04} " ,
158
+ month = f"{ date .month :02} " ,
159
+ day = f"{ date .day :02} " ,
160
+ )
135
161
logger .info (f"{ version_major } .{ version_minor } -> { build_version } " )
136
162
137
163
if not main .args .dry_run :
@@ -141,24 +167,26 @@ def main() -> None:
141
167
142
168
try :
143
169
# Build the wheel
144
- run (
145
- [
146
- sys .executable ,
147
- "-m" ,
148
- "pip" ,
149
- "wheel" ,
150
- "." ,
151
- "-q" ,
152
- "--no-deps" ,
153
- "-C--python-tag=py3" ,
154
- "-w" ,
155
- "build" ,
156
- ],
157
- cwd = root_dir ,
158
- )
170
+ if not main .args .no_build :
171
+ run (
172
+ [
173
+ sys .executable ,
174
+ "-m" ,
175
+ "pip" ,
176
+ "wheel" ,
177
+ "." ,
178
+ "-q" ,
179
+ "--no-deps" ,
180
+ "-C--python-tag=py3" ,
181
+ "-w" ,
182
+ "build" ,
183
+ ],
184
+ cwd = root_dir ,
185
+ )
159
186
finally :
160
187
# Revert change to version file
161
- run (["git" , "restore" , version_path ], cwd = root_dir , fail_ok = True )
188
+ if not main .args .no_restore :
189
+ run (["git" , "restore" , version_path ], cwd = root_dir , fail_ok = True )
162
190
163
191
164
192
if __name__ == "__main__" :
0 commit comments