在 coockiecutter 模板中,只有当 choice 变量具有给定值时才添加文件夹
In a coockiecutter template, add folder only if choice variable has a given value
我正在创建一个 cookiecutter 模板,并希望仅当变量具有给定值时才添加一个文件夹(及其包含的文件)。例如 cookiecutter.json:
{
"project_slug":"project_folder"
"i_want_this_folder":['y','n']
}
我的模板结构如下:
template
└── {{ cookiecutter.project_slug }}
├── config.ini
├── data
│ └── data.csv
├── {% if cookiecutter.i_want_this_folder == 'y' %}my_folder{% endif %}
└── some_files
但是,当 运行 cookiecutter template
并选择 'n' 我得到一个错误
Error: "~/project_folder" directory already exists
我的文件夹名称语法正确吗?
我遇到了同样的问题,可以选择添加或不添加具有不同内容的文件夹(所有文件夹可以同时存在)。项目结构如下:
├── {{cookiecutter.project_slug}}
│ │
│ ├── folder_1_to_add_or_no
│ │ ├── file1.py
│ │ ├── file2.py
│ │ └── file3.txt
│ │
│ ├── folder_2_to_add_or_no
│ │ ├── image.png
│ │ ├── data.csv
│ │ └── file.txt
│ │
│ └── folder_3_to_add_or_no
│ ├── file1.py
│ └── some_dir
│
├── hooks
│ └── post_gen_project.py
│
└── cookiecutter.json
其中 cookiecutter.json
包含以下内容
{
"project_owner": "some-name",
"project_slug": "some-project",
"add_folder_one": ["yes", "no"],
"add_folder_two": ["yes", "no"],
"add_folder_three": ["yes", "no"],
}
由于每个目录 folder_X_to_add_or_no
包含不同的文件,诀窍是删除那些答案为“否”的文件夹,您可以通过挂钩来完成。在 post_gen_project.py
文件中
# post_gen_project.py
import os
import shutil
from pathlib import Path
# Current path
path = Path(os.getcwd())
# Source path
parent_path = path.parent.absolute()
def remove(filepath):
if os.path.isfile(filepath):
os.remove(filepath)
elif os.path.isdir(filepath):
shutil.rmtree(filepath)
folders_to_add = [
'folder_one',
'folder_two',
'folder_three'
]
for folder in folders_to_add:
# Check if user wants the folder
cookiecutter_var = '{{cookiecutter.' + f'{folder}' + '}}'
add_folder = cookiecutter_var == 'yes'
# User does not want folder so remove it
if not add_folder:
folder_path = os.path.join(
parent_path,
'{{cookiecutter.project_slug}}',
'folder'
)
remove(folder_path)
现在用户选择不添加的文件夹将被删除。
Select add_folder_one:
1 - yes
2 - no
Choose from 1, 2 [1]:
参考资料
此答案基于 briancapello 对此 github issue
的回答
我正在创建一个 cookiecutter 模板,并希望仅当变量具有给定值时才添加一个文件夹(及其包含的文件)。例如 cookiecutter.json:
{
"project_slug":"project_folder"
"i_want_this_folder":['y','n']
}
我的模板结构如下:
template
└── {{ cookiecutter.project_slug }}
├── config.ini
├── data
│ └── data.csv
├── {% if cookiecutter.i_want_this_folder == 'y' %}my_folder{% endif %}
└── some_files
但是,当 运行 cookiecutter template
并选择 'n' 我得到一个错误
Error: "~/project_folder" directory already exists
我的文件夹名称语法正确吗?
我遇到了同样的问题,可以选择添加或不添加具有不同内容的文件夹(所有文件夹可以同时存在)。项目结构如下:
├── {{cookiecutter.project_slug}}
│ │
│ ├── folder_1_to_add_or_no
│ │ ├── file1.py
│ │ ├── file2.py
│ │ └── file3.txt
│ │
│ ├── folder_2_to_add_or_no
│ │ ├── image.png
│ │ ├── data.csv
│ │ └── file.txt
│ │
│ └── folder_3_to_add_or_no
│ ├── file1.py
│ └── some_dir
│
├── hooks
│ └── post_gen_project.py
│
└── cookiecutter.json
其中 cookiecutter.json
包含以下内容
{
"project_owner": "some-name",
"project_slug": "some-project",
"add_folder_one": ["yes", "no"],
"add_folder_two": ["yes", "no"],
"add_folder_three": ["yes", "no"],
}
由于每个目录 folder_X_to_add_or_no
包含不同的文件,诀窍是删除那些答案为“否”的文件夹,您可以通过挂钩来完成。在 post_gen_project.py
文件中
# post_gen_project.py
import os
import shutil
from pathlib import Path
# Current path
path = Path(os.getcwd())
# Source path
parent_path = path.parent.absolute()
def remove(filepath):
if os.path.isfile(filepath):
os.remove(filepath)
elif os.path.isdir(filepath):
shutil.rmtree(filepath)
folders_to_add = [
'folder_one',
'folder_two',
'folder_three'
]
for folder in folders_to_add:
# Check if user wants the folder
cookiecutter_var = '{{cookiecutter.' + f'{folder}' + '}}'
add_folder = cookiecutter_var == 'yes'
# User does not want folder so remove it
if not add_folder:
folder_path = os.path.join(
parent_path,
'{{cookiecutter.project_slug}}',
'folder'
)
remove(folder_path)
现在用户选择不添加的文件夹将被删除。
Select add_folder_one:
1 - yes
2 - no
Choose from 1, 2 [1]:
参考资料
此答案基于 briancapello 对此 github issue
的回答