Skip to content

Commit 5057e1a

Browse files
committed
Add ASPECT_LAUNCHER env vars
1 parent 1ce9476 commit 5057e1a

File tree

3 files changed

+89
-38
lines changed

3 files changed

+89
-38
lines changed

crates/aspect-launcher/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
With a bare minimum of code, perform the following.
44

55
- Look for an `.aspect/config.toml`
6-
- Read `.cli.version`
7-
- Check ``
6+
- Read `.aspect_cli.version`
7+
- ...

crates/aspect-launcher/src/config.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ use serde::Deserialize;
99
const AXL_MODULE_FILE: &str = "MODULE.aspect";
1010

1111
#[derive(Debug, Clone)]
12-
pub struct AspectConfig {
13-
pub cli: CliConfig,
12+
pub struct AspectLauncherConfig {
13+
pub aspect_cli: AspectCliConfig,
1414
}
1515

1616
#[derive(Deserialize, Debug, Clone)]
1717
struct RawAspectConfig {
1818
#[serde(rename = "aspect-cli")]
19-
pub cli: Option<CliConfig>,
19+
pub aspect_cli: Option<AspectCliConfig>,
2020
}
2121

2222
fn default_cli_sources() -> Vec<ToolSource> {
2323
vec![{
24-
ToolSource::Github {
24+
ToolSource::GitHub {
2525
org: "aspect-build".into(),
2626
repo: "aspect-cli".into(),
2727
release: "v{{ version }}".into(),
@@ -31,7 +31,7 @@ fn default_cli_sources() -> Vec<ToolSource> {
3131
}
3232

3333
#[derive(Deserialize, Debug, Clone)]
34-
pub struct CliConfig {
34+
pub struct AspectCliConfig {
3535
#[serde(default = "default_cli_sources")]
3636
sources: Vec<ToolSource>,
3737
#[serde(default = "cargo_pkg_short_version")]
@@ -41,7 +41,7 @@ pub struct CliConfig {
4141
#[derive(Deserialize, Debug, Clone)]
4242
#[serde(untagged)]
4343
pub enum ToolSource {
44-
Github {
44+
GitHub {
4545
org: String,
4646
repo: String,
4747
release: String,
@@ -65,7 +65,7 @@ pub trait ToolSpec: Debug {
6565
fn sources(&self) -> &Vec<ToolSource>;
6666
}
6767

68-
impl ToolSpec for CliConfig {
68+
impl ToolSpec for AspectCliConfig {
6969
fn name(&self) -> String {
7070
"aspect-cli".to_owned()
7171
}
@@ -79,7 +79,7 @@ impl ToolSpec for CliConfig {
7979
}
8080
}
8181

82-
pub fn load_config(path: &PathBuf) -> Result<AspectConfig> {
82+
pub fn load_config(path: &PathBuf) -> Result<AspectLauncherConfig> {
8383
let content = match fs::read_to_string(path) {
8484
Ok(c) => c,
8585
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(default_config()),
@@ -91,23 +91,23 @@ pub fn load_config(path: &PathBuf) -> Result<AspectConfig> {
9191
Err(e) => return Err(miette!("failed to parse config file {:?}: {}", path, e)),
9292
};
9393

94-
let config = AspectConfig {
95-
cli: raw.cli.unwrap_or_else(default_cli_config),
94+
let config = AspectLauncherConfig {
95+
aspect_cli: raw.aspect_cli.unwrap_or_else(default_aspect_cli_config),
9696
};
9797

9898
Ok(config)
9999
}
100100

101-
fn default_cli_config() -> CliConfig {
102-
CliConfig {
101+
fn default_aspect_cli_config() -> AspectCliConfig {
102+
AspectCliConfig {
103103
sources: default_cli_sources(),
104104
version: cargo_pkg_short_version(),
105105
}
106106
}
107107

108-
pub fn default_config() -> AspectConfig {
109-
AspectConfig {
110-
cli: default_cli_config(),
108+
pub fn default_config() -> AspectLauncherConfig {
109+
AspectLauncherConfig {
110+
aspect_cli: default_aspect_cli_config(),
111111
}
112112
}
113113

@@ -123,14 +123,14 @@ pub fn default_config() -> AspectConfig {
123123
///
124124
/// # Returns
125125
///
126-
/// A `Result` containing a tuple `(PathBuf, AspectConfig)` where:
126+
/// A `Result` containing a tuple `(PathBuf, AspectLauncherConfig)` where:
127127
/// - The first element is the determined root directory.
128-
/// - The second element is the loaded `AspectConfig`.
128+
/// - The second element is the loaded `AspectLauncherConfig`.
129129
///
130130
/// # Errors
131131
///
132132
/// Returns an error if the current working directory cannot be obtained or if loading the config fails.
133-
pub fn autoconf() -> Result<(PathBuf, AspectConfig)> {
133+
pub fn autoconf() -> Result<(PathBuf, AspectLauncherConfig)> {
134134
let current_dir =
135135
current_dir().map_err(|e| miette!("failed to get current directory: {}", e))?;
136136

crates/aspect-launcher/src/main.rs

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod cache;
22
mod config;
33

4+
use std::collections::HashMap;
45
use std::env;
56
use std::env::var;
67
use 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+
4045
async 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

Comments
 (0)