千篇一律:为提示指定变量的最简单方法是什么

JL *_*ret 2 cookiecutter

通过指向预定义的提示-答案文件,是否有任何提供重播类型功能的东西?

什么有效,我想实现什么。

举个例子,使用cookiecutter为pypi准备一个Python包

cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git

You've downloaded /Users/jluc/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]:
full_name [Audrey Roy Greenfeld]: Spartacus   constant for me/my organization
email [audreyr@example.com]: spartacus@example.com  constant for me/my organization
...
project_name [Python Boilerplate]: GladiatorRevolt  this will vary.
project_slug [q]: gladiator-revolt  this too
...
Run Code Online (Sandbox Code Playgroud)

OK完成。

现在,对于这个项目,我可以通过以下方式轻松重做:

cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --replay

这很棒!

我想要的是:

假设我创建了另一个项目,UnleashHell

我想以某种方式准备一个文件,其中包含Unleash 的开发人员信息项目级别信息。而且我希望能够针对这个模板多次运行它,而不必处理提示。这个特殊的 pypi 模板会定期更新,例如,python 2.7 支持已被删除。

问题:

A--replay将只为这个 cookiecutter 模板注入最后一次运行。如果它是针对不同的 pypi 项目运行的,那就太糟糕了。

我很擅长开发人员级别的信息,但我需要更改所有项目级别的信息。

我尝试通过以下方式复制重播文件:

cp ~/.cookiecutter_replay/cookiecutter-pypackage.json unleash.json

编辑unleash.json以反映必要的更改。

然后通过--config-file标志指定它

cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --config-file unleash.json

我收到一个丑陋的错误,它显然需要 YAML。

cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file .../000.packaging/unleash.json. Error: None of the known patterns match for {
    "cookiecutter": {
        "full_name": "Spartacus",

Run Code Online (Sandbox Code Playgroud)

没问题,json2yaml来救援。

那也行不通。

cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file ./cookie.yaml. Error: Unable to determine type for "
    full_name: "Spartacus"

Run Code Online (Sandbox Code Playgroud)

我也试过< stdin重定向:

cookiecutter.prompts.txt

yes
Spartacus
...
Run Code Online (Sandbox Code Playgroud)

它似乎没有使用它,只是中止了。

cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git < ./cookiecutter.prompts.txt
You've downloaded ~/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]
: full_name [Audrey Roy Greenfeld]
: email [audreyr@example.com]
: Aborted
Run Code Online (Sandbox Code Playgroud)

我怀疑我遗漏了一些明显的东西,不确定是什么。首先,--config 文件的意图和格式是什么?

汇报 - 我是如何从接受的答案中得到它的。

接受了答案,但对其进行了调整以供~/.cookiecutterrc使用。它有效,但格式不是很清楚。尤其不是rc必须是 yaml 的文件,尽管 rc 文件并不总是/经常是这种情况。

这最终起作用了:

文件 ~/.cookiecutterrc:

没有嵌套在default_context......大量无用的 yaml 解析错误(在有效的 yaml 文档中)。

default_context:
    #... cut out for privacy
    add_pyup_badge: y
    command_line_interface: "Click"
    create_author_file: "y"
    open_source_license: "MIT license"

# the names to use here are:
    # full_name: 
    # email: 
    # github_username: 
    # project_name: 
    # project_slug: 
    # project_short_description: 
    # pypi_username: 
    # version: 
    # use_pytest: 
    # use_pypi_deployment_with_travis: 
    # add_pyup_badge: 
    # command_line_interface:
    # create_author_file: 
    # open_source_license:

Run Code Online (Sandbox Code Playgroud)

我仍然无法获得~/.cookiecutterrc特定于项目的组合config.yaml于工作。太糟糕了,预期的配置格式记录如此之少。

所以我将使用 .rc 但每次都输入项目名称、slug 和描述。哦,好吧,现在已经足够了。

小智 6

你在附近。

尝试这个 cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --no-input --config-file config.yaml

--no-input参数将抑制终端用户输入,当然它是可选的。

config.yaml 文件可能如下所示:

default_context:
    full_name: "Audrey Roy"
    email: "audreyr@example.com"
    github_username: "audreyr"
cookiecutters_dir: "/home/audreyr/my-custom-cookiecutters-dir/"
replay_dir: "/home/audreyr/my-custom-replay-dir/"
abbreviations:
    pp: https://github.com/audreyr/cookiecutter-pypackage.git
    gh: https://github.com/{0}.git
    bb: https://bitbucket.org/{0}
Run Code Online (Sandbox Code Playgroud)

参考这个示例文件:https : //cookiecutter.readthedocs.io/en/1.7.0/advanced/user_config.html

您可能只需要default_context块,因为这是用户输入的地方。