aboutsummaryrefslogtreecommitdiffstats
path: root/xtask/src/main.rs
blob: 2282efd042adf30fd266e8e45b37a78546b176e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! See <https://github.com/matklad/cargo-xtask/>.
//!
//! This binary defines various auxiliary build commands, which are not
//! expressible with just `cargo`.
//!
//! This binary is integrated into the `cargo` command line by using an alias in
//! `.cargo/config`.

use std::path::PathBuf;

use anyhow::Result;
use clap::{Parser, Subcommand};

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

    match cli.command {
        Commands::Dist { tag } => {
            let targz = xtask::generate_tar_gz(&cli.directory, &cli.profile, tag.as_deref())?;
            println!("{}", targz.display());
        }
        Commands::OutDir => {
            let profile_dir = cli.directory.join("target").join(&cli.profile);
            let out_dir = xtask::find_out_dir(profile_dir)?;
            println!("{}", out_dir.display());
        }
    };

    Ok(())
}

#[derive(Debug, Clone, Parser)]
#[command(author, version, about)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Cargo profile to use.
    #[arg(short, long, default_value = "release")]
    profile: String,

    /// Path to root directory of package.
    #[arg(short='C', long, default_value_os_t = get_package_dir())]
    directory: PathBuf,
}

#[derive(Debug, Clone, Subcommand)]
enum Commands {
    /// Print the default value of OUT_DIR used by cargo when building the package.
    OutDir,

    /// Generate distributable package and print it's path
    Dist {
        /// Verify the package version matches the provided tag.
        #[arg(short, long)]
        tag: Option<String>,
    },
}

fn get_package_dir() -> PathBuf {
    std::path::Path::new(&env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .to_path_buf()
}