Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --profile command line option #73

Merged
merged 1 commit into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Because all packages in the workspace share a common output directory that is lo

### Cross compilation

This command supports `--target-dir` and `--target` option like `cargo build`.
This command supports `--target-dir`, `--target`, and `--profile` options like `cargo build`.
Depending on these options, this command changes the RPM package file location and replaces `target/release/` of
the source locations of the assets.

Expand All @@ -189,6 +189,9 @@ In this case, the source of the asset `{ source = "target/release/XXX", dest = "
You can use `CARGO_BUILD_TARGET` environment variable instead of `--target` option and `CARGO_BUILD_TARGET_DIR` or
`CARGO_TARGET_DIR` instead of `--target-dir`.

Similarly, if using a custom build profile with, for example, `--profile custom` the source of the asset
`{ source = "target/release/XXX" }` will be treated as `target/custom/XXX`.

### Payload compress type

The default payload compress type of the generated RPM file is zstd.
Expand Down
1 change: 1 addition & 0 deletions src/build_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
pub struct BuildTarget {
pub target_dir: Option<String>,
pub target: Option<String>,
pub profile: Option<String>,
pub arch: Option<String>,
}

Expand Down
24 changes: 16 additions & 8 deletions src/config/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ impl FileInfo<'_, '_, '_, '_> {
parent: P,
idx: usize,
) -> Result<Vec<(PathBuf, String)>, ConfigError> {
let profile = build_target.profile.as_deref().unwrap_or("release");
let source = self
.source
.strip_prefix("target/release/")
.or_else(|| self.source.strip_prefix(&format!("target/{profile}/")))
.and_then(|rel_path| {
build_target
.target_path("release")
.target_path(profile)
.join(rel_path)
.to_str()
.map(|v| v.to_string())
Expand Down Expand Up @@ -411,11 +413,16 @@ mod test {
)]
);

std::fs::create_dir_all(tempdir.path().join("target/foobarbaz/release")).unwrap();
File::create(tempdir.path().join("target/foobarbaz/release/foobarbaz")).unwrap();
std::fs::create_dir_all(tempdir.path().join("target/target-triple/my-profile")).unwrap();
File::create(
tempdir
.path()
.join("target/target-triple/my-profile/my-bin"),
)
.unwrap();
let file_info = FileInfo {
source: "target/release/foobarbaz".into(),
dest: "/usr/bin/foobarbaz".into(),
source: "target/release/my-bin".into(),
dest: "/usr/bin/my-bin".into(),
user: None,
group: None,
mode: None,
Expand All @@ -432,7 +439,8 @@ mod test {
.unwrap()
.to_string(),
),
target: Some("foobarbaz".to_string()),
target: Some("target-triple".to_string()),
profile: Some("my-profile".to_string()),
..Default::default()
};
let expanded = file_info
Expand All @@ -447,7 +455,7 @@ mod test {
Some(
tempdir
.path()
.join("target/foobarbaz/release/foobarbaz")
.join("target/target-triple/my-profile/my-bin")
.to_str()
.unwrap()
),
Expand Down Expand Up @@ -490,7 +498,7 @@ mod test {
"/usr/share/doc/cargo-generate-rpm/", // specifying directory
0
)
.unwrap(),
.unwrap(),
vec![(
PathBuf::from("README.md"),
"/usr/share/doc/cargo-generate-rpm/README.md".into()
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, CliSetti
May be specified with CARGO_BUILD_TARGET_DIR or CARGO_TARGET_DIR environment variables.",
"DIRECTORY",
);
opts.optopt(
"",
"profile",
"Select which build profile to package. Defaults to \"release\".",
"PROFILE",
);
opts.optopt(
"",
"payload-compress",
Expand Down Expand Up @@ -179,6 +185,9 @@ fn parse_arg() -> Result<(BuildTarget, Option<PathBuf>, Option<String>, CliSetti
if let Some(target_dir) = opt_matches.opt_str("target-dir") {
build_target.target_dir = Some(target_dir);
}
if let Some(profile) = opt_matches.opt_str("profile") {
build_target.profile = Some(profile);
}
let payload_compress = opt_matches
.opt_str("payload-compress")
.unwrap_or("zstd".to_string());
Expand Down