Skip to content

Commit

Permalink
cli: automatically use distro qualities if available (microsoft#166961)
Browse files Browse the repository at this point in the history
A fully-functioning CLI with tunnel capabilities requires product-specific
configuration that is not included in the OSS. This change has the CLI's build
script automatically look for a peer `vscode-distro` folder and, in a debug
build, set its build variables based on that.

It also works to set correct variables if vscode-distro is not found, based on
the OSS product.json (though this is not sufficient to run a fully fledged
tunnel.)
  • Loading branch information
connor4312 authored Nov 22, 2022
1 parent fb54bb1 commit 94ee5f5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
18 changes: 11 additions & 7 deletions build/azure-pipelines/cli/prepare.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions build/azure-pipelines/cli/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as packageJson from '../../../package.json';

const root = path.dirname(path.dirname(path.dirname(__dirname)));
const root = process.env.VSCODE_CLI_PREPARE_ROOT || path.dirname(path.dirname(path.dirname(__dirname)));
const readJSON = (path: string) => JSON.parse(fs.readFileSync(path, 'utf8'));

let productJsonPath: string;
Expand All @@ -19,8 +19,7 @@ if (isOSS) {
productJsonPath = path.join(root, 'quality', process.env.VSCODE_QUALITY!, 'product.json');
}


console.log('Loading product.json from', productJsonPath);
console.error('Loading product.json from', productJsonPath);
const product = readJSON(productJsonPath);
const allProductsAndQualities = isOSS ? [product] : fs.readdirSync(path.join(root, 'quality'))
.map(quality => ({ quality, json: readJSON(path.join(root, 'quality', quality, 'product.json')) }));
Expand Down Expand Up @@ -78,13 +77,16 @@ const setLauncherEnvironmentVars = () => {
],
]);

console.log(JSON.stringify([...vars].reduce((obj, kv) => ({ ...obj, [kv[0]]: kv[1] }), {})));

for (const [key, value] of vars) {
if (value) {
console.log(`##vso[task.setvariable variable=${key}]${value}`);
if (process.env.VSCODE_CLI_PREPARE_OUTPUT === 'json') {
console.log(JSON.stringify([...vars].filter(([, v]) => !!v)));
} else {
for (const [key, value] of vars) {
if (value) {
console.log(`##vso[task.setvariable variable=${key}]${value}`);
}
}
}

};

if (require.main === module) {
Expand Down
4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async-trait = "0.1"
log = "0.4"
const_format = "0.2"

[build-dependencies]
serde = { version = "1.0" }
serde_json = { version = "1.0" }

[target.'cfg(windows)'.dependencies]
windows-service = "0.5"
winreg = "0.10"
Expand Down
45 changes: 44 additions & 1 deletion cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,54 @@

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::{env, fs, io, path::PathBuf, process};
use std::{
env, fs, io,
path::PathBuf,
process::{self, Command},
str::FromStr,
};

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" {
return;
}

let pkg_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut cmd = Command::new("node");
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");
}

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)
);
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 ensure_file_headers(files: &[PathBuf]) -> Result<(), io::Error> {
Expand Down
3 changes: 1 addition & 2 deletions src/vs/code/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export async function main(argv: string[]): Promise<any> {
return new Promise((resolve, reject) => {
let tunnelProcess;
if (process.env['VSCODE_DEV']) {
const env = { ...process.env, VSCODE_CLI_EDITOR_WEB_URL: product.tunnelApplicationConfig?.editorWebUrl };
tunnelProcess = spawn('cargo', ['run', '--', 'tunnel', ...argv.slice(5)], { cwd: join(getAppRoot(), 'cli'), env });
tunnelProcess = spawn('cargo', ['run', '--', 'tunnel', ...argv.slice(5)], { cwd: join(getAppRoot(), 'cli') });
} else {
const appPath = process.platform === 'darwin'
// ./Contents/MacOS/Electron => ./Contents/Resources/app/bin/code-tunnel-insiders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ export class RemoteTunnelService extends Disposable implements IRemoteTunnelServ
if (!this.environmentService.isBuilt) {
onOutput('Building tunnel CLI from sources and run', false);
onOutput(`${logLabel} Spawning: cargo run -- tunnel ${commandArgs.join(' ')} `, false);
const env = { ...process.env, VSCODE_CLI_EDITOR_WEB_URL: this.productService.tunnelApplicationConfig?.editorWebUrl };
tunnelProcess = spawn('cargo', ['run', '--', 'tunnel', ...commandArgs], { cwd: join(this.environmentService.appRoot, 'cli'), env });
tunnelProcess = spawn('cargo', ['run', '--', 'tunnel', ...commandArgs], { cwd: join(this.environmentService.appRoot, 'cli') });
} else {
onOutput('Running tunnel CLI', false);
const tunnelCommand = this.getTunnelCommandLocation();
Expand Down

0 comments on commit 94ee5f5

Please sign in to comment.