11mod cache;
22mod config;
33
4+ use std:: collections:: HashMap ;
45use std:: env;
56use std:: env:: var;
67use std:: fs;
@@ -37,6 +38,10 @@ fn debug_mode() -> bool {
3738 }
3839}
3940
41+ const ASPECT_LAUNCHER_METHOD_HTTP : & str = "http" ;
42+ const ASPECT_LAUNCHER_METHOD_GITHUB : & str = "github" ;
43+ const ASPECT_LAUNCHER_METHOD_LOCAL : & str = "local" ;
44+
4045async fn _download_into_cache (
4146 client : & Client ,
4247 cache_entry : & PathBuf ,
@@ -167,11 +172,15 @@ async fn configure_tool_task(
167172 cache : AspectCache ,
168173 root_dir : PathBuf ,
169174 tool : Box < dyn ToolSpec + Send > ,
170- ) -> JoinHandle < Result < PathBuf > > {
175+ ) -> JoinHandle < Result < ( PathBuf , String , HashMap < String , String > ) > > {
171176 task:: spawn ( ( async move |cache : AspectCache ,
172177 root_dir : PathBuf ,
173178 tool : Box < dyn ToolSpec + Send > |
174- -> Result < PathBuf > {
179+ -> Result < (
180+ PathBuf ,
181+ String ,
182+ HashMap < String , String > ,
183+ ) > {
175184 let mut errs: Vec < Result < ( ) > > = Vec :: new ( ) ;
176185
177186 let client = reqwest:: Client :: new ( ) ;
@@ -204,6 +213,8 @@ async fn configure_tool_task(
204213 . build ( )
205214 . into_diagnostic ( ) ?;
206215 let tool_dest_file = cache. tool_path ( & tool. name ( ) , & url) ;
216+ let mut extra_envs = HashMap :: new ( ) ;
217+ extra_envs. insert ( "ASPECT_LAUNCHER_ASPECT_CLI_URL" . to_string ( ) , url. clone ( ) ) ;
207218 if tool_dest_file. exists ( ) {
208219 if debug_mode ( ) {
209220 eprintln ! (
@@ -213,7 +224,11 @@ async fn configure_tool_task(
213224 url
214225 ) ;
215226 } ;
216- return Ok ( tool_dest_file) ;
227+ return Ok ( (
228+ tool_dest_file,
229+ ASPECT_LAUNCHER_METHOD_HTTP . to_string ( ) ,
230+ extra_envs,
231+ ) ) ;
217232 }
218233 fs:: create_dir_all ( tool_dest_file. parent ( ) . unwrap ( ) ) . into_diagnostic ( ) ?;
219234 if debug_mode ( ) {
@@ -232,21 +247,25 @@ async fn configure_tool_task(
232247 errs. push ( err) ;
233248 continue ;
234249 } ;
235- return Ok ( tool_dest_file) ;
250+ return Ok ( (
251+ tool_dest_file,
252+ ASPECT_LAUNCHER_METHOD_HTTP . to_string ( ) ,
253+ extra_envs,
254+ ) ) ;
236255 }
237- ToolSource :: Github {
256+ ToolSource :: GitHub {
238257 org,
239258 repo,
240259 release,
241260 artifact,
242261 } => {
243262 let release = liquid_parser
244- . parse ( release)
263+ . parse ( & release)
245264 . into_diagnostic ( ) ?
246265 . render ( & liquid_globals)
247266 . into_diagnostic ( ) ?;
248267 let artifact = liquid_parser
249- . parse ( artifact)
268+ . parse ( & artifact)
250269 . into_diagnostic ( ) ?
251270 . render ( & liquid_globals)
252271 . into_diagnostic ( ) ?;
@@ -256,6 +275,17 @@ async fn configure_tool_task(
256275 ) ;
257276
258277 let tool_dest_file = cache. tool_path ( & tool. name ( ) , & url) ;
278+ let mut extra_envs = HashMap :: new ( ) ;
279+ extra_envs. insert ( "ASPECT_LAUNCHER_ASPECT_CLI_ORG" . to_string ( ) , org. clone ( ) ) ;
280+ extra_envs. insert ( "ASPECT_LAUNCHER_ASPECT_CLI_REPO" . to_string ( ) , repo. clone ( ) ) ;
281+ extra_envs. insert (
282+ "ASPECT_LAUNCHER_ASPECT_CLI_RELEASE" . to_string ( ) ,
283+ release. clone ( ) ,
284+ ) ;
285+ extra_envs. insert (
286+ "ASPECT_LAUNCHER_ASPECT_CLI_ARTIFACT" . to_string ( ) ,
287+ artifact. clone ( ) ,
288+ ) ;
259289 if tool_dest_file. exists ( ) {
260290 if debug_mode ( ) {
261291 eprintln ! (
@@ -265,7 +295,11 @@ async fn configure_tool_task(
265295 & url
266296 ) ;
267297 } ;
268- return Ok ( tool_dest_file) ;
298+ return Ok ( (
299+ tool_dest_file,
300+ ASPECT_LAUNCHER_METHOD_GITHUB . to_string ( ) ,
301+ extra_envs,
302+ ) ) ;
269303 }
270304 fs:: create_dir_all ( tool_dest_file. parent ( ) . unwrap ( ) ) . into_diagnostic ( ) ?;
271305
@@ -311,28 +345,32 @@ async fn configure_tool_task(
311345 errs. push ( err) ;
312346 break ;
313347 }
314- return Ok ( tool_dest_file) ;
348+ return Ok ( (
349+ tool_dest_file,
350+ ASPECT_LAUNCHER_METHOD_GITHUB . to_string ( ) ,
351+ extra_envs,
352+ ) ) ;
315353 }
316354 }
317355 errs. push ( Err ( miette ! ( "unable to find a release artifact in github!" ) ) ) ;
318356 continue ;
319357 }
320358 ToolSource :: Local { path } => {
321- let tool_dest_file = cache. tool_path ( & tool. name ( ) , & path) ;
359+ let tool_dest_file = cache. tool_path ( & tool. name ( ) , path) ;
322360 // Don't pull local sources from the cache since the local development flow will
323361 // always be to copy the latest
324362 fs:: create_dir_all ( tool_dest_file. parent ( ) . unwrap ( ) ) . into_diagnostic ( ) ?;
325363
326- let path = root_dir. join ( path) ;
327- if fs:: exists ( & path ) . into_diagnostic ( ) ? {
364+ let full_path = root_dir. join ( path) ;
365+ if fs:: exists ( & full_path ) . into_diagnostic ( ) ? {
328366 if fs:: exists ( & tool_dest_file) . into_diagnostic ( ) ? {
329367 tokio:: fs:: remove_file ( & tool_dest_file)
330368 . await
331369 . into_diagnostic ( ) ?;
332370 }
333371
334372 // We use copies because Bazel nukes the output tree on build errors and we want to resist that
335- tokio:: fs:: copy ( & path , & tool_dest_file)
373+ tokio:: fs:: copy ( & full_path , & tool_dest_file)
336374 . await
337375 . into_diagnostic ( ) ?;
338376
@@ -341,7 +379,7 @@ async fn configure_tool_task(
341379 "{:} source {:?} copying from {:?} to {:?}" ,
342380 tool. name( ) ,
343381 source,
344- path ,
382+ full_path ,
345383 tool_dest_file
346384 ) ;
347385 } ;
@@ -351,7 +389,14 @@ async fn configure_tool_task(
351389 let new_mode = 0o755 ;
352390 permissions. set_mode ( new_mode) ;
353391 fs:: set_permissions ( & tool_dest_file, permissions) . into_diagnostic ( ) ?;
354- return Ok ( tool_dest_file) ;
392+ let mut extra_envs = HashMap :: new ( ) ;
393+ extra_envs
394+ . insert ( "ASPECT_LAUNCHER_ASPECT_CLI_PATH" . to_string ( ) , path. clone ( ) ) ;
395+ return Ok ( (
396+ tool_dest_file,
397+ ASPECT_LAUNCHER_METHOD_LOCAL . to_string ( ) ,
398+ extra_envs,
399+ ) ) ;
355400 }
356401 }
357402 }
@@ -412,17 +457,17 @@ fn main() -> Result<ExitCode> {
412457 let cli_task = configure_tool_task (
413458 cache. clone ( ) ,
414459 root_dir. clone ( ) ,
415- Box :: new ( config. cli . clone ( ) ) ,
460+ Box :: new ( config. aspect_cli . clone ( ) ) ,
416461 )
417462 . await ;
418463
419464 // Wait for fetches
420- let cli = & config. cli ;
465+ let cli = & config. aspect_cli ;
421466 if debug_mode ( ) {
422467 eprintln ! ( "attempting to provision {cli:?}" ) ;
423468 } ;
424469
425- let cli_path = cli_task. await . into_diagnostic ( ) ??;
470+ let ( cli_path, method , extra_envs ) = cli_task. await . into_diagnostic ( ) ??;
426471 if debug_mode ( ) {
427472 eprintln ! ( "provisioned at {cli_path:?}" ) ;
428473 } ;
@@ -433,6 +478,12 @@ fn main() -> Result<ExitCode> {
433478
434479 // Punt
435480 let mut cmd = UnixCommand :: new ( & cli_path) ;
481+ cmd. env ( "ASPECT_LAUNCHER" , "true" ) ;
482+ cmd. env ( "ASPECT_LAUNCHER_VERSION" , cargo_pkg_short_version ( ) ) ;
483+ cmd. env ( "ASPECT_LAUNCHER_ASPECT_CLI_METHOD" , method) ;
484+ for ( k, v) in extra_envs {
485+ cmd. env ( k, v) ;
486+ }
436487 if let Some ( args) = matches. get_many :: < String > ( "args" ) {
437488 cmd. args ( args) ;
438489 } ;
0 commit comments