From 2e562ed74fabc560b3af7edb48f39b9f3f6944e8 Mon Sep 17 00:00:00 2001 From: "J. Gerhards" Date: Wed, 18 Mar 2026 18:57:08 +0100 Subject: [PATCH] add --pkgname option to xtask install Add an extra --pkgname option to xtask install so that the doc and license files get placed to the correct path instead of mpdris if compiling the -bin or -git pakcage versions --- xtask/src/args.rs | 22 +++++++++++++++++++++- xtask/src/dist.rs | 24 +++++++++++++----------- xtask/src/main.rs | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/xtask/src/args.rs b/xtask/src/args.rs index e8195f9..c1ab7a1 100644 --- a/xtask/src/args.rs +++ b/xtask/src/args.rs @@ -1,7 +1,9 @@ -use std::{env, path::PathBuf}; +use std::{borrow::Cow, env, path::PathBuf}; use argh::FromArgs; +use crate::NAME; + /// XTasks #[derive(FromArgs)] #[argh(help_triggers("-h", "--help"))] @@ -52,6 +54,10 @@ pub(crate) struct InstallTask { #[argh(option, default = "env::consts::ARCH.to_string()")] /// the arch to compile for pub(crate) arch: String, + #[argh(option, default = "default_pkgname()", from_str_fn(to_cow))] + /// the package name that gets installed, used for directory paths. Does not affect binary name + /// Defaults to $pkgname if set or "mpdris" if not. Panics if $pkgname is not Unicode + pub(crate) pkgname: Cow<'static, str>, #[argh(positional)] /// path to install the files to instead of target/dist/ pub(crate) path: Option, @@ -66,3 +72,17 @@ pub(crate) struct CleanTask {} /// Create release assets (tarballs, binaries and SHA256 checksums) for x86_64, aarch64, i68 #[argh(subcommand, name = "make-release-assets", help_triggers("-h", "--help"))] pub(crate) struct ReleaseTask {} + +fn default_pkgname() -> Cow<'static, str> { + env::var("pkgname").map_or_else( + |e| match e { + env::VarError::NotPresent => Cow::Borrowed(NAME), + env::VarError::NotUnicode(_) => panic!("$pkgname set, but contains non-unicode values"), + }, + |s| Cow::Owned(s), + ) +} + +fn to_cow(v: &str) -> Result, String> { + Ok(Cow::Owned(v.to_string())) +} diff --git a/xtask/src/dist.rs b/xtask/src/dist.rs index 61c8cea..a5cf5bd 100644 --- a/xtask/src/dist.rs +++ b/xtask/src/dist.rs @@ -2,6 +2,7 @@ use std::fs; use std::path::{Path, PathBuf}; use std::{env, fs::Permissions, io::Write, os::unix::fs::PermissionsExt, process::Command, sync::Arc}; +use crate::args::InstallTask; use crate::{DIST_DIR, NAME, PROJECT_ROOT, TARGET_DIR, Task, build_man}; use anyhow::{Context, Result, anyhow}; use flate2::{Compression, write::GzEncoder}; @@ -92,22 +93,23 @@ pub(crate) fn build(path: Option, arch: &str) -> Result<()> { Ok(()) } -pub(crate) fn install(path: Option, arch: &str) -> Result<()> { - let outdir = path.unwrap_or(DIST_DIR.join(arch)); +pub(crate) fn install(task: InstallTask) -> Result<()> { + let outdir = task.path.unwrap_or(DIST_DIR.join(&task.arch)); - install_create_dirs(&outdir).with_context(|| "Failed to create dist directory structure")?; - install_copy_files(&outdir, arch).with_context(|| "Failed to copy assets to install dir")?; + println!("Creating install with pkgname: {}", task.pkgname); + install_create_dirs(&outdir, &task.pkgname).with_context(|| "Failed to create dist directory structure")?; + install_copy_files(&outdir, &task.arch, &task.pkgname).with_context(|| "Failed to copy assets to install dir")?; Ok(()) } -fn install_create_dirs(outdir: &Path) -> Result<()> { +fn install_create_dirs(outdir: &Path, pkgname: &str) -> Result<()> { let t = Task::new("Creating directory structure"); fs::create_dir_all(outdir)?; fs::create_dir_all(outdir.join("usr/bin"))?; fs::create_dir_all(outdir.join("usr/lib/systemd/user"))?; - fs::create_dir_all(outdir.join(format!("usr/share/doc/{NAME}")))?; - fs::create_dir_all(outdir.join(format!("usr/share/licenses/{NAME}")))?; + fs::create_dir_all(outdir.join(format!("usr/share/doc/{pkgname}")))?; + fs::create_dir_all(outdir.join(format!("usr/share/licenses/{pkgname}")))?; fs::create_dir_all(outdir.join("usr/share/man"))?; t.success(); @@ -115,13 +117,13 @@ fn install_create_dirs(outdir: &Path) -> Result<()> { } #[rustfmt::skip] -fn install_copy_files(outdir: &Path, arch: &str) -> Result<()> { +fn install_copy_files(outdir: &Path, arch: &str, pkgname: &str) -> Result<()> { let t = Task::new("Copying files to dist"); cp!(&DIST_DIR, outdir, "{NAME}_{arch}-linux-gnu", "usr/bin/{NAME}", 0o755)?; cp!(outdir, "resources/mpdris.service", "usr/lib/systemd/user/mpdris.service", 0o644)?; - cp!(outdir, "resources/sample.mpdris.conf", "usr/share/doc/{NAME}/sample.mpdris.conf", 0o644)?; - cp!(outdir, "README.md", "usr/share/doc/{NAME}/README.md", 0o644)?; - cp!(outdir, "LICENSE", "usr/share/licenses/{NAME}/LICENSE", 0o644)?; + cp!(outdir, "resources/sample.mpdris.conf", "usr/share/doc/{pkgname}/sample.mpdris.conf", 0o644)?; + cp!(outdir, "README.md", "usr/share/doc/{pkgname}/README.md", 0o644)?; + cp!(outdir, "LICENSE", "usr/share/licenses/{pkgname}/LICENSE", 0o644)?; cp!(DIST_DIR, outdir, "man", "usr/share/man")?; t.success(); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 3b88e58..c3c7ef2 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -40,7 +40,7 @@ fn try_main() -> Result<()> { match args.task { Man(task) => build_man(&task.dir), Build(task) => build(task.path, &task.arch), - Install(task) => install(task.path, &task.arch), + Install(task) => install(task), CleanDist(..) => clean_dist(), MakeRelease(..) => make_release_assets(), }