Skip to content

Commit

Permalink
refactor: include comments & rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev committed Mar 14, 2024
1 parent a40d267 commit 1c0bb47
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ pub struct BuildCommand {
#[clap(short, long)]
pub output: Option<PathBuf>,

/// The target to compile for - defaults to the host triple
#[clap(short, long)]
pub target: Option<String>,

/// The path to the base executable to use - defaults to
/// the currently running executable, used for cross-compilation
/// for targets not directly supported
#[clap(short, long)]
pub base: Option<PathBuf>,
}
Expand Down Expand Up @@ -109,6 +113,7 @@ impl BuildCommand {
}
}

/// Wrapper function to asynchronously create a file at the given path with the given contents and permissions
async fn write_file_to(
path: impl AsRef<Path>,
bytes: impl AsRef<[u8]>,
Expand Down Expand Up @@ -147,6 +152,7 @@ pub enum BasePathDiscoveryError<T> {
None,
}

/// Discovers the path to the base executable to use for cross-compilation
async fn get_base_exe_path(
base: Option<PathBuf>,
target: Option<String>,
Expand All @@ -173,12 +179,14 @@ async fn get_base_exe_path(
target_exe_extension
});

// Create the target base directory in the lune home if it doesn't already exist
if !TARGET_BASE_DIR.exists() {
fs::create_dir_all(TARGET_BASE_DIR.to_path_buf())
.await
.map_err(BasePathDiscoveryError::IoError)?;
}

// If a cached target base executable doesn't exist, attempt to download it
if !path.exists() {
println!("Requested target hasn't been downloaded yet, attempting to download");

Expand All @@ -200,7 +208,8 @@ async fn get_base_exe_path(
target_full_display
);

// Maybe we should use the custom net client used in `@lune/net`
// FIXME: Maybe we should use the custom net client used in `@lune/net`
// Request the precompiled target from GitHub releases
let resp = reqwest::get(release_url).await.map_err(|err| {
eprintln!(
" {} Unable to download base binary found for target `{}`",
Expand Down Expand Up @@ -228,13 +237,17 @@ async fn get_base_exe_path(
.into());
}

// Wrap the request response in bytes so that we can decompress it, since `async_zip`
// requires the underlying reader to implement `AsyncRead` and `Seek`, which `Bytes`
// doesn't implement
let compressed_data = Cursor::new(
resp.bytes()
.await
.map_err(BasePathDiscoveryError::IoError)?
.to_vec(),
);

// Construct a decoder and decompress the ZIP file using deflate
let mut decoder = ZipFileReader::new(compressed_data.compat())
.await
.map_err(BasePathDiscoveryError::Decompression)?;
Expand All @@ -250,6 +263,7 @@ async fn get_base_exe_path(
.await
.map_err(BasePathDiscoveryError::Decompression)?;

// Finally write the decompressed data to the target base directory
write_file_to(&path, decompressed, 0o644)
.await
.map_err(BasePathDiscoveryError::IoError)?;
Expand Down

0 comments on commit 1c0bb47

Please sign in to comment.