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+ def zig (cmd ):
24+ cmd = f"zig { cmd } "
25+ # zig prints all logs to stderr and adds a prefix like `info:` or `error:`
26+ process = subprocess .Popen (cmd , shell = True , text = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
27+ for line in process .stdout :
28+ if line .startswith ("error: " ):
29+ logging .error (line .strip ().removeprefix ('error: ' ))
30+ else :
31+ logging .info (line .strip ().removeprefix ('info: ' ))
32+ process .wait ()
33+ if process .returncode != 0 :
34+ logging .critical (f"'{ cmd } ' failed with exit code { process .returncode } " )
35+ exit (1 )
36+
37+ def system (cmd ):
38+ process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True )
39+ for line in process .stdout :
40+ logging .info (line .strip ())
41+ process .wait ()
42+ if process .returncode != 0 :
43+ logging .critical (f"Failed with exit code { process .returncode } " )
44+ exit (1 )
1845
1946def _WriteFile (filename : str , payload : str ):
2047 """
@@ -43,13 +70,16 @@ def _WriteFile(filename: str, payload: str):
4370
4471
4572def _SetupGitConfigs ():
73+ logging .info ("Generate .gitattributes" )
4674 _WriteFile (
4775 ".gitattributes" ,
4876 """
4977 *.zig text eol=lf
5078 *.zon text eol=lf
5179 """ ,
5280 )
81+
82+ logging .info ("Generate .gitignore" )
5383 _WriteFile (
5484 ".gitignore" ,
5585 """
@@ -60,6 +90,7 @@ def _SetupGitConfigs():
6090
6191
6292def _SetupGithubActions ():
93+ logging .info ("Generate library build workflow" )
6394 _WriteFile (
6495 ".github/workflows/library.yml" ,
6596 f"""
@@ -76,10 +107,11 @@ def _SetupGithubActions():
76107 jobs:
77108 build:
78109 name: Build and test library
79- uses: { GITHUB_ORG } /.github/.github/workflows/library.yml@main
110+ uses: { GITHUB_ORG } /.github/.github/workflows/library.yml@latest
80111 """ ,
81112 )
82113
114+ logging .info ("Generate library release workflow" )
83115 _WriteFile (
84116 ".github/workflows/release.yml" ,
85117 f"""
@@ -92,14 +124,15 @@ def _SetupGithubActions():
92124 jobs:
93125 release:
94126 name: Prepare GitHub release
95- uses: { GITHUB_ORG } /.github/.github/workflows/release.yml@main
127+ uses: { GITHUB_ORG } /.github/.github/workflows/release.yml@latest
96128 permissions:
97129 contents: write
98130 """ ,
99131 )
100132
101133
102134def _SetupAutoUpdate (git : str ):
135+ logging .info ("Generate nvchecker config" )
103136 _WriteFile (
104137 ".nvchecker.toml" ,
105138 f"""
@@ -114,7 +147,8 @@ def _SetupAutoUpdate(git: str):
114147 """ ,
115148 )
116149
117- os .system ("nvchecker -c .nvchecker.toml" )
150+ logging .info ("Fetch latest version" )
151+ system ("nvchecker -c .nvchecker.toml -l error" ) # TODO: use python api instead of subprocess
118152 os .rename (".github/newver.json" , ".github/oldver.json" )
119153
120154 git = git .removesuffix (".git" ).removesuffix ("/" )
@@ -123,13 +157,17 @@ def _SetupAutoUpdate(git: str):
123157 manifest = json .load (f )
124158 upstream = manifest ["data" ]["upstream" ]
125159
160+ logging .info (f"Latest version detected: { upstream } " )
126161 return upstream ["version" ], upstream ["revision" ]
127162
128163
129164def _SetupLicenses (project_licenses : list [str ]):
130165 project_licenses = [spdx for spdx in project_licenses if spdx != INTERNAL_LICENSE ]
131166 project_licenses = [INTERNAL_LICENSE ] + project_licenses
132167
168+ logging .info (f"Detected licenses: { project_licenses } " )
169+
170+ logging .info ("Generate reuse config" )
133171 _WriteFile (
134172 "REUSE.toml" ,
135173 f"""
@@ -154,14 +192,23 @@ def _SetupLicenses(project_licenses: list[str]):
154192 """ ,
155193 )
156194
157- os .system ("reuse download --all" )
195+ logging .info ("Download licenses files..." )
196+ system ("reuse download --all" )
158197
159198 return project_licenses
160199
161200
162201def _SetupZigPackage (name : str , version : str , git : str , revision : str ):
163- os .system ("zig init --minimal" )
202+ logging .info ("Init zig package" )
203+ zig ("init --minimal" )
204+
205+ fingerprint = subprocess .check_output (
206+ "cat build.zig.zon | sed -n 's/\\ s*\\ .fingerprint = \\ (.*\\ ),/\\ 1/p'" ,
207+ text = True , shell = True
208+ ).strip ()
209+ logging .info (f"Detect project fingerprint: { fingerprint } " )
164210
211+ logging .info ("Generate build.zig boilerplate" )
165212 _WriteFile (
166213 "build.zig" ,
167214 f"""
@@ -198,15 +245,7 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
198245 """ ,
199246 )
200247
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-
248+ logging .info ("Generate build.zig.zon boilerplate" )
210249 _WriteFile (
211250 "build.zig.zon" ,
212251 f"""
@@ -228,9 +267,7 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
228267 """ ,
229268 )
230269
231- archive_link = f"{ git } /archive/{ revision } .tar.gz"
232- os .system (f"zig fetch --save={ name } { archive_link } " )
233-
270+ logging .info ("Generate tests.zig boilerplate" )
234271 _WriteFile (
235272 "tests.zig" ,
236273 f"""
@@ -245,6 +282,12 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
245282 """ ,
246283 )
247284
285+ git = git .removesuffix (".git" ).removesuffix ("/" )
286+ archive_link = f"{ git } /archive/{ revision } .tar.gz"
287+
288+ logging .info (f"Download upstream sources from { archive_link } " )
289+ zig (f"fetch --save={ name } { archive_link } " )
290+
248291
249292def _Licenselink (spdx : str ):
250293 return f"[{ spdx } ](./LICENSES/{ spdx } .txt)"
@@ -306,31 +349,30 @@ def main(argv):
306349
307350 args = parser .parse_args (argv )
308351
352+ logging .basicConfig (level = logging .NOTSET , format = "%(message)s" , datefmt = "[%X]" , handlers = [RichHandler ()])
309353 if args .verbose :
310354 logging .basicConfig (level = logging .DEBUG )
311355
312- logging .debug (args .license )
313-
314- logging .info ("Init repository" )
315- os .system (f"git init { args .name } " )
356+ console .print ("[bold]Init git repository...[/bold]" )
357+ system (f"git init { args .name } " )
316358 os .chdir (args .name )
317359
318- logging . info ( "Add Git configs" )
360+ console . print ( "[bold]Setup git configs...[/bold] " )
319361 _SetupGitConfigs ()
320362
321- logging . info ( "Add GitHubActions configs " )
363+ console . print ( "[bold]Setup GitHub Actions...[/bold] " )
322364 _SetupGithubActions ()
323365
324- logging . info ( "Add autoupdate configs " )
366+ console . print ( "[bold]Configure nvchecker...[/bold] " )
325367 version , revision = _SetupAutoUpdate (args .git )
326368
327- logging . info ( "Add licenses" )
369+ console . print ( "[bold]Configure licenses...[/bold] " )
328370 licenses = _SetupLicenses (args .license )
329371
330- logging . info ( "Setup zig package" )
372+ console . print ( "[bold]Init zig package...[/bold] " )
331373 _SetupZigPackage (args .name , version , args .git , revision )
332374
333- logging . info ( "Add readme" )
375+ console . print ( "[bold]Generate readme...[/bold] " )
334376 _SetupDocs (args .name , args .description , args .url , version , licenses )
335377
336378
0 commit comments