|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
Main entry point for the `cookiecutter` command. |
|
5
|
|
|
|
|
6
|
|
|
The code in this module is also a good example of how to use Cookiecutter as a |
|
7
|
|
|
library rather than a script. |
|
8
|
|
|
""" |
|
9
|
|
|
|
|
10
|
|
|
from __future__ import unicode_literals |
|
11
|
|
|
import logging |
|
12
|
|
|
import os |
|
13
|
|
|
|
|
14
|
|
|
from .config import get_user_config |
|
15
|
|
|
from .generate import generate_context, generate_files |
|
16
|
|
|
from .exceptions import InvalidModeException |
|
17
|
|
|
from .prompt import prompt_for_config |
|
18
|
|
|
from .replay import dump, load |
|
19
|
|
|
from .repository import determine_repo_dir |
|
20
|
|
|
|
|
21
|
|
|
logger = logging.getLogger(__name__) |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def cookiecutter( |
|
25
|
|
|
template, checkout=None, no_input=False, extra_context=None, |
|
26
|
|
|
replay=False, overwrite_if_exists=False, output_dir='.', |
|
27
|
|
|
config_file=None, default_config=False, render_context=None): |
|
28
|
|
|
""" |
|
29
|
|
|
API equivalent to using Cookiecutter at the command line. |
|
30
|
|
|
|
|
31
|
|
|
:param template: A directory containing a project template directory, |
|
32
|
|
|
or a URL to a git repository. |
|
33
|
|
|
:param checkout: The branch, tag or commit ID to checkout after clone. |
|
34
|
|
|
:param no_input: Prompt the user at command line for manual configuration? |
|
35
|
|
|
:param extra_context: A dictionary of context that overrides default |
|
36
|
|
|
and user configuration. |
|
37
|
|
|
:param: overwrite_if_exists: Overwrite the contents of output directory |
|
38
|
|
|
if it exists |
|
39
|
|
|
:param output_dir: Where to output the generated project dir into. |
|
40
|
|
|
:param config_file: User configuration file path. |
|
41
|
|
|
:param default_config: Use default values rather than a config file. |
|
42
|
|
|
:param render_context: Create your custom rendering context. |
|
43
|
|
|
You can use this to add extra render vars. |
|
44
|
|
|
For instance, you might want to create |
|
45
|
|
|
custom rendering values on the fly depending on users' input. |
|
46
|
|
|
You can pass this dictionary that will be used to render templates. |
|
47
|
|
|
""" |
|
48
|
|
|
if replay and ((no_input is not False) or (extra_context is not None)): |
|
49
|
|
|
err_msg = ( |
|
50
|
|
|
"You can not use both replay and no_input or extra_context " |
|
51
|
|
|
"at the same time." |
|
52
|
|
|
) |
|
53
|
|
|
raise InvalidModeException(err_msg) |
|
54
|
|
|
|
|
55
|
|
|
config_dict = get_user_config( |
|
56
|
|
|
config_file=config_file, |
|
57
|
|
|
default_config=default_config, |
|
58
|
|
|
) |
|
59
|
|
|
|
|
60
|
|
|
repo_dir = determine_repo_dir( |
|
61
|
|
|
template=template, |
|
62
|
|
|
abbreviations=config_dict['abbreviations'], |
|
63
|
|
|
clone_to_dir=config_dict['cookiecutters_dir'], |
|
64
|
|
|
checkout=checkout, |
|
65
|
|
|
no_input=no_input, |
|
66
|
|
|
) |
|
67
|
|
|
|
|
68
|
|
|
template_name = os.path.basename(os.path.abspath(repo_dir)) |
|
69
|
|
|
|
|
70
|
|
|
if replay: |
|
71
|
|
|
context = load(config_dict['replay_dir'], template_name) |
|
72
|
|
|
else: |
|
73
|
|
|
context_file = os.path.join(repo_dir, 'cookiecutter.json') |
|
74
|
|
|
logger.debug('context_file is {}'.format(context_file)) |
|
75
|
|
|
|
|
76
|
|
|
if render_context: |
|
77
|
|
|
context = render_context |
|
78
|
|
|
else: |
|
79
|
|
|
context = get_render_context( |
|
80
|
|
|
context_file, config_dict, |
|
81
|
|
|
extra_context=extra_context, |
|
82
|
|
|
no_input=no_input) |
|
83
|
|
|
|
|
84
|
|
|
dump(config_dict['replay_dir'], template_name, context) |
|
85
|
|
|
|
|
86
|
|
|
# Create project from local context and project template. |
|
87
|
|
|
return generate_files( |
|
88
|
|
|
repo_dir=repo_dir, |
|
89
|
|
|
context=context, |
|
90
|
|
|
overwrite_if_exists=overwrite_if_exists, |
|
91
|
|
|
output_dir=output_dir |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def get_render_context( |
|
96
|
|
|
context_file, config_dict, extra_context=None, no_input=False): |
|
97
|
|
|
""" |
|
98
|
|
|
:param context_file: cookiecutter.json path. |
|
99
|
|
|
:param config_dict: User configuration. |
|
100
|
|
|
:param no_input: Prompt the user at command line for manual configuration? |
|
101
|
|
|
""" |
|
102
|
|
|
context = generate_context( |
|
103
|
|
|
context_file=context_file, |
|
104
|
|
|
default_context=config_dict['default_context'], |
|
105
|
|
|
extra_context=extra_context, |
|
106
|
|
|
) |
|
107
|
|
|
|
|
108
|
|
|
# prompt the user to manually configure at the command line. |
|
109
|
|
|
# except when 'no-input' flag is set |
|
110
|
|
|
context['cookiecutter'] = prompt_for_config(context, no_input) |
|
111
|
|
|
return context |
|
112
|
|
|
|