From d8f3b0d01d961fc95caea7b680f397753b243ff9 Mon Sep 17 00:00:00 2001 From: funkill2 Date: Mon, 18 Mar 2019 20:27:11 +0300 Subject: added helper module for appending vscode path --- crates/tools/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'crates/tools/src/main.rs') diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index 4a1b2ff9a..484858a1e 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -63,3 +63,57 @@ fn verify_installed_extensions() -> Result<()> { } Ok(()) } + +#[cfg(target_os = "macos")] +mod vscode_path_helpers { + use super::Result; + use std::{path::{PathBuf}, env}; + use failure::bail; + + pub(crate) fn append_vscode_path() -> Result<()> { + let vars = match env::var_os("PATH") { + Some(path) => path, + None => bail!("Could not get PATH variable from env."), + }; + + let vscode_path = get_vscode_path()?; + let mut paths = env::split_paths(&vars).collect::>(); + paths.push(vscode_path); + let new_paths = env::join_paths(paths)?; + env::set_var("PATH", &new_paths); + + Ok(()) + } + + fn get_vscode_path() -> Result { + 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), + }; + + for dir in [ROOT_DIR, &home_dir].iter() { + let path = String::from(dir.clone()) + COMMON_APP_PATH; + let path = PathBuf::from(path); + if path.exists() { + return Ok(path); + } + } + + bail!( + "Could not find Visual Studio Code application. Please make sure you \ + have Visual Studio Code installed and try again or install extension \ + manually." + ) + } +} + +#[cfg(not(target_os = "macos"))] +mod vscode_path_helpers { + use super::Result; + pub(crate) fn append_vscode_path() -> Result<()> { + Ok(()) + } +} -- cgit v1.2.3