4

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

什么有效,我想达到什么。

举个例子,使用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
...

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",

没问题,json2yaml来拯救。

那也行不通。

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

我还尝试了< stdin重定向:

cookiecutter.prompts.txt

yes
Spartacus
...

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

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

我怀疑我遗漏了一些明显的东西,不确定是什么。首先, --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:

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

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

4

1 回答 1

7

你就在附近。

尝试这个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}

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

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

于 2020-03-28T19:33:08.357 回答