44Uses several global dependencies (including zig, reuse, and nvchecker).
55"""
66
7- import os
8- import sys
9- import json
107import argparse
8+ import json
119import logging
10+ import os
1211import subprocess
12+ import sys
1313from pathlib import Path
1414
15+ from rich .console import Console
16+ from rich .logging import RichHandler
17+
1518GITHUB_ORG = "zig-devel"
1619INTERNAL_LICENSE = "0BSD"
1720
21+ console = Console ()
22+
23+
24+ def zig (cmd ):
25+ cmd = f"zig { cmd } "
26+ # zig prints all logs to stderr and adds a prefix like `info:` or `error:`
27+ process = subprocess .Popen (
28+ cmd , shell = True , text = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT
29+ )
30+ for line in process .stdout :
31+ if line .startswith ("error: " ):
32+ logging .error (line .strip ().removeprefix ("error: " ))
33+ else :
34+ logging .info (line .strip ().removeprefix ("info: " ))
35+ process .wait ()
36+ if process .returncode != 0 :
37+ logging .critical (f"'{ cmd } ' failed with exit code { process .returncode } " )
38+ exit (1 )
39+
40+
41+ def system (cmd ):
42+ process = subprocess .Popen (
43+ cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True
44+ )
45+ for line in process .stdout :
46+ logging .info (line .strip ())
47+ process .wait ()
48+ if process .returncode != 0 :
49+ logging .critical (f"Failed with exit code { process .returncode } " )
50+ exit (1 )
51+
1852
1953def _WriteFile (filename : str , payload : str ):
2054 """
@@ -43,13 +77,16 @@ def _WriteFile(filename: str, payload: str):
4377
4478
4579def _SetupGitConfigs ():
80+ logging .info ("Generate .gitattributes" )
4681 _WriteFile (
4782 ".gitattributes" ,
4883 """
4984 *.zig text eol=lf
5085 *.zon text eol=lf
5186 """ ,
5287 )
88+
89+ logging .info ("Generate .gitignore" )
5390 _WriteFile (
5491 ".gitignore" ,
5592 """
@@ -60,6 +97,7 @@ def _SetupGitConfigs():
6097
6198
6299def _SetupGithubActions ():
100+ logging .info ("Generate library build workflow" )
63101 _WriteFile (
64102 ".github/workflows/library.yml" ,
65103 f"""
@@ -76,10 +114,11 @@ def _SetupGithubActions():
76114 jobs:
77115 build:
78116 name: Build and test library
79- uses: { GITHUB_ORG } /.github/.github/workflows/library.yml@main
117+ uses: { GITHUB_ORG } /.github/.github/workflows/library.yml@latest
80118 """ ,
81119 )
82120
121+ logging .info ("Generate library release workflow" )
83122 _WriteFile (
84123 ".github/workflows/release.yml" ,
85124 f"""
@@ -92,14 +131,15 @@ def _SetupGithubActions():
92131 jobs:
93132 release:
94133 name: Prepare GitHub release
95- uses: { GITHUB_ORG } /.github/.github/workflows/release.yml@main
134+ uses: { GITHUB_ORG } /.github/.github/workflows/release.yml@latest
96135 permissions:
97136 contents: write
98137 """ ,
99138 )
100139
101140
102141def _SetupAutoUpdate (git : str ):
142+ logging .info ("Generate nvchecker config" )
103143 _WriteFile (
104144 ".nvchecker.toml" ,
105145 f"""
@@ -114,7 +154,10 @@ def _SetupAutoUpdate(git: str):
114154 """ ,
115155 )
116156
117- os .system ("nvchecker -c .nvchecker.toml" )
157+ logging .info ("Fetch latest version" )
158+ system (
159+ "nvchecker -c .nvchecker.toml -l error"
160+ ) # TODO: use python api instead of subprocess
118161 os .rename (".github/newver.json" , ".github/oldver.json" )
119162
120163 git = git .removesuffix (".git" ).removesuffix ("/" )
@@ -123,13 +166,17 @@ def _SetupAutoUpdate(git: str):
123166 manifest = json .load (f )
124167 upstream = manifest ["data" ]["upstream" ]
125168
169+ logging .info (f"Latest version detected: { upstream } " )
126170 return upstream ["version" ], upstream ["revision" ]
127171
128172
129173def _SetupLicenses (project_licenses : list [str ]):
130174 project_licenses = [spdx for spdx in project_licenses if spdx != INTERNAL_LICENSE ]
131175 project_licenses = [INTERNAL_LICENSE ] + project_licenses
132176
177+ logging .info (f"Detected licenses: { project_licenses } " )
178+
179+ logging .info ("Generate reuse config" )
133180 _WriteFile (
134181 "REUSE.toml" ,
135182 f"""
@@ -154,14 +201,24 @@ def _SetupLicenses(project_licenses: list[str]):
154201 """ ,
155202 )
156203
157- os .system ("reuse download --all" )
204+ logging .info ("Download licenses files..." )
205+ system ("reuse download --all" )
158206
159207 return project_licenses
160208
161209
162210def _SetupZigPackage (name : str , version : str , git : str , revision : str ):
163- os .system ("zig init --minimal" )
211+ logging .info ("Init zig package" )
212+ zig ("init --minimal" )
213+
214+ fingerprint = subprocess .check_output (
215+ "cat build.zig.zon | sed -n 's/\\ s*\\ .fingerprint = \\ (.*\\ ),/\\ 1/p'" ,
216+ text = True ,
217+ shell = True ,
218+ ).strip ()
219+ logging .info (f"Detect project fingerprint: { fingerprint } " )
164220
221+ logging .info ("Generate build.zig boilerplate" )
165222 _WriteFile (
166223 "build.zig" ,
167224 f"""
@@ -198,15 +255,7 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
198255 """ ,
199256 )
200257
201- fingerprint = subprocess .check_output (
202- [
203- "bash" ,
204- "-c" ,
205- "cat build.zig.zon | sed -n 's/\\ s*\\ .fingerprint = \\ (.*\\ ),/\\ 1/p'" ,
206- ],
207- text = True ,
208- )
209-
258+ logging .info ("Generate build.zig.zon boilerplate" )
210259 _WriteFile (
211260 "build.zig.zon" ,
212261 f"""
@@ -228,9 +277,7 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
228277 """ ,
229278 )
230279
231- archive_link = f"{ git } /archive/{ revision } .tar.gz"
232- os .system (f"zig fetch --save={ name } { archive_link } " )
233-
280+ logging .info ("Generate tests.zig boilerplate" )
234281 _WriteFile (
235282 "tests.zig" ,
236283 f"""
@@ -245,6 +292,12 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
245292 """ ,
246293 )
247294
295+ git = git .removesuffix (".git" ).removesuffix ("/" )
296+ archive_link = f"{ git } /archive/{ revision } .tar.gz"
297+
298+ logging .info (f"Download upstream sources from { archive_link } " )
299+ zig (f"fetch --save={ name } { archive_link } " )
300+
248301
249302def _Licenselink (spdx : str ):
250303 return f"[{ spdx } ](./LICENSES/{ spdx } .txt)"
@@ -306,31 +359,35 @@ def main(argv):
306359
307360 args = parser .parse_args (argv )
308361
362+ logging .basicConfig (
363+ level = logging .NOTSET ,
364+ format = "%(message)s" ,
365+ datefmt = "[%X]" ,
366+ handlers = [RichHandler ()],
367+ )
309368 if args .verbose :
310369 logging .basicConfig (level = logging .DEBUG )
311370
312- logging .debug (args .license )
313-
314- logging .info ("Init repository" )
315- os .system (f"git init { args .name } " )
371+ console .print ("[bold]Init git repository...[/bold]" )
372+ system (f"git init { args .name } " )
316373 os .chdir (args .name )
317374
318- logging . info ( "Add Git configs" )
375+ console . print ( "[bold]Setup git configs...[/bold] " )
319376 _SetupGitConfigs ()
320377
321- logging . info ( "Add GitHubActions configs " )
378+ console . print ( "[bold]Setup GitHub Actions...[/bold] " )
322379 _SetupGithubActions ()
323380
324- logging . info ( "Add autoupdate configs " )
381+ console . print ( "[bold]Configure nvchecker...[/bold] " )
325382 version , revision = _SetupAutoUpdate (args .git )
326383
327- logging . info ( "Add licenses" )
384+ console . print ( "[bold]Configure licenses...[/bold] " )
328385 licenses = _SetupLicenses (args .license )
329386
330- logging . info ( "Setup zig package" )
387+ console . print ( "[bold]Init zig package...[/bold] " )
331388 _SetupZigPackage (args .name , version , args .git , revision )
332389
333- logging . info ( "Add readme" )
390+ console . print ( "[bold]Generate readme...[/bold] " )
334391 _SetupDocs (args .name , args .description , args .url , version , licenses )
335392
336393
0 commit comments