author | wenzelm |
Sat, 05 Mar 2022 14:31:29 +0100 | |
changeset 75223 | 8c09e1f82f81 |
parent 75215 | 1129e82dc1ec |
child 75224 | 419781ac89bf |
permissions | -rw-r--r-- |
75083 | 1 |
/* Title: Tools/VSCode/src/vscode_setup.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Setup VSCode from VSCodium distribution. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.vscode |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
75155
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
12 |
import java.security.MessageDigest |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
13 |
import java.util.Base64 |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
14 |
|
75083 | 15 |
|
16 |
object VSCode_Setup |
|
17 |
{ |
|
18 |
/* global resources */ |
|
19 |
||
20 |
def vscode_home: Path = Path.variable("ISABELLE_VSCODE_HOME") |
|
21 |
def vscode_settings: Path = Path.variable("ISABELLE_VSCODE_SETTINGS") |
|
75157 | 22 |
def vscode_settings_user: Path = vscode_settings + Path.explode("user-data/User/settings.json") |
75083 | 23 |
def vscode_version: String = Isabelle_System.getenv_strict("ISABELLE_VSCODE_VERSION") |
75209
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
24 |
def vscode_workspace: Path = Path.variable("ISABELLE_VSCODE_WORKSPACE") |
75083 | 25 |
|
75214
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
26 |
def vscodium_home: Path = Path.variable("ISABELLE_VSCODIUM_HOME") |
75215 | 27 |
def vscodium_home_ok(): Boolean = |
28 |
try { vscodium_home.is_dir } |
|
29 |
catch { case ERROR(_) => false } |
|
75214
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
30 |
|
75089
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
31 |
def exe_path(dir: Path): Path = dir + Path.explode("bin/codium") |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
32 |
|
75083 | 33 |
def vscode_installation(version: String, platform: Platform.Family.Value): (Boolean, Path) = |
75156 | 34 |
{ |
35 |
val platform_name = |
|
36 |
if (platform == Platform.Family.windows) Platform.Family.native(platform) |
|
37 |
else Platform.Family.standard(platform) |
|
38 |
val install_dir = |
|
39 |
vscode_settings + Path.basic("installation") + |
|
40 |
Path.basic(version) + Path.basic(platform_name) |
|
41 |
val install_ok = exe_path(install_dir).is_file |
|
42 |
(install_ok, install_dir) |
|
43 |
} |
|
75083 | 44 |
|
45 |
||
75155
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
46 |
/* patch resources */ |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
47 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
48 |
// see https://github.com/microsoft/vscode/blob/main/build/gulpfile.vscode.js |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
49 |
// function computeChecksum(filename) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
50 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
51 |
def file_checksum(path: Path): String = |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
52 |
{ |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
53 |
val digest = MessageDigest.getInstance("MD5") |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
54 |
digest.update(Bytes.read(path).array) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
55 |
Bytes(Base64.getEncoder.encode(digest.digest())) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
56 |
.text.replaceAll("=", "") |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
57 |
} |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
58 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
59 |
def patch_resources(dir: Path): Unit = |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
60 |
{ |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
61 |
HTML.init_fonts(dir + Path.explode("app/out/vs/base/browser/ui")) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
62 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
63 |
val workbench_css = dir + Path.explode("app/out/vs/workbench/workbench.desktop.main.css") |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
64 |
val checksum1 = file_checksum(workbench_css) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
65 |
File.append(workbench_css, "\n\n" + HTML.fonts_css_dir(prefix = "../base/browser/ui")) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
66 |
val checksum2 = file_checksum(workbench_css) |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
67 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
68 |
val file_name = workbench_css.file_name |
75205 | 69 |
File.change_lines(dir + Path.explode("app/product.json")) { _.map(line => |
70 |
if (line.containsSlice(file_name) && line.contains(checksum1)) { |
|
71 |
line.replace(checksum1, checksum2) |
|
72 |
} |
|
73 |
else line) |
|
75202 | 74 |
} |
75155
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
75 |
} |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
76 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
77 |
|
75209
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
78 |
/* workspace */ |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
79 |
|
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
80 |
def init_workspace(dir: Path): Unit = |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
81 |
{ |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
82 |
Isabelle_System.make_directory(dir) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
83 |
Isabelle_System.chmod("700", dir) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
84 |
|
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
85 |
File.change(dir + Path.explode("symbols.json"), init = true) { _ => |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
86 |
JSON.Format( |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
87 |
for (entry <- Symbol.symbols.entries; code <- entry.code) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
88 |
yield JSON.Object( |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
89 |
"symbol" -> entry.symbol, |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
90 |
"name" -> entry.name, |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
91 |
"code" -> code, |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
92 |
"abbrevs" -> entry.abbrevs |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
93 |
) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
94 |
) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
95 |
} |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
96 |
} |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
97 |
|
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
98 |
|
75083 | 99 |
/* vscode setup */ |
100 |
||
101 |
val default_download_url: String = "https://github.com/VSCodium/vscodium/releases/download" |
|
75088 | 102 |
def default_platform: Platform.Family.Value = Platform.family |
75083 | 103 |
|
75166 | 104 |
private val init_settings = """ { |
105 |
"editor.fontFamily": "'Isabelle DejaVu Sans Mono'", |
|
106 |
"editor.fontSize": 18, |
|
107 |
"editor.lineNumbers": "off", |
|
108 |
"editor.renderIndentGuides": false, |
|
109 |
"editor.rulers": [80, 100], |
|
75171 | 110 |
"editor.unicodeHighlight.ambiguousCharacters": false, |
75166 | 111 |
"extensions.autoCheckUpdates": false, |
75167 | 112 |
"extensions.autoUpdate": false, |
75178 | 113 |
"terminal.integrated.fontFamily": "monospace", |
75167 | 114 |
"update.mode": "none" |
75166 | 115 |
} |
116 |
""" |
|
117 |
||
75083 | 118 |
def vscode_setup( |
119 |
check: Boolean = false, |
|
120 |
download_url: String = default_download_url, |
|
121 |
version: String = vscode_version, |
|
75088 | 122 |
platform: Platform.Family.Value = default_platform, |
75099 | 123 |
quiet: Boolean = false, |
75100 | 124 |
fresh: Boolean = false, |
75083 | 125 |
progress: Progress = new Progress): Unit = |
126 |
{ |
|
127 |
val (install_ok, install_dir) = vscode_installation(version, platform) |
|
128 |
||
75163 | 129 |
if (!vscode_settings_user.is_file) { |
130 |
Isabelle_System.make_directory(vscode_settings_user.dir) |
|
131 |
File.write(vscode_settings_user, init_settings) |
|
132 |
} |
|
75157 | 133 |
|
75083 | 134 |
if (check) { |
75215 | 135 |
if (vscodium_home_ok()) { |
75214
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
136 |
init_workspace(vscode_workspace) |
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
137 |
progress.echo(vscodium_home.expand.implode) |
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
138 |
} |
a51a0a704854
build component for VSCodium (cross-compiled from sources for all platforms);
wenzelm
parents:
75209
diff
changeset
|
139 |
else if (install_ok) { |
75209
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
140 |
init_workspace(vscode_workspace) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
141 |
progress.echo(install_dir.expand.implode) |
4187f6f18232
provide symbols statically via ISABELLE_VSCODE_WORKSPACE, instead of LSP/PIDE protocol;
wenzelm
parents:
75205
diff
changeset
|
142 |
} |
75083 | 143 |
else { |
144 |
error("Bad Isabelle/VSCode installation: " + install_dir.expand + |
|
75092 | 145 |
"\n(use \"isabelle vscode_setup\" for download and installation)") |
75083 | 146 |
} |
147 |
} |
|
148 |
else { |
|
149 |
if (install_ok) { |
|
150 |
progress.echo_warning("Isabelle/VSCode installation already present: " + install_dir.expand) |
|
151 |
} |
|
75100 | 152 |
if (!install_ok || fresh) { |
75166 | 153 |
val download_name = |
154 |
{ |
|
155 |
val a = "VSCodium" |
|
156 |
val (b, c) = |
|
157 |
platform match { |
|
158 |
case Platform.Family.linux_arm => ("linux-arm64", "tar.gz") |
|
159 |
case Platform.Family.linux => ("linux-x64", "tar.gz") |
|
160 |
case Platform.Family.macos => ("darwin-x64", "zip") |
|
161 |
case Platform.Family.windows => ("win32-x64", "zip") |
|
162 |
} |
|
163 |
a + "-" + b + "-" + version + "." + c |
|
164 |
} |
|
165 |
val is_zip = download_name.endsWith(".zip") |
|
75100 | 166 |
if (is_zip) Isabelle_System.require_command("unzip", test = "-h") |
75083 | 167 |
|
75100 | 168 |
Isabelle_System.make_directory(install_dir) |
169 |
val exe = exe_path(install_dir) |
|
75089
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
170 |
|
75100 | 171 |
Isabelle_System.with_tmp_file("download")(download => |
75083 | 172 |
{ |
75166 | 173 |
Isabelle_System.download_file(download_url + "/" + version + "/" + download_name, |
174 |
download, progress = if (quiet) new Progress else progress) |
|
75099 | 175 |
if (!quiet) progress.echo("Installing " + install_dir.expand) |
75083 | 176 |
if (is_zip) { |
177 |
Isabelle_System.bash("unzip -x " + File.bash_path(download), |
|
178 |
cwd = install_dir.file).check |
|
179 |
} |
|
180 |
else { |
|
181 |
Isabelle_System.gnutar("-xzf " + File.bash_path(download), |
|
182 |
dir = install_dir).check |
|
183 |
} |
|
75155
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
184 |
|
75089
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
185 |
platform match { |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
186 |
case Platform.Family.macos => |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
187 |
Isabelle_System.make_directory(exe.dir) |
75223 | 188 |
File.write(exe, Build_VSCodium.macos_exe) |
75089
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
189 |
File.set_executable(exe, true) |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
190 |
case Platform.Family.windows => |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
191 |
val files1 = File.find_files(exe.dir.file) |
75223 | 192 |
val files2 = File.find_files(install_dir.file, pred = Build_VSCodium.is_windows_exe) |
75089
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
193 |
for (file <- files1 ::: files2) File.set_executable(File.path(file), true) |
1e230ff31fb0
provide macos_exe, based on bin/codium from linux;
wenzelm
parents:
75088
diff
changeset
|
194 |
case _ => |
75083 | 195 |
} |
75155
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
196 |
|
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
197 |
patch_resources( |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
198 |
if (platform == Platform.Family.macos) { |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
199 |
install_dir + Path.explode("VSCodium.app/Contents/Resources") |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
200 |
} |
0b6c43a87fa6
support Isabelle fonts via patch of vscode resources;
wenzelm
parents:
75100
diff
changeset
|
201 |
else install_dir + Path.explode("resources")) |
75083 | 202 |
}) |
75100 | 203 |
} |
75083 | 204 |
} |
205 |
} |
|
206 |
||
207 |
||
208 |
/* Isabelle tool wrapper */ |
|
209 |
||
210 |
val isabelle_tool = |
|
211 |
Isabelle_Tool("vscode_setup", "setup VSCode from VSCodium distribution", |
|
212 |
Scala_Project.here, args => |
|
213 |
{ |
|
214 |
var check = false |
|
215 |
var download_url = default_download_url |
|
216 |
var version = vscode_version |
|
75100 | 217 |
var fresh = false |
75088 | 218 |
var platforms = List(default_platform) |
75099 | 219 |
var quiet = false |
75083 | 220 |
|
221 |
val getopts = Getopts(""" |
|
222 |
Usage: vscode_setup [OPTIONS] |
|
223 |
||
224 |
Options are: |
|
225 |
-C check and print installation directory |
|
226 |
-U URL download URL |
|
227 |
(default: """" + default_download_url + """") |
|
228 |
-V VERSION version (default: """" + vscode_version + """") |
|
75100 | 229 |
-f force fresh installation |
75088 | 230 |
-p NAMES platform families: comma-separated names |
231 |
(""" + commas_quote(Platform.Family.list.map(_.toString)) + """, default: """ + |
|
232 |
quote(default_platform.toString) + """) |
|
75099 | 233 |
-q quiet mode |
75083 | 234 |
|
235 |
Maintain local installation of VSCode, see also https://vscodium.com |
|
236 |
||
237 |
Option -C checks the existing installation (without download), and |
|
238 |
prints its directory location. |
|
75163 | 239 |
|
240 |
The following initial settings are provided for a fresh installation: |
|
75165 | 241 |
""" + init_settings, |
75083 | 242 |
"C" -> (_ => check = true), |
243 |
"U:" -> (arg => download_url = arg), |
|
244 |
"V:" -> (arg => version = arg), |
|
75100 | 245 |
"f" -> (_ => fresh = true), |
75088 | 246 |
"p:" -> (arg => platforms = Library.space_explode(',', arg).map(Platform.Family.parse)), |
75099 | 247 |
"q" -> (_ => quiet = true)) |
75083 | 248 |
|
249 |
val more_args = getopts(args) |
|
250 |
if (more_args.nonEmpty) getopts.usage() |
|
251 |
||
252 |
val progress = new Console_Progress() |
|
75088 | 253 |
for (platform <- platforms) { |
254 |
vscode_setup(check = check, download_url = download_url, version = version, |
|
75100 | 255 |
platform = platform, quiet = quiet, fresh = fresh, progress = progress) |
75088 | 256 |
} |
75083 | 257 |
}) |
258 |
} |