Skip to content

Commit

Permalink
cli: cleanup build
Browse files Browse the repository at this point in the history
- Remove the `prepare` script entirely
- Variables are now populated from the product.json during build. Most
  variables are mapped automatically, with some special handling in a
	few cases. `build.rs` is now much more self-contained.
- Look for the `product.overrides.json` for vscode developers instead of
  looking for a peer `vscode-distro` folder

Fixes #178691
  • Loading branch information
connor4312 committed Aug 9, 2023
1 parent 1094b5d commit f4eb66e
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 423 deletions.
7 changes: 0 additions & 7 deletions build/azure-pipelines/alpine/cli-build-alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ steps:
sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++" || echo "link exists"
displayName: Install musl build dependencies
- script: node build/azure-pipelines/cli/prepare.js
displayName: Prepare CLI build
env:
VSCODE_CLI_PREPARE_ROOT: $(Build.SourcesDirectory)/.build/distro
VSCODE_QUALITY: ${{ parameters.VSCODE_QUALITY }}
GITHUB_TOKEN: "$(github-distro-mixin-password)"

- template: ../cli/install-rust-posix.yml
parameters:
targets:
Expand Down
93 changes: 0 additions & 93 deletions build/azure-pipelines/cli/prepare.js

This file was deleted.

99 changes: 0 additions & 99 deletions build/azure-pipelines/cli/prepare.ts

This file was deleted.

7 changes: 0 additions & 7 deletions build/azure-pipelines/darwin/cli-build-darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ steps:
tar -xvzf $(Build.ArtifactStagingDirectory)/vscode-internal-openssl-prebuilt-0.0.8.tgz --strip-components=1 --directory=$(Build.ArtifactStagingDirectory)/openssl
displayName: Extract openssl prebuilt
- script: node build/azure-pipelines/cli/prepare.js
displayName: Prepare CLI build
env:
VSCODE_CLI_PREPARE_ROOT: $(Build.SourcesDirectory)/.build/distro
VSCODE_QUALITY: ${{ parameters.VSCODE_QUALITY }}
GITHUB_TOKEN: "$(github-distro-mixin-password)"

- template: ../cli/install-rust-posix.yml
parameters:
targets:
Expand Down
7 changes: 0 additions & 7 deletions build/azure-pipelines/linux/cli-build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ steps:
- bash: sudo apt-get install -yq gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
displayName: Install arm64 toolchains

- script: node build/azure-pipelines/cli/prepare.js
displayName: Prepare CLI build
env:
VSCODE_CLI_PREPARE_ROOT: $(Build.SourcesDirectory)/.build/distro
VSCODE_QUALITY: ${{ parameters.VSCODE_QUALITY }}
GITHUB_TOKEN: "$(github-distro-mixin-password)"

- template: ../cli/install-rust-posix.yml
parameters:
targets:
Expand Down
7 changes: 0 additions & 7 deletions build/azure-pipelines/win32/cli-build-win32.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ steps:
tar -xvzf $(Build.ArtifactStagingDirectory)/vscode-internal-openssl-prebuilt-0.0.8.tgz --strip-components=1 --directory=$(Build.ArtifactStagingDirectory)/openssl
displayName: Extract openssl prebuilt
- powershell: node build/azure-pipelines/cli/prepare.js
displayName: Prepare CLI build
env:
VSCODE_CLI_PREPARE_ROOT: $(Build.SourcesDirectory)/.build/distro
VSCODE_QUALITY: ${{ parameters.VSCODE_QUALITY }}
GITHUB_TOKEN: "$(github-distro-mixin-password)"

- template: ../cli/install-rust-win32.yml
parameters:
targets:
Expand Down
124 changes: 94 additions & 30 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,117 @@
const FILE_HEADER: &str = "/*---------------------------------------------------------------------------------------------\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License. See License.txt in the project root for license information.\n *--------------------------------------------------------------------------------------------*/";

use std::{
collections::HashMap,
env, fs, io,
path::PathBuf,
process::{self, Command},
path::{Path, PathBuf},
process::{self},
str::FromStr,
};

use serde_json::Value;

fn main() {
let files = enumerate_source_files().expect("expected to enumerate files");
ensure_file_headers(&files).expect("expected to ensure file headers");
apply_build_environment_variables();
}

