|
1 | 1 | import os |
| 2 | +import shutil |
2 | 3 |
|
3 | 4 | import click |
4 | 5 |
|
@@ -29,25 +30,24 @@ def _copy_template( |
29 | 30 | dest: str, |
30 | 31 | name: str, |
31 | 32 | ) -> 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) |
51 | 51 |
|
52 | 52 | if __name__ == "__main__": |
53 | 53 | cli() |
0 commit comments