Skip to content

Commit

Permalink
feat: add build version to command
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao authored and oknozor committed Mar 8, 2024
1 parent 9f7fcd6 commit 1680042
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/bin/cog/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ enum Command {
#[arg(long)]
pre: Option<String>,

/// Set the build suffix
#[arg(long)]
build: Option<String>,

/// Specify the bump profile hooks to run
#[arg(short = 'H', long, value_parser = hook_profiles())]
hook_profile: Option<String>,
Expand Down Expand Up @@ -390,6 +394,7 @@ fn main() -> Result<()> {
minor,
patch,
pre,
build,
hook_profile,
package,
annotated,
Expand Down Expand Up @@ -431,6 +436,7 @@ fn main() -> Result<()> {
package,
increment,
pre_release: pre.as_deref(),
build: build.as_deref(),
hooks_config: hook_profile.as_deref(),
annotated,
dry_run,
Expand All @@ -446,6 +452,7 @@ fn main() -> Result<()> {
let opts = BumpOptions {
increment,
pre_release: pre.as_deref(),
build: build.as_deref(),
hooks_config: hook_profile.as_deref(),
annotated,
dry_run,
Expand All @@ -462,6 +469,7 @@ fn main() -> Result<()> {
let opts = BumpOptions {
increment,
pre_release: pre.as_deref(),
build: build.as_deref(),
hooks_config: hook_profile.as_deref(),
annotated,
dry_run,
Expand Down
2 changes: 2 additions & 0 deletions src/command/bump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod standard;
pub struct BumpOptions<'a> {
pub increment: IncrementCommand,
pub pre_release: Option<&'a str>,
pub build: Option<&'a str>,
pub hooks_config: Option<&'a str>,
pub annotated: Option<String>,
pub dry_run: bool,
Expand All @@ -44,6 +45,7 @@ pub struct PackageBumpOptions<'a> {
pub package: &'a MonoRepoPackage,
pub increment: IncrementCommand,
pub pre_release: Option<&'a str>,
pub build: Option<&'a str>,
pub hooks_config: Option<&'a str>,
pub annotated: Option<String>,
pub dry_run: bool,
Expand Down
29 changes: 24 additions & 5 deletions src/command/bump/monorepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use anyhow::Result;
use colored::*;

use log::{info, warn};
use semver::Prerelease;
use semver::{Prerelease, BuildMetadata};
use tera::Tera;

use crate::conventional::error::BumpError;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl CocoGitto {

self.repository.add_all()?;
self.unwrap_or_stash_and_exit(&Tag::default(), hook_result);
self.bump_packages(opts.pre_release, opts.hooks_config, &bumps)?;
self.bump_packages(opts.pre_release, opts.build, opts.hooks_config, &bumps)?;

if !disable_bump_commit {
let sign = self.repository.gpg_sign();
Expand Down Expand Up @@ -127,7 +127,7 @@ impl CocoGitto {
fn create_monorepo_version_auto(&mut self, opts: BumpOptions) -> Result<()> {
self.pre_bump_checks(opts.skip_untracked)?;
// Get package bumps
let bumps = self.get_packages_bumps(opts.pre_release)?;
let bumps = self.get_packages_bumps(opts.pre_release, opts.build)?;
if bumps.is_empty() {
print!("No conventional commits found for your packages that required a bump. Changelogs will be updated on the next bump.\nPre-Hooks and Post-Hooks have been skiped.\n");
return Ok(());
Expand Down Expand Up @@ -156,6 +156,10 @@ impl CocoGitto {
tag.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = opts.build {
tag.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(tag.version, None);

if opts.dry_run {
Expand Down Expand Up @@ -225,9 +229,11 @@ impl CocoGitto {
);

self.repository.add_all()?;
self.unwrap_or_stash_and_exit(&Tag::default(), hook_result);
self.unwrap_or_stash_and_exit(&tag, hook_result);
self.bump_packages(opts.pre_release, opts.hooks_config, &bumps)?;

self.bump_packages(opts.pre_release, opts.build, opts.build, opts.hooks_config, &bumps)?;

let disable_bump_commit = opts.disable_bump_commit || SETTINGS.disable_bump_commit;

if !disable_bump_commit {
Expand Down Expand Up @@ -308,6 +314,10 @@ impl CocoGitto {
tag.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = opts.build {
tag.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(tag.version, None);

if opts.dry_run {
Expand Down Expand Up @@ -427,7 +437,7 @@ impl CocoGitto {
}

// Calculate all package bump
fn get_packages_bumps(&self, pre_release: Option<&str>) -> Result<Vec<PackageBumpData>> {
fn get_packages_bumps(&self, pre_release: Option<&str>, build: Option<&str>) -> Result<Vec<PackageBumpData>> {
let mut package_bumps = vec![];
for (package_name, package) in SETTINGS.packages.iter() {
let old = self.repository.get_latest_package_tag(package_name);
Expand All @@ -452,6 +462,10 @@ impl CocoGitto {
next_version.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = build {
next_version.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(next_version.version, Some(package_name.to_string()));
let increment = tag.get_increment_from(&old);

Expand Down Expand Up @@ -480,6 +494,7 @@ impl CocoGitto {
fn bump_packages(
&mut self,
pre_release: Option<&str>,
build: Option<&str>,
hooks_config: Option<&str>,
package_bumps: &Vec<PackageBumpData>,
) -> Result<()> {
Expand All @@ -505,6 +520,10 @@ impl CocoGitto {
next_version.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = build {
next_version.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(next_version.version, Some(package_name.to_string()));
let pattern = self.get_bump_revspec(&old);

Expand Down
6 changes: 5 additions & 1 deletion src/command/bump/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{CocoGitto, SETTINGS};
use anyhow::Result;
use colored::*;
use log::info;
use semver::Prerelease;
use semver::{Prerelease, BuildMetadata};
use tera::Tera;

impl CocoGitto {
Expand All @@ -33,6 +33,10 @@ impl CocoGitto {
next_version.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = opts.build {
tag.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(
next_version.version.clone(),
Some(opts.package_name.to_string()),
Expand Down
6 changes: 5 additions & 1 deletion src/command/bump/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{settings, CocoGitto, SETTINGS};
use anyhow::Result;
use colored::*;
use log::info;
use semver::Prerelease;
use semver::{Prerelease, BuildMetadata};
use tera::Tera;

impl CocoGitto {
Expand All @@ -31,6 +31,10 @@ impl CocoGitto {
tag.version.pre = Prerelease::new(pre_release)?;
}

if let Some(build) = opts.build {
tag.version.build = BuildMetadata::new(build)?;
}

let tag = Tag::create(tag.version, None);

if opts.dry_run {
Expand Down
20 changes: 20 additions & 0 deletions tests/cog_tests/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ fn pre_release_bump() -> Result<()> {
Ok(())
}

#[sealed_test]
fn build_release_bump() -> Result<()> {
git_init()?;
git_commit("chore: init")?;
git_tag("1.0.0")?;
git_commit("feat: feature")?;

Command::cargo_bin("cog")?
.arg("bump")
.arg("--major")
.arg("--build")
.arg("a.b.c")
.assert()
.success();

assert_that!(Path::new("CHANGELOG.md")).exists();
assert_tag_exists("2.0.0+a.b.c")?;
Ok(())
}

#[sealed_test]
#[cfg(target_os = "linux")]
fn bump_with_hook() -> Result<()> {
Expand Down

0 comments on commit 1680042

Please sign in to comment.