cvm_cli 1.1.2

A powerful command-line tool for managing semantic versioning of Rust crates. Easily bump versions (major, minor, patch), update Cargo.toml files, and streamline your release workflow with automated version management.
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use anyhow::{Context, Result};
use serde::Serialize;
use std::fs;
use std::path::{Path, PathBuf};
use toml::{Table, Value};

#[derive(Debug, Clone, Serialize)]
pub struct CrateInfo {
    pub name: String,
    pub version: String,
    pub path: String,
}

pub fn analyze_project() -> Result<Vec<CrateInfo>> {
    let root_toml = "Cargo.toml";
    let content =
        fs::read_to_string(root_toml).with_context(|| format!("Failed to read {}", root_toml))?;
    let toml: Table =
        toml::from_str(&content).with_context(|| format!("Failed to parse {}", root_toml))?;

    if toml.contains_key("workspace") {
        // It's a workspace
        let workspace = toml
            .get("workspace")
            .and_then(|w: &Value| w.as_table())
            .context("Invalid [workspace] section")?;
        let workspace_version = workspace
            .get("package")
            .and_then(|p: &Value| p.as_table())
            .and_then(|package| package.get("version"))
            .and_then(|v: &Value| v.as_str());
        let members = workspace
            .get("members")
            .and_then(|m: &Value| m.as_array())
            .context("No members in [workspace]")?;
        let mut crates = Vec::new();
        for member in members {
            if let Some(path_str) = member.as_str() {
                let crate_toml = format!("{}/Cargo.toml", path_str);
                let crate_info = read_crate_info(&crate_toml, workspace_version)?;
                crates.push(crate_info);
            }
        }
        Ok(crates)
    } else if toml.contains_key("package") {
        // Single crate
        let crate_info = read_crate_info(root_toml, None)?;
        Ok(vec![crate_info])
    } else {
        Err(anyhow::anyhow!(
            "No [workspace] or [package] found in Cargo.toml"
        ))
    }
}

pub fn is_workspace_project() -> Result<bool> {
    let content = fs::read_to_string("Cargo.toml")
        .with_context(|| "Failed to read Cargo.toml".to_string())?;
    let toml: Table =
        toml::from_str(&content).with_context(|| "Failed to parse Cargo.toml".to_string())?;
    Ok(toml.contains_key("workspace"))
}

fn workspace_package_version_from_manifest(manifest_path: &Path) -> Result<Option<String>> {
    let content = fs::read_to_string(manifest_path)
        .with_context(|| format!("Failed to read {}", manifest_path.display()))?;
    let toml: Table = toml::from_str(&content)
        .with_context(|| format!("Failed to parse {}", manifest_path.display()))?;

    let Some(workspace) = toml.get("workspace").and_then(|w| w.as_table()) else {
        return Ok(None);
    };

    let version = workspace
        .get("package")
        .and_then(|p| p.as_table())
        .and_then(|package| package.get("version"))
        .and_then(|v| v.as_str())
        .map(str::to_string);

    Ok(version)
}

/// Returns `[workspace.package].version` when the root manifest defines a workspace package table.
pub fn workspace_package_version() -> Result<Option<String>> {
    workspace_package_version_from_manifest(Path::new("Cargo.toml"))
}

/// Walks upward from a crate manifest until a workspace root `Cargo.toml` is found.
pub fn find_workspace_root(crate_manifest_path: &Path) -> Option<PathBuf> {
    let mut dir = crate_manifest_path.parent()?;
    loop {
        let manifest = dir.join("Cargo.toml");
        if manifest.is_file() {
            if let Ok(content) = fs::read_to_string(&manifest) {
                if let Ok(toml) = toml::from_str::<Table>(&content) {
                    if toml.contains_key("workspace") {
                        return Some(dir.to_path_buf());
                    }
                }
            }
        }
        dir = dir.parent()?;
    }
}

