Skip to content

Commit

Permalink
Add aggregate command (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaddeo authored Jul 28, 2020
1 parent 508e9af commit 0b79105
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .cl/add-aggregate-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- fixed: Fix bug that caused cl entries to be not properly nested in their branch directories
- added: Add an aggregate subcommand to aggregate change entries into the CHANGELOG.md file
94 changes: 91 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ serde_yaml = "0.8.11"
serde_json = "1.0.41"
err-derive = "0.1.6"
anyhow = "1.0.31"
clparse = "0.7.0"
clparse = "0.8.0"
semver = "0.10.0"
remove_empty_subdirs = "0.1.1"
gag = "0.1.10"
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ SUBCOMMANDS:
security Creates a change entry to be placed in the Security section of the CHANGELOG
edit Opens the change file for direct editing
yank Mark a specific release as [YANKED]
aggregate Aggregate change entries into the Unreleased section of the CHANGELOG
```

## Examples
Expand Down Expand Up @@ -128,3 +129,18 @@ Sometimes a release doesn't go as plan after the fact, and needs to be yanked
from history. To do so, simply type `cl yank 1.2.3` where `1.2.3` is the
release you wish to yank. This will tag the release as `[YANKED]` and remove
it's link from the CHANGELOG.

### Aggregating unreleased changes
After you've made some changes and they've been merged, you'll likely want to
aggregate your cl entries into the actual CHANGELOG.md file so they can be
easily viewed by users. This can be achieved with the `aggregate` subcommand.

```
$ cl aggregate
```

This will take any change entries in the `.cl` directory, put them into the
`CHANGELOG.md` file, and then remove them and any empty directories within the
`.cl` directory.

This command is useful to run via CI after a pull request has been merged.
45 changes: 42 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use clparse::changelog::{Change, Changelog, ReleaseBuilder};
use err_derive::Error;
use semver::Version;
use scan_dir::ScanDir;
use gag::Gag;
use remove_empty_subdirs::remove_empty_subdirs;
use std::env;
use std::fs::{create_dir_all, OpenOptions};
use std::io::{self, Write};
Expand Down Expand Up @@ -139,6 +141,10 @@ fn main() -> Result<()> {
.required(true)
)
)
.subcommand(
SubCommand::with_name("aggregate")
.about("Aggregate change entries into the Unreleased section of the CHANGELOG")
)
.get_matches();

match matches.subcommand() {
Expand Down Expand Up @@ -203,6 +209,19 @@ fn main() -> Result<()> {

Ok(())
}
("aggregate", Some(_)) => {
let mut changelog = get_changelog()?;

if let Some(unreleased) = changelog.unreleased_mut() {
unreleased.set_changes(get_all_changes()?);
std::fs::write(get_changelog_path()?, format!("{}", changelog))?;
remove_cl_change_entries()?;
} else {
bail!(ClError::ReleaseNotFound("Unreleased".to_string()));
}

Ok(())
}
(kind, Some(sub_matches)) => {
let description = sub_matches
.values_of("description")
Expand Down Expand Up @@ -250,7 +269,7 @@ fn get_cl_path() -> Result<PathBuf> {

cl_path.push(head);
create_dir_all(cl_path.clone())?;
cl_path.set_extension("yml");
cl_path.push("changes.yml");

Ok(cl_path)
}
Expand Down Expand Up @@ -284,8 +303,7 @@ fn get_unreleased_changes() -> Result<Vec<Change>> {
Ok(get_changelog()?.unreleased_changes())
}

fn get_all_changes() -> Result<Vec<Change>> {
let mut changes: Vec<Change> = get_unreleased_changes()?;
fn get_cl_entry_paths() -> Result<Vec<PathBuf>> {
let mut logs: Vec<PathBuf> = Vec::new();
let cl_dir = get_cl_dir()?;

Expand All @@ -302,6 +320,27 @@ fn get_all_changes() -> Result<Vec<Change>> {
})
.map_err(ClError::ScanError)?;

Ok(logs)
}

fn remove_cl_change_entries() -> Result<()> {
let logs = get_cl_entry_paths()?;

for log in logs {
std::fs::remove_file(log)?;
}

let gag = Gag::stdout()?;
remove_empty_subdirs(get_cl_dir()?.as_path())?;
drop(gag);

Ok(())
}

fn get_all_changes() -> Result<Vec<Change>> {
let mut changes: Vec<Change> = get_unreleased_changes()?;
let logs = get_cl_entry_paths()?;

for log in logs {
let mut cl_changes = get_changes(log)?;
changes.append(&mut cl_changes);
Expand Down

0 comments on commit 0b79105

Please sign in to comment.