fn apply_build_environment_variables() {
// only do this for local, debug builds
if env::var("PROFILE").unwrap() != "debug" || env::var("VSCODE_CLI_ALREADY_PREPARED").is_ok() {
return;
fn camel_case_to_constant_case(key: &str) -> String {
let mut output = String::new();
let mut prev_upper = false;
for c in key.chars() {
if c.is_uppercase() {
if prev_upper {
output.push(c.to_ascii_lowercase());
} else {
output.push('_');
output.push(c.to_ascii_uppercase());
}
prev_upper = true;
} else if c.is_lowercase() {
output.push(c.to_ascii_uppercase());
prev_upper = false;
} else {
output.push(c);
prev_upper = false;
}
}

let pkg_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut cmd = Command::new(env::var("NODE_PATH").unwrap_or_else(|_| "node".to_string()));
cmd.arg("../build/azure-pipelines/cli/prepare.js");
cmd.current_dir(&pkg_dir);
cmd.env("VSCODE_CLI_PREPARE_OUTPUT", "json");

let mut distro_location = PathBuf::from_str(&pkg_dir).unwrap();
distro_location.pop(); // vscode dir
distro_location.pop(); // parent dir
distro_location.push("vscode-distro"); // distro dir, perhaps?
if distro_location.exists() {
cmd.env("VSCODE_CLI_PREPARE_ROOT", distro_location);
cmd.env("VSCODE_QUALITY", "insider");
output
}

fn set_env_vars_from_map_keys(prefix: &str, map: impl IntoIterator<Item = (String, Value)>) {
let mut win32_app_ids = vec![];

for (key, value) in map {
//#region special handling
let value = match key.as_str() {
"tunnelServerQualities" | "serverLicense" => Value::String(serde_json::to_string(&value).unwrap()),
"nameLong" => {
if let Value::String(s) = &value {
let idx = s.find(" - ");
println!(
"cargo:rustc-env=VSCODE_CLI_QUALITYLESS_PRODUCT_NAME={}",
idx.map(|i| &s[..i]).unwrap_or(s)
);
}

value
}
"tunnelApplicationConfig" => {
if let Value::Object(v) = value {
set_env_vars_from_map_keys(&format!("{}_{}", prefix, "TUNNEL"), v);
}
continue;
}
_ => value,
};
if key.contains("win32") && key.contains("AppId") {
if let Value::String(s) = value {
win32_app_ids.push(s);
continue;
}
}
//#endregion

if let Value::String(s) = value {
println!(
"cargo:rustc-env={}_{}={}",
prefix,
camel_case_to_constant_case(&key),
s
);
}
}

let output = cmd.output().expect("expected to run prepare script");
if !output.status.success() {
eprint!(
"error running prepare script: {}",
String::from_utf8_lossy(&output.stderr)
if !win32_app_ids.is_empty() {
println!(
"cargo:rustc-env=VSCODE_CLI_WIN32_APP_IDS={}",
win32_app_ids.join(",")
);
process::exit(output.status.code().unwrap_or(1));
}
}

let vars = serde_json::from_slice::<Vec<(String, String)>>(&output.stdout)
.expect("expected to deserialize output");
for (key, value) in vars {
println!("cargo:rustc-env={}={}", key, value);
}
fn apply_build_from_package_json(path: &Path) {
let file = fs::read_to_string(path).expect("err reading package.json");
let json: HashMap<String, Value> =
serde_json::from_str(&file).expect("err deserializing package.json");
set_env_vars_from_map_keys("VSCODE_CLI", json);
}

fn apply_build_environment_variables() {
match env::var("VSCODE_CLI_PRODUCT_JSON") {
Ok(v) => apply_build_from_package_json(&PathBuf::from_str(&v).unwrap()),
Err(_) => {
let parent = env::current_dir().unwrap().join("..");
apply_build_from_package_json(&parent.join("product.json"));

let overrides = parent.join("product.overrides.json");
if overrides.exists() {
apply_build_from_package_json(&overrides);
}
}
};
}

fn ensure_file_headers(files: &[PathBuf]) -> Result<(), io::Error> {
Expand Down
Loading

0 comments on commit f4eb66e

Please sign in to comment.