Skip to content

Commit

Permalink
feat: add --target flag and respect target-triple config option w…
Browse files Browse the repository at this point in the history
…hen packaging rust binaries (#277)
  • Loading branch information
amr-crabnebula authored Sep 26, 2024
1 parent 93b2327 commit 41b05d0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/target-flag-option-rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-packager": patch
---

Respect `target-triple` config option when packaging rust binaries.
6 changes: 6 additions & 0 deletions .changes/target-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cargo-packager": patch
"@crabnebula/packager": patch
---

Add `--target` flag to specify target triple to package.
30 changes: 17 additions & 13 deletions crates/packager/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,27 @@ pub fn find_config_files() -> crate::Result<Vec<PathBuf>> {

#[tracing::instrument(level = "trace")]
pub fn load_configs_from_cargo_workspace(
release: bool,
profile: Option<String>,
manifest_path: Option<PathBuf>,
cli: &super::Cli,
) -> crate::Result<Vec<(Option<PathBuf>, Config)>> {
let profile = if release {
let profile = if cli.release {
"release"
} else if let Some(profile) = &profile {
} else if let Some(profile) = &cli.profile {
profile.as_str()
} else {
"debug"
};

let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
if let Some(manifest_path) = &manifest_path {
if let Some(manifest_path) = &cli.manifest_path {
metadata_cmd.manifest_path(manifest_path);
}
let Ok(metadata) = metadata_cmd.exec() else {
return Ok(Vec::new());

let metadata = match metadata_cmd.exec() {
Ok(m) => m,
Err(e) => {
tracing::debug!("cargo metadata failed: {e}");
return Ok(Vec::new());
}
};

let mut configs = Vec::new();
Expand Down Expand Up @@ -139,11 +142,12 @@ pub fn load_configs_from_cargo_workspace(
.replace(format!("com.{}.{}", author, package.name));
}

let cargo_out_dir = metadata
.target_directory
.as_std_path()
.to_path_buf()
.join(profile);
let mut cargo_out_dir = metadata.target_directory.as_std_path().to_path_buf();
if let Some(target_triple) = cli.target.as_ref().or(config.target_triple.as_ref()) {
cargo_out_dir.push(target_triple);
}
cargo_out_dir.push(profile);

if config.binaries_dir.is_none() {
config.binaries_dir.replace(cargo_out_dir.clone());
}
Expand Down
18 changes: 10 additions & 8 deletions crates/packager/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) struct Cli {
#[clap(short, long, global = true)]
quite: bool,

/// Specify the package fromats to build.
/// The package fromats to build.
#[clap(short, long, value_enum, value_delimiter = ',')]
formats: Option<Vec<PackageFormat>>,
/// Specify a configuration to read, which could be a JSON file,
Expand All @@ -55,15 +55,15 @@ pub(crate) struct Cli {
/// The password for the signing private key.
#[clap(long, env = "CARGO_PACKAGER_SIGN_PRIVATE_KEY_PASSWORD")]
password: Option<String>,
/// Specify which packages to use from the current workspace.
/// Which packages to use from the current workspace.
#[clap(short, long, value_delimiter = ',')]
packages: Option<Vec<String>>,
/// Specify The directory where the packages will be placed.
pub(crate) packages: Option<Vec<String>>,
/// The directory where the packages will be placed.
///
/// If [`Config::binaries_dir`] is not defined, it is also the path where the binaries are located if they use relative paths.
#[clap(short, long, alias = "out")]
out_dir: Option<PathBuf>,
/// Specify The directory where the [`Config::binaries`] exist.
/// The directory where the [`Config::binaries`] exist.
///
/// Defaults to [`Config::out_dir`]
#[clap(long)]
Expand All @@ -76,10 +76,13 @@ pub(crate) struct Cli {
/// Ignored when `--config` is used.
#[clap(long, group = "cargo-profile")]
profile: Option<String>,
/// Specify the manifest path to use for reading the configuration.
/// Path to Cargo.toml manifest path to use for reading the configuration.
/// Ignored when `--config` is used.
#[clap(long)]
manifest_path: Option<PathBuf>,
/// Target triple to use for detecting your app binaries.
#[clap(long)]
target: Option<String>,

#[command(subcommand)]
command: Option<Commands>,
Expand Down Expand Up @@ -113,8 +116,7 @@ fn run_cli(cli: Cli) -> Result<()> {
.filter_map(|c| parse_config_file(c).ok())
.collect::<Vec<_>>()
.concat();
let cargo_configs =
load_configs_from_cargo_workspace(cli.release, cli.profile, cli.manifest_path)?;
let cargo_configs = load_configs_from_cargo_workspace(&cli)?;
[config_files, cargo_configs]
.concat()
.into_iter()
Expand Down
3 changes: 1 addition & 2 deletions crates/updater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,10 @@ impl Update {

impl<R: Read, C: Fn(usize, Option<u64>)> Read for DownloadProgress<R, C> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.inner.read(buf).map(|n| {
self.inner.read(buf).inspect(|&n| {
if let Some(on_chunk) = &self.on_chunk {
(on_chunk)(n, self.content_length);
}
n
})
}
}
Expand Down

0 comments on commit 41b05d0

Please sign in to comment.