From 24574aef2299925c10eeb9d20a942f36d1d806d4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 22 Apr 2021 08:27:04 +0300 Subject: feat: use project's name in git tag template --- src/bin/bumper.rs | 11 +++++++++-- src/debian.rs | 18 ++++++++++++++++++ src/errors.rs | 12 ++++++++++++ src/project.rs | 8 ++++++++ src/python.rs | 22 +++++++++++++++++++++- src/rust.rs | 4 ++++ 6 files changed, 72 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/bin/bumper.rs b/src/bin/bumper.rs index fb5194a..9fbf898 100644 --- a/src/bin/bumper.rs +++ b/src/bin/bumper.rs @@ -21,7 +21,14 @@ fn bumper() -> Result<(), BumperError> { let cwd = abspath(".")?; println!("Setting version for project in {}", cwd.display()); - for mut kind in ProjectKind::detect(&cwd)? { + + let mut kinds = ProjectKind::detect(&cwd)?; + if kinds.is_empty() { + return Err(BumperError::UnknownProjectKind(cwd)); + } + let name = kinds[0].name()?; + + for kind in kinds.iter_mut() { let version = kind.set_version(&opt.version)?; println!("{} project set to {}", kind.desc(), version); } @@ -30,7 +37,7 @@ fn bumper() -> Result<(), BumperError> { git::commit(".", &msg)?; let tag = Tag::new(&opt.tag)?; - let tag = tag.apply("", &opt.version); + let tag = tag.apply(&name, &opt.version); println!("release tag: {}", tag); git::tag(".", &tag, &msg)?; debug!("Bumper ends OK"); diff --git a/src/debian.rs b/src/debian.rs index 3f9e01b..15b87ed 100644 --- a/src/debian.rs +++ b/src/debian.rs @@ -19,6 +19,24 @@ impl Debian { Err(BumperError::UnknownProjectKind(dirname.to_path_buf())) } + pub fn name(&self) -> Result { + let output = Command::new("dpkg-parsechangelog") + .arg("-SSource") + .current_dir(&self.dirname) + .output() + .map_err(|err| BumperError::ParseChangelogInvoke(self.dirname.to_path_buf(), err))?; + if output.status.success() { + let name = String::from_utf8_lossy(&output.stdout).into_owned(); + Ok(name.trim_end().to_string()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + Err(BumperError::ParseChangelog( + self.dirname.to_path_buf(), + stderr, + )) + } + } + pub fn set_version(&mut self, version: &str) -> Result { let version = format!("{}-1", version); self.dch(&["-v", &version, ""])?; diff --git a/src/errors.rs b/src/errors.rs index 19a758d..a296afd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -47,6 +47,18 @@ pub enum BumperError { #[error("dch failed in {0}: {1}")] Dch(PathBuf, String), + #[error("Failed to run dpkg-parsechangelog in {0}: {1}")] + ParseChangelogInvoke(PathBuf, #[source] std::io::Error), + + #[error("dpkg-parsechangelog failed in {0}: {1}")] + ParseChangelog(PathBuf, String), + + #[error("Failed to run setup.py in {0}: {1}")] + Setupnvoke(PathBuf, #[source] std::io::Error), + + #[error("setup.py failed in {0}: {1}")] + Setup(PathBuf, String), + #[error("Failed to run cargo in {0}: {1}")] CargoInvoke(PathBuf, #[source] std::io::Error), diff --git a/src/project.rs b/src/project.rs index 655fb23..9941ef7 100644 --- a/src/project.rs +++ b/src/project.rs @@ -48,6 +48,14 @@ impl ProjectKind { } } + pub fn name(&mut self) -> Result { + match self { + Self::Debian(x) => x.name(), + Self::Python(x) => x.name(), + Self::Rust(x) => x.name(), + } + } + pub fn set_version(&mut self, version: &str) -> Result { Ok(match self { Self::Rust(ref mut rust) => rust.set_version(version)?, diff --git a/src/python.rs b/src/python.rs index 4cc043e..28c0aec 100644 --- a/src/python.rs +++ b/src/python.rs @@ -2,8 +2,10 @@ use crate::errors::BumperError; use glob::glob; use log::{debug, info}; use std::path::{Path, PathBuf}; +use std::process::Command; pub struct Python { + dirname: PathBuf, version_pys: Vec, } @@ -17,13 +19,31 @@ impl Python { debug!("no version.py files in {}", dirname.display()); Err(BumperError::NoVersionPy(dirname.to_path_buf())) } else { - Ok(Self { version_pys: files }) + Ok(Self { + dirname: dirname.to_path_buf(), + version_pys: files, + }) } } else { Err(BumperError::UnknownProjectKind(dirname.to_path_buf())) } } + pub fn name(&mut self) -> Result { + let output = Command::new("./setup.py") + .arg("--name") + .current_dir(&self.dirname) + .output() + .map_err(|err| BumperError::Setupnvoke(self.dirname.to_path_buf(), err))?; + if output.status.success() { + let name = String::from_utf8_lossy(&output.stdout).into_owned(); + Ok(name.trim_end().to_string()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + Err(BumperError::Setup(self.dirname.to_path_buf(), stderr)) + } + } + pub fn set_version(&mut self, version: &str) -> Result { for filename in self.version_pys.iter() { info!("writing Python version to {}", filename.display()); diff --git a/src/rust.rs b/src/rust.rs index 0ffe8c0..2dbe1f7 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -27,6 +27,10 @@ impl Rust { Err(BumperError::UnknownProjectKind(dirname.to_path_buf())) } + pub fn name(&mut self) -> Result { + self.cargo_toml.name() + } + pub fn set_version(&mut self, version: &str) -> Result { debug!("parsing Cargo.toml from {}", self.dirname.display()); // debug!("Cargo.toml:\n{:#?}", self.cargo_toml); -- cgit v1.2.1