diff --git a/.changes/binaries-dir.md b/.changes/binaries-dir.md new file mode 100644 index 00000000..9e95b980 --- /dev/null +++ b/.changes/binaries-dir.md @@ -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. diff --git a/.changes/out-dir.md b/.changes/out-dir.md new file mode 100644 index 00000000..4f12b7d9 --- /dev/null +++ b/.changes/out-dir.md @@ -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. diff --git a/bindings/packager/nodejs/schema.json b/bindings/packager/nodejs/schema.json index 5dee8bf8..5d858c93 100644 --- a/bindings/packager/nodejs/schema.json +++ b/bindings/packager/nodejs/schema.json @@ -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": [ diff --git a/bindings/packager/nodejs/src-ts/config.d.ts b/bindings/packager/nodejs/src-ts/config.d.ts index 0c2262ee..b15b2477 100644 --- a/bindings/packager/nodejs/src-ts/config.d.ts +++ b/bindings/packager/nodejs/src-ts/config.d.ts @@ -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`]. * diff --git a/crates/packager/schema.json b/crates/packager/schema.json index 5dee8bf8..5d858c93 100644 --- a/crates/packager/schema.json +++ b/crates/packager/schema.json @@ -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": [ diff --git a/crates/packager/src/cli/config.rs b/crates/packager/src/cli/config.rs index 4c496c4f..5351030d 100644 --- a/crates/packager/src/cli/config.rs +++ b/crates/packager/src/cli/config.rs @@ -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(); } diff --git a/crates/packager/src/cli/mod.rs b/crates/packager/src/cli/mod.rs index e11b2849..f566a931 100644 --- a/crates/packager/src/cli/mod.rs +++ b/crates/packager/src/cli/mod.rs @@ -60,9 +60,16 @@ pub(crate) struct Cli { /// Specify which packages to use from the current workspace. #[clap(short, long, value_delimiter = ',')] packages: Option>, - /// 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, - + /// Specify The directory where the [`Config::binaries`] exist. + /// + /// Defaults to [`Config::out_dir`] + #[clap(long)] + binaries_dir: Option, /// Package the release version of your app. /// Ignored when `--config` is used. #[clap(short, long, group = "cargo-profile")] @@ -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 { diff --git a/crates/packager/src/config/mod.rs b/crates/packager/src/config/mod.rs index 356db61f..316df97c 100644 --- a/crates/packager/src/config/mod.rs +++ b/crates/packager/src/config/mod.rs @@ -1470,9 +1470,16 @@ pub struct Config { pub log_level: Option, /// The packaging formats to create, if not present, [`PackageFormat::platform_default`] is used. pub formats: Option>, - /// 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, /// The target triple we are packaging for. This mainly affects [`Config::external_binaries`]. /// /// Defaults to the current OS target triple. @@ -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) } } @@ -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() } }