generated from T99/website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
executable file
·160 lines (116 loc) · 5.43 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3.7
#
# Created by Trevor Sears <[email protected]>.
# 5:10 PM -- October 10th, 2019.
# Project: website-template
#
import json
import sys
import os
from os import path
from typing import List
sys.dont_write_bytecode = True
def find_and_replace_in_file(file_path: str, content: str, replacement: str) -> None:
if path.exists(file_path) and path.isfile(file_path):
lines: List[str] = []
with open(file_path) as f:
lines = f.readlines()
lines = [line.replace(content, replacement) for line in lines]
with open(file_path, "w") as f:
f.writelines(lines)
else:
print("Could not find file in order to replace text.")
def set_if_empty(original: str, default: str) -> str:
return original if original != "" else default
# Check whether or not this file is being run directly.
if __name__ == "__main__":
# Attempt to read in the project initialization configuration file.
CONFIG_FILE_PATH = "./init.config.json"
if path.exists(CONFIG_FILE_PATH) and path.isfile(CONFIG_FILE_PATH):
with open(CONFIG_FILE_PATH) as config_file:
config = json.loads(config_file.read())
else:
print("Could not find the initialization config file ('init.config.json').")
print("Exiting...")
exit(1)
# Calculate configuration options as needed.
STRING_INDENT = " " * config["output-indentation"] if isinstance(config["output-indentation"], int) else str(
config["output-indentation"])
# Print start message.
print("Running website-template initialization script...")
# Prompt for relevant package information to find-and-replace template placeholder strings.
package_name = input(STRING_INDENT + "Package name: ")
package_desc_short = input(STRING_INDENT + "Short package description: ")
package_desc_long = input(STRING_INDENT + "Long package description (defaults to short desc.): ")
github_org = input(STRING_INDENT + "Github user/org (default: " + config["default-github-user-org"] + "): ")
github_org = set_if_empty(github_org, config["default-github-user-org"])
readme_title = input(STRING_INDENT + "README title (default: " + package_name + "): ")
print()
# Set the default value for values not specified by the user.
package_version = "0.1.0"
package_desc_long = set_if_empty(package_desc_long, package_desc_short)
readme_title = set_if_empty(readme_title, package_name)
# If the "delete-files" key exists and has items to delete...
if "delete-files" in config and len(config["delete-files"]) > 0:
# Delete the files specified in the config file.
print(STRING_INDENT + "Deleting the specified project files...")
for file in config["delete-files"]:
print((STRING_INDENT * 2) + "--> " + path.relpath(file, "."), end="")
if path.exists(file) and path.isfile(file):
os.remove(file)
print()
else:
print(" (failed: file not found)")
print()
# If the "move-files" key exists and has items to move...
if "move-files" in config and len(config["move-files"]) > 0:
# Move the files specified in the config file.
print(STRING_INDENT + "Moving the specified project files...")
for orig_name, new_name in config["move-files"]:
print((STRING_INDENT * 2) + "--> " + path.relpath(orig_name, ".") + " to " + path.relpath(new_name, "."), end="")
if path.exists(orig_name) and path.isfile(orig_name):
os.replace(orig_name, new_name)
print()
else:
print(" (failed: source file not found)")
print()
# If the "find-and-replace-files" key exists and has items to refactor...
if "find-and-replace-files" in config and len(config["find-and-replace-files"]) > 0:
# Perform a find-and-replace over the files specified in the config file.
print(STRING_INDENT + "Finding-and-replacing information in the specified project files...")
for file in config["find-and-replace-files"]:
print((STRING_INDENT * 2) + "--> " + path.relpath(file, "."), end="")
if path.exists(file) and path.isfile(file):
find_and_replace_in_file(file, "<title>", readme_title)
find_and_replace_in_file(file, "<name>", package_name)
find_and_replace_in_file(file, "<base_name>", package_name)
find_and_replace_in_file(file, "<version>", package_version)
find_and_replace_in_file(file, "<desc>", package_desc_short)
find_and_replace_in_file(file, "<desc_long>", package_desc_long)
find_and_replace_in_file(file, "<github_org>", github_org)
print()
else:
print(" (failed: file not found)")
print()
# Read in the package.json file and modify is according to the user's input.
print(STRING_INDENT + "Configuring information in package.json...\n")
with open("package.json", "r+") as npm_config:
github_url = "https://github.com/" + github_org + "/" + package_name
npm_config_json = json.loads(npm_config.read())
npm_config_json["name"] = package_name
npm_config_json["version"] = package_version
npm_config_json["description"] = package_desc_short
npm_config_json["repository"]["url"] = "git+" + github_url
npm_config_json["bugs"]["url"] = github_url + "/issues"
npm_config_json["homepage"] = github_url + "#readme"
npm_config.seek(0)
npm_config.write(json.dumps(npm_config_json, indent="\t"))
npm_config.truncate()
# Install the required packages from NPM.
print(STRING_INDENT + "Installing packages from package.json...\n")
os.system("npm install > /dev/null 2>&1")
print(STRING_INDENT + "Done! Exiting...\n")
# If the initialization file is configured to delete itself on-completion, comply.
if config["self-destruct"]:
os.remove(__file__)
os.remove(CONFIG_FILE_PATH)