aboutsummaryrefslogtreecommitdiff
path: root/development/tools/cargo-workspace2/src/ops/publish/mod.rs
blob: 6022496af27339f1ed3eb64dbfde31ebbeda7be9 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! 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
    }
}