Skip to content

Commit

Permalink
feat(packager): config for binaries dir (#197)
Browse files Browse the repository at this point in the history
* feat(packager): config for binaries dir

* --binaries-dir

* aliases typo

---------

Co-authored-by: amr-crabnebula <[email protected]>
  • Loading branch information
lucasfernog-crabnebula and amr-crabnebula committed Apr 15, 2024
1 parent 2164d02 commit 3057a4a
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/binaries-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-packager": patch
---

Added `Config::binaries_dir` and `--binaries-dir` so you can specify the location of the binaries without modifying the output directory.
5 changes: 5 additions & 0 deletions .changes/out-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-packager": minor
---

Added `--out-dir/-o` flags and removed the positional argument to specify where to ouput packages, use the newly added flags instead.
10 changes: 9 additions & 1 deletion bindings/packager/nodejs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@
}
},
"outDir": {
"description": "The directory where the [`Config::binaries`] exist and where the generated packages will be placed.",
"description": "The directory where the generated packages will be placed.\n\nIf [`Config::binaries_dir`] is not set, this is also where the [`Config::binaries`] exist.",
"default": "",
"type": "string"
},
"binariesDir": {
"description": "The directory where the [`Config::binaries`] exist.\n\nDefaults to [`Config::out_dir`].",
"default": null,
"type": [
"string",
"null"
]
},
"targetTriple": {
"description": "The target triple we are packaging for. This mainly affects [`Config::external_binaries`].\n\nDefaults to the current OS target triple.",
"type": [
Expand Down
10 changes: 9 additions & 1 deletion bindings/packager/nodejs/src-ts/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,17 @@ export interface Config {
*/
formats?: PackageFormat[] | null;
/**
* The directory where the [`Config::binaries`] exist and where the generated packages will be placed.
* The directory where the generated packages will be placed.
*
* If [`Config::binaries_dir`] is not set, this is also where the [`Config::binaries`] exist.
*/
outDir?: string;
/**
* The directory where the [`Config::binaries`] exist.
*
* Defaults to [`Config::out_dir`].
*/
binariesDir?: string | null;
/**
* The target triple we are packaging for. This mainly affects [`Config::external_binaries`].
*
Expand Down
10 changes: 9 additions & 1 deletion crates/packager/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@
}
},
"outDir": {
"description": "The directory where the [`Config::binaries`] exist and where the generated packages will be placed.",
"description": "The directory where the generated packages will be placed.\n\nIf [`Config::binaries_dir`] is not set, this is also where the [`Config::binaries`] exist.",
"default": "",
"type": "string"
},
"binariesDir": {
"description": "The directory where the [`Config::binaries`] exist.\n\nDefaults to [`Config::out_dir`].",
"default": null,
"type": [
"string",
"null"
]
},
"targetTriple": {
"description": "The target triple we are packaging for. This mainly affects [`Config::external_binaries`].\n\nDefaults to the current OS target triple.",
"type": [
Expand Down
16 changes: 11 additions & 5 deletions crates/packager/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,19 @@ pub fn load_configs_from_cargo_workspace(
.identifier
.replace(format!("com.{}.{}", author, package.name));
}

let cargo_out_dir = metadata
.target_directory
.as_std_path()
.to_path_buf()
.join(profile);
if config.binaries_dir.is_none() {
config.binaries_dir.replace(cargo_out_dir.clone());
}
if config.out_dir.as_os_str().is_empty() {
config.out_dir = metadata
.target_directory
.as_std_path()
.to_path_buf()
.join(profile);
config.out_dir = cargo_out_dir;
}

if config.description.is_none() {
config.description = package.description.clone();
}
Expand Down
24 changes: 21 additions & 3 deletions crates/packager/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ pub(crate) struct Cli {
/// Specify which packages to use from the current workspace.
#[clap(short, long, value_delimiter = ',')]
packages: Option<Vec<String>>,
/// Specify The directory where the `binaries` exist and where the packages will be placed.
/// Specify 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.
///
/// Defaults to [`Config::out_dir`]
#[clap(long)]
binaries_dir: Option<PathBuf>,
/// Package the release version of your app.
/// Ignored when `--config` is used.
#[clap(short, long, group = "cargo-profile")]
Expand Down Expand Up @@ -139,7 +146,18 @@ fn run_cli(cli: Cli) -> Result<()> {
std::process::exit(1);
}

let cli_out_dir = cli.out_dir.as_ref().map(dunce::canonicalize).transpose()?;
let cli_out_dir = cli
.out_dir
.as_ref()
.map(|p| {
if p.exists() {
dunce::canonicalize(p)
} else {
std::fs::create_dir_all(p)?;
Ok(p.to_owned())
}
})
.transpose()?;

for (_, config) in &mut configs {
if let Some(dir) = &cli_out_dir {
Expand Down
25 changes: 22 additions & 3 deletions crates/packager/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,9 +1470,16 @@ pub struct Config {
pub log_level: Option<LogLevel>,
/// The packaging formats to create, if not present, [`PackageFormat::platform_default`] is used.
pub formats: Option<Vec<PackageFormat>>,
/// The directory where the [`Config::binaries`] exist and where the generated packages will be placed.
/// The directory where the generated packages will be placed.
///
/// If [`Config::binaries_dir`] is not set, this is also where the [`Config::binaries`] exist.
#[serde(default, alias = "out-dir", alias = "out_dir")]
pub out_dir: PathBuf,
/// The directory where the [`Config::binaries`] exist.
///
/// Defaults to [`Config::out_dir`].
#[serde(default, alias = "binaries-dir", alias = "binaries_dir")]
pub binaries_dir: Option<PathBuf>,
/// The target triple we are packaging for. This mainly affects [`Config::external_binaries`].
///
/// Defaults to the current OS target triple.
Expand Down Expand Up @@ -1624,7 +1631,7 @@ impl Config {
if binary.path.is_absolute() {
binary.path.clone()
} else {
self.out_dir().join(&binary.path)
self.binaries_dir().join(&binary.path)
}
}

Expand All @@ -1649,8 +1656,20 @@ impl Config {
pub fn out_dir(&self) -> PathBuf {
if self.out_dir.as_os_str().is_empty() {
std::env::current_dir().expect("failed to resolve cwd")
} else {
} else if self.out_dir.exists() {
dunce::canonicalize(&self.out_dir).unwrap_or_else(|_| self.out_dir.clone())
} else {
std::fs::create_dir_all(&self.out_dir).expect("failed to create output directory");
self.out_dir.clone()
}
}

/// Returns the binaries dir. Defaults to [`Self::out_dir`] if [`Self::binaries_dir`] is not set.
pub fn binaries_dir(&self) -> PathBuf {
if let Some(path) = &self.binaries_dir {
dunce::canonicalize(path).unwrap_or_else(|_| path.clone())
} else {
self.out_dir()
}
}

Expand Down

0 comments on commit 3057a4a

Please sign in to comment.