【问题标题】:How to get cookiecutter to output to current working directory instead of nested directory?如何让 cookiecutter 输出到当前工作目录而不是嵌套目录?
【发布时间】:2021-11-22 21:48:22
【问题描述】:

我们有许多用例,我们希望能够调用 cookiecutter 并将生成的文件和目录放入当前工作目录。

简单示例

本质上想要什么。

$ cd path/to/repo
$ ls
a.txt
$ cookiecutter path/to/template
$ls
a.txt b.txt

实际结果是什么

$ cd path/to/repo
$ ls
a.txt
$ cookiecutter path/to/template
$ls
a.txt 
template/
  b.txt

我尝试过的事情

  1. 您可以想象模板是这样的。
template/
  cookiecutter.json
  b.txt

但 cookiecutter 似乎默认需要有一个模板根目录以避免cookiecutter.exceptions.NonTemplatedInputDirException

  1. 因此,添加一个目录来包含模板文件并尝试将其输出到特定目录。
template/
  cookiecutter.json
  {{cookiecutter.name}}

但是模板文件只是嵌套在当前工作目录的子目录中,如上所示。

  1. 我还希望人们可以使用生成后挂钩将模板的输出文件移动到您想要的位置,但这似乎有点不合时宜...

我一直在 GitHub 上看到 cmets 说基本上“这已解决”,但我似乎找不到有关如何使用添加的任何功能来适应此用例的文档。

【问题讨论】:

    标签: cookiecutter


    【解决方案1】:

    问题已解决,但这并不意味着它已解决。

    执行此操作的唯一方法是使用生成后挂钩将文件移出文件夹并删除文件夹。

    如果您在 *NIX 系统上,您可以使用以下简单脚本:

    #!/bin/sh
    
    mv * ..
    rm -rfv ../{{ cookiecutter.project_name }}
    

    cookiecutter.project_name 是您的模板文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      相关资源
      最近更新 更多