@@ -149,8 +149,8 @@ def mkdir(path):
149
149
os .makedirs (path , exist_ok = True )
150
150
151
151
152
- def write_file (path , d , zip_path ):
153
- if os .path .exists (path ):
152
+ def write_file (path , d , overwrite , zip_path ):
153
+ if not overwrite and os .path .exists (path ):
154
154
return
155
155
156
156
def replacer (match ):
@@ -169,15 +169,15 @@ def replacer(match):
169
169
f .write (contents % d )
170
170
171
171
172
- def write_dir (path , d , zip_path ):
172
+ def write_dir (path , d , overwrite , zip_path ):
173
173
for entry in zip_path .iterdir ():
174
174
name = entry .name .replace ("__name__" , d ["name" ])
175
175
next_path = os .path .join (path , name )
176
176
if entry .is_file ():
177
- write_file (next_path , d , entry )
177
+ write_file (next_path , d , overwrite , entry )
178
178
elif name != "example" or d ["examples" ]:
179
179
mkdir (next_path )
180
- write_dir (next_path , d , entry )
180
+ write_dir (next_path , d , overwrite , entry )
181
181
182
182
183
183
def git_init (cwd ):
@@ -241,8 +241,10 @@ def create(args):
241
241
"""Create a CMake project according to the provided information
242
242
"""
243
243
path = args .path
244
- root_exists = os .path .exists (path )
245
- if root_exists and os .path .isdir (path ) and len (os .listdir (path )) != 0 :
244
+ if not args .overwrite \
245
+ and os .path .exists (path ) \
246
+ and os .path .isdir (path ) \
247
+ and len (os .listdir (path )) != 0 :
246
248
print (
247
249
f"Error - directory exists and is not empty:\n { path } " ,
248
250
file = sys .stderr ,
@@ -255,8 +257,11 @@ def create(args):
255
257
d = get_substitutes (args , os .path .basename (path ))
256
258
mkdir (path )
257
259
mapping = {"s" : "shared/" , "e" : "executable/" , "h" : "header/" }
258
- write_dir (path , d , zipfile .Path (zip , "templates/" + mapping [d ["type_id" ]]))
259
- write_dir (path , d , zipfile .Path (zip , "templates/common/" ))
260
+ zip_paths = ["templates/" + mapping [d ["type_id" ]], "templates/common/" ]
261
+ if args .overwrite :
262
+ zip_paths .reverse ()
263
+ for zip_path in zip_paths :
264
+ write_dir (path , d , args .overwrite , zipfile .Path (zip , zip_path ))
260
265
git_init (path )
261
266
print_tips (d )
262
267
print ("""\
@@ -287,6 +292,7 @@ def main():
287
292
type = os .path .realpath ,
288
293
help = "path to generate to, the name is also derived from this" ,
289
294
)
295
+ p .set_defaults (overwrite = False )
290
296
create_flags = ["type_id" , "std" , "use_clang_tidy" , "use_cppcheck" ]
291
297
p .set_defaults (** {k : "" for k in create_flags })
292
298
type_g = p .add_mutually_exclusive_group ()
@@ -322,6 +328,11 @@ def main():
322
328
const = "n" ,
323
329
help = "omit the cppcheck preset from the dev preset" ,
324
330
)
331
+ p .add_argument (
332
+ "--overwrite" ,
333
+ action = "store_true" ,
334
+ help = "omit checks for existing files and non-empty project root" ,
335
+ )
325
336
args = p .parse_args ()
326
337
flags_used = any (getattr (args , k ) != "" for k in create_flags )
327
338
setattr (args , "flags_used" , flags_used )
0 commit comments