993: Fix installing vscode extension on MacOS r=matklad a=funkill

VSCode often installed in MacOS as `Visual Studio Code.app` package and `code` binary located at `Contents/Resources/app/bin` in package. This path not exists in `$PATH` variable and we can't run `code`.

In previous version of `do_run` function all before space was command and all after - arguments. If path or command has spaces, extracting command breaks. To fix this i extracted command to separated argument of function.

All packages can be placed in system app dir (`/Applications`) or user app dir (`~/Applications`). I created helper function for find app in this directories.



Co-authored-by: funkill2 <funkill2@gmail.com>
This commit is contained in:
bors[bot] 2019-03-18 19:26:45 +00:00
commit 7fc35d391c

View file

@ -5,6 +5,7 @@ use tools::{
generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt,
Overwrite, Result, run_fuzzer,
};
use std::{path::{PathBuf}, env};
fn main() -> Result<()> {
let matches = App::new("tasks")
@ -17,7 +18,12 @@ fn main() -> Result<()> {
.subcommand(SubCommand::with_name("fuzz-tests"))
.get_matches();
match matches.subcommand_name().expect("Subcommand must be specified") {
"install-code" => install_code_extension()?,
"install-code" => {
if cfg!(target_os = "macos") {
fix_path_for_mac()?;
}
install_code_extension()?;
}
"gen-tests" => gen_tests(Overwrite)?,
"gen-syntax" => generate(Overwrite)?,
"format" => run_rustfmt(Overwrite)?,
@ -63,3 +69,36 @@ fn verify_installed_extensions() -> Result<()> {
}
Ok(())
}
fn fix_path_for_mac() -> Result<()> {
let mut vscode_path: Vec<PathBuf> = {
const COMMON_APP_PATH: &str =
r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
const ROOT_DIR: &str = "";
let home_dir = match env::var("HOME") {
Ok(home) => home,
Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
};
[ROOT_DIR, &home_dir]
.iter()
.map(|dir| String::from(dir.clone()) + COMMON_APP_PATH)
.map(PathBuf::from)
.filter(|path| path.exists())
.collect()
};
if !vscode_path.is_empty() {
let vars = match env::var_os("PATH") {
Some(path) => path,
None => bail!("Could not get PATH variable from env."),
};
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
paths.append(&mut vscode_path);
let new_paths = env::join_paths(paths)?;
env::set_var("PATH", &new_paths);
}
Ok(())
}