xtask: Switch to clap

This commit is contained in:
Kévin Commaille 2022-03-17 23:03:42 +01:00 committed by Kévin Commaille
parent d94e194b75
commit 097d329718
5 changed files with 56 additions and 129 deletions

View File

@ -8,11 +8,11 @@ publish = false
default = ["isahc", "semver", "toml_edit"]
[dependencies]
clap = { version = "3.1.6", features = ["derive"] }
isahc = { version = "1.2.0", features = ["json"], optional = true }
semver = { version = "1.0.4", features = ["serde"], optional = true }
serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.60"
toml = "0.5.8"
toml_edit = { version = "0.6.0", optional = true }
xflags = "0.2.1"
xshell = "0.1.9"

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use clap::{Args, Subcommand};
use xshell::pushd;
use crate::{cmd, Metadata, Result};
@ -10,17 +11,31 @@ use spec_links::check_spec_links;
const MSRV: &str = "1.55";
#[derive(Args)]
pub struct CiArgs {
/// Which version of Rust to test against.
#[clap(subcommand)]
pub version: Option<CiVersion>,
}
#[derive(Subcommand)]
pub enum CiVersion {
Msrv,
Nightly,
Stable,
}
/// Task to run CI tests.
pub struct CiTask {
/// Which version of Rust to test against.
version: Option<String>,
version: Option<CiVersion>,
/// The root of the workspace.
project_root: PathBuf,
}
impl CiTask {
pub(crate) fn new(version: Option<String>) -> Result<Self> {
pub(crate) fn new(version: Option<CiVersion>) -> Result<Self> {
let project_root = Metadata::load()?.workspace_root;
Ok(Self { version, project_root })
}
@ -28,11 +43,10 @@ impl CiTask {
pub(crate) fn run(self) -> Result<()> {
let _p = pushd(&self.project_root)?;
match self.version.as_deref() {
Some("msrv") => self.build_msrv()?,
Some("stable") => self.build_stable()?,
Some("nightly") => self.build_nightly()?,
Some(_) => return Err("Wrong Rust version specified.".into()),
match self.version {
Some(CiVersion::Msrv) => self.build_msrv()?,
Some(CiVersion::Stable) => self.build_stable()?,
Some(CiVersion::Nightly) => self.build_nightly()?,
None => {
self.build_msrv().and(self.build_stable()).and(self.build_nightly())?;
}

View File

@ -1,92 +0,0 @@
#![allow(dead_code)] // silence never-used warning for from_vec in generated code
#[cfg(not(feature = "default"))]
use std::string::String as Version;
#[cfg(feature = "default")]
use semver::Version;
xflags::xflags! {
src "./src/flags.rs"
/// Run custom task.
cmd xtask {
default cmd help {
/// Print help information.
optional -h, --help
}
/// Create a new release of the given crate.
cmd release
/// The crate to release
required name: String
/// The new version of the crate
required version: Version
{}
/// Alias for release.
cmd publish
/// The crate to release
required name: String
/// The new version of the crate
required version: Version
{}
/// Run CI tests.
cmd ci
optional version: String
{}
}
}
// generated start
// The following code is generated by `xflags` macro.
// Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
#[derive(Debug)]
pub struct Xtask {
pub subcommand: XtaskCmd,
}
#[derive(Debug)]
pub enum XtaskCmd {
Help(Help),
Release(Release),
Publish(Publish),
Ci(Ci),
}
#[derive(Debug)]
pub struct Help {
pub help: bool,
}
#[derive(Debug)]
pub struct Release {
pub name: String,
pub version: Version,
}
#[derive(Debug)]
pub struct Publish {
pub name: String,
pub version: Version,
}
#[derive(Debug)]
pub struct Ci {
pub version: Option<String>,
}
impl Xtask {
pub const HELP: &'static str = Self::HELP_;
pub fn from_env() -> xflags::Result<Self> {
Self::from_env_()
}
pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
Self::from_vec_(args)
}
}
// generated end

View File

@ -7,56 +7,51 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use serde::Deserialize;
use serde_json::from_str as from_json_str;
#[cfg(feature = "default")]
mod cargo;
mod ci;
mod flags;
#[cfg(feature = "default")]
mod release;
#[cfg(feature = "default")]
mod util;
use ci::CiTask;
use ci::{CiArgs, CiTask};
#[cfg(feature = "default")]
use release::ReleaseTask;
use release::{ReleaseArgs, ReleaseTask};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn main() {
if let Err(e) = try_main() {
eprintln!("{}", e);
std::process::exit(-1);
}
#[derive(Parser)]
struct Xtask {
#[clap(subcommand)]
cmd: Command,
}
fn try_main() -> Result<()> {
let flags = flags::Xtask::from_env()?;
match flags.subcommand {
flags::XtaskCmd::Help(_) => {
println!("{}", flags::Xtask::HELP);
Ok(())
}
flags::XtaskCmd::Ci(ci) => {
let task = CiTask::new(ci.version)?;
task.run()
#[derive(Subcommand)]
enum Command {
/// Run continuous integration checks
Ci(CiArgs),
/// Publish a new version of a crate on crates.io, `publish` can be used as an alias
#[cfg(feature = "default")]
#[clap(alias = "publish")]
Release(ReleaseArgs),
}
fn main() -> Result<()> {
match Xtask::parse().cmd {
Command::Ci(args) => {
let ci = CiTask::new(args.version)?;
ci.run()
}
#[cfg(feature = "default")]
flags::XtaskCmd::Release(cmd) => {
let mut task = ReleaseTask::new(cmd.name, cmd.version)?;
Command::Release(args) => {
let mut task = ReleaseTask::new(args.package, args.version)?;
task.run()
}
#[cfg(feature = "default")]
flags::XtaskCmd::Publish(cmd) => {
let mut task = ReleaseTask::new(cmd.name, cmd.version)?;
task.run()
}
#[cfg(not(feature = "default"))]
_ => {
Err("This command is only available when xtask is built with default features.".into())
}
}
}

View File

@ -4,6 +4,7 @@ use std::{
time::Duration,
};
use clap::Args;
use isahc::{
auth::{Authentication, Credentials},
config::Configurable,
@ -17,6 +18,15 @@ use crate::{cargo::Package, cmd, util::ask_yes_no, GithubConfig, Metadata, Resul
const GITHUB_API_RUMA: &str = "https://api.github.com/repos/ruma/ruma";
#[derive(Args)]
pub struct ReleaseArgs {
/// The crate to release
pub package: String,
/// The new version of the crate
pub version: Version,
}
/// Task to create a new release of the given crate.
#[derive(Debug)]
pub struct ReleaseTask {