mod cargo;
mod go;
mod npm;
mod workspace;
use std::path::Path;
use semver::Version;
pub use cargo::{CargoManifest, bump_workspace_dependency, read_name};
pub use go::GoManifest;
pub use npm::NpmManifest;
pub use crate::bump::{apply_replacements, bump_flake_nix};
pub use workspace::WorkspaceManifest;
use crate::errors::Result;
pub trait ManifestBackend {
fn read_version(&self, root: &Path) -> Result<Version>;
fn write_version(&self, root: &Path, version: &Version) -> Result<()>;
fn sync_lockfile(&self, _root: &Path) -> Result<()> {
Ok(())
}
}
#[must_use]
pub fn detect_manifest(root: &Path) -> Box<dyn ManifestBackend> {
let cargo_toml = root.join("Cargo.toml");
if cargo_toml.exists() {
if let Ok(doc) = cargo::load_doc(&cargo_toml)
&& doc.contains_key("workspace")
{
return Box::new(WorkspaceManifest);
}
return Box::new(CargoManifest);
}
if root.join("package.json").exists() {
return Box::new(NpmManifest);
}
if root.join("go.mod").exists() {
return Box::new(GoManifest);
}
Box::new(CargoManifest)
}
#[cfg(test)]
mod tests {
use std::fs;
use super::*;
#[test]
fn detects_workspace_manifest() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("Cargo.toml"),
"[workspace]\nmembers = []\n\n[workspace.package]\nversion = \"0.3.0\"\n",
)
.unwrap();
let v = detect_manifest(dir.path())
.read_version(dir.path())
.unwrap();
assert_eq!(v, Version::parse("0.3.0").unwrap());
}
#[test]
fn commented_workspace_is_not_a_workspace() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("Cargo.toml"),
"# [workspace]\n[package]\nname = \"test\"\nversion = \"0.4.0\"\n",
)
.unwrap();
let v = detect_manifest(dir.path())
.read_version(dir.path())
.unwrap();
assert_eq!(v, Version::parse("0.4.0").unwrap());
}
#[test]
fn detects_npm_manifest() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("package.json"),
r#"{"name":"test","version":"1.0.0"}"#,
)
.unwrap();
let v = detect_manifest(dir.path())
.read_version(dir.path())
.unwrap();
assert_eq!(v, Version::parse("1.0.0").unwrap());
}
#[test]
fn detects_cargo_manifest() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("Cargo.toml"),
"[package]\nname=\"test\"\nversion=\"0.1.0\"\nedition=\"2024\"\n",
)
.unwrap();
let v = detect_manifest(dir.path())
.read_version(dir.path())
.unwrap();
assert_eq!(v, Version::parse("0.1.0").unwrap());
}
}