-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCvscode.py
executable file
·278 lines (253 loc) · 10.8 KB
/
Cvscode.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
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
#!/usr/bin/env python3
"""Install VS Code extensions and configuration."""
# Python includes.
import argparse
import functools
import json
import os
import shutil
import subprocess
# Custom includes
import CFunc
# Disable buffered stdout (to ensure prints are in order)
print = functools.partial(print, flush=True)
print("Running {0}".format(__file__))
# Get arguments
parser = argparse.ArgumentParser(description='Install Visual Studio Code configuration.')
parser.add_argument("-t", "--type", help='''Type of configuration. Leave blank for autodetect.
1: Native (Linux)
2: VSCodium Windows
3: VSCode Windows
4: VSCodium
5: VSCodium Flatpak
''', type=int, default=None)
args = parser.parse_args()
# Get user details.
usernamevar, usergroup, userhome = CFunc.getnormaluser()
# Exit if root.
CFunc.is_root(False)
########################## Functions ##########################
def cmd_silent(cmd=list):
"""Run a command silently"""
status = subprocess.run(cmd, check=False, shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
return status
def ce_ins(vscode_cmd=list, extension=str):
"""Install an extension"""
subprocess.run(vscode_cmd + ["--install-extension", extension, "--force"], check=False, shell=False)
def ce_unins(vscode_cmd=list, extension=str):
"""Uninstall an extension"""
subprocess.run(vscode_cmd + ["--uninstall-extension", extension, "--force"], check=False, shell=False)
def codeconfig_installext(vscode_cmd=list):
"""Install vscode extensions"""
print("\nInstalling VS Code extensions.")
ce_ins(vscode_cmd, "ms-pyright.pyright")
ce_ins(vscode_cmd, "ms-python.python")
ce_unins(vscode_cmd, "ms-python.vscode-pylance")
ce_ins(vscode_cmd, "ms-python.flake8")
ce_ins(vscode_cmd, "ms-azuretools.vscode-docker")
ce_ins(vscode_cmd, "mikestead.dotenv")
ce_ins(vscode_cmd, "timonwong.shellcheck")
ce_ins(vscode_cmd, "eamodio.gitlens")
ce_ins(vscode_cmd, "donjayamanne.githistory")
ce_ins(vscode_cmd, "vscode-icons-team.vscode-icons")
ce_ins(vscode_cmd, "yzhang.markdown-all-in-one")
ce_ins(vscode_cmd, "davidanson.vscode-markdownlint")
ce_ins(vscode_cmd, "dendron.dendron")
ce_ins(vscode_cmd, "dendron.dendron-paste-image")
ce_ins(vscode_cmd, "bbenoist.Nix")
ce_ins(vscode_cmd, "danielroedl.meld-diff")
ce_ins(vscode_cmd, "aaron-bond.better-comments")
ce_ins(vscode_cmd, "ms-toolsai.jupyter")
def codeconfig_writeconfiguration(json_data=dict, json_path=str, json_file: str = "settings.json"):
"""Write the config.json"""
if os.path.isdir(json_path):
vscode_userconfig = os.path.join(json_path, json_file)
print("Writing {0}.".format(vscode_userconfig))
with open(vscode_userconfig, 'w') as f:
json.dump(json_data, f, indent=2)
else:
print("ERROR: {0} config path missing. Not writing config.".format(json_path))
########################## Variables ##########################
# Build List
# List positions - en: Enabled
# cmd: Command
# path: settings.json path
code_array = {}
for idx in range(1, 6):
code_array[idx] = {}
code_array[idx]["en"] = [""]
code_array[idx]["cmd"] = []
code_array[idx]["path"] = [""]
# Native (Linux)
code_array[1]["cmd"] = ["code"]
if not CFunc.is_windows() and shutil.which("code"):
code_array[1]["en"] = True
if cmd_silent(code_array[1]["cmd"] + ["-h"]) == 0:
if os.path.isdir(os.path.join(userhome, ".config", "Code - OSS", "User")):
code_array[1]["path"] = os.path.join(userhome, ".config", "Code - OSS", "User")
else:
code_array[1]["path"] = os.path.join(userhome, ".config", "Code", "User")
else:
code_array[1]["en"] = False
# VSCodium Windows
code_array[2]["cmd"] = [os.path.join("C:", os.sep, "Program Files", "VSCodium", "bin", "codium.cmd")]
# Since the command is in an array, index the 0th element to run which on it.
if CFunc.is_windows() and shutil.which(code_array[2]["cmd"][0]):
code_array[2]["en"] = True
else:
code_array[2]["en"] = False
code_array[2]["path"] = os.path.join(userhome, "AppData", "Roaming", "VSCodium", "User")
# VSCode Windows
code_array[3]["cmd"] = [os.path.join("C:", os.sep, "Program Files", "Microsoft VS Code", "bin", "code.cmd")]
# Since the command is in an array, index the 0th element to run which on it.
if CFunc.is_windows() and shutil.which(code_array[3]["cmd"][0]):
code_array[3]["en"] = True
else:
code_array[3]["en"] = False
code_array[3]["path"] = os.path.join(userhome, "AppData", "Roaming", "Code", "User")
# VSCodium
if shutil.which("vscodium") and not CFunc.is_windows():
code_array[4]["cmd"] = ["vscodium"]
code_array[4]["en"] = True
elif shutil.which("codium") and not CFunc.is_windows():
code_array[4]["cmd"] = ["codium"]
code_array[4]["en"] = True
else:
code_array[4]["cmd"] = None
code_array[4]["en"] = False
code_array[4]["path"] = os.path.join(userhome, ".config", "VSCodium", "User")
# VSCodium Flatpak
code_array[5]["cmd"] = ["flatpak", "run", "--command=codium", "com.vscodium.codium"]
if shutil.which("flatpak") and cmd_silent(code_array[5]["cmd"] + ["-h"]) == 0:
code_array[5]["en"] = True
else:
code_array[5]["en"] = False
code_array[5]["path"] = os.path.join(userhome, ".var", "app", "com.vscodium.codium", "config", "VSCodium", "User")
# Force config to use argument type if specified.
if args.type is not None:
for idx in range(1, 6):
if idx != args.type:
code_array[idx]["en"] = False
print(f"""Enabled choices:
1 (Native): {code_array[1]["en"]}
2 (VSCodium Windows): {code_array[2]["en"]}
3 (VSCode Windows): {code_array[3]["en"]}
4 (VSCodium): {code_array[4]["en"]}
5 (VSCodium Flatpak): {code_array[5]["en"]}
""")
########################## Begin Code ##########################
# Process options
for idx in range(1, 6):
# Only process enabled options.
if code_array[idx]["en"] is True:
print("\nProcessing option {0}\n".format(idx))
# Add marketplace for vscodium
if idx == 2 or idx == 4 or idx == 5:
os.makedirs(os.path.dirname(code_array[idx]["path"]), exist_ok=True)
product_json_path = os.path.join(os.path.dirname(code_array[idx]["path"]), "product.json")
# Json data
productjson = {}
# Uncomment if issues with extensions not being enabled.
# productjson["nameShort"] = "Visual Studio Code"
# productjson["nameLong"] = "Visual Studio Code"
productjson["extensionsGallery"] = {
"serviceUrl": "https://marketplace.visualstudio.com/_apis/public/gallery",
"cacheUrl": "https://vscode.blob.core.windows.net/gallery/index",
"itemUrl": "https://marketplace.visualstudio.com/items",
"controlUrl": "",
"recommendationsUrl": ""
}
with open(product_json_path, 'w') as f:
json.dump(productjson, f, indent=2)
# Extensions
codeconfig_installext(code_array[idx]["cmd"])
# Keyboard bindings
kb_data = [
# Dendron/markdown preview fixes
{
"key": "ctrl+shift+v",
"command": "markdown.showPreviewToSide"
},
{
"key": "ctrl+k v",
"command": "-markdown.showPreviewToSide",
"when": "!notebookEditorFocused && editorLangId == 'markdown'"
},
{
"key": "ctrl+k v",
"command": "markdown.showPreview",
"when": "!notebookEditorFocused && editorLangId == 'markdown'"
},
{
"key": "ctrl+shift+v",
"command": "-markdown.showPreview",
"when": "!notebookEditorFocused && editorLangId == 'markdown'"
},
{
"key": "ctrl+shift+b",
"command": "dendron.togglePreview",
"when": "dendron:pluginActive"
},
{
"key": "ctrl+k v",
"command": "-dendron.togglePreview",
"when": "dendron:pluginActive"
},
# Fix jupyter undo cells issue
# https://stackoverflow.com/a/69421121
{"key": "ctrl+z", "command": "-undo"},
{"key": "ctrl+z", "command": "undo", "when": "!notebookEditorFocused || inputFocus"},
{"key": "ctrl+shift+z", "command": "-redo"},
{"key": "ctrl+shift+z", "command": "redo", "when": "!notebookEditorFocused || inputFocus"},
{"key": "ctrl+y", "command": "-redo"},
{"key": "ctrl+y", "command": "redo", "when": "!notebookEditorFocused || inputFocus"},
]
# print(json.dumps(kb_data, indent=4))
codeconfig_writeconfiguration(kb_data, code_array[idx]["path"], "keybindings.json")
# Json data
data = {}
data["workbench.startupEditor"] = "newUntitledFile"
data["window.titleBarStyle"] = "custom"
data["editor.renderWhitespace"] = "all"
data["editor.wordWrap"] = "on"
# Flatpak specific options
if idx == 2 or idx == 5:
# Flatpak specific options
data["terminal.integrated.profiles.linux"] = {
"bash": {
"path": "bash"
},
"fphost": {
"path": "flatpak-spawn",
"args": ["--host", "bash"]
},
}
data["terminal.integrated.defaultProfile.linux"] = "fphost"
data["security.workspace.trust.enabled"] = False
data["telemetry.telemetryLevel"] = "error"
data["workbench.iconTheme"] = "vscode-icons"
data["workbench.colorTheme"] = "Visual Studio Dark"
data["window.commandCenter"] = False
data["vsicons.dontShowNewVersionMessage"] = True
data["update.showReleaseNotes"] = False
data["git.confirmSync"] = False
data["git.autofetch"] = True
data["[nix]"] = {"editor.tabSize": 2}
data["markdown.extension.preview.autoShowPreviewToSide"] = False
data["pasteImage.path"] = "${currentFileDir}/assets/images"
data["gitlens.plusFeatures.enabled"] = False
data["gitlens.showWelcomeOnInstall"] = False
data["gitlens.showWhatsNewAfterUpgrades"] = False
data["gitlens.telemetry.enabled"] = False
# Markdown lint config
data["markdownlint.config"] = {
"default": True,
"MD033": False,
}
# Python Config
data["flake8.args"] = ["--ignore=E501,E302,E266"]
data["python.analysis.typeCheckingMode"] = "off"
# Print the json data for debugging purposes.
# print(json.dumps(data, indent=2))
# Write json configuration
codeconfig_writeconfiguration(data, code_array[idx]["path"])