Skip to content

Commit 2f0943a

Browse files
committed
fix: refactor template copying to use shutil and replace placeholders in files and directories
1 parent 4a2e00e commit 2f0943a

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

adf_core_python/cli/cli.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import shutil
23

34
import click
45

@@ -29,25 +30,24 @@ def _copy_template(
2930
dest: str,
3031
name: str,
3132
) -> None:
32-
if os.path.isdir(src):
33-
if not os.path.exists(dest):
34-
os.makedirs(dest)
35-
for item in os.listdir(src):
36-
s = os.path.join(src, item)
37-
d = os.path.join(
38-
dest,
39-
item.replace(NAME_PLACEHOLDER, name),
40-
)
41-
_copy_template(s, d, name)
42-
else:
43-
with open(src, "r") as f:
44-
content = f.read()
45-
with open(dest, "w") as f:
46-
f.write(content.replace(NAME_PLACEHOLDER, name))
47-
new_dest = dest.replace(NAME_PLACEHOLDER, name)
48-
if new_dest != dest:
49-
os.rename(dest, new_dest)
50-
33+
dest = os.path.join(dest, name)
34+
shutil.copytree(src, dest)
35+
36+
# dest以下のファイル内のNAME_PLACEHOLDERをnameに置換
37+
for root, dirs, files in os.walk(dest):
38+
for file in files:
39+
file_path = os.path.join(root, file)
40+
with open(file_path, "r") as f:
41+
content = f.read()
42+
with open(file_path, "w") as f:
43+
f.write(content.replace(NAME_PLACEHOLDER, name))
44+
45+
# ディレクトリ名のNAME_PLACEHOLDERをnameに置換
46+
for root, dirs, files in os.walk(dest):
47+
for dir in dirs:
48+
dir_path = os.path.join(root, dir)
49+
new_dir_path = dir_path.replace(NAME_PLACEHOLDER, name)
50+
os.rename(dir_path, new_dir_path)
5151

5252
if __name__ == "__main__":
5353
cli()

0 commit comments

Comments
 (0)