use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use super::{say_err, say_out};
use oakum::changeset::resolve_package_name;
use oakum::detect::ReleaseTool;
use oakum::discover::child::{deadline, find_program, output_within, said};
use oakum::discover::DiscoverError;
use oakum::plan::{PackageId, Workspace};
use semver::Version;
use serde::Deserialize;
#[derive(Debug)]
pub(super) enum SourceBeforePlan {
Available {
tool: ReleaseTool,
fingerprint: BTreeMap<PackageId, (Version, Version)>,
under_convention: bool,
},
Unavailable { tool: ReleaseTool, reason: String },
}
#[must_use]
pub(super) fn primary_plan_tool(knope: bool, bumpy: bool, changesets: bool) -> Option<ReleaseTool> {
if knope {
Some(ReleaseTool::Knope)
} else if bumpy {
Some(ReleaseTool::Bumpy)
} else if changesets {
Some(ReleaseTool::Changesets)
} else {
None
}
}
pub(super) fn fetch_source_before_plan(
tool: ReleaseTool,
cwd: &Path,
workspace: &Workspace,
) -> SourceBeforePlan {
match tool {
ReleaseTool::Bumpy => bumpy_status_json(cwd, workspace),
ReleaseTool::Changesets => changesets_status_output(cwd, workspace),
ReleaseTool::Knope => knope_release_dry_run(cwd, workspace),
other => SourceBeforePlan::Unavailable {
tool: other,
reason: format!("{} has no migrate before-plan command", other.name()),
},
}
}
fn bumpy_status_json(cwd: &Path, workspace: &Workspace) -> SourceBeforePlan {
let Some(bin) = resolve_tool_bin("bumpy", cwd) else {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Bumpy,
reason: not_found("bumpy"),
};
};
let output = match run_tool(
Command::new(&bin)
.args(["status", "--json"])
.current_dir(cwd),
"bumpy",
"bumpy status --json",
) {
Ok(output) => output,
Err(reason) => {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Bumpy,
reason,
};
}
};
let stdout = String::from_utf8_lossy(&output.stdout);
match parse_bumpy_status_json(&stdout, workspace) {
Ok(fingerprint) => {
let quiet_nothing_pending = output.status.code() == Some(1)
&& fingerprint.is_empty()
&& output.stderr.is_empty();
if quiet_nothing_pending {
say_out(
"plan comparison: `bumpy status --json` exited 1 with no releases, read as nothing pending"
);
}
if !output.status.success() && !quiet_nothing_pending {
let stderr = said(&output.stderr);
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Bumpy,
reason: format!(
"`bumpy status --json` {}{}",
how_it_ended(output.status),
if stderr.is_empty() {
String::new()
} else {
format!(": {stderr}")
}
),
};
}
SourceBeforePlan::Available {
tool: ReleaseTool::Bumpy,
fingerprint,
under_convention: quiet_nothing_pending,
}
}
Err(reason) => {
let stderr = said(&output.stderr);
let reason = if output.status.success() {
reason
} else {
format!("{} ({reason})", how_it_ended(output.status))
};
let detail = if stderr.is_empty() {
reason
} else {
format!("{reason}; stderr: {stderr}")
};
SourceBeforePlan::Unavailable {
tool: ReleaseTool::Bumpy,
reason: detail,
}
}
}
}
#[derive(Debug, Deserialize)]
struct BumpyStatusJson {
releases: Vec<NamedRelease>,
}
#[derive(Debug, Deserialize)]
struct NamedRelease {
name: String,
#[serde(rename = "oldVersion")]
old_version: String,
#[serde(rename = "newVersion")]
new_version: String,
#[serde(rename = "type", default)]
release_type: Option<String>,
}
fn parse_bumpy_status_json(
json: &str,
workspace: &Workspace,
) -> Result<BTreeMap<PackageId, (Version, Version)>, String> {
let parsed: BumpyStatusJson =
serde_json::from_str(json).map_err(|err| format!("bumpy JSON: {err}"))?;
releases_to_fingerprint(
parsed
.releases
.into_iter()
.filter(|r| r.release_type.as_deref() != Some("none"))
.map(|r| (r.name, r.old_version, r.new_version)),
workspace,
"bumpy",
)
}
fn changesets_status_output(cwd: &Path, workspace: &Workspace) -> SourceBeforePlan {
let Some(bin) = resolve_tool_bin("changeset", cwd) else {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason: not_found("changeset"),
};
};
let out_path = match exclusive_temp_json() {
Ok(path) => path,
Err(reason) => {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason,
};
}
};
let output = match run_tool(
Command::new(&bin)
.args(["status", "--output"])
.arg(&out_path)
.current_dir(cwd),
"changeset",
"changeset status --output",
) {
Ok(output) => output,
Err(reason) => {
discard_temp(&out_path);
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason,
};
}
};
if !output.status.success() {
discard_temp(&out_path);
let stderr = said(&output.stderr);
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason: format!(
"`changeset status` {}{}",
how_it_ended(output.status),
if stderr.is_empty() {
String::new()
} else {
format!(": {stderr}")
}
),
};
}
let body = match fs::read_to_string(&out_path) {
Ok(body) => {
discard_temp(&out_path);
body
}
Err(err) => {
discard_temp(&out_path);
let stderr = said(&output.stderr);
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason: format!(
"changeset did not write `--output` file ({err}){}",
if stderr.is_empty() {
String::new()
} else {
format!("; stderr: {stderr}")
}
),
};
}
};
match parse_changesets_status_json(&body, workspace) {
Ok(fingerprint) => SourceBeforePlan::Available {
under_convention: false,
tool: ReleaseTool::Changesets,
fingerprint,
},
Err(reason) => SourceBeforePlan::Unavailable {
tool: ReleaseTool::Changesets,
reason,
},
}
}
#[derive(Debug, Deserialize)]
struct ChangesetsStatusJson {
#[serde(default)]
releases: Vec<ChangesetsRelease>,
}
#[derive(Debug, Deserialize)]
struct ChangesetsRelease {
name: String,
#[serde(rename = "type", default)]
release_type: String,
#[serde(rename = "oldVersion")]
old_version: Option<String>,
#[serde(rename = "newVersion")]
new_version: Option<String>,
}
fn parse_changesets_status_json(
json: &str,
workspace: &Workspace,
) -> Result<BTreeMap<PackageId, (Version, Version)>, String> {
let parsed: ChangesetsStatusJson =
serde_json::from_str(json).map_err(|err| format!("changesets JSON: {err}"))?;
let triples = parsed
.releases
.into_iter()
.filter(|r| r.release_type != "none")
.filter_map(|r| {
let old = r.old_version?;
let new = r.new_version?;
Some((r.name, old, new))
});
releases_to_fingerprint(triples, workspace, "changesets")
}
fn knope_release_dry_run(cwd: &Path, workspace: &Workspace) -> SourceBeforePlan {
let Some(bin) = resolve_tool_bin("knope", cwd) else {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason: not_found("knope"),
};
};
let KnopeWorkflow {
name: workflow,
prepares_release,
} = match knope_workflow(cwd) {
Ok(workflow) => workflow,
Err(reason) => {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason,
};
}
};
let output = match run_tool(
Command::new(&bin)
.args([&workflow, "--dry-run"])
.current_dir(cwd),
"knope",
&format!("knope {workflow} --dry-run"),
) {
Ok(output) => output,
Err(reason) => {
return SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason,
};
}
};
let combined = format!(
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
match parse_knope_dry_run(&combined, workspace) {
Ok(fingerprint) if !fingerprint.is_empty() && output.status.success() => {
SourceBeforePlan::Available {
under_convention: false,
tool: ReleaseTool::Knope,
fingerprint,
}
}
Ok(fingerprint) if !fingerprint.is_empty() => SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason: format!(
"`knope {workflow} --dry-run` {} with version lines (not treated as verified evidence)",
how_it_ended(output.status)
),
},
Ok(fingerprint)
if prepares_release && output.status.success() && combined.trim().is_empty() =>
{
SourceBeforePlan::Available {
under_convention: false,
tool: ReleaseTool::Knope,
fingerprint,
}
}
Ok(_) if !prepares_release && output.status.success() && combined.trim().is_empty() => {
SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason: format!(
"`knope {workflow} --dry-run` printed nothing, and `knope.toml`'s `{workflow}` workflow has no `PrepareRelease` step, so knope did not plan"
),
}
}
Ok(_) => {
let first = combined.lines().map(str::trim).find(|line| !line.is_empty());
SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason: match first {
Some(first) => format!(
"`knope {workflow} --dry-run` produced no usable version lines ({}; first line: {first:?})",
how_it_ended(output.status)
),
None => format!(
"`knope {workflow} --dry-run` produced no usable version lines ({})",
how_it_ended(output.status)
),
},
}
}
Err(reason) => SourceBeforePlan::Unavailable {
tool: ReleaseTool::Knope,
reason,
},
}
}
struct KnopeWorkflow {
name: String,
prepares_release: bool,
}
fn knope_workflow(cwd: &Path) -> Result<KnopeWorkflow, String> {
let body = match fs::read_to_string(cwd.join("knope.toml")) {
Ok(body) => body,
Err(err) if err.kind() != std::io::ErrorKind::NotFound => {
return Err(format!("could not read `knope.toml`: {err}"));
}
Err(_) => String::new(),
};
let config: toml::Value = toml::from_str(&body)
.map_err(|err| format!("could not parse `knope.toml`: {}", err.message()))?;
let declared: Vec<(&str, &toml::Value)> = config
.get("workflows")
.and_then(toml::Value::as_array)
.into_iter()
.flatten()
.filter_map(|workflow| Some((workflow.get("name")?.as_str()?, workflow)))
.collect();
if declared.is_empty() {
return Ok(KnopeWorkflow {
name: String::from("release"),
prepares_release: true,
});
}
let (name, workflow) = declared
.iter()
.find(|(name, _)| *name == "prepare-release")
.or_else(|| declared.iter().find(|(name, _)| *name == "release"))
.copied()
.unwrap_or(("release", &toml::Value::Boolean(false)));
let prepares_release = workflow
.get("steps")
.and_then(toml::Value::as_array)
.is_some_and(|steps| {
steps.iter().any(|step| {
step.get("type").and_then(toml::Value::as_str) == Some("PrepareRelease")
})
});
Ok(KnopeWorkflow {
name: name.to_owned(),
prepares_release,
})
}
fn parse_knope_dry_run(
text: &str,
workspace: &Workspace,
) -> Result<BTreeMap<PackageId, (Version, Version)>, String> {
let mut fingerprint = BTreeMap::new();
let prefix = "Would add the following to ";
for line in text.lines() {
let Some(rest) = line.strip_prefix(prefix) else {
continue;
};
let Some((path, version_text)) = rest.rsplit_once(':') else {
continue;
};
let path = path.trim();
let version_text = version_text.trim();
if !path.ends_with("package.json") && !path.ends_with("Cargo.toml") {
continue;
}
let to = parse_knope_version_payload(version_text)
.map_err(|err| format!("knope dry-run version `{version_text}`: {err}"))?;
let id = package_id_for_manifest_path(path, workspace).ok_or_else(|| {
format!("knope dry-run named `{path}` which matches no workspace package")
})?;
let from = workspace
.get(&id)
.map(|pkg| pkg.version().clone())
.ok_or_else(|| format!("workspace missing package for knope dry-run path `{path}`"))?;
fingerprint.insert(id, (from, to));
}
Ok(fingerprint)
}
fn parse_knope_version_payload(payload: &str) -> Result<Version, String> {
let trimmed = payload.trim();
let after_version = match trimmed.strip_prefix("version") {
Some(rest) => {
let rest = rest.trim_start();
match rest.strip_prefix('=') {
Some(value) => value.trim(),
None => rest,
}
.trim_matches(|c| c == '"' || c == '\'')
}
None => trimmed,
};
let first = after_version
.split_whitespace()
.next()
.unwrap_or(after_version)
.trim_matches(|c| c == ',' || c == ';');
Version::parse(first).map_err(|err| err.to_string())
}
fn package_id_for_manifest_path(path: &str, workspace: &Workspace) -> Option<PackageId> {
let normalized = path.replace('\\', "/");
let mut matches = Vec::new();
for pkg in workspace.packages() {
let expected = pkg.manifest_path().replace('\\', "/");
if paths_equal_or_suffix(&normalized, &expected) {
matches.push(pkg.id().clone());
}
}
match matches.as_slice() {
[only] => Some(only.clone()),
_ => None,
}
}
fn paths_equal_or_suffix(got: &str, expected: &str) -> bool {
if got == expected {
return true;
}
if !expected.contains('/') {
return false;
}
got.ends_with(&format!("/{expected}"))
}
fn releases_to_fingerprint(
releases: impl IntoIterator<Item = (String, String, String)>,
workspace: &Workspace,
tool: &str,
) -> Result<BTreeMap<PackageId, (Version, Version)>, String> {
let mut fingerprint = BTreeMap::new();
for (name, old, new) in releases {
let id = match resolve_package_name(&name, workspace) {
Ok(id) => id,
Err(oakum::changeset::UnknownReason::Missing) => {
return Err(format!(
"{tool} release `{name}` matches no workspace package"
));
}
Err(oakum::changeset::UnknownReason::Ambiguous) => {
return Err(format!(
"{tool} release `{name}` matches more than one workspace package"
));
}
};
let from = Version::parse(&old)
.map_err(|err| format!("{tool} oldVersion `{old}` for `{name}`: {err}"))?;
let to = Version::parse(&new)
.map_err(|err| format!("{tool} newVersion `{new}` for `{name}`: {err}"))?;
fingerprint.insert(id, (from, to));
}
Ok(fingerprint)
}
fn run_tool(command: &mut Command, tool: &'static str, shown: &str) -> Result<Output, String> {
let limit = deadline()?;
output_within(command, limit).map_err(|failure| {
match DiscoverError::unanswered(tool, failure) {
Ok(unanswered) => unanswered.to_string(),
Err(err) => format!("failed to run `{shown}`: {err}"),
}
})
}
fn how_it_ended(status: std::process::ExitStatus) -> String {
match status.code() {
Some(code) => format!("exited {code}"),
None => String::from("was terminated by a signal"),
}
}
fn discard_temp(path: &Path) {
if let Err(err) = fs::remove_file(path) {
if err.kind() != std::io::ErrorKind::NotFound {
say_err(&format!("could not remove `{}`: {err}", path.display()));
}
}
}
fn resolve_tool_bin(name: &str, cwd: &Path) -> Option<PathBuf> {
let bin_dir = cwd.join("node_modules").join(".bin");
for candidate in local_bin_names(name) {
let local = bin_dir.join(candidate);
if local.is_file() {
return Some(local);
}
}
find_program(name)
}
fn local_bin_names(name: &str) -> Vec<String> {
if cfg!(windows) {
return Vec::from([format!("{name}.cmd"), format!("{name}.CMD")]);
}
Vec::from([name.to_owned()])
}
fn not_found(name: &str) -> String {
format!("`{name}` not found (no local `node_modules/.bin/{name}`, none on PATH)")
}
fn exclusive_temp_json() -> Result<PathBuf, String> {
use std::fs::OpenOptions;
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
let path = std::env::temp_dir().join(format!(
"oakum-migrate-changeset-status-{}-{nanos}.json",
std::process::id()
));
OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.map_err(|err| {
format!(
"failed to create exclusive changeset status file {}: {err}",
path.display()
)
})?;
Ok(path)
}
#[cfg(test)]
mod local_shims {
use super::local_bin_names;
#[test]
fn each_platform_looks_for_the_names_it_can_execute() {
let names = local_bin_names("changeset");
#[cfg(windows)]
assert_eq!(names, ["changeset.cmd", "changeset.CMD"]);
#[cfg(not(windows))]
assert_eq!(names, ["changeset"]);
}
}
#[cfg(test)]
mod tests {
use super::*;
use oakum::plan::{Ecosystem, Package, ResolvesDependenciesAt};
fn cargo_ws(name: &str, version: &str) -> Workspace {
let pkg = Package::new(
PackageId::new(Ecosystem::Cargo, name),
Version::parse(version).unwrap(),
ResolvesDependenciesAt::Install,
true,
vec![],
);
Workspace::new(vec![pkg]).unwrap()
}
#[test]
fn bumpy_json_parses_releases() {
let ws = cargo_ws("core", "0.1.0");
let json = r#"{
"releases": [
{"name": "core", "type": "minor", "oldVersion": "0.1.0", "newVersion": "0.2.0"}
],
"packageNames": ["core"]
}"#;
let fp = parse_bumpy_status_json(json, &ws).unwrap();
let id = PackageId::new(Ecosystem::Cargo, "core");
assert_eq!(
fp.get(&id),
Some(&(Version::new(0, 1, 0), Version::new(0, 2, 0)))
);
}
#[test]
fn changesets_json_skips_none() {
let ws = cargo_ws("core", "1.0.0");
let json = r#"{
"releases": [
{"name": "other", "type": "none"},
{"name": "core", "type": "patch", "oldVersion": "1.0.0", "newVersion": "1.0.1"}
]
}"#;
let fp = parse_changesets_status_json(json, &ws).unwrap();
assert_eq!(fp.len(), 1);
}
#[test]
fn knope_dry_run_root_manifest() {
let ws = cargo_ws("core", "0.1.0");
let text = "Would add the following to Cargo.toml: 0.1.1\nWould delete: .changeset/x.md\n";
let fp = parse_knope_dry_run(text, &ws).unwrap();
let id = PackageId::new(Ecosystem::Cargo, "core");
assert_eq!(
fp.get(&id),
Some(&(Version::new(0, 1, 0), Version::new(0, 1, 1)))
);
}
#[test]
fn knope_dry_run_accepts_version_assignment() {
let ws = cargo_ws("core", "0.1.0");
let text = "Would add the following to Cargo.toml: version = 0.1.1\n";
let fp = parse_knope_dry_run(text, &ws).unwrap();
let id = PackageId::new(Ecosystem::Cargo, "core");
assert_eq!(
fp.get(&id),
Some(&(Version::new(0, 1, 0), Version::new(0, 1, 1)))
);
}
#[test]
fn knope_path_does_not_suffix_match_sibling_names() {
let core = Package::new(
PackageId::new(Ecosystem::Cargo, "core"),
Version::new(0, 1, 0),
ResolvesDependenciesAt::Install,
true,
vec![],
)
.with_manifest_dir("core");
let notcore = Package::new(
PackageId::new(Ecosystem::Cargo, "notcore"),
Version::new(0, 2, 0),
ResolvesDependenciesAt::Install,
true,
vec![],
)
.with_manifest_dir("notcore");
let ws = Workspace::new(vec![core, notcore]).unwrap();
let text = "Would add the following to notcore/Cargo.toml: 0.2.1\n";
let fp = parse_knope_dry_run(text, &ws).unwrap();
assert_eq!(fp.len(), 1);
assert!(fp.contains_key(&PackageId::new(Ecosystem::Cargo, "notcore")));
assert!(!fp.contains_key(&PackageId::new(Ecosystem::Cargo, "core")));
}
#[test]
fn knope_bare_manifest_rejects_when_no_root_package() {
let left = Package::new(
PackageId::new(Ecosystem::Cargo, "app"),
Version::new(0, 1, 0),
ResolvesDependenciesAt::Install,
true,
vec![],
)
.with_manifest_dir("app");
let right = Package::new(
PackageId::new(Ecosystem::Cargo, "aaa"),
Version::new(0, 1, 0),
ResolvesDependenciesAt::Install,
true,
vec![],
)
.with_manifest_dir("aaa");
let ws = Workspace::new(vec![left, right]).unwrap();
let text = "Would add the following to Cargo.toml: 0.1.1\n";
let err = parse_knope_dry_run(text, &ws).unwrap_err();
assert!(err.contains("Cargo.toml"), "{err}");
}
#[test]
fn primary_prefers_knope() {
assert_eq!(
primary_plan_tool(true, true, true),
Some(ReleaseTool::Knope)
);
assert_eq!(
primary_plan_tool(false, true, true),
Some(ReleaseTool::Bumpy)
);
assert_eq!(
primary_plan_tool(false, false, true),
Some(ReleaseTool::Changesets)
);
assert_eq!(primary_plan_tool(false, false, false), None);
}
}