/// Single release version for CI tags and summaries.
///
/// Prefers `[workspace.package].version` in workspaces; otherwise requires every crate
/// in the project to share the same version string.
pub fn release_version() -> Result<String> {
    if let Some(version) = workspace_package_version()? {
        return Ok(version);
    }

    let crates = analyze_project()?;
    if crates.is_empty() {
        return Err(anyhow::anyhow!("No crates found in project"));
    }

    let version = crates[0].version.clone();
    if crates.iter().all(|c| c.version == version) {
        return Ok(version);
    }

    Err(anyhow::anyhow!(
        "Crates have different versions; use `cvm info` for per-crate JSON or set [workspace.package].version"
    ))
}

/// True when the crate manifest inherits its version from `[workspace.package]`.
pub fn uses_workspace_version(crate_manifest_path: &str) -> Result<bool> {
    let content = fs::read_to_string(crate_manifest_path)
        .with_context(|| format!("Failed to read {}", crate_manifest_path))?;
    let toml: Table = toml::from_str(&content)
        .with_context(|| format!("Failed to parse {}", crate_manifest_path))?;
    let package = toml
        .get("package")
        .and_then(|p| p.as_table())
        .context("No [package] section")?;
    let version_value = package.get("version");
    Ok(version_value
        .and_then(|v: &Value| v.as_table())
        .and_then(|v| v.get("workspace"))
        .and_then(|v: &Value| v.as_bool())
        == Some(true))
}

/// Path to edit and the resolved semver string for version bumps.
///
/// Workspace-inherited crates bump the root `Cargo.toml` `[workspace.package].version`.
pub fn resolve_version_for_crate(crate_manifest_path: &str) -> Result<(String, String)> {
    let manifest_path = Path::new(crate_manifest_path);
    let workspace_version = find_workspace_root(manifest_path)
        .map(|root| workspace_package_version_from_manifest(&root.join("Cargo.toml")))
        .transpose()?
        .flatten();
    let info = read_crate_info(crate_manifest_path, workspace_version.as_deref())?;
    let edit_path = if uses_workspace_version(crate_manifest_path)? {
        let root = find_workspace_root(manifest_path)
            .context("workspace root not found for crate using version.workspace = true")?;
        root.join("Cargo.toml").to_string_lossy().into_owned()
    } else {
        crate_manifest_path.to_string()
    };
    Ok((edit_path, info.version))
}

