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

问题描述 投票:0回答:2

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

什么有效以及我想实现什么。

我们举个例子,使用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 [[email protected]]: [email protected] 👈 constant for me/my organization
...
project_name [Python Boilerplate]: GladiatorRevolt 👈 this will vary.
project_slug [q]: gladiator-revolt 👈 this too
...

好的,完成了。

现在,我可以通过以下方式轻松重做此项目:

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 [[email protected]]
: 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 和描述。哦,好吧,现在已经足够了。

cookiecutter
2个回答
13
投票

你就在附近。

试试这个

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: "[email protected]"
    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
块,因为那是用户输入的地方。


0
投票

谢谢你的例子。我有一个场景,用户需要填写配置文件,并通过终端提示仅回答那些未填写的问题。 Cookiecutter 支持此功能吗?

© www.soinside.com 2019 - 2024. All rights reserved.