aboutsummaryrefslogtreecommitdiffstats
path: root/xtask/src/bump.rs
blob: da54e23f00cc5c9a408fd89f8fb263699741d562 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::{
    fmt::Display,
    fs::File,
    io::{BufReader, BufWriter, Read, Write},
    path::Path,
    process::Command,
    str::FromStr,
};

use anyhow::Result;
use semver::Version;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
    Major,
    Minor,
    #[default]
    Patch,
}

impl Level {
    pub fn bump(&self, version: &Version) -> Version {
        match self {
            Self::Major => Version::new(version.major + 1, 0, 0),
            Self::Minor => Version::new(version.major, version.minor + 1, 0),
            Self::Patch => Version::new(version.major, version.minor, version.patch + 1),
        }
    }
}

impl FromStr for Level {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "major" => Ok(Level::Major),
            "minor" => Ok(Level::Minor),
            "patch" => Ok(Level::Patch),
            s => Err(anyhow::anyhow!("Invalid bump level: {s}")),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Bump {
    pub prev: Version,
    pub next: Version,
}

impl Bump {
    fn check_modified<P>(path: P) -> Result<bool>
    where
        P: AsRef<Path>,
    {
        Ok(Command::new("git")
            .arg("diff-index")
            .arg("--quiet")
            .arg("HEAD")
            .arg(path.as_ref())
            .output()?
            .status
            .success())
    }

    pub fn bump_file<P, F>(&self, path: P, mutator: F) -> Result<()>
    where
        P: AsRef<Path>,
        F: Fn(String, &Self) -> Result<String>,
    {
        let path = path.as_ref();

        anyhow::ensure!(
            Self::check_modified(path)?,
            "{} has uncommited changes. Aborting.",
            path.display()
        );

        let reader = File::options().read(true).open(path)?;
        let writer = File::options().write(true).open(path)?;

        self.bump(reader, writer, mutator)?;

        let git_added = Command::new("git").arg("add").arg(path).status()?;

        anyhow::ensure!(git_added.success(), "Failed to add bumped files to git");

        Ok(())
    }

    fn bump<R, W, F>(&self, reader: R, writer: W, mutator: F) -> Result<()>
    where
        R: Read,
        W: Write,
        F: Fn(String, &Self) -> Result<String>,
    {
        let mut reader = BufReader::new(reader);
        let mut writer = BufWriter::new(writer);
        let mut buf = String::new();

        reader.read_to_string(&mut buf)?;

        let buf = mutator(buf, self)?;

        writer.write_all(buf.as_bytes())?;
        writer.flush().map_err(Into::into)
    }
}

impl Display for Bump {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} -> {}", self.prev, self.next)
    }
}

/// Utility function for replacing version with next in a string.
pub fn replace(
    buf: String,
    Bump {
        prev: version,
        next,
    }: &Bump,
) -> Result<String> {
    Ok(buf.replace(&version.to_string(), &next.to_string()))
}

/// Utility function for bumping the version in a Cargo.toml file.
pub fn cargo(buf: String, Bump { prev: _, next }: &Bump) -> Result<String> {
    let mut cargo_toml: toml_edit::Document = buf.parse()?;

    if cargo_toml["package"]["version"]["workspace"]
        .as_bool()
        .unwrap_or_default()
    {
        cargo_toml["workspace"]["package"]["version"] = toml_edit::value(next.to_string());
    } else if cargo_toml["package"]["version"].is_str() {
        cargo_toml["package"]["version"] = toml_edit::value(next.to_string());
    } else {
        anyhow::bail!("Failed to find version in Cargo.toml");
    };

    Ok(cargo_toml.to_string())
}

/// Utility function for bumping the version in a VSC PKGBUILD file.
pub fn vsc_pkgbuild(buf: String, _: &Bump) -> Result<String> {
    let stdout = std::process::Command::new("git")
        .arg("describe")
        .arg("--long")
        .arg("--abbrev=7")
        .output()?
        .stdout;

    let pkgver = std::str::from_utf8(&stdout)?
        .trim()
        .trim_start_matches('v')
        .replacen("-g", ".g", 1)
        .replacen('-', "-r", 1)
        .replace('-', ".");

    if let Some(from) = buf.lines().find(|l| l.starts_with("pkgver=")) {
        Ok(buf.replace(from, &format!("pkgver={pkgver}")))
    } else {
        Ok(buf)
    }
}