fn read_crate_info(path: &str, workspace_version: Option<&str>) -> Result<CrateInfo> {
    let content = fs::read_to_string(path).with_context(|| format!("Failed to read {}", path))?;
    let toml: Table =
        toml::from_str(&content).with_context(|| format!("Failed to parse {}", path))?;
    let package = toml
        .get("package")
        .and_then(|p: &Value| p.as_table())
        .context("No [package] section")?;
    let name = package
        .get("name")
        .and_then(|n: &Value| n.as_str())
        .context("No name in [package]")?
        .to_string();
    let version_value = package.get("version");
    let version = if let Some(version) = version_value.and_then(|v: &Value| v.as_str()) {
        version.to_string()
    } else if version_value
        .and_then(|v: &Value| v.as_table())
        .and_then(|v| v.get("workspace"))
        .and_then(|v: &Value| v.as_bool())
        == Some(true)
    {
        workspace_version
            .context("No version in [workspace.package] for crate using version.workspace = true")?
            .to_string()
    } else {
        return Err(anyhow::anyhow!("No version in [package]"));
    };
    Ok(CrateInfo {
        name,
        version,
        path: path.to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::read_crate_info;
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn create_temp_manifest(contents: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let counter = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!(
            "cvm-project-test-{}-{}-{}",
            std::process::id(),
            nanos,
            counter
        ));
        fs::create_dir_all(&dir).unwrap();
        let manifest_path = dir.join("Cargo.toml");
        fs::write(&manifest_path, contents).unwrap();
        manifest_path
    }

    fn cleanup_temp_manifest(path: &Path) {
        if let Err(err) = fs::remove_file(path) {
            assert_eq!(
                err.kind(),
                std::io::ErrorKind::NotFound,
                "failed to remove test manifest {}: {}",
                path.display(),
                err
            );
        }
        if let Some(parent) = path.parent() {
            if let Err(err) = fs::remove_dir_all(parent) {
                assert_eq!(
                    err.kind(),
                    std::io::ErrorKind::NotFound,
                    "failed to remove test temp dir {}: {}",
                    parent.display(),
                    err
                );
            }
        }
    }

    #[test]
    fn resolves_workspace_inherited_version() {
        let manifest_path = create_temp_manifest(
            r#"
[package]
name = "foo"
version.workspace = true
"#,
        );

        let info =
            read_crate_info(manifest_path.to_string_lossy().as_ref(), Some("0.1.0")).unwrap();
        assert_eq!(info.name, "foo");
        assert_eq!(info.version, "0.1.0");
        cleanup_temp_manifest(&manifest_path);
    }

    #[test]
    fn workspace_package_version_reads_workspace_package_table() {
        let dir = std::env::temp_dir().join(format!(
            "cvm-workspace-version-test-{}",
            TEST_COUNTER.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir_all(&dir).unwrap();
        fs::write(
            dir.join("Cargo.toml"),
            r#"
[workspace]
members = ["crates/foo"]

[workspace.package]
version = "1.2.3"
"#,
        )
        .unwrap();

        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(&dir).unwrap();
        let version = super::workspace_package_version().unwrap();
        std::env::set_current_dir(original).unwrap();
        fs::remove_dir_all(&dir).unwrap();

        assert_eq!(version.as_deref(), Some("1.2.3"));
    }

    #[test]
    fn release_version_uses_workspace_package_version() {
        let dir = std::env::temp_dir().join(format!(
            "cvm-release-version-test-{}",
            TEST_COUNTER.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir_all(&dir).unwrap();
        fs::write(
            dir.join("Cargo.toml"),
            r#"
[workspace]
members = ["crates/foo", "crates/bar"]

[workspace.package]
version = "2.0.0"
"#,
        )
        .unwrap();
        fs::create_dir_all(dir.join("crates/foo")).unwrap();
        fs::create_dir_all(dir.join("crates/bar")).unwrap();
        fs::write(
            dir.join("crates/foo/Cargo.toml"),
            r#"
[package]
name = "foo"
version.workspace = true
"#,
        )
        .unwrap();
        fs::write(
            dir.join("crates/bar/Cargo.toml"),
            r#"
[package]
name = "bar"
version.workspace = true
"#,
        )
        .unwrap();

        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(&dir).unwrap();
        let version = super::release_version().unwrap();
        std::env::set_current_dir(original).unwrap();
        fs::remove_dir_all(&dir).unwrap();

        assert_eq!(version, "2.0.0");
    }

    #[test]
    fn resolve_version_for_crate_points_at_workspace_root() {
        let dir = std::env::temp_dir().join(format!(
            "cvm-resolve-version-test-{}",
            TEST_COUNTER.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir_all(dir.join("crates/lemon")).unwrap();
        fs::write(
            dir.join("Cargo.toml"),
            r#"
[workspace]
members = ["crates/lemon"]

[workspace.package]
version = "0.1.0"

[workspace.dependencies]
lemon = { path = "crates/lemon", version = "0.1.0" }
"#,
        )
        .unwrap();
        fs::write(
            dir.join("crates/lemon/Cargo.toml"),
            r#"
[package]
name = "lemon"
version.workspace = true
"#,
        )
        .unwrap();

        let crate_manifest = dir.join("crates/lemon/Cargo.toml");
        let (edit_path, version) =
            super::resolve_version_for_crate(crate_manifest.to_str().unwrap()).unwrap();
        fs::remove_dir_all(&dir).unwrap();

        assert_eq!(edit_path, dir.join("Cargo.toml").to_string_lossy());
        assert_eq!(version, "0.1.0");
    }

    #[test]
    fn errors_when_workspace_inherited_version_has_no_workspace_value() {
        let manifest_path = create_temp_manifest(
            r#"
[package]
name = "foo"
version.workspace = true
"#,
        );

        let err = read_crate_info(manifest_path.to_string_lossy().as_ref(), None).unwrap_err();
        assert_eq!(
            err.to_string(),
            "No version in [workspace.package] for crate using version.workspace = true"
        );
        cleanup_temp_manifest(&manifest_path);
    }
}