-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
118 lines (87 loc) · 3.17 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Init command for Ginpar projects.
This module implements the initialization command for the ginpar static content
generator.
`init` will prompt for a series of values to write the site configuration file.
Examples
--------
To initialize a project in a standard way to specify the configuration values::
ginpar init
To skip the prompts and initialize the project with the default values::
ginpar init --quick
ginpar init --q
To force the initialization in case there is a directory with the same name
of the project to initialize::
ginpar init --force
ginpar init -f
"""
import os
import click
import yaml
from ginpar.utils.echo import info, echo, success, error, alert
from ginpar.utils.files import create_file, create_folder, try_remove
from ginpar.utils.strings import space_to_kebab
def prompt_site_config(quick):
"""Echo the prompts and create the configuration dict.
Echo the instructions and configuration fields, store each input,
and create a dictionary containing those values.
Parameters
----------
quick : bool
Returns the default values immediatle if True.
Returns
-------
dict
Used to generate the site configuration file.
"""
site = {
"author": "David Omar",
"sitename": "My site",
"description": "This is a Ginpar project",
"url": "/",
"theme": "davidomarf/gart",
"content_path": "sketches",
"build_path": "public",
}
if quick:
return site
info("Welcome to ginpar! We'll ask for some values to initialize your project.")
echo("")
site["sitename"] = click.prompt("Site name", default=site["sitename"])
site["description"] = click.prompt("Description", default=site["description"])
site["author"] = click.prompt("Author", default=site["author"])
site["url"] = click.prompt("url", default=site["url"])
info("\nIf you're unsure about the next prompts, accept the defaults")
echo("")
site["theme"] = click.prompt("Theme", default=site["theme"])
site["content_path"] = click.prompt("Sketches path", default=site["content_path"])
site["build_path"] = click.prompt("Build path", default=site["build_path"])
return site
def init(force, quick):
"""Main function of the module. This is what `ginpar init` calls.
Parameters
----------
force : bool
Remove conflicting files when true.
quick : bool
Skip prompts when true.
"""
if force:
alert("You're forcing the initialization.")
alert("This will replace any existent file relevant to the project.")
click.confirm("Do you want to proceed?", abort=True)
site = prompt_site_config(quick)
path = space_to_kebab(site["sitename"]).lower()
content_path = os.path.join(path, site["content_path"])
config_yaml = os.path.join(path, "config.yaml")
echo("\n---\n")
if force:
echo("\n---\n")
try_remove(path)
echo("\n---\n")
create_folder(content_path)
with open(config_yaml, "w") as file:
yaml.dump(site, file)
echo("\n---\n")
success(
"Done!\nRun `ginpar serve` or `ginpar build` and see your new site in action!\n"
)