11#!/usr/bin/env python
2- import json
3- import os
42import shutil
53import subprocess
4+ import tomllib
5+ import shlex
66
77from termcolor import cprint , colored
88from pathlib import Path
99
10- CONDITIONAL_MANIFEST = "conditional_files.json"
11- REPLACE_MANIFEST = "replaceable_files.json"
10+ CONDITIONAL_MANIFEST = Path ( "conditional_files.toml" )
11+ REPLACE_MANIFEST = Path ( "replaceable_files.toml" )
1212
1313
14- def delete_resource (resource ):
15- if os . path . isfile ( resource ):
16- os . remove ( resource )
17- elif os . path . isdir ( resource ):
14+ def delete_resource (resource : Path ):
15+ if resource . is_file ( ):
16+ resource . unlink ( )
17+ elif resource . is_dir ( ):
1818 shutil .rmtree (resource )
1919
2020
2121def delete_resources_for_disabled_features ():
22- with open (CONDITIONAL_MANIFEST ) as manifest_file :
23- manifest = json .load (manifest_file )
24- for feature_name , feature in manifest .items ():
25- if feature ["enabled" ].lower () != "true" :
22+ with CONDITIONAL_MANIFEST .open ("rb" ) as manifest_file :
23+ manifest = tomllib .load (manifest_file )
24+
25+ for feature in manifest ["features" ]:
26+ enabled = feature ["enabled" ].lower () != "true"
27+ name = feature ["name" ]
28+ resources = feature ["resources" ]
29+ if enabled :
2630 text = "{} resources for disabled feature {}..." .format (
2731 colored ("Removing" , color = "red" ),
28- colored (feature_name , color = "magenta" , attrs = ["underline" ]),
32+ colored (name , color = "magenta" , attrs = ["underline" ]),
2933 )
3034 print (text )
31- for resource in feature [ " resources" ] :
32- delete_resource (resource )
35+ for resource in resources :
36+ delete_resource (Path ( resource ) )
3337 delete_resource (CONDITIONAL_MANIFEST )
3438 cprint ("cleanup complete!" , color = "green" )
3539
@@ -40,14 +44,15 @@ def replace_resources():
4044 colored ("resources" , color = "green" ), colored ("new project" , color = "blue" )
4145 )
4246 )
43- with open (REPLACE_MANIFEST ) as replace_manifest :
44- manifest = json .load (replace_manifest )
45- for target , replaces in manifest .items ():
46- target_path = Path (target )
47- delete_resource (target_path )
48- for src_file in map (Path , replaces ):
47+ with REPLACE_MANIFEST .open ("rb" ) as replace_manifest :
48+ manifest = tomllib .load (replace_manifest )
49+ for substitution in manifest ["sub" ]:
50+ target = Path (substitution ["target" ])
51+ replaces = [Path (path ) for path in substitution ["replaces" ]]
52+ delete_resource (target )
53+ for src_file in replaces :
4954 if src_file .exists ():
50- shutil .move (src_file , target_path )
55+ shutil .move (src_file , target )
5156 delete_resource (REPLACE_MANIFEST )
5257 print (
5358 "Resources are happy to be where {}." .format (
@@ -56,17 +61,41 @@ def replace_resources():
5661 )
5762
5863
64+ def run_cmd (cmd : str , ignore_error : bool = False ):
65+ out = subprocess .run (
66+ shlex .split (cmd ),
67+ stdout = subprocess .PIPE ,
68+ stderr = subprocess .PIPE ,
69+ )
70+ if out .returncode != 0 and not ignore_error :
71+ cprint (" WARNING " .center (50 , "=" ))
72+ cprint (
73+ f"[WARN] Command `{ cmd } ` was not successfull. Check output below." ,
74+ "yellow" ,
75+ )
76+ cprint (
77+ "However, the project was generated. So it could be a false-positive." ,
78+ "yellow" ,
79+ )
80+ cprint (out .stdout .decode (), "red" )
81+ cprint (out .stderr .decode (), "red" )
82+ exit (1 )
83+
84+
5985def init_repo ():
60- subprocess .run (["git" , "init" ], stdout = subprocess .PIPE )
61- cprint ("Git repository initialized." , "green" )
62- subprocess .run (["git" , "add" , "." ], stdout = subprocess .PIPE )
63- cprint ("Added files to index." , "green" )
64- subprocess .run (["uv" , "sync" ])
65- subprocess .run (["uv" , "run" , "pre-commit" , "install" ])
66- cprint ("pre-commit installed." , "green" )
67- subprocess .run (["uv" , "run" , "pre-commit" , "run" , "-a" ])
68- subprocess .run (["git" , "add" , "." ], stdout = subprocess .PIPE )
69- subprocess .run (["git" , "commit" , "-m" , "Initial commit" ], stdout = subprocess .PIPE )
86+ run_cmd ("git init" )
87+ cprint (" Git repository initialized" , "green" )
88+ run_cmd ("git add ." )
89+ cprint ("🐍 Installing python dpendencies with UV" , "green" )
90+ run_cmd ("uv sync" )
91+ run_cmd ("uv run pre-commit install" )
92+ cprint ("📚🖌️📄📏 Tidying up the project" , "green" )
93+ for _ in range (2 ):
94+ run_cmd ("uv run pre-commit run -a" , ignore_error = True )
95+ run_cmd ("git add ." )
96+ cprint ("🚀Creating your first commit" , "green" )
97+ run_cmd ("git commit -m 'Initial commit'" )
98+
7099
71100if __name__ == "__main__" :
72101 delete_resources_for_disabled_features ()
0 commit comments