@@ -5,7 +5,7 @@ use std::{env, fs, path::PathBuf, str};
55use anyhow:: { bail, format_err, Context , Result } ;
66use walkdir:: WalkDir ;
77
8- use crate :: cmd :: { run , run_with_output , Cmd } ;
8+ use crate :: not_bash :: { pushd , run } ;
99
1010// Latest stable, feel free to send a PR if this lags behind.
1111const REQUIRED_RUST_VERSION : u32 = 41 ;
@@ -83,21 +83,9 @@ fn fix_path_for_mac() -> Result<()> {
8383}
8484
8585fn install_client ( ClientOpt :: VsCode : ClientOpt ) -> Result < ( ) > {
86- let npm_version = Cmd {
87- unix : r"npm --version" ,
88- windows : r"cmd.exe /c npm --version" ,
89- work_dir : "./editors/code" ,
90- }
91- . run ( ) ;
92-
93- if npm_version. is_err ( ) {
94- bail ! ( "`npm --version` failed, `npm` is required to build the VS Code plugin" )
95- }
86+ let _dir = pushd ( "./editors/code" ) ;
9687
97- Cmd { unix : r"npm install" , windows : r"cmd.exe /c npm install" , work_dir : "./editors/code" }
98- . run ( ) ?;
99-
100- let vsixes = || {
88+ let list_vsixes = || {
10189 WalkDir :: new ( "./editors/code" )
10290 . max_depth ( 1 )
10391 . into_iter ( )
@@ -106,50 +94,45 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
10694 . filter ( |it| it. file_name ( ) . unwrap_or_default ( ) . to_string_lossy ( ) . ends_with ( ".vsix" ) )
10795 } ;
10896
109- for path in vsixes ( ) {
110- fs:: remove_file ( path) ?
111- }
97+ let find_code = |f : fn ( & str ) -> bool | -> Result < & ' static str > {
98+ [ "code" , "code-insiders" , "codium" , "code-oss" ]
99+ . iter ( )
100+ . copied ( )
101+ . find ( |bin| f ( bin) )
102+ . ok_or_else ( || {
103+ format_err ! ( "Can't execute `code --version`. Perhaps it is not in $PATH?" )
104+ } )
105+ } ;
112106
113- Cmd {
114- unix : r"npm run package --scripts-prepend-node-path" ,
115- windows : r"cmd.exe /c npm run package" ,
116- work_dir : "./editors/code" ,
117- }
118- . run ( ) ?;
119-
120- let extension = vsixes ( ) . next ( ) . unwrap ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ;
121-
122- let code_binary = [ "code" , "code-insiders" , "codium" , "code-oss" ]
123- . iter ( )
124- . find ( |bin| {
125- Cmd {
126- unix : & format ! ( "{} --version" , bin) ,
127- windows : & format ! ( "cmd.exe /c {}.cmd --version" , bin) ,
128- work_dir : "./editors/code" ,
129- }
130- . run ( )
131- . is_ok ( )
132- } )
133- . ok_or_else ( || {
134- format_err ! ( "Can't execute `code --version`. Perhaps it is not in $PATH?" )
135- } ) ?;
136-
137- Cmd {
138- unix : & format ! ( r"{} --install-extension ./{} --force" , code_binary, extension) ,
139- windows : & format ! (
140- r"cmd.exe /c {}.cmd --install-extension ./{} --force" ,
141- code_binary, extension
142- ) ,
143- work_dir : "./editors/code" ,
144- }
145- . run ( ) ?;
107+ let installed_extensions;
108+ if cfg ! ( unix) {
109+ run ! ( "npm --version" ) . context ( "`npm` is required to build the VS Code plugin" ) ?;
110+ run ! ( "npm install" ) ?;
146111
147- let installed_extensions = Cmd {
148- unix : & format ! ( r"{} --list-extensions" , code_binary) ,
149- windows : & format ! ( r"cmd.exe /c {}.cmd --list-extensions" , code_binary) ,
150- work_dir : "." ,
112+ let vsix_pkg = {
113+ list_vsixes ( ) . try_for_each ( fs:: remove_file) ?;
114+ run ! ( "npm run package --scripts-prepend-node-path" ) ?;
115+ list_vsixes ( ) . next ( ) . unwrap ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( )
116+ } ;
117+
118+ let code = find_code ( |bin| run ! ( "{} --version" , bin) . is_ok ( ) ) ?;
119+ run ! ( "{} --install-extension ./{} --force" , code, vsix_pkg) ?;
120+ installed_extensions = run ! ( "{} --list-extensions" , code; echo = false ) ?;
121+ } else {
122+ run ! ( "cmd.exe /c npm --version" )
123+ . context ( "`npm` is required to build the VS Code plugin" ) ?;
124+ run ! ( "cmd.exe /c npm install" ) ?;
125+
126+ let vsix_pkg = {
127+ list_vsixes ( ) . try_for_each ( fs:: remove_file) ?;
128+ run ! ( "cmd.exe /c npm run package" ) ?;
129+ list_vsixes ( ) . next ( ) . unwrap ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( )
130+ } ;
131+
132+ let code = find_code ( |bin| run ! ( "cmd.exe /c {}.cmd --version" , bin) . is_ok ( ) ) ?;
133+ run ! ( r"cmd.exe /c {}.cmd --install-extension ./{} --force" , code, vsix_pkg) ?;
134+ installed_extensions = run ! ( "cmd.exe /c {}.cmd --list-extensions" , code; echo = false ) ?;
151135 }
152- . run_with_output ( ) ?;
153136
154137 if !installed_extensions. contains ( "rust-analyzer" ) {
155138 bail ! (
@@ -163,8 +146,7 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
163146
164147fn install_server ( opts : ServerOpt ) -> Result < ( ) > {
165148 let mut old_rust = false ;
166- if let Ok ( stdout) = run_with_output ( "cargo --version" , "." ) {
167- println ! ( "{}" , stdout) ;
149+ if let Ok ( stdout) = run ! ( "cargo --version" ) {
168150 if !check_version ( & stdout, REQUIRED_RUST_VERSION ) {
169151 old_rust = true ;
170152 }
@@ -177,20 +159,17 @@ fn install_server(opts: ServerOpt) -> Result<()> {
177159 )
178160 }
179161
180- let res = if opts. jemalloc {
181- run ( "cargo install --path crates/ra_lsp_server --locked --force --features jemalloc" , "." )
182- } else {
183- run ( "cargo install --path crates/ra_lsp_server --locked --force" , "." )
184- } ;
162+ let jemalloc = if opts. jemalloc { "--features jemalloc" } else { "" } ;
163+ let res = run ! ( "cargo install --path crates/ra_lsp_server --locked --force {}" , jemalloc) ;
185164
186165 if res. is_err ( ) && old_rust {
187166 eprintln ! (
188167 "\n WARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n " ,
189168 REQUIRED_RUST_VERSION ,
190- )
169+ ) ;
191170 }
192171
193- res
172+ res. map ( drop )
194173}
195174
196175fn check_version ( version_output : & str , min_minor_version : u32 ) -> bool {
0 commit comments