2

我想为我们的内部项目创建一个模板。

项目的布局包含可变数量的子文件夹。

我想在一些配置文件中指定要创建的子文件夹。例如,我有以下文件夹结构:

my_project
|
|___ Tables
|  |
|  |__ Table1    <--- The folders I would like to create
|  |__ Table2    <---|
|  |__ Table3    <---|
|
|___ Other_folders

配置文件应该是这样的:

{
  "prj_name": "my_project",
  "tables": ["Table1", "Table2", "Table3"]
}

我试图创建以下结构:

{{cookiecutter.prj_name}}
|
|___ Tables
|  |
|  |__ {% for table in cookiecutter.tables %}{{table}}{% endfor %}
|
|___ Other_folders

并添加到cookiecutter.json上述配置中。

我使用--no-input. 结果只有一个子文件夹(表 1)。

还尝试使用以下文件夹名称:{% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %} 使用以下配置:

{
  "prj_name": "my_project",
  "tables": {
    "table1": "table1",
    "table2": "table2",
    "table3": "table3"
  }
}

输出仍然是一个子文件夹(Table2Table1Table3)

知道如何实现所需的结构吗?

4

2 回答 2

4

好吧,最终我编写了一个 python 脚本,它使用Cookiecutter 的包函数和两个模板文件夹。

一个用于主项目的模板文件夹,一个用于表文件夹的模板文件夹。

并且脚本将使用cookiecutter.main.cookiecutter函数来创建主项目,并循环创建子文件夹。

我在博客中写了一篇更详细的文章。不确定此处关于发布个人博客链接的规则。因此,如果将来有人想看它,请在评论中告诉我。

于 2020-02-05T12:18:48.240 回答
0

我在这里采用了低技术解决方案。我的用例非常简单,我需要创建的文件夹数量有限,因此我的解决方案对我来说是可管理的。YMMV,但我想我会在这里提供它,以防它适用于其他人。

我基本上预先填充了我认为我需要的目录数量。我只是formatrange循环中使用 Python 来预填充我需要的许多文件夹。

cookiecutter/mycookiecutter/
├── cookiecutter.json
└── {{cookiecutter.project_name}}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 0\ %}{{cookiecutter.account_info["accounts"][0]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 1\ %}{{cookiecutter.account_info["accounts"][1]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 10\ %}{{cookiecutter.account_info["accounts"][10]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 11\ %}{{cookiecutter.account_info["accounts"][11]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 12\ %}{{cookiecutter.account_info["accounts"][12]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 13\ %}{{cookiecutter.account_info["accounts"][13]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 14\ %}{{cookiecutter.account_info["accounts"][14]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 15\ %}{{cookiecutter.account_info["accounts"][15]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 16\ %}{{cookiecutter.account_info["accounts"][16]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 17\ %}{{cookiecutter.account_info["accounts"][17]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 18\ %}{{cookiecutter.account_info["accounts"][18]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 19\ %}{{cookiecutter.account_info["accounts"][19]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 2\ %}{{cookiecutter.account_info["accounts"][2]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 3\ %}{{cookiecutter.account_info["accounts"][3]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 4\ %}{{cookiecutter.account_info["accounts"][4]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 5\ %}{{cookiecutter.account_info["accounts"][5]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 6\ %}{{cookiecutter.account_info["accounts"][6]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 7\ %}{{cookiecutter.account_info["accounts"][7]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 8\ %}{{cookiecutter.account_info["accounts"][8]}}{%\ endif\ %}
    └── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 9\ %}{{cookiecutter.account_info["accounts"][9]}}{%\ endif\ %}

cookiecutter.json

{
    "project_name": "test",
    "account_info": {
        "accounts": [
            "root",
            "corp",
            "dns",
            "dev",
            "prod"
        ]
    }
}
于 2022-02-10T20:58:51.497 回答