From d8f3b0d01d961fc95caea7b680f397753b243ff9 Mon Sep 17 00:00:00 2001
From: funkill2 <funkill2@gmail.com>
Date: Mon, 18 Mar 2019 20:27:11 +0300
Subject: [PATCH 1/3] added helper module for appending vscode path

---
 crates/tools/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index 4a1b2ff9a44..484858a1e2f 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::<Vec<_>>();
+        paths.push(vscode_path);
+        let new_paths = env::join_paths(paths)?;
+        env::set_var("PATH", &new_paths);
+
+        Ok(())
+    }
+
+    fn get_vscode_path() -> Result<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),
+        };
+
+        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(())
+    }
+}

From 9c2177026f5fac8464a610a426a974cf691b2a89 Mon Sep 17 00:00:00 2001
From: funkill2 <funkill2@gmail.com>
Date: Mon, 18 Mar 2019 20:27:31 +0300
Subject: [PATCH 2/3] added setup environment

---
 crates/tools/src/main.rs | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index 484858a1e2f..6583b700c37 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -17,7 +17,10 @@ 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" => {
+            setup_environment()?;
+            install_code_extension()?;
+        }
         "gen-tests" => gen_tests(Overwrite)?,
         "gen-syntax" => generate(Overwrite)?,
         "format" => run_rustfmt(Overwrite)?,
@@ -28,6 +31,14 @@ fn main() -> Result<()> {
     Ok(())
 }
 
+fn setup_environment() -> Result<()> {
+    if cfg!(target_os = "macos") {
+        vscode_path_helpers::append_vscode_path()?;
+    }
+
+    Ok(())
+}
+
 fn install_code_extension() -> Result<()> {
     run("cargo install --path crates/ra_lsp_server --force", ".")?;
     if cfg!(windows) {

From 69edc10f3529add3de65b62a7304d3fc258c7a62 Mon Sep 17 00:00:00 2001
From: funkill2 <funkill2@gmail.com>
Date: Mon, 18 Mar 2019 22:18:54 +0300
Subject: [PATCH 3/3] set code less generic

---
 crates/tools/src/main.rs | 74 +++++++++++++---------------------------
 1 file changed, 24 insertions(+), 50 deletions(-)

diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index 6583b700c37..0c33396857d 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -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")
@@ -18,7 +19,9 @@ fn main() -> Result<()> {
         .get_matches();
     match matches.subcommand_name().expect("Subcommand must be specified") {
         "install-code" => {
-            setup_environment()?;
+            if cfg!(target_os = "macos") {
+                fix_path_for_mac()?;
+            }
             install_code_extension()?;
         }
         "gen-tests" => gen_tests(Overwrite)?,
@@ -31,14 +34,6 @@ fn main() -> Result<()> {
     Ok(())
 }
 
-fn setup_environment() -> Result<()> {
-    if cfg!(target_os = "macos") {
-        vscode_path_helpers::append_vscode_path()?;
-    }
-
-    Ok(())
-}
-
 fn install_code_extension() -> Result<()> {
     run("cargo install --path crates/ra_lsp_server --force", ".")?;
     if cfg!(windows) {
@@ -75,28 +70,8 @@ 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::<Vec<_>>();
-        paths.push(vscode_path);
-        let new_paths = env::join_paths(paths)?;
-        env::set_var("PATH", &new_paths);
-
-        Ok(())
-    }
-
-    fn get_vscode_path() -> Result<PathBuf> {
+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 = "";
@@ -105,26 +80,25 @@ mod vscode_path_helpers {
             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);
-            }
-        }
+        [ROOT_DIR, &home_dir]
+            .iter()
+            .map(|dir| String::from(dir.clone()) + COMMON_APP_PATH)
+            .map(PathBuf::from)
+            .filter(|path| path.exists())
+            .collect()
+    };
 
-        bail!(
-            "Could not find Visual Studio Code application. Please make sure you \
-             have Visual Studio Code installed and try again or install extension \
-             manually."
-        )
-    }
-}
+    if !vscode_path.is_empty() {
+        let vars = match env::var_os("PATH") {
+            Some(path) => path,
+            None => bail!("Could not get PATH variable from env."),
+        };
 
-#[cfg(not(target_os = "macos"))]
-mod vscode_path_helpers {
-    use super::Result;
-    pub(crate) fn append_vscode_path() -> Result<()> {
-        Ok(())
+        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(())
 }