Skip to content

Commit 8e1b9c8

Browse files
committed
Refactor: define and use common strings.
Fix: correct install of cargo binstall on non-Windows platforms.
1 parent 0e32543 commit 8e1b9c8

File tree

1 file changed

+70
-81
lines changed

1 file changed

+70
-81
lines changed

builder/src/main.rs

Lines changed: 70 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ struct TypeScriptBuildOptions {
112112
skip_check_errors: bool,
113113
}
114114

115+
// Constants
116+
// ---------
117+
static VSCODE_PATH: &str = "../extensions/VSCode";
118+
static CLIENT_PATH: &str = "../client";
119+
static BUILDER_PATH: &str = "../builder";
120+
115121
// Code
116122
// ----
117123
//
@@ -307,7 +313,7 @@ fn patch_client_libs() -> io::Result<()> {
307313
selectionNotFocus = this.view.state.facet(editable) ? focused : hasSelection(this.dom, this.view.observer.selectionRange)",
308314
" let selectionNotFocus = !focused && !(this.view.state.facet(editable) || this.dom.tabIndex > -1) &&
309315
hasSelection(this.dom, this.view.observer.selectionRange) && !(activeElt && this.dom.contains(activeElt));",
310-
"../client/node_modules/@codemirror/view/dist/index.js"
316+
&format!("{CLIENT_PATH}/node_modules/@codemirror/view/dist/index.js")
311317
)?;
312318
// In [older
313319
// releases](https://www.tiny.cloud/docs/tinymce/5/6.0-upcoming-changes/#options),
@@ -317,34 +323,34 @@ fn patch_client_libs() -> io::Result<()> {
317323
patch_file(
318324
" wc-mermaid",
319325
"const whitespaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea video audio iframe object code",
320-
"../client/node_modules/tinymce/tinymce.js",
326+
&format!("{CLIENT_PATH}/node_modules/tinymce/tinymce.js"),
321327
)?;
322328

323329
// Copy across the parts of MathJax that are needed, since bundling it is
324330
// difficult.
325-
remove_dir_all_if_exists("../client/static/mathjax")?;
331+
remove_dir_all_if_exists(format!("{CLIENT_PATH}/static/mathjax"))?;
326332
for subdir in ["a11y", "adaptors", "input", "output", "sre", "ui"] {
327333
quick_copy_dir(
328-
format!("../client/node_modules/mathjax/{subdir}/"),
329-
format!("../client/static/mathjax/{subdir}"),
334+
format!("{CLIENT_PATH}/node_modules/mathjax/{subdir}/"),
335+
format!("{CLIENT_PATH}/static/mathjax/{subdir}"),
330336
None,
331337
)?;
332338
}
333339
quick_copy_dir(
334-
"../client/node_modules/mathjax/",
335-
"../client/static/mathjax",
336-
Some("tex-chtml.js"),
340+
format!("{CLIENT_PATH}/node_modules/mathjax/"),
341+
format!("{CLIENT_PATH}/static/mathjax"),
342+
Some("tex-chtml.js".to_string()),
337343
)?;
338344
quick_copy_dir(
339-
"../client/node_modules/@mathjax/mathjax-newcm-font/chtml/",
340-
"../client/static/mathjax-newcm-font/chtml",
345+
format!("{CLIENT_PATH}/node_modules/@mathjax/mathjax-newcm-font/chtml/"),
346+
format!("{CLIENT_PATH}/static/mathjax-newcm-font/chtml"),
341347
None,
342348
)?;
343349
// Copy over the graphviz files needed.
344350
quick_copy_dir(
345-
"../client/node_modules/graphviz-webcomponent/dist/",
346-
"../client/static/graphviz-webcomponent",
347-
Some("renderer.min.js*"),
351+
format!("{CLIENT_PATH}/node_modules/graphviz-webcomponent/dist/"),
352+
format!("{CLIENT_PATH}/static/graphviz-webcomponent"),
353+
Some("renderer.min.js*".to_string()),
348354
)?;
349355

350356
Ok(())
@@ -356,27 +362,18 @@ fn run_install(dev: bool) -> io::Result<()> {
356362
}
357363
// See [the client manifest](../../client/package.json5) for an explanation
358364
// of `--no-frozen-lockfile`.
359-
run_script("pnpm", &["install"], "../client", true)?;
365+
run_script("pnpm", &["install"], CLIENT_PATH, true)?;
360366
patch_client_libs()?;
361-
run_script("pnpm", &["install"], "../extensions/VSCode", true)?;
367+
run_script("pnpm", &["install"], VSCODE_PATH, true)?;
362368
run_cmd!(
363369
info "Builder: cargo fetch";
364-
cargo fetch --manifest-path=../builder/Cargo.toml;
370+
cargo fetch --manifest-path=$BUILDER_PATH/Cargo.toml;
365371
info "VSCode extension: cargo fetch";
366-
cargo fetch --manifest-path=../extensions/VSCode/Cargo.toml;
372+
cargo fetch --manifest-path=$VSCODE_PATH/Cargo.toml;
367373
info "cargo fetch";
368374
cargo fetch;
369375
)?;
370376
if dev {
371-
// If the dist install reports an error, perhaps it's already installed.
372-
if run_cmd!(
373-
info "cargo install cargo-dist";
374-
cargo install --locked cargo-dist;
375-
)
376-
.is_err()
377-
{
378-
run_cmd!(dist --version;)?;
379-
}
380377
// Install the cargo binstall binary, taken from the
381378
// [docs](https://docs.rs/crate/cargo-binstall/1.15.5).
382379
#[cfg(windows)]
@@ -385,9 +382,12 @@ fn run_install(dev: bool) -> io::Result<()> {
385382
}?;
386383
#[cfg(not(windows))]
387384
run_cmd! {
388-
curl -L --proto "=https" --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
385+
r#"curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash"#
389386
}?;
387+
390388
run_cmd!(
389+
info "cargo binstall cargo-dist";
390+
cargo binstall cargo-dist;
391391
info "cargo binstall cargo-outdated";
392392
cargo binstall cargo-outdated --disable-telemetry ;
393393
info "cargo binstall cargo-sort";
@@ -398,25 +398,25 @@ fn run_install(dev: bool) -> io::Result<()> {
398398
}
399399

400400
fn run_update() -> io::Result<()> {
401-
run_script("pnpm", &["update"], "../client", true)?;
401+
run_script("pnpm", &["update"], CLIENT_PATH, true)?;
402402
patch_client_libs()?;
403-
run_script("pnpm", &["update"], "../extensions/VSCode", true)?;
403+
run_script("pnpm", &["update"], VSCODE_PATH, true)?;
404404
run_cmd!(
405405
info "Builder: cargo update";
406-
cargo update --manifest-path=../builder/Cargo.toml;
406+
cargo update --manifest-path=$BUILDER_PATH/Cargo.toml;
407407
info "VSCoe extension: cargo update";
408-
cargo update --manifest-path=../extensions/VSCode/Cargo.toml;
408+
cargo update --manifest-path=$VSCODE_PATH/Cargo.toml;
409409
info "cargo update";
410410
cargo update;
411411
)?;
412412
// Simply display outdated dependencies, but don't consider them an error.
413-
run_script("pnpm", &["outdated"], "../client", false)?;
414-
run_script("pnpm", &["outdated"], "../extensions/VSCode", false)?;
413+
run_script("pnpm", &["outdated"], CLIENT_PATH, false)?;
414+
run_script("pnpm", &["outdated"], VSCODE_PATH, false)?;
415415
run_cmd!(
416416
info "Builder: cargo outdated";
417-
cargo outdated --manifest-path=../builder/Cargo.toml;
417+
cargo outdated --manifest-path=$BUILDER_PATH/Cargo.toml;
418418
info "VSCode extension: cargo outdated";
419-
cargo outdated --manifest-path=../extensions/VSCode/Cargo.toml;
419+
cargo outdated --manifest-path=$VSCODE_PATH/Cargo.toml;
420420
info "cargo outdated";
421421
cargo outdated;
422422
)?;
@@ -431,27 +431,22 @@ fn run_test() -> io::Result<()> {
431431
cargo clippy --all-targets -- -D warnings;
432432
cargo fmt --all --check;
433433
info "Builder: cargo clippy and fmt";
434-
cargo clippy --all-targets --manifest-path=../builder/Cargo.toml -- -D warnings;
435-
cargo fmt --all --check --manifest-path=../builder/Cargo.toml;
434+
cargo clippy --all-targets --manifest-path=$BUILDER_PATH/Cargo.toml -- -D warnings;
435+
cargo fmt --all --check --manifest-path=$BUILDER_PATH/Cargo.toml;
436436
info "VSCode extension: cargo clippy and fmt";
437-
cargo clippy --all-targets --manifest-path=../extensions/VSCode/Cargo.toml -- -D warnings;
438-
cargo fmt --all --check --manifest-path=../extensions/VSCode/Cargo.toml;
437+
cargo clippy --all-targets --manifest-path=$VSCODE_PATH/Cargo.toml -- -D warnings;
438+
cargo fmt --all --check --manifest-path=$VSCODE_PATH/Cargo.toml;
439439
info "cargo sort";
440440
cargo sort --check;
441-
cd ../builder;
441+
cd $BUILDER_PATH;
442442
info "Builder: cargo sort";
443443
cargo sort --check;
444-
cd ../extensions/VSCode;
444+
cd $VSCODE_PATH;
445445
info "VSCode extension: cargo sort";
446446
cargo sort --check;
447447
)?;
448-
run_script("npx", &["prettier", "src", "--check"], "../client", true)?;
449-
run_script(
450-
"npx",
451-
&["prettier", "src", "--check"],
452-
"../extensions/VSCode",
453-
true,
454-
)?;
448+
run_script("npx", &["prettier", "src", "--check"], CLIENT_PATH, true)?;
449+
run_script("npx", &["prettier", "src", "--check"], VSCODE_PATH, true)?;
455450
run_build()?;
456451
// Verify that compiling for release produces no errors.
457452
run_cmd!(
@@ -461,9 +456,9 @@ fn run_test() -> io::Result<()> {
461456
)?;
462457
run_cmd!(
463458
info "Builder: cargo test";
464-
cargo test --manifest-path=../builder/Cargo.toml;
459+
cargo test --manifest-path=$BUILDER_PATH/Cargo.toml;
465460
info "VSCode extension: cargo test";
466-
cargo test --manifest-path=../extensions/VSCode/Cargo.toml;
461+
cargo test --manifest-path=$VSCODE_PATH/Cargo.toml;
467462
info "cargo test";
468463
cargo test;
469464
)?;
@@ -473,14 +468,14 @@ fn run_test() -> io::Result<()> {
473468
fn run_build() -> io::Result<()> {
474469
run_cmd!(
475470
info "Builder: cargo build";
476-
cargo build --manifest-path=../builder/Cargo.toml;
471+
cargo build --manifest-path=$BUILDER_PATH/Cargo.toml;
477472
info "cargo build";
478473
cargo build;
479474
info "cargo test export_bindings";
480475
cargo test export_bindings;
481476
)?;
482477
// Clean out all bundled files before the rebuild.
483-
remove_dir_all_if_exists("../client/static/bundled")?;
478+
remove_dir_all_if_exists(format!("{CLIENT_PATH}/static/bundled"))?;
484479
run_client_build(false, false)?;
485480
run_extensions_build(false, false)?;
486481
Ok(())
@@ -502,9 +497,6 @@ fn run_client_build(
502497

503498
let esbuild = PathBuf::from_slash("node_modules/.bin/esbuild");
504499
let distflag = if dist { "--minify" } else { "--sourcemap" };
505-
// This makes the program work from either the `server/` or `client/`
506-
// directories.
507-
let rel_path = "../client";
508500

509501
// The main build for the Client.
510502
run_script(
@@ -523,7 +515,7 @@ fn run_client_build(
523515
"--metafile=meta.json",
524516
"--entry-names=[dir]/[name]-[hash]",
525517
],
526-
rel_path,
518+
CLIENT_PATH,
527519
true,
528520
)?;
529521
// <a id="#pdf.js>The PDF viewer for use with VSCode. Built it separately,
@@ -541,13 +533,13 @@ fn run_client_build(
541533
"--loader:.svg=dataurl",
542534
"--loader:.gif=dataurl",
543535
],
544-
rel_path,
536+
CLIENT_PATH,
545537
true,
546538
)?;
547539
// Copy over the cmap (color map?) files, which the bundler doesn't handle.
548540
quick_copy_dir(
549-
format!("{rel_path}/node_modules/pdfjs-dist/cmaps/"),
550-
format!("{rel_path}/static/bundled/node_modules/pdfjs-dist/cmaps/"),
541+
format!("{CLIENT_PATH}/node_modules/pdfjs-dist/cmaps/"),
542+
format!("{CLIENT_PATH}/static/bundled/node_modules/pdfjs-dist/cmaps/"),
551543
None,
552544
)?;
553545
// The HashReader isn't bundled; instead, it's used to translate the JSON
@@ -561,16 +553,16 @@ fn run_client_build(
561553
"--platform=node",
562554
"--format=esm",
563555
],
564-
rel_path,
556+
CLIENT_PATH,
565557
true,
566558
)?;
567-
run_script("node", &["HashReader.js"], rel_path, true)?;
559+
run_script("node", &["HashReader.js"], CLIENT_PATH, true)?;
568560
// Finally, check the TypeScript with the (slow) TypeScript compiler.
569561
if !skip_check_errors {
570562
run_script(
571563
PathBuf::from_slash("node_modules/.bin/tsc"),
572564
&["-noEmit"],
573-
rel_path,
565+
CLIENT_PATH,
574566
true,
575567
)?;
576568
}
@@ -587,18 +579,15 @@ fn run_extensions_build(
587579
) -> io::Result<()> {
588580
let esbuild = PathBuf::from_slash("node_modules/.bin/esbuild");
589581
let distflag = if dist { "--minify" } else { "--sourcemap" };
590-
// This makes the program work from either the `server/` or `client/`
591-
// directories.
592-
let rel_path = "../extensions/VSCode";
593582

594583
// The NAPI build.
595584
let mut napi_args = vec!["napi", "build", "--platform", "--output-dir", "src"];
596585
if dist {
597586
napi_args.push("--release");
598587
}
599-
run_script("npx", &napi_args, rel_path, true)?;
588+
run_script("npx", &napi_args, VSCODE_PATH, true)?;
600589

601-
// The main build for the Client.
590+
// The main build for the extension.
602591
run_script(
603592
&esbuild,
604593
&[
@@ -616,15 +605,15 @@ fn run_extensions_build(
616605
// generated.
617606
"--asset-names=[name]",
618607
],
619-
rel_path,
608+
VSCODE_PATH,
620609
true,
621610
)?;
622611
// Finally, check the TypeScript with the (slow) TypeScript compiler.
623612
if !skip_check_errors {
624613
run_script(
625614
PathBuf::from_slash("node_modules/.bin/tsc"),
626615
&["-noEmit"],
627-
rel_path,
616+
VSCODE_PATH,
628617
true,
629618
)?;
630619
}
@@ -636,40 +625,40 @@ fn run_change_version(new_version: &String) -> io::Result<()> {
636625
let replacement_string = format!("${{1}}{new_version}${{2}}");
637626
search_and_replace_file("Cargo.toml", cargo_regex, &replacement_string)?;
638627
search_and_replace_file(
639-
"../extensions/VSCode/Cargo.toml",
628+
format!("{VSCODE_PATH}/Cargo.toml"),
640629
cargo_regex,
641630
&replacement_string,
642631
)?;
643632
search_and_replace_file(
644-
"../client/package.json5",
645-
r#"(\r?\n version: ')[\d.]+(',\r?\n)"#,
633+
format!("{VSCODE_PATH}/package.json"),
634+
r#"(\r?\n "version": ")[\d.]+(",\r?\n)"#,
646635
&replacement_string,
647636
)?;
648637
search_and_replace_file(
649-
"../extensions/VSCode/package.json",
650-
r#"(\r?\n "version": ")[\d.]+(",\r?\n)"#,
638+
format!("{CLIENT_PATH}/package.json5"),
639+
r#"(\r?\n version: ')[\d.]+(',\r?\n)"#,
651640
&replacement_string,
652641
)?;
653642
Ok(())
654643
}
655644

656645
fn run_prerelease() -> io::Result<()> {
657646
// Clean out all bundled files before the rebuild.
658-
remove_dir_all_if_exists("../client/static/bundled")?;
647+
remove_dir_all_if_exists(format!("{CLIENT_PATH}/static/bundled"))?;
659648
run_install(true)?;
660649
run_client_build(true, false)
661650
}
662651

663652
fn run_postrelease(target: &str) -> io::Result<()> {
664653
// Copy all the Client static files needed by the embedded Server to the
665654
// VSCode extension.
666-
let client_static_dir = "../extensions/VSCode/static";
667-
remove_dir_all_if_exists(client_static_dir)?;
668-
quick_copy_dir("../client/static/", client_static_dir, None)?;
669-
copy_file("log4rs.yml", "../extensions/VSCode/log4rs.yml")?;
655+
let client_static_dir = format!("{VSCODE_PATH}/static");
656+
remove_dir_all_if_exists(&client_static_dir)?;
657+
quick_copy_dir(&format!("{CLIENT_PATH}/static/"), &client_static_dir, None)?;
658+
copy_file("log4rs.yml", &format!("{VSCODE_PATH}/log4rs.yml"))?;
670659
copy_file(
671660
"hashLocations.json",
672-
"../extensions/VSCode/hashLocations.json",
661+
&format!("{VSCODE_PATH}/hashLocations.json"),
673662
)?;
674663

675664
// Translate from the target triple to VSCE's target parameter.
@@ -692,7 +681,7 @@ fn run_postrelease(target: &str) -> io::Result<()> {
692681
"--target",
693682
vsce_target,
694683
],
695-
"../extensions/VSCode",
684+
VSCODE_PATH,
696685
true,
697686
)?;
698687

0 commit comments

Comments
 (0)