-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcreate-from-template.xsh
313 lines (236 loc) · 10.5 KB
/
create-from-template.xsh
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env xonsh
# Copyright (c) 2025 Toradex
# SPDX-License-Identifier: MIT
##
# This script is used to create a new project from a template.
##
# use the xonsh environment to update the OS environment
$UPDATE_OS_ENVIRON = True
# Get the full log of error
$XONSH_SHOW_TRACEBACK = True
# always return if a cmd fails
$RAISE_SUBPROC_ERROR = True
import os
import sys
import json
from pathlib import Path
from typing import TypeVar
from xonsh.procs.pipelines import CommandPipeline
from torizon_templates_utils.tasks import replace_tasks_input
from torizon_templates_utils.args import get_arg_not_empty,get_optional_arg
from torizon_templates_utils.errors import Error,Error_Out
from torizon_templates_utils.colors import Color,BgColor,print
if len(sys.argv) < 5:
print(
"""
Usage:
create-from-template.xsh <template_folder> <project_name> <container_name> <new_project_path> [template] [vscode] [telemetry]
<template_folder> The folder where the template that will be used to create
the new project is located.
<project_name> The name of the new project.
<container_name> The name of the container that will be used for the new project.
<new_project_path> The path where the new project will be created.
Optional:
[template] The name of the template to be used. If not provided, the
script will use the folder name from <template_folder>.
[vscode] This is a bool like argument. This signals if the script
is being used from VS Code extension.
[telemetry] This is a bool like argument. This signals if the script
is being used from VS Code extension.
"""
)
Error_Out("", Error.EUSER)
_old_cwd = os.getcwd()
template_folder = get_arg_not_empty(1)
project_name = get_arg_not_empty(2)
container_name = get_arg_not_empty(3)
new_project_path = get_arg_not_empty(4)
# get the template_folder name
_template = Path(template_folder).name
# optional
template = get_optional_arg(5, _template)
vscode = get_optional_arg(6, False)
telemetry = get_optional_arg(7, True)
# the new_project_path need to be a full path
new_project_path = f"{new_project_path}/{project_name}"
print("Data:")
print(f"\tTemplate Folder: {template_folder}")
print(f"\tProject Name: {project_name}")
print(f"\tContainer Name: {container_name}")
print(f"\tNew Project Path: {new_project_path}")
print(f"\tTemplate: {template}")
print(f"\tIs VS Code: {vscode}")
print(f"\tSend Telemetry: {telemetry}")
# get the template metadata from ../templates.json
try:
with open(f"{template_folder}/../templates.json", 'r') as file:
_metadata = json.load(file)
except FileNotFoundError as fex:
Error_Out(
f"Error: {fex.strerror} :: {fex.filename}",
Error.ENOFOUND
)
_template_metadata = next(
(t for t in _metadata["Templates"] if t["folder"] == template),
None
)
if _template_metadata is None:
Error_Out(
f"Error: Template '{template}' not found in templates.json",
Error.ENOFOUND
)
# send telemetry
if telemetry:
try:
import http.client
import urllib.parse
_query = urllib.parse.urlencode({
"template": template
}).encode("utf-8")
_conn = http.client.HTTPConnection("ec2-3-133-114-116.us-east-2.compute.amazonaws.com")
_conn.request(
"GET", "/api/template/plus",
body=_query
)
_res = _conn.getresponse()
if _res.status != 200:
print(f"Telemetry failed: {_res.status}", Color.YELLOW)
_conn.close()
except Exception as ex:
print(f"Telemetry error: {ex}", Color.YELLOW)
else:
print(f"Telemetry disabled", color=Color.BLUE)
# create the copy
print("Creating from template ...", color=Color.YELLOW)
cp -r @(template_folder) @(new_project_path)
print("✅ Folder copy done!", color=Color.GREEN)
# apply the common tasks and inputs
if "mergeCommon" not in _template_metadata or _template_metadata['mergeCommon'] != False:
print("Applying common tasks ...", color=Color.YELLOW)
_f_commontasks = open(f"{template_folder}/../assets/tasks/common.json", "r")
_common_tasks = json.load(_f_commontasks)
_f_commontasks.close()
_f_commoninputs = open(f"{template_folder}/../assets/tasks/inputs.json", "r")
_common_inputs = json.load(_f_commoninputs)
_f_commoninputs.close()
_f_projtasks = open(f"{new_project_path}/.vscode/tasks.json", "r")
_proj_tasks = json.load(_f_projtasks)
_f_projtasks.close()
# merge then
_proj_tasks["tasks"] += _common_tasks["tasks"]
_proj_tasks["inputs"] += _common_inputs["inputs"]
# write back
_f_projtasks = open(f"{new_project_path}/.vscode/tasks.json", "w+")
_f_projtasks.write(json.dumps(_proj_tasks, indent=4))
_f_projtasks.close()
print("✅ Common tasks applied!", color=Color.GREEN)
# we have to also copy the scripts
cp -r @(template_folder)/../scripts/check-deps.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/run-container-if-not-exists.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/share-wsl-ports.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/docker-login.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/create-docker-compose-production.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/torizon-packages.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/.vscode/tasks.xsh @(new_project_path)/.vscode/
cp -r @(template_folder)/../scripts/bash/tcb-env-setup.sh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/torizon-io.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/check-ci-env.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/validate-deps-running.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/apply-ci-settings-file.xsh @(new_project_path)/.conf/
cp -r @(template_folder)/../scripts/validate-json.xsh @(new_project_path)/.conf/
template_name = os.path.basename(template_folder)
# torizonPackages.json fixups
# TCB template does not use it
if template_name != "tcb":
_tor_package_json_file = open(f"{template_folder}/../assets/json/torizonPackages.json", "r")
_tor_package_json = json.load(_tor_package_json_file)
_tor_package_json_file.close()
_dockerfile_file = open(f"{template_folder}/Dockerfile", "r")
_dockerfile_lines = _dockerfile_file.readlines()
_dockerfile_file.close()
_build_dep_dockerfile = False
for line in _dockerfile_lines:
if "torizon_packages_build" in line:
_build_dep_dockerfile = True
break
# the torizonPackages.json comes with the buildDeps, devRuntimeDeps and prodRuntimeDeps
# but some templates can not use all of them
# so we groom the JSON object to remove the unnecessary keys
if not os.path.exists(f"{template_folder}/Dockerfile.sdk"):
_tor_package_json.pop("buildDeps")
if not os.path.exists(f"{template_folder}/Dockerfile.debug"):
_tor_package_json.pop("devRuntimeDeps")
# save the modified JSON object
_tor_package_json_file = open(f"{new_project_path}/torizonPackages.json", "w+")
_tor_package_json_file.write(json.dumps(_tor_package_json, indent=4))
_tor_package_json_file.close()
# check .conf/deps.json
_deps_json_file = open(f"{template_folder}/.conf/deps.json", "r")
_deps_json = json.load(_deps_json_file)
_deps_json_file.close()
# if there are installation scripts listed on the .conf/deps.json
# we need to copy them to the new project
if "installDepsScripts" in _deps_json and len(_deps_json["installDepsScripts"]) > 0:
if not os.path.exists(f"{new_project_path}/.conf/installDepsScripts"):
os.makedirs(f"{new_project_path}/.conf/installDepsScripts")
# copy the scripts
for script in _deps_json["installDepsScripts"]:
if not os.path.exists(f"{new_project_path}/{script}") and ".conf/installDepsScripts" in script:
_script_source = script.replace(".conf", "scripts")
cp -r @(template_folder)/../@(_script_source) @(new_project_path)/@(script)
# copy the github actions if not exists
if not os.path.exists(f"{new_project_path}/.github"):
mkdir -p @(new_project_path)/.github
cp -r @(template_folder)/../assets/github/workflows @(new_project_path)/.github
# copy the .gitlab ci if not exists
if not os.path.exists(f"{new_project_path}/.gitlab-ci.yml"):
cp -r @(template_folder)/../assets/gitlab/.gitlab-ci.yml @(new_project_path)/.gitlab-ci.yml
# create a metadata.json to store
# template name
# container name
# base TOR used when created
_proj_metadata_json = {
"projectName": project_name,
"templateName": template,
"containerName": container_name,
"torizonOSMajor": _metadata["TorizonOSMajor"]
}
# save the metadata json file
_proj_metadata_json_file = open(f"{new_project_path}/.conf/metadata.json", "w+")
_proj_metadata_json_file.write(json.dumps(_proj_metadata_json, indent=4))
_proj_metadata_json_file.close()
print("✅ Scripts copy done", color=Color.GREEN)
os.chdir(new_project_path)
# change the folders that is needed
print("Renaming folders ...", color=Color.YELLOW)
for item in Path('.').rglob('*__change__*'):
print(item)
new_name = str(item).replace('__change__', project_name)
item.rename(new_name)
print("✅ Project folders ok", color=Color.GREEN)
# change the contents
print("Renaming file contents ...", color=Color.YELLOW)
for item in Path('.').rglob('*'):
if item.is_file():
mime_type: CommandPipeline
mime_type = !(file --mime-encoding @(item))
if "binary" not in mime_type.out:
if "id_rsa" not in str(item):
with open(item, 'r') as file:
content = file.read()
content = content.replace("__change__", project_name)
content = content.replace("__container__", container_name)
content = content.replace("__home__", os.environ['HOME'])
content = content.replace("__templateFolder__", template)
with open(item, 'w') as file:
file.write(content)
elif "id_rsa.pub" not in str(item):
os.chmod(item, 0o400)
# the project updater does not need to change the contents
cp -r @(template_folder)/../scripts/project-updater.xsh @(new_project_path)/.conf/
# if from vs code we need to replace the inputs
if vscode != False:
replace_tasks_input()
print("✅ Renaming file contents ok", color=Color.GREEN)
# back
os.chdir(_old_cwd)