-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use anyhow::Result; | ||
use cargo_metadata::MetadataCommand; | ||
use clap::{arg, ArgMatches, Command}; | ||
use toml_edit::{value, Document}; | ||
|
||
pub fn cli() -> Command { | ||
Command::new("bump") | ||
.about("Bump the version of the current crate") | ||
.arg(arg!(<VERSION> "The version to bump to")) | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Modifier { | ||
Major, | ||
Minor, | ||
Patch, | ||
Custom(semver::Version), | ||
} | ||
|
||
impl Modifier { | ||
pub fn parse(s: impl AsRef<str>) -> Result<Self> { | ||
let s = s.as_ref().trim(); | ||
if s == "major" { | ||
Ok(Self::Major) | ||
} else if s == "minor" { | ||
Ok(Self::Minor) | ||
} else if s == "patch" { | ||
Ok(Self::Patch) | ||
} else { | ||
Ok(Self::Custom(semver::Version::parse(s)?)) | ||
} | ||
} | ||
} | ||
|
||
pub trait VersionModifier { | ||
fn bump(&self, version: Modifier) -> semver::Version; | ||
} | ||
|
||
impl VersionModifier for semver::Version { | ||
fn bump(&self, version: Modifier) -> semver::Version { | ||
match version { | ||
Modifier::Major => { | ||
let mut v = self.clone(); | ||
v.major += 1; | ||
v.minor = 0; | ||
v.patch = 0; | ||
v | ||
} | ||
Modifier::Minor => { | ||
let mut v = self.clone(); | ||
v.minor += 1; | ||
v.patch = 0; | ||
v | ||
} | ||
Modifier::Patch => { | ||
let mut v = self.clone(); | ||
v.patch += 1; | ||
v | ||
} | ||
Modifier::Custom(v) => v, | ||
} | ||
} | ||
} | ||
|
||
pub fn exec(matches: &ArgMatches) -> Result<()> { | ||
let metadata_cmd = MetadataCommand::new(); | ||
let metadata = metadata_cmd.exec()?; | ||
let root_package = metadata.root_package().unwrap(); | ||
let manifest = root_package.manifest_path.to_owned(); | ||
let source = std::fs::read_to_string(manifest.to_owned())?; | ||
let mut doc = source.parse::<Document>()?; | ||
let modifier_str = matches | ||
.get_one::<String>("VERSION") | ||
.expect("VERSION is required"); | ||
let modifier = Modifier::parse(modifier_str)?; | ||
|
||
if let Some(version_str) = doc["package"]["version"].as_str() { | ||
let version = semver::Version::parse(version_str)?; | ||
let new_version = version.bump(modifier.clone()); | ||
doc["package"]["version"] = value(new_version.to_string()); | ||
std::fs::write(manifest, doc.to_string())?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
use clap::Command; | ||
|
||
mod bump; | ||
|
||
fn main() -> Result<(), anyhow::Error> { | ||
xtaskops::tasks::main() | ||
let app = Command::new("xtask") | ||
.about("A task runner for the xtask crate") | ||
.subcommand(bump::cli()) | ||
.arg_required_else_help(true); | ||
let matches = app.get_matches(); | ||
|
||
match matches.subcommand() { | ||
Some(("bump", submatches)) => bump::exec(submatches), | ||
_ => Ok(()), | ||
} | ||
} |