= contents
.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"))
})
}
fn bump_cargo(next: &Version) -> Result<()> {
bump_file("./Cargo.toml", |contents| {
let mut cargo_toml: toml_edit::Document = contents.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())
})
}
fn bump_file(path: P, mutator: F) -> Result<()>
where
P: AsRef,
F: Fn(String) -> Result,
{
let path = path.as_ref();
let mut reader = BufReader::new(File::open(path)?);
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
let buf = mutator(buf)?;
let mut writer = BufWriter::new(File::open(path)?);
writer.write_all(buf.as_bytes())?;
writer.flush()?;
let git_added = Command::new("git").arg("add").arg(path).status()?;
anyhow::ensure!(git_added.success(), "Failed to add bumped files to git");
Ok(())
}
static PROJECT_ROOT: Lazy = Lazy::new(|| {
let dir = std::env::current_dir().unwrap_or_else(|_| {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
});
dir.ancestors()
.find(|p| p.join(".git").is_dir())
.unwrap_or(&dir)
.to_path_buf()
});
static DIST_DIR: Lazy = Lazy::new(|| PROJECT_ROOT.join("target").join("dist"));
static RELEASE_DIR: Lazy = Lazy::new(|| PROJECT_ROOT.join("target").join("release"));
build_info::build_info!(fn build_info);