cargo-workspace2 0.2.2

A tool to query and manage complex cargo workspaces
Documentation
//! Publishing operation handling

mod exec;
pub(super) use exec::run;

use super::RenderHelp;

/// Publish a single crate to crates.io
///
/// This command does the following things
///
/// 0. Determine if the git tree has modifications, `cargo publish`
///    will refuse to work otherwise.
/// 1. Find the crate in question
/// 2. Bump the version number according to the user input
/// 3. Find all dependent crates in the workspace with a version bound
/// 4. Update the version bound to the new version
pub struct Publish {
    /// The version type to publish
    pub tt: PublishType,
    /// Whether to set a new devel version after publish
    pub devel: bool,
}

impl RenderHelp for Publish {
    fn render_help(code: i32) -> ! {
        eprintln!("Publish the selected set of crates");
        eprintln!("Usage: cargo ws2 <...> publish [=]<level> [OPTIONS]");
        eprintln!("");
        eprintln!("When prepending `=` to the level, bump crates in sync\n");
        eprintln!("Available levels:\n");
        eprintln!(" - major: Bump major version (1.0.0 -> 2.0.0)");
        eprintln!(" - minor: Bump minor version (0.5.0 -> 0.6.0)");
        eprintln!(" - patch: Bump patch version (0.5.0 -> 0.5.1)");
        eprintln!("");
        eprintln!("Available options:\n");
        eprintln!(" - alpha: Create a new alpha (append `-alpha.X`)");
        eprintln!(" - beta: Create a new beta (append `-beta.X`)");
        eprintln!(" - rc: Create a new rc (append `-rc.X`)");
        eprintln!(" - devel: Tag next version as -devel");
        std::process::exit(code)
    }
}

/// The level to which to update
///
/// New versions are based on the previous version, and are always
/// computed on the fly.
///
/// It's recommended you use the [`versions`](./versions/index.html)
/// builder functions.
pub enum PublishType {
    /// A fixed version to set the set to
    Fixed(String),
    /// A major bump (e.g. 1.0 -> 2.0)
    Major(PublishMod),
    /// A minor bump (e.g. 0.1 -> 0.2)
    Minor(PublishMod),
    /// A patch bump (e.g. 1.5.0 -> 1.5.1)
    Patch(PublishMod),
}

impl PublishType {
    pub(crate) fn _mod(&self) -> Option<&PublishMod> {
        match self {
            Self::Major(ref m) => Some(m),
            Self::Minor(ref m) => Some(m),
            Self::Patch(ref m) => Some(m),
            Self::Fixed(_) => None,
        }
        .and_then(|_mod| match _mod {
            PublishMod::None => None,
            other => Some(other),
        })
    }
}

/// Version string modifier
///
/// It's recommended you use the [`versions`](./versions/index.html)
/// builder functions.
pub enum PublishMod {
    /// No version modification
    None,
    /// Append `-alpha$X` where `$X` is a continuously increasing number
    Alpha,
    /// Append `-beta$X` where `$X` is a continuously increasing number
    Beta,
    /// Append `-rc$X` where `$X` is a continuously increasing number
    Rc,
}

/// Version bump management
pub mod versions {
    use super::{PublishMod, PublishType};

    /// Create a major publish
    pub fn major(_mod: PublishMod) -> PublishType {
        PublishType::Major(_mod)
    }

    /// Create a minor publish
    pub fn minor(_mod: PublishMod) -> PublishType {
        PublishType::Minor(_mod)
    }

    /// Create a patch publish
    pub fn patch(_mod: PublishMod) -> PublishType {
        PublishType::Patch(_mod)
    }

    /// Create a none modifier
    pub fn mod_none() -> PublishMod {
        PublishMod::None
    }

    /// Create an alpha modifier
    pub fn mod_alpha() -> PublishMod {
        PublishMod::Alpha
    }

    /// Create a beta modifier
    pub fn mod_beta() -> PublishMod {
        PublishMod::Beta
    }

    /// Create an rc modifier
    pub fn mod_rc() -> PublishMod {
        PublishMod::Rc
    }
}