Skip to content

Commit

Permalink
Add experimental dist task to xtask
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharparam committed Aug 22, 2023
1 parent 5978315 commit ea9f91f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pre-release-replacements = []

[dependencies]
anyhow = "1.0.74"
clap = { version = "4.3.21", features = ["derive", "string"] }
clap = { version = "4.3.21", features = ["derive", "env", "string"] }
clap_mangen = "0.2.12"
facti = { version = "0.2.3", path = "../cli" }
xshell = "0.2.5"
56 changes: 53 additions & 3 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use std::{

use anyhow::{Context, Result};

use clap::{CommandFactory, Parser, Subcommand};
use clap::{Args, CommandFactory, Parser, Subcommand};

use facti::__xtask::Cli;
use xshell::{cmd, Shell};

fn main() -> Result<()> {
cwd_to_workspace_root()?;
Expand All @@ -21,6 +22,28 @@ fn main() -> Result<()> {
let cmd = Cli::command();
gen_manpages(&out_dir, &cmd, None)?;
}
Tasks::Dist(dist_args) => {
let verbose = if cli.verbose { Some("--verbose") } else { None };
let cargo = &if cli.cross {
"cross".to_owned()
} else {
cli.cargo.unwrap_or_else(|| "cargo".to_owned())
};
let sh = Shell::new()?;
dbg!(cmd!(sh, "pwd").read()?);

eprintln!("Clean dist folder");
cmd!(sh, "rm -rf target/dist").run()?;

let target = &dist_args.target;

eprintln!("Build facti");
cmd!(
sh,
"{cargo} build {verbose...} --profile dist --all-features --locked --target {target} --target-dir target/dist --package facti"
)
.run()?;
}
}

Ok(())
Expand Down Expand Up @@ -60,6 +83,15 @@ fn gen_manpages(out_dir: &Path, cmd: &clap::Command, current_name: Option<&str>)
#[derive(Parser, Debug)]
#[command(bin_name = "cargo xtask")]
struct BuildCli {
#[arg(long)]
pub verbose: bool,

#[arg(long, env = "CARGO", global = true)]
pub cargo: Option<String>,

#[arg(long, global = true)]
pub cross: bool,

#[command(subcommand)]
pub task: Tasks,
}
Expand All @@ -68,13 +100,31 @@ struct BuildCli {
enum Tasks {
/// Generate manpages for Facti.
Man,

/// `cargo-dist` lookalike.
///
/// We use our own until there's cross-platform build support
/// in the actual cargo-dist.
Dist(DistArgs),
}

#[derive(Args, Debug)]
struct DistArgs {
#[arg(long)]
pub target: String,

#[arg(raw = true)]
pub cargo_args: Vec<String>,
}

// Shamelessly stolen^Wcopied from cargo itself:
// https://github.com/rust-lang/cargo/blob/e5e68c4093af9de3f80e9427b979fa5a0d8361cc/crates/xtask-build-man/src/main.rs#L78-L82
fn cwd_to_workspace_root() -> Result<()> {
let pkg_root = std::env!("CARGO_MANIFEST_DIR");
let ws_root = format!("{pkg_root}/..");
let ws_root = format!("{pkg_root}/../..");
eprintln!("Performing CWD to workspace root {}", ws_root);
env::set_current_dir(ws_root).context("Failed to CWD to workspace root")
env::set_current_dir(ws_root).context("Failed to CWD to workspace root")?;
let pwd = env::current_dir().context("Failed to get PWD after change")?;
eprintln!("Now in: {}", pwd.display());
Ok(())
}

0 comments on commit ea9f91f

Please sign in to comment.