Skip to content
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
22 changes: 21 additions & 1 deletion xtask/src/args.rs
Original file line number Diff line number Diff line change
@@ -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"))]
Expand Down Expand Up @@ -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/<arch>
pub(crate) path: Option<PathBuf>,
Expand All @@ -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<Cow<'static, str>, String> {
Ok(Cow::Owned(v.to_string()))
}
24 changes: 13 additions & 11 deletions xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -92,36 +93,37 @@ pub(crate) fn build(path: Option<PathBuf>, arch: &str) -> Result<()> {
Ok(())
}

pub(crate) fn install(path: Option<PathBuf>, 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();
Ok(())
}

#[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();
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
Loading