Skip to content

Commit 1ce08e5

Browse files
committed
Add an overwrite switch
1 parent 44eb5e2 commit 1ce08e5

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

cmake-init/__main__.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def mkdir(path):
149149
os.makedirs(path, exist_ok=True)
150150

151151

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):
154154
return
155155

156156
def replacer(match):
@@ -169,15 +169,15 @@ def replacer(match):
169169
f.write(contents % d)
170170

171171

172-
def write_dir(path, d, zip_path):
172+
def write_dir(path, d, overwrite, zip_path):
173173
for entry in zip_path.iterdir():
174174
name = entry.name.replace("__name__", d["name"])
175175
next_path = os.path.join(path, name)
176176
if entry.is_file():
177-
write_file(next_path, d, entry)
177+
write_file(next_path, d, overwrite, entry)
178178
elif name != "example" or d["examples"]:
179179
mkdir(next_path)
180-
write_dir(next_path, d, entry)
180+
write_dir(next_path, d, overwrite, entry)
181181

182182

183183
def git_init(cwd):
@@ -241,8 +241,10 @@ def create(args):
241241
"""Create a CMake project according to the provided information
242242
"""
243243
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:
246248
print(
247249
f"Error - directory exists and is not empty:\n{path}",
248250
file=sys.stderr,
@@ -255,8 +257,11 @@ def create(args):
255257
d = get_substitutes(args, os.path.basename(path))
256258
mkdir(path)
257259
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))
260265
git_init(path)
261266
print_tips(d)
262267
print("""\
@@ -287,6 +292,7 @@ def main():
287292
type=os.path.realpath,
288293
help="path to generate to, the name is also derived from this",
289294
)
295+
p.set_defaults(overwrite=False)
290296
create_flags = ["type_id", "std", "use_clang_tidy", "use_cppcheck"]
291297
p.set_defaults(**{k: "" for k in create_flags})
292298
type_g = p.add_mutually_exclusive_group()
@@ -322,6 +328,11 @@ def main():
322328
const="n",
323329
help="omit the cppcheck preset from the dev preset",
324330
)
331+
p.add_argument(
332+
"--overwrite",
333+
action="store_true",
334+
help="omit checks for existing files and non-empty project root",
335+
)
325336
args = p.parse_args()
326337
flags_used = any(getattr(args, k) != "" for k in create_flags)
327338
setattr(args, "flags_used", flags_used)

0 commit comments

Comments
 (0)