@@ -5,6 +5,7 @@ use binary_install::{Cache, Download};
5
5
use child;
6
6
use emoji;
7
7
use failure:: { self , ResultExt } ;
8
+ use install;
8
9
use log:: debug;
9
10
use log:: { info, warn} ;
10
11
use std:: env;
@@ -21,6 +22,27 @@ mod tool;
21
22
pub use self :: mode:: InstallMode ;
22
23
pub use self :: tool:: Tool ;
23
24
25
+ /// Possible outcomes of attempting to find/install a tool
26
+ pub enum Status {
27
+ /// Couldn't install tool because downloads are forbidden by user
28
+ CannotInstall ,
29
+ /// The current platform doesn't support precompiled binaries for this tool
30
+ PlatformNotSupported ,
31
+ /// We found the tool at the specified path
32
+ Found ( Download ) ,
33
+ }
34
+
35
+ /// Handles possible installs status and returns the download or a error message
36
+ pub fn get_tool_path ( status : & Status , tool : Tool ) -> Result < & Download , failure:: Error > {
37
+ match status {
38
+ Status :: Found ( download) => Ok ( download) ,
39
+ Status :: CannotInstall => bail ! ( "Not able to find or install a local {}." , tool) ,
40
+ install:: Status :: PlatformNotSupported => {
41
+ bail ! ( "{} does not currently support your platform." , tool)
42
+ }
43
+ }
44
+ }
45
+
24
46
/// Install a cargo CLI tool
25
47
///
26
48
/// Prefers an existing local install, if any exists. Then checks if there is a
@@ -32,7 +54,7 @@ pub fn download_prebuilt_or_cargo_install(
32
54
cache : & Cache ,
33
55
version : & str ,
34
56
install_permitted : bool ,
35
- ) -> Result < Download , failure:: Error > {
57
+ ) -> Result < Status , failure:: Error > {
36
58
// If the tool is installed globally and it has the right version, use
37
59
// that. Assume that other tools are installed next to it.
38
60
//
@@ -41,7 +63,8 @@ pub fn download_prebuilt_or_cargo_install(
41
63
if let Ok ( path) = which ( tool. to_string ( ) ) {
42
64
debug ! ( "found global {} binary at: {}" , tool, path. display( ) ) ;
43
65
if check_version ( & tool, & path, version) ? {
44
- return Ok ( Download :: at ( path. parent ( ) . unwrap ( ) ) ) ;
66
+ let download = Download :: at ( path. parent ( ) . unwrap ( ) ) ;
67
+ return Ok ( Status :: Found ( download) ) ;
45
68
}
46
69
}
47
70
@@ -101,7 +124,7 @@ pub fn download_prebuilt(
101
124
cache : & Cache ,
102
125
version : & str ,
103
126
install_permitted : bool ,
104
- ) -> Result < Download , failure:: Error > {
127
+ ) -> Result < Status , failure:: Error > {
105
128
let url = match prebuilt_url ( tool, version) {
106
129
Ok ( url) => url,
107
130
Err ( e) => bail ! (
@@ -114,29 +137,53 @@ pub fn download_prebuilt(
114
137
Tool :: WasmBindgen => {
115
138
let binaries = & [ "wasm-bindgen" , "wasm-bindgen-test-runner" ] ;
116
139
match cache. download ( install_permitted, "wasm-bindgen" , binaries, & url) ? {
117
- Some ( download) => Ok ( download) ,
140
+ Some ( download) => Ok ( Status :: Found ( download) ) ,
118
141
None => bail ! ( "wasm-bindgen v{} is not installed!" , version) ,
119
142
}
120
143
}
121
144
Tool :: CargoGenerate => {
122
145
let binaries = & [ "cargo-generate" ] ;
123
146
match cache. download ( install_permitted, "cargo-generate" , binaries, & url) ? {
124
- Some ( download) => Ok ( download) ,
147
+ Some ( download) => Ok ( Status :: Found ( download) ) ,
125
148
None => bail ! ( "cargo-generate v{} is not installed!" , version) ,
126
149
}
127
150
}
151
+ Tool :: WasmOpt => {
152
+ let binaries = & [ "wasm-opt" ] ;
153
+ match cache. download ( install_permitted, "wasm-opt" , binaries, & url) ? {
154
+ Some ( download) => Ok ( Status :: Found ( download) ) ,
155
+ // TODO(ag_dubs): why is this different? i forget...
156
+ None => Ok ( Status :: CannotInstall ) ,
157
+ }
158
+ }
128
159
}
129
160
}
130
161
131
162
/// Returns the URL of a precompiled version of wasm-bindgen, if we have one
132
163
/// available for our host platform.
133
164
fn prebuilt_url ( tool : & Tool , version : & str ) -> Result < String , failure:: Error > {
134
165
let target = if target:: LINUX && target:: x86_64 {
135
- "x86_64-unknown-linux-musl"
166
+ match tool {
167
+ Tool :: WasmOpt => "x86-linux" ,
168
+ _ => "x86_64-unknown-linux-musl" ,
169
+ }
170
+ } else if target:: LINUX && target:: x86 {
171
+ match tool {
172
+ Tool :: WasmOpt => "x86-linux" ,
173
+ _ => bail ! ( "Unrecognized target!" ) ,
174
+ }
136
175
} else if target:: MACOS && target:: x86_64 {
137
176
"x86_64-apple-darwin"
138
177
} else if target:: WINDOWS && target:: x86_64 {
139
- "x86_64-pc-windows-msvc"
178
+ match tool {
179
+ Tool :: WasmOpt => "x86-windows" ,
180
+ _ => "x86_64-pc-windows-msvc" ,
181
+ }
182
+ } else if target:: WINDOWS && target:: x86 {
183
+ match tool {
184
+ Tool :: WasmOpt => "x86-windows" ,
185
+ _ => bail ! ( "Unrecognized target!" ) ,
186
+ }
140
187
} else {
141
188
bail ! ( "Unrecognized target!" )
142
189
} ;
@@ -155,6 +202,13 @@ fn prebuilt_url(tool: &Tool, version: &str) -> Result<String, failure::Error> {
155
202
Krate :: new( & Tool :: CargoGenerate ) ?. max_version,
156
203
target
157
204
) )
205
+ } ,
206
+ Tool :: WasmOpt => {
207
+ Ok ( format ! (
208
+ "https://github.com/WebAssembly/binaryen/releases/download/{vers}/binaryen-{vers}-{target}.tar.gz" ,
209
+ vers = "version_90" ,
210
+ target = target,
211
+ ) )
158
212
}
159
213
}
160
214
}
@@ -166,7 +220,7 @@ pub fn cargo_install(
166
220
cache : & Cache ,
167
221
version : & str ,
168
222
install_permitted : bool ,
169
- ) -> Result < Download , failure:: Error > {
223
+ ) -> Result < Status , failure:: Error > {
170
224
debug ! (
171
225
"Attempting to use a `cargo install`ed version of `{}={}`" ,
172
226
tool, version,
@@ -181,11 +235,12 @@ pub fn cargo_install(
181
235
version,
182
236
destination. display( )
183
237
) ;
184
- return Ok ( Download :: at ( & destination) ) ;
238
+ let download = Download :: at ( & destination) ;
239
+ return Ok ( Status :: Found ( download) ) ;
185
240
}
186
241
187
242
if !install_permitted {
188
- bail ! ( "{} v{} is not installed!" , tool , version )
243
+ return Ok ( Status :: CannotInstall ) ;
189
244
}
190
245
191
246
// Run `cargo install` to a temporary location to handle ctrl-c gracefully
@@ -210,23 +265,20 @@ pub fn cargo_install(
210
265
. arg ( "--root" )
211
266
. arg ( & tmp) ;
212
267
213
- if PBAR . quiet ( ) {
214
- cmd. arg ( "--quiet" ) ;
215
- }
216
-
217
268
let context = format ! ( "Installing {} with cargo" , tool) ;
218
269
child:: run ( cmd, "cargo install" ) . context ( context) ?;
219
270
220
271
// `cargo install` will put the installed binaries in `$root/bin/*`, but we
221
272
// just want them in `$root/*` directly (which matches how the tarballs are
222
273
// laid out, and where the rest of our code expects them to be). So we do a
223
274
// little renaming here.
224
- let binaries = match tool {
225
- Tool :: WasmBindgen => vec ! [ "wasm-bindgen" , "wasm-bindgen-test-runner" ] ,
226
- Tool :: CargoGenerate => vec ! [ "cargo-genrate" ] ,
275
+ let binaries: Result < Vec < & str > , failure:: Error > = match tool {
276
+ Tool :: WasmBindgen => Ok ( vec ! [ "wasm-bindgen" , "wasm-bindgen-test-runner" ] ) ,
277
+ Tool :: CargoGenerate => Ok ( vec ! [ "cargo-genrate" ] ) ,
278
+ Tool :: WasmOpt => bail ! ( "Cannot install wasm-opt with cargo." ) ,
227
279
} ;
228
280
229
- for b in binaries. iter ( ) . cloned ( ) {
281
+ for b in binaries? . iter ( ) . cloned ( ) {
230
282
let from = tmp
231
283
. join ( "bin" )
232
284
. join ( b)
@@ -245,5 +297,6 @@ pub fn cargo_install(
245
297
// Finally, move the `tmp` directory into our binary cache.
246
298
fs:: rename ( & tmp, & destination) ?;
247
299
248
- Ok ( Download :: at ( & destination) )
300
+ let download = Download :: at ( & destination) ;
301
+ Ok ( Status :: Found ( download) )
249
302
}
0 commit comments