-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkit_build.py
More file actions
346 lines (318 loc) · 14.1 KB
/
kit_build.py
File metadata and controls
346 lines (318 loc) · 14.1 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import subprocess
import platform
import os
import urllib.request
import zipfile
import shutil
import json
import sys
import codecs
import typing
try:
import colorama
except ImportError:
subprocess.run(["pip", "install", "colorama"])
import colorama
colorama.init()
system = platform.system()
def path_add(new_path: str):
if new_path not in os.getenv('PATH'):
os.environ["PATH"] += os.pathsep + new_path
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
RED = "\033[1;31m"
BLUE = "\033[1;34m"
CYAN = "\033[1;36m"
GREEN = "\033[0;32m"
RESET = "\033[0;0m"
BOLD = "\033[;1m"
REVERSE = "\033[;7m"
if not (system.lower() in ["windows", "win", "win32", "win64"]):
colors_supported = codecs.decode(subprocess.run(["tput", "colors"], capture_output=True).stdout, "ascii").replace("\n", "")
else:
colors_supported = "256"
if colors_supported == "256":
KIT1 = "\033[38;5;79m"
KIT2 = "\033[38;5;80m"
KIT3 = "\033[38;5;45m"
KITSUCCESS = "\033[38;5;77m"
KITFAIL = "\033[38;5;124m"
KITYELLOW = "\033[38;5;220m"
else:
KIT1 = "\033[1;36m"
KIT2 = "\033[1;36m"
KIT3 = "\033[1;36m"
KITSUCCESS = "\033[1;36m"
KITFAIL = "\033[1;31m"
KITYELLOW = "\033[0;93m"
def reset_format():
sys.stdout.write(RESET)
print(flush=True, end="")
def text_gradient(text, colors=(KIT1, KIT2, KIT3)):
out = ""
lastwhere = 0
for n in range(len(colors) + 1):
where = len(text) * n // len(colors)
clen = [0]
counter = 0
incr = 0
for x in text.split(" "):
incr = len(x) + incr + 1
clen.append(incr)
counter += 1
clen = clen[0:len(clen) - 1]
clendiff = []
for c in clen:
clendiff.append(abs(c - where))
where = clen[clendiff.index(min(clendiff))]
try:
out += text[lastwhere:where] + colors[n]
except IndexError:
out += text[lastwhere:where]
lastwhere = where
return out
def prompt(text, default="y", kitcolor=False):
"""returns true if yes and false if no"""
default_indicator = " [Y/n] " if "y" == default.lower()[0] else " [y/N] "
out = text + default_indicator
if kitcolor:
sys.stdout.write(BOLD)
out = text_gradient(out)
try:
out = True if "y" == input(out).lower()[0] else False
except IndexError:
out = True if "y" == default.lower()[0] else False
if kitcolor:
reset_format()
return out
def notify(text, color):
sys.stdout.write(color)
print(text, flush=True)
reset_format()
def set_global_env_var(name, what, message=""):
startup_files = {"csh": os.environ["HOME"] + "/.cshrc", # --------------- # csh
"tcsh": os.environ["HOME"] + "/.tcshrc", # -------------- # tcsh
"sh": "/etc/profile", # ------------------------------- # sh
"bash": os.environ["HOME"] + "/.bash_profile", # -------- # bash
"bash2": os.environ["HOME"] + "/.bashrc", # -------------- # bash
"zsh": os.environ["HOME"] + "/.zshenv", # -------------- # zsh
"rc": os.environ["HOME"] + "/.rcrc", # ---------------- # rc
"fish": os.environ["HOME"] + "/.config/fish/config.fish"} # fish
commands = {"csh": (lambda n, w: "setenv " + n + " " + w), # ------------ # csh
"tcsh": (lambda n, w: "setenv " + n + " " + w), # ------------ # tcsh
"sh": (lambda n, w: "export " + n + "=" + w), # ------------ # sh
"bash": (lambda n, w: "export " + n + "=" + w), # ------------ # bash
"zsh": (lambda n, w: "export " + n + "=" + w), # ------------ # zsh
"rc": (lambda n, w: n + "=" + w), # ------------------------ # rc
"fish": (lambda n, w: "set -x " + n + " " + w)} # ------------ # fish
for n in startup_files.keys():
try:
if not get_global_env_var(name, what, n.replace("2", "")):
try:
with open(startup_files[n], "r+") as file:
append = True
for line in file.readlines():
if line.replace("\n", "") == commands[n.replace("2", "")](name, what):
append = False
if append:
file.seek(0, 2)
file.write("\n" + commands[n.replace("2", "")](name, what) + "\n")
except FileNotFoundError:
with open(startup_files[n], "a+") as file:
file.write("\n" + message + "\n" + commands[n.replace("2", "")](name, what) + "\n")
except PermissionError:
notify("Insufficient permissions to edit " + startup_files[n] + " for " + n.replace("2", "") + ", try running with sudo. File might also not exist.", BOLD + KITFAIL)
notify("Continuing to next startup file...", BOLD + KITFAIL)
def get_global_env_var(name, what, which: typing.Union[str, bool] = True):
commands = {"csh": (lambda n: "echo $" + n),
"tcsh": (lambda n: "echo $" + n),
"sh": (lambda n: "echo $" + n),
"bash": (lambda n: "echo $" + n),
"zsh": (lambda n: "echo $" + n),
"rc": (lambda n: "echo $" + n),
"fish": (lambda n: "echo $" + n)}
if which and (which.__class__ == bool):
output = {}
all = True
for n in commands.keys():
shell_path = subprocess.run(["which", n], stdout=subprocess.PIPE).stdout.decode('utf-8').replace("\n", "")
if shell_path == "":
output[n] = "Shell not found"
else:
this_success = subprocess.run(commands[n](name), stdout=subprocess.PIPE, shell=True, executable=shell_path).stdout.decode('utf-8').replace("\n", "")
# print(shell_path)
# print(this_success, flush=True)
output[n] = this_success
all &= (this_success == what)
return all, output
else:
shell_path = subprocess.run("which " + which, stdout=subprocess.PIPE, shell=True).stdout.decode('utf-8').replace("\n", "")
return what == subprocess.run(commands[which](name).split(" "), stdout=subprocess.PIPE, shell=True, executable=shell_path).stdout.decode('utf-8')
if not (os.path.exists("kit") and os.path.isdir("kit")):
subprocess.run(["git", "clone", "https://github.com/kitlang/kit.git"])
with cd("kit"):
if prompt("Use dev branch?", "y", True):
subprocess.run(["git", "fetch", "origin"])
subprocess.run(["git", "checkout", "dev"])
subprocess.run(["git", "pull"])
print("Running on " + system)
if system.lower() in ["windows", "win32", "win", "win64"]:
localbin = os.path.abspath(os.getenv('APPDATA') + "\local\\bin")
if prompt("Use new scoop install?", "y", True):
# new build process with scoop
try:
subprocess.run(["scoop", "install", "'https://raw.githubusercontent.com/kitlang/kit/dev/.packages/scoop/kitlang-prerelease.json'"])
except:
this_path = os.path.dirname("\\".join(os.path.realpath(__file__).split("\\")[:-1]))
try:
subprocess.call(["Set-ExecutionPolicy", "RemoteSigned", "-scope", "CurrentUser"])
subprocess.call(["C:\WINDOWS\system32\WindowsPowerShell\\v1.0\powershell.exe", "iex (new-object net.webclient).downloadstring('https://get.scoop.sh')"])
subprocess.run(["scoop", "install", "'https://raw.githubusercontent.com/kitlang/kit/dev/.packages/scoop/kitlang-prerelease.json'"])
except:
urllib.request.urlretrieve("https://download.microsoft.com/download/0/5/C/05C1EC0E-D5EE-463B-BFE3-9311376A6809/NDP472-KB4054531-Web.exe", this_path + "\dotnet_4_7_2.exe")
dotnet_installer = this_path + "\dotnet_4_7_2.exe"
subprocess.call(dotnet_installer)
subprocess.call(["Set-ExecutionPolicy", "RemoteSigned", "-scope", "CurrentUser"])
subprocess.call(["C:\WINDOWS\system32\WindowsPowerShell\\v1.0\powershell.exe", "iex (new-object net.webclient).downloadstring('https://get.scoop.sh')"])
subprocess.run(["scoop", "install", "'https://raw.githubusercontent.com/kitlang/kit/dev/.packages/scoop/kitlang-prerelease.json'"])
if prompt("Run tests?", "n", True):
notify("I don't know how to do this for scoop installs yet.", BOLD + KITFAIL)
else:
try:
subprocess.run(["stack", "upgrade"])
except:
if "32" in system:
urllib.request.urlretrieve("https://get.haskellstack.org/stable/windows-i386.zip", localbin + "\stack32.zip")
location = localbin + "\stack32.zip"
else:
urllib.request.urlretrieve("https://get.haskellstack.org/stable/windows-x86_64.zip", localbin + "\stack64.zip")
location = localbin + "\stack64.zip"
path_add(localbin)
zipfile.ZipFile(location, "r").extractall()
os.remove(location)
subprocess.run(["stack", "build"])
if prompt("Run tests?", "n", True):
subprocess.run(["stack", "test"])
subprocess.run(["stack", "install"])
try:
shutil.rmtree(localbin + "\std")
except FileNotFoundError:
pass
shutil.copytree(os.path.abspath("std"), os.path.abspath(localbin + "\std"))
if prompt("Add Visual Studio Code extension?", "y", True):
with cd("utils"):
with cd("vscode-kitlang"):
subprocess.run(["npm", "install", "-g", "vsce"])
this_path = os.path.dirname("\\".join(os.path.realpath(__file__).split("\\")[:-1]) + "\kit\\utils\\vscode-kitlang")
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if f.split(".")[-1] == "vsix":
os.remove(f)
subprocess.run(["vsce", "package"])
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if f.split(".")[-1] == "vsix":
subprocess.run(["code", "--uninstall-extension", "kitlang.kitlang"])
subprocess.run(["code", "--install-extension", os.path.abspath(f)])
os.remove(f)
if prompt("Modify Code Runner extension?", "y", True):
with cd(os.path.abspath(os.environ["USERPROFILE"] + "\.vscode\extensions")):
this_path = os.path.abspath(os.environ["USERPROFILE"] + "\.vscode\extensions")
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if "code-runner" in f:
with cd(f):
with open("package.json", "r") as read_file:
data = json.load(read_file)
data["contributes"]["configuration"]["properties"]["code-runner.executorMap"]["default"]["kit"] = "kitc --run"
data["contributes"]["configuration"]["properties"]["code-runner.executorMapByFileExtension"]["default"][".kit"] = "kitc --run"
with open("package.json", "w") as write_file:
json.dump(data, write_file)
notify("Please restart Visual Studio Code", KITYELLOW)
else:
try:
subprocess.run(["stack", "upgrade"])
except:
subprocess.run(["curl", "-sSL", "https://get.haskellstack.org/ | sh"])
subprocess.run(["stack", "build"])
if prompt("Run tests?", "n", True):
subprocess.run(["stack", "test"])
subprocess.run(["stack", "install"])
this_path = os.path.dirname("/".join(os.path.realpath(__file__).split("/")))
notify("Attempting to add the Kit toolchain path to shell startup files...", BOLD + KITYELLOW)
set_global_env_var("KIT_TOOLCHAIN_PATH", this_path + "/toolchains", "# kitc toolchain path - added by kit_build.py")
success_condition = get_global_env_var("KIT_TOOLCHAIN_PATH", this_path + "/toolchains", True)
if success_condition.__class__ == tuple:
if success_condition[0]:
notify("Toolchain path added to environment variable successfully", BOLD + KITSUCCESS)
else:
notify("Toolchain path not added to environment variable", BOLD + KITFAIL)
notify(success_condition[1], BOLD + KITFAIL)
else:
if success_condition:
notify("Toolchain path added to environment variable successfully", BOLD + KITSUCCESS)
else:
notify("Toolchain path not added to environment variable", BOLD + KITFAIL)
try:
if system.lower() == "darwin":
# path_add(os.path.abspath("/usr/local/lib/"))
shutil.copyfile(os.path.abspath(os.environ["HOME"] + "/.local/bin/kitc"), os.path.abspath("/usr/local/bin/kitc"))
os.remove(os.path.abspath(os.environ["HOME"] + "/.local/bin/kitc"))
if not os.path.exists(os.path.abspath("/usr/local/lib/kit/")):
os.makedirs(os.path.abspath("/usr/local/lib/kit/"))
try:
shutil.rmtree(os.path.abspath("/usr/local/lib/kit/"))
except FileNotFoundError:
pass
shutil.copytree(os.path.abspath("std"), os.path.abspath("/usr/local/lib/kit/"))
else:
# path_add(os.path.abspath("/usr/lib/"))
shutil.copyfile(os.path.abspath(os.environ["HOME"] + "/.local/bin/kitc"), os.path.abspath("/usr/bin/kitc"))
os.remove(os.path.abspath(os.environ["HOME"] + "/.local/bin/kitc"))
if not os.path.exists(os.path.abspath("/usr/lib/kit/")):
os.makedirs(os.path.abspath("/usr/lib/kit/"))
try:
shutil.rmtree(os.path.abspath("/usr/lib/kit/"))
except FileNotFoundError:
pass
shutil.copytree(os.path.abspath("std"), os.path.abspath("/usr/lib/kit/"))
except FileExistsError:
pass
if prompt("Add Visual Studio Code extension?", "y", True):
with cd("utils"):
with cd("vscode-kitlang"):
subprocess.run(["npm", "install", "-g", "vsce"])
this_path = os.path.dirname("/".join(os.path.realpath(__file__).split("/")[:-1]))
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if f.split(".")[-1] == "vsix":
os.remove(f)
subprocess.run(["vsce", "package"])
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if f.split(".")[-1] == "vsix":
subprocess.run(["code", "--uninstall-extension", "kitlang.kitlang"])
subprocess.run(["code", "--install-extension", os.path.abspath(f)])
os.remove(f)
if prompt("Modify Code Runner extension?", "y", True):
with cd(os.path.abspath(os.environ["HOME"] + "/.vscode/extensions")):
this_path = os.path.abspath(os.environ["HOME"] + "/.vscode/extensions")
files_in_here = [f for f in os.listdir(this_path) if os.path.isfile(os.path.join(this_path, f))]
for f in files_in_here:
if "code-runner" in f:
with cd(f):
with open("package.json", "r") as read_file:
data = json.load(read_file)
data["contributes"]["configuration"]["properties"]["code-runner.executorMap"]["default"]["kit"] = "kitc --run"
data["contributes"]["configuration"]["properties"]["code-runner.executorMapByFileExtension"]["default"][".kit"] = "kitc --run"
with open("package.json", "w") as write_file:
json.dump(data, write_file)
notify("Please restart Visual Studio Code", KITYELLOW)
colorama.deinit()