pub fn changelog(buf: String, Bump { prev: _, next }: &Bump) -> Result<String> {
    let date = chrono::Utc::now().format("%Y-%m-%d");

    let lines: Vec<String> = buf
        .lines()
        .flat_map(|line| {
            if line.starts_with("## [Unreleased]") {
                vec![
                    line.to_owned(),
                    "".to_owned(),
                    format!("## [{next}] - {date}"),
                ]
            } else if line.starts_with("[Unreleased]: ") {
                vec![
                    line.to_owned(),
                    line.replace("[Unreleased]", &format!("[{next}]"))
                        .replace("HEAD", &format!("v{next}")),
                ]
            } else {
                vec![line.to_owned()]
            }
        })
        .collect();

    Ok(lines.join("\n"))
}

#[cfg(test)]
mod test {

    use similar_asserts::SimpleDiff;

    use crate::PKG_VER;

    use super::*;

    fn setup_test() -> Result<Bump, std::io::Error> {
        let project_root = std::path::Path::new(&env!("CARGO_MANIFEST_DIR"))
            .parent()
            .expect("Failed to get parent directory.")
            .to_path_buf();

        std::env::set_current_dir(project_root)?;

        let version = PKG_VER.parse().unwrap_or_else(|_| Version::new(0, 1, 0));
        Ok(Bump {
            next: Level::default().bump(&version),
            prev: version,
        })
    }

    fn print_diff(reader: &[u8], writer: &[u8]) {
        let left = std::str::from_utf8(reader).unwrap();
        let right = std::str::from_utf8(writer).unwrap();
        let diff = SimpleDiff::from_str(left, right, "old", "new");

        assert_ne!(left, right);
        println!("{diff}")
    }

    #[test]
    fn test_bump_cargo() {
        let bump = setup_test().unwrap();
        let file = "Cargo.toml";

        let content = std::fs::read_to_string(file).unwrap();
        let reader = content.as_bytes();
        let mut writer = Vec::new();

        bump.bump(reader, &mut writer, cargo).unwrap();

        println!("{file}: {bump}");
        print_diff(reader, &writer)
    }

    #[test]
    fn test_bump_changelog() {
        let bump = setup_test().unwrap();
        let file = "CHANGELOG.md";

        let content = std::fs::read_to_string(file).unwrap();
        let reader = content.as_bytes();
        let mut writer = Vec::new();

        bump.bump(reader, &mut writer, |buf, Bump { prev: _, next }| {
            let date = chrono::Utc::now().format("%Y-%m-%d");
            Ok(buf
                .replace(
                    "## [Unreleased]",
                    &format!(
                        "## [Unreleased]\n\n\
                        ## [{next}] - {date}"
                    ),
                )
                .replace(
                    "[Unreleased]: https://git.sr.ht/~tobyvin/projectr/log/HEAD",
                    &format!(
                        "[Unreleased]: https://git.sr.ht/~tobyvin/projectr/log/HEAD\n\
                        [{next}]: https://git.sr.ht/~tobyvin/projectr/log/v{next}"
                    ),
                ))
        })
        .unwrap();

        println!("{file}: {bump}");
        print_diff(reader, &writer)
    }

    #[test]
    fn test_bump_readme() {
        let bump = setup_test().unwrap();
        let file = "README.md";

        let content = std::fs::read_to_string(file);
        let reader = content.as_deref().unwrap().as_bytes();
        let mut writer = Vec::new();

        bump.bump(reader, &mut writer, replace).unwrap();

        println!("{file}: {bump}");
        print_diff(reader, &writer)
    }

    #[test]
    fn test_bump_vsc_pkgbuild() {
        let bump = setup_test().unwrap();
        let file = "pkg/archlinux/projectr-git/PKGBUILD";

        let content = std::fs::read_to_string(file).unwrap();
        let reader = content.as_bytes();
        let mut writer = Vec::new();

        bump.bump(reader, &mut writer, |buf, _| {
            let stdout = std::process::Command::new("git")
                .arg("describe")
                .arg("--long")
                .arg("--abbrev=7")
                .output()?
                .stdout;

            let pkgver = std::str::from_utf8(&stdout)?
                .trim()
                .trim_start_matches('v')
                .replacen("-g", ".g", 1)
                .replacen('-', "-r", 1)
                .replace('-', ".");

            if let Some(from) = buf.lines().find(|l| l.starts_with("pkgver=")) {
                Ok(buf.replace(from, &format!("pkgver={pkgver}")))
            } else {
                Ok(buf)
            }
        })
        .unwrap();

        println!("{file}: {bump}");
        print_diff(reader, &writer)
    }
}