22
33use std:: { env, path:: PathBuf , str} ;
44
5- use anyhow:: { Context , Result } ;
5+ use anyhow:: { bail , format_err , Context , Result } ;
66
7- use crate :: cmd :: { run , run_with_output , Cmd } ;
7+ use crate :: not_bash :: { ls , pushd , rm , run } ;
88
99// Latest stable, feel free to send a PR if this lags behind.
1010const REQUIRED_RUST_VERSION : u32 = 41 ;
@@ -55,7 +55,7 @@ fn fix_path_for_mac() -> Result<()> {
5555 const ROOT_DIR : & str = "" ;
5656 let home_dir = match env:: var ( "HOME" ) {
5757 Ok ( home) => home,
58- Err ( e) => anyhow :: bail!( "Failed getting HOME from environment with error: {}." , e) ,
58+ Err ( e) => bail ! ( "Failed getting HOME from environment with error: {}." , e) ,
5959 } ;
6060
6161 [ ROOT_DIR , & home_dir]
@@ -69,7 +69,7 @@ fn fix_path_for_mac() -> Result<()> {
6969 if !vscode_path. is_empty ( ) {
7070 let vars = match env:: var_os ( "PATH" ) {
7171 Some ( path) => path,
72- None => anyhow :: bail!( "Could not get PATH variable from env." ) ,
72+ None => bail ! ( "Could not get PATH variable from env." ) ,
7373 } ;
7474
7575 let mut paths = env:: split_paths ( & vars) . collect :: < Vec < _ > > ( ) ;
@@ -82,84 +82,61 @@ fn fix_path_for_mac() -> Result<()> {
8282}
8383
8484fn install_client ( ClientOpt :: VsCode : ClientOpt ) -> Result < ( ) > {
85- let npm_version = Cmd {
86- unix : r"npm --version" ,
87- windows : r"cmd.exe /c npm --version" ,
88- work_dir : "./editors/code" ,
89- }
90- . run ( ) ;
91-
92- if npm_version. is_err ( ) {
93- eprintln ! ( "\n ERROR: `npm --version` failed, `npm` is required to build the VS Code plugin" )
94- }
85+ let _dir = pushd ( "./editors/code" ) ;
9586
96- Cmd { unix : r"npm install" , windows : r"cmd.exe /c npm install" , work_dir : "./editors/code" }
97- . run ( ) ?;
98- Cmd {
99- unix : r"npm run package --scripts-prepend-node-path" ,
100- windows : r"cmd.exe /c npm run package" ,
101- work_dir : "./editors/code" ,
102- }
103- . run ( ) ?;
87+ let find_code = |f : fn ( & str ) -> bool | -> Result < & ' static str > {
88+ [ "code" , "code-insiders" , "codium" , "code-oss" ]
89+ . iter ( )
90+ . copied ( )
91+ . find ( |bin| f ( bin) )
92+ . ok_or_else ( || {
93+ format_err ! ( "Can't execute `code --version`. Perhaps it is not in $PATH?" )
94+ } )
95+ } ;
10496
105- let code_binary = [ "code" , "code-insiders" , "codium" , "code-oss" ] . iter ( ) . find ( |bin| {
106- Cmd {
107- unix : & format ! ( "{} --version" , bin) ,
108- windows : & format ! ( "cmd.exe /c {}.cmd --version" , bin) ,
109- work_dir : "./editors/code" ,
110- }
111- . run ( )
112- . is_ok ( )
113- } ) ;
97+ let installed_extensions;
98+ if cfg ! ( unix) {
99+ run ! ( "npm --version" ) . context ( "`npm` is required to build the VS Code plugin" ) ?;
100+ run ! ( "npm install" ) ?;
114101
115- let code_binary = match code_binary {
116- Some ( it) => it,
117- None => anyhow:: bail!( "Can't execute `code --version`. Perhaps it is not in $PATH?" ) ,
118- } ;
102+ let vsix_pkg = {
103+ rm ( "*.vsix" ) ?;
104+ run ! ( "npm run package --scripts-prepend-node-path" ) ?;
105+ ls ( "*.vsix" ) ?. pop ( ) . unwrap ( )
106+ } ;
119107
120- Cmd {
121- unix : & format ! ( r"{} --install-extension ./rust-analyzer-0.1.0.vsix --force" , code_binary) ,
122- windows : & format ! (
123- r"cmd.exe /c {}.cmd --install-extension ./rust-analyzer-0.1.0.vsix --force" ,
124- code_binary
125- ) ,
126- work_dir : "./editors/code" ,
127- }
128- . run ( ) ?;
108+ let code = find_code ( |bin| run ! ( "{} --version" , bin) . is_ok ( ) ) ?;
109+ run ! ( "{} --install-extension {} --force" , code, vsix_pkg. display( ) ) ?;
110+ installed_extensions = run ! ( "{} --list-extensions" , code; echo = false ) ?;
111+ } else {
112+ run ! ( "cmd.exe /c npm --version" )
113+ . context ( "`npm` is required to build the VS Code plugin" ) ?;
114+ run ! ( "cmd.exe /c npm install" ) ?;
115+
116+ let vsix_pkg = {
117+ rm ( "*.vsix" ) ?;
118+ run ! ( "cmd.exe /c npm run package" ) ?;
119+ ls ( "*.vsix" ) ?. pop ( ) . unwrap ( )
120+ } ;
129121
130- let installed_extensions = Cmd {
131- unix : & format ! ( r"{} --list-extensions" , code_binary) ,
132- windows : & format ! ( r"cmd.exe /c {}.cmd --list-extensions" , code_binary) ,
133- work_dir : "." ,
122+ let code = find_code ( |bin| run ! ( "cmd.exe /c {}.cmd --version" , bin) . is_ok ( ) ) ?;
123+ run ! ( r"cmd.exe /c {}.cmd --install-extension {} --force" , code, vsix_pkg. display( ) ) ?;
124+ installed_extensions = run ! ( "cmd.exe /c {}.cmd --list-extensions" , code; echo = false ) ?;
134125 }
135- . run_with_output ( ) ?;
136126
137127 if !installed_extensions. contains ( "rust-analyzer" ) {
138- anyhow :: bail!(
128+ bail ! (
139129 "Could not install the Visual Studio Code extension. \
140130 Please make sure you have at least NodeJS 10.x together with the latest version of VS Code installed and try again."
141131 ) ;
142132 }
143133
144- if installed_extensions. contains ( "ra-lsp" ) {
145- Cmd {
146- unix : & format ! ( r"{} --uninstall-extension matklad.ra-lsp" , code_binary) ,
147- windows : & format ! (
148- r"cmd.exe /c {}.cmd --uninstall-extension matklad.ra-lsp" ,
149- code_binary
150- ) ,
151- work_dir : "./editors/code" ,
152- }
153- . run ( ) ?;
154- }
155-
156134 Ok ( ( ) )
157135}
158136
159137fn install_server ( opts : ServerOpt ) -> Result < ( ) > {
160138 let mut old_rust = false ;
161- if let Ok ( stdout) = run_with_output ( "cargo --version" , "." ) {
162- println ! ( "{}" , stdout) ;
139+ if let Ok ( stdout) = run ! ( "cargo --version" ) {
163140 if !check_version ( & stdout, REQUIRED_RUST_VERSION ) {
164141 old_rust = true ;
165142 }
@@ -172,20 +149,17 @@ fn install_server(opts: ServerOpt) -> Result<()> {
172149 )
173150 }
174151
175- let res = if opts. jemalloc {
176- run ( "cargo install --path crates/ra_lsp_server --locked --force --features jemalloc" , "." )
177- } else {
178- run ( "cargo install --path crates/ra_lsp_server --locked --force" , "." )
179- } ;
152+ let jemalloc = if opts. jemalloc { "--features jemalloc" } else { "" } ;
153+ let res = run ! ( "cargo install --path crates/ra_lsp_server --locked --force {}" , jemalloc) ;
180154
181155 if res. is_err ( ) && old_rust {
182156 eprintln ! (
183157 "\n WARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n " ,
184158 REQUIRED_RUST_VERSION ,
185- )
159+ ) ;
186160 }
187161
188- res
162+ res. map ( drop )
189163}
190164
191165fn check_version ( version_output : & str , min_minor_version : u32 ) -> bool {
0 commit comments