From fc23ad83fe73f2fcf8485fc48b6a8e7f8ec2fdb6 Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Tue, 5 Mar 2024 14:34:28 +0900 Subject: [PATCH 1/4] feat: support `files` field in package metadata --- CHANGELOG.md | 11 +++++++++++ Cargo.toml | 4 ++++ src/lib.rs | 14 ++++++++++++++ src/main.rs | 13 +++++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e4e3c6..1858a70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ - The `--output` flag for customizing the location of the output produced by `cargo aur`. If unused, the default remains `target/cargo-aur/`. +- A new `files` field in `[package.metadata.aur]`, which accepts a list-of-pairs + of additional files you want copied to the user's filesystem upon package + installation. Output looks like: + +``` +package() { + install -Dm755 cargo-aur -t "$pkgdir/usr/bin" + install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE" + install -Dm644 "/path/to/original" "/path/to/target" +} +``` #### Fixed diff --git a/Cargo.toml b/Cargo.toml index bf47a50..8d1f6e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,7 @@ strip = true opt-level = "z" codegen-units = 1 panic = "abort" + +[package.metadata.aur] +depends = ["blah"] +files = [["/path/to/original", "/path/to/target"]] diff --git a/src/lib.rs b/src/lib.rs index 7f29c1b..918f127 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,18 @@ pub struct Metadata { pub aur: Option, } +impl Metadata { + /// The metadata block actually has some contents. + pub fn non_empty(&self) -> bool { + self.depends.is_empty().not() + || self.optdepends.is_empty().not() + || self + .aur + .as_ref() + .is_some_and(|aur| aur.depends.is_empty().not() || aur.optdepends.is_empty().not()) + } +} + impl std::fmt::Display for Metadata { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // Reconcile which section to read extra dependency information from. @@ -150,4 +162,6 @@ pub struct AUR { depends: Vec, #[serde(default)] optdepends: Vec, + #[serde(default)] + pub files: Vec<(String, String)>, } diff --git a/src/main.rs b/src/main.rs index 9411599..d39c0a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -208,8 +208,11 @@ where writeln!(file, "provides=(\"{}\")", package.name)?; writeln!(file, "conflicts=(\"{}\")", package.name)?; - if let Some(metadata) = package.metadata.as_ref() { - writeln!(file, "{}", metadata)?; + match package.metadata.as_ref() { + Some(metadata) if metadata.non_empty() => { + writeln!(file, "{}", metadata)?; + } + Some(_) | None => {} } writeln!(file, "source=(\"{}\")", source)?; @@ -234,6 +237,12 @@ where )?; } + if let Some(aur) = package.metadata.as_ref().and_then(|m| m.aur.as_ref()) { + for (source, target) in aur.files.iter() { + writeln!(file, " install -Dm644 \"{}\" \"{}\"", source, target)?; + } + } + writeln!(file, "}}")?; Ok(()) } From d08e994b528ee149996a3ada575626b57212cf43 Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Tue, 5 Mar 2024 21:57:27 +0900 Subject: [PATCH 2/4] feat: actually copy the designated files into the tarball --- Cargo.toml | 4 ++-- src/main.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d1f6e6..05b6859 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,5 +26,5 @@ codegen-units = 1 panic = "abort" [package.metadata.aur] -depends = ["blah"] -files = [["/path/to/original", "/path/to/target"]] +# depends = ["blah"] +# files = [[".github/dependabot.yml", "$pkgdir/usr/local/share/cargo-aur/dependabot.yml"]] diff --git a/src/main.rs b/src/main.rs index d39c0a8..9e36749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -289,6 +289,17 @@ fn tarball( if let Some(lic) = license { command.arg(lic.path()); } + if let Some(files) = config + .package + .metadata + .as_ref() + .and_then(|m| m.aur.as_ref()) + .map(|a| a.files.as_slice()) + { + for (file, _) in files { + command.arg(file); + } + } command.status()?; std::fs::remove_file(binary_name)?; From 5df440310efae640a0d60c4f833adc922444c255 Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Tue, 5 Mar 2024 21:57:39 +0900 Subject: [PATCH 3/4] docs: show examples of `files` --- CHANGELOG.md | 2 +- README.md | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1858a70..87ea018 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ package() { install -Dm755 cargo-aur -t "$pkgdir/usr/bin" install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE" - install -Dm644 "/path/to/original" "/path/to/target" + install -Dm644 "/path/to/original/foo.txt" "$pkgdir/path/to/target/foo.txt" } ``` diff --git a/README.md b/README.md index 20d8933..ecaf8be 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,26 @@ optdepends = ["sushi", "ramen"] And these settings will be copied to your PKGBUILD. +### Including Additional Files + +The `files` list can be used to designated initial files to be copied the user's +filesystem. So this: + +```toml +[package.metadata.aur] +files = [["path/to/local/foo.txt", "$pkgdir/usr/local/share/your-app/foo.txt"]] +``` + +will result in this: + +```toml +package() { + install -Dm755 your-app -t "$pkgdir/usr/bin" + install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE" + install -Dm644 "path/to/local/foo.txt" "$pkgdir/usr/local/share/your-app/foo.txt" +} +``` + ### Static Binaries Run with `--musl` to produce a release binary that is statically linked via From 4bebc6233135b604ccb9cb009290e33e14874a47 Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Thu, 7 Mar 2024 20:39:11 +0900 Subject: [PATCH 4/4] feat: prepend `$pkgdir` to avoid user hassle --- Cargo.toml | 2 +- README.md | 2 +- src/error.rs | 6 +++++- src/lib.rs | 2 +- src/main.rs | 11 ++++++++++- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 05b6859..5676478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ panic = "abort" [package.metadata.aur] # depends = ["blah"] -# files = [[".github/dependabot.yml", "$pkgdir/usr/local/share/cargo-aur/dependabot.yml"]] +# files = [[".github/dependabot.yml", "/usr/local/share/cargo-aur/dependabot.yml"]] diff --git a/README.md b/README.md index ecaf8be..bfcc99a 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ filesystem. So this: ```toml [package.metadata.aur] -files = [["path/to/local/foo.txt", "$pkgdir/usr/local/share/your-app/foo.txt"]] +files = [["path/to/local/foo.txt", "/usr/local/share/your-app/foo.txt"]] ``` will result in this: diff --git a/src/error.rs b/src/error.rs index 87531c1..21dd65d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ //! Errors that can occur in this application. -use std::fmt::Display; +use std::{fmt::Display, path::PathBuf}; pub(crate) enum Error { IO(std::io::Error), @@ -9,6 +9,7 @@ pub(crate) enum Error { Utf8OsString, MissingMuslTarget, MissingLicense, + TargetNotAbsolute(PathBuf), } impl Display for Error { @@ -25,6 +26,9 @@ impl Display for Error { Error::MissingLicense => { write!(f, "Missing LICENSE file. See https://choosealicense.com/") } + Error::TargetNotAbsolute(p) => { + write!(f, "Target filepath is not absolute: {}", p.display()) + } } } } diff --git a/src/lib.rs b/src/lib.rs index 918f127..3dc379e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,5 +163,5 @@ pub struct AUR { #[serde(default)] optdepends: Vec, #[serde(default)] - pub files: Vec<(String, String)>, + pub files: Vec<(PathBuf, PathBuf)>, } diff --git a/src/main.rs b/src/main.rs index 9e36749..0b99026 100644 --- a/src/main.rs +++ b/src/main.rs @@ -239,7 +239,16 @@ where if let Some(aur) = package.metadata.as_ref().and_then(|m| m.aur.as_ref()) { for (source, target) in aur.files.iter() { - writeln!(file, " install -Dm644 \"{}\" \"{}\"", source, target)?; + if target.has_root().not() { + return Err(Error::TargetNotAbsolute(target.to_path_buf())); + } else { + writeln!( + file, + " install -Dm644 \"{}\" \"$pkgdir{}\"", + source.display(), + target.display() + )?; + } } }