til: cookiecutter_conditionally-creating-directories.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| cookiecutter_conditionally-creating-directories.md | cookiecutter | Conditionally creating directories in cookiecutter | https://github.com/simonw/til/blob/main/cookiecutter/conditionally-creating-directories.md | I wanted my [datasette-plugin](https://github.com/simonw/datasette-plugin) cookiecutter template to create empty `static` and `templates` directories if the user replied `y` to the `include_static_directory` and `include_templates_directory` prompts. The solution was to add a `hooks/post_gen_project.py` script containing the following: ```python import os import shutil include_static_directory = bool("{{ cookiecutter.include_static_directory }}") include_templates_directory = bool("{{ cookiecutter.include_templates_directory }}") if include_static_directory: os.makedirs( os.path.join( os.getcwd(), "datasette_{{ cookiecutter.underscored }}", "static", ) ) if include_templates_directory: os.makedirs( os.path.join( os.getcwd(), "datasette_{{ cookiecutter.underscored }}", "templates", ) ) ``` Note that these scripts are run through the cookiecutter Jinja template system, so they can use `{{ }}` Jinja syntax to read cookiecutter inputs. | <p>I wanted my <a href="https://github.com/simonw/datasette-plugin">datasette-plugin</a> cookiecutter template to create empty <code>static</code> and <code>templates</code> directories if the user replied <code>y</code> to the <code>include_static_directory</code> and <code>include_templates_directory</code> prompts.</p> <p>The solution was to add a <code>hooks/post_gen_project.py</code> script containing the following:</p> <div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">os</span> <span class="pl-k">import</span> <span class="pl-s1">shutil</span> <span class="pl-s1">include_static_directory</span> <span class="pl-c1">=</span> <span class="pl-en">bool</span>(<span class="pl-s">"{{ cookiecutter.include_static_directory }}"</span>) <span class="pl-s1">include_templates_directory</span> <span class="pl-c1">=</span> <span class="pl-en">bool</span>(<span class="pl-s">"{{ cookiecutter.include_templates_directory }}"</span>) <span class="pl-k">if</span> <span class="pl-s1">include_static_directory</span>: <span class="pl-s1">os</span>.<span class="pl-en">makedirs</span>( <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">join</span>( <span class="pl-s1">os</span>.<span class="pl-en">getcwd</span>(), <span class="pl-s">"datasette_{{ cookiecutter.underscored }}"</span>, <span class="pl-s">"static"</span>, ) ) <span class="pl-k">if</span> <span class="pl-s1">include_templates_directory</span>: <span class="pl-s1">os</span>.<span class="pl-en">makedirs</span>( <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">join</span>( <span class="pl-s1">os</span>.<span class="pl-en">getcwd</span>(), <span class="pl-s">"datasette_{{ cookiecutter.underscored }}"</span>, <span class="pl-s">"templates"</span>, ) )</pre></div> <p>Note that these scripts are run through the cookiecutter Jinja template system, so they can use <code>{{ }}</code> Jinja syntax to read cookiecutter inputs.</p> | <Binary: 57,163 bytes> | 2021-01-27T15:56:28-08:00 | 2021-01-27T23:56:28+00:00 | 2021-01-27T15:56:28-08:00 | 2021-01-27T23:56:28+00:00 | e0f45335a94143e5aac8b22e5820e564 | conditionally-creating-directories |