xtask: Switch to clap
This commit is contained in:
parent
d94e194b75
commit
097d329718
@ -8,11 +8,11 @@ publish = false
|
|||||||
default = ["isahc", "semver", "toml_edit"]
|
default = ["isahc", "semver", "toml_edit"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "3.1.6", features = ["derive"] }
|
||||||
isahc = { version = "1.2.0", features = ["json"], optional = true }
|
isahc = { version = "1.2.0", features = ["json"], optional = true }
|
||||||
semver = { version = "1.0.4", features = ["serde"], optional = true }
|
semver = { version = "1.0.4", features = ["serde"], optional = true }
|
||||||
serde = { version = "1.0.118", features = ["derive"] }
|
serde = { version = "1.0.118", features = ["derive"] }
|
||||||
serde_json = "1.0.60"
|
serde_json = "1.0.60"
|
||||||
toml = "0.5.8"
|
toml = "0.5.8"
|
||||||
toml_edit = { version = "0.6.0", optional = true }
|
toml_edit = { version = "0.6.0", optional = true }
|
||||||
xflags = "0.2.1"
|
|
||||||
xshell = "0.1.9"
|
xshell = "0.1.9"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use clap::{Args, Subcommand};
|
||||||
use xshell::pushd;
|
use xshell::pushd;
|
||||||
|
|
||||||
use crate::{cmd, Metadata, Result};
|
use crate::{cmd, Metadata, Result};
|
||||||
@ -10,17 +11,31 @@ use spec_links::check_spec_links;
|
|||||||
|
|
||||||
const MSRV: &str = "1.55";
|
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.
|
/// Task to run CI tests.
|
||||||
pub struct CiTask {
|
pub struct CiTask {
|
||||||
/// Which version of Rust to test against.
|
/// Which version of Rust to test against.
|
||||||
version: Option<String>,
|
version: Option<CiVersion>,
|
||||||
|
|
||||||
/// The root of the workspace.
|
/// The root of the workspace.
|
||||||
project_root: PathBuf,
|
project_root: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CiTask {
|
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;
|
let project_root = Metadata::load()?.workspace_root;
|
||||||
Ok(Self { version, project_root })
|
Ok(Self { version, project_root })
|
||||||
}
|
}
|
||||||
@ -28,11 +43,10 @@ impl CiTask {
|
|||||||
pub(crate) fn run(self) -> Result<()> {
|
pub(crate) fn run(self) -> Result<()> {
|
||||||
let _p = pushd(&self.project_root)?;
|
let _p = pushd(&self.project_root)?;
|
||||||
|
|
||||||
match self.version.as_deref() {
|
match self.version {
|
||||||
Some("msrv") => self.build_msrv()?,
|
Some(CiVersion::Msrv) => self.build_msrv()?,
|
||||||
Some("stable") => self.build_stable()?,
|
Some(CiVersion::Stable) => self.build_stable()?,
|
||||||
Some("nightly") => self.build_nightly()?,
|
Some(CiVersion::Nightly) => self.build_nightly()?,
|
||||||
Some(_) => return Err("Wrong Rust version specified.".into()),
|
|
||||||
None => {
|
None => {
|
||||||
self.build_msrv().and(self.build_stable()).and(self.build_nightly())?;
|
self.build_msrv().and(self.build_stable()).and(self.build_nightly())?;
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
@ -7,56 +7,51 @@
|
|||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::from_str as from_json_str;
|
use serde_json::from_str as from_json_str;
|
||||||
|
|
||||||
#[cfg(feature = "default")]
|
#[cfg(feature = "default")]
|
||||||
mod cargo;
|
mod cargo;
|
||||||
mod ci;
|
mod ci;
|
||||||
mod flags;
|
|
||||||
#[cfg(feature = "default")]
|
#[cfg(feature = "default")]
|
||||||
mod release;
|
mod release;
|
||||||
#[cfg(feature = "default")]
|
#[cfg(feature = "default")]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
use ci::CiTask;
|
use ci::{CiArgs, CiTask};
|
||||||
#[cfg(feature = "default")]
|
#[cfg(feature = "default")]
|
||||||
use release::ReleaseTask;
|
use release::{ReleaseArgs, ReleaseTask};
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
fn main() {
|
#[derive(Parser)]
|
||||||
if let Err(e) = try_main() {
|
struct Xtask {
|
||||||
eprintln!("{}", e);
|
#[clap(subcommand)]
|
||||||
std::process::exit(-1);
|
cmd: Command,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_main() -> Result<()> {
|
#[derive(Subcommand)]
|
||||||
let flags = flags::Xtask::from_env()?;
|
enum Command {
|
||||||
match flags.subcommand {
|
/// Run continuous integration checks
|
||||||
flags::XtaskCmd::Help(_) => {
|
Ci(CiArgs),
|
||||||
println!("{}", flags::Xtask::HELP);
|
/// Publish a new version of a crate on crates.io, `publish` can be used as an alias
|
||||||
Ok(())
|
#[cfg(feature = "default")]
|
||||||
}
|
#[clap(alias = "publish")]
|
||||||
flags::XtaskCmd::Ci(ci) => {
|
Release(ReleaseArgs),
|
||||||
let task = CiTask::new(ci.version)?;
|
}
|
||||||
task.run()
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
match Xtask::parse().cmd {
|
||||||
|
Command::Ci(args) => {
|
||||||
|
let ci = CiTask::new(args.version)?;
|
||||||
|
ci.run()
|
||||||
}
|
}
|
||||||
#[cfg(feature = "default")]
|
#[cfg(feature = "default")]
|
||||||
flags::XtaskCmd::Release(cmd) => {
|
Command::Release(args) => {
|
||||||
let mut task = ReleaseTask::new(cmd.name, cmd.version)?;
|
let mut task = ReleaseTask::new(args.package, args.version)?;
|
||||||
task.run()
|
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ use std::{
|
|||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use clap::Args;
|
||||||
use isahc::{
|
use isahc::{
|
||||||
auth::{Authentication, Credentials},
|
auth::{Authentication, Credentials},
|
||||||
config::Configurable,
|
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";
|
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.
|
/// Task to create a new release of the given crate.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ReleaseTask {
|
pub struct ReleaseTask {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user