Skip to content

Commit

Permalink
Switch to xtask-common repo
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Jan 29, 2025
1 parent 5bdd3b3 commit 44a9bfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 82 deletions.
29 changes: 19 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ edition = "2021"

[dependencies]
anyhow = "1.0.95"
clap = { version = "4.5.27", features = ["derive", "cargo", "env", "unicode", "wrap_help"] }
xshell = "0.2.7"
xtask-common = { git = "https://github.com/morr0ne/xtask-common", version = "0.1.0" }
75 changes: 5 additions & 70 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,11 @@
use anyhow::{bail, Context, Result};
use clap::{Parser, Subcommand};
use std::{
fs,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
};
use xshell::{cmd, Shell};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand)]
enum Commands {
/// Build the project
Build,
/// Install the project
Install {
#[arg(long, default_value = "/")]
destdir: PathBuf,
#[arg(long, default_value = "usr")]
prefix: PathBuf,
#[arg(long, default_value = "755")]
mode: String,
},
}
use anyhow::Result;
use clap::Parser;
use xtask_common::{clap, Cli, Empty};

fn main() -> Result<()> {
let cli = Cli::parse();

match &cli.command {
Commands::Build => build(),
Commands::Install {
destdir,
prefix,
mode,
} => install(destdir, prefix, mode),
}
}

fn build() -> Result<()> {
let sh = Shell::new()?;
println!("Building release version...");
cmd!(sh, "cargo build --release").run()?;
Ok(())
}

fn install(destdir: &Path, prefix: &Path, mode: &str) -> Result<()> {
if !fs::exists("target/release/verdi")? {
bail!("You must build the project first!")
}

let binary_dir = destdir.join(prefix).join("bin");

// Create target directory if it doesn't exist
fs::create_dir_all(&binary_dir).context("Failed to create binary directory")?;

let target = binary_dir.join("verdi");

fs::copy("target/release/verdi", &target)
.with_context(|| format!("Failed to copy binary to {:?}", target))?;

// Parse octal mode string (e.g., "755" or "0755")
let mode = u32::from_str_radix(mode.trim_start_matches('0'), 8)
.with_context(|| format!("Invalid mode: {mode}"))?;
let cli: Cli<Empty> = Cli::parse();

fs::set_permissions(&target, fs::Permissions::from_mode(mode))
.context("Failed to set binary permissions")?;
cli.command.execute("verdi")?;

println!("Installation complete!");
Ok(())
}

0 comments on commit 44a9bfe

Please sign in to comment.