Skip to content

Commit

Permalink
feat: prepend to changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ramos committed Jul 22, 2024
1 parent 1d36616 commit ab908e0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
[![Docs.rs](https://docs.rs/workspace-node-tools/badge.svg)](https://docs.rs/workspace-node-tools)
[![CI](https://github.com/websublime/workspace-node-tools/workflows/CI/badge.svg)](https://github.com/websublime/workspace-node-tools/actions)

## About

This is a tool to help manage packages in a monorepo style. It can give info about packages existence, package manager defined (node), git helpers to check which package as changes, manage those changes thur a file (.changes.json), give output of conventional commit and changelog generation.

## Installation

`cargo install workspace-node-tools`

### Cargo

* Install the rust toolchain in order to have cargo installed by following
- Install the rust toolchain in order to have cargo installed by following
[this](https://www.rust-lang.org/tools/install) guide.
* run `cargo install workspace-node-tools`
- run `cargo install workspace-node-tools`

## License

Licensed under either of

* Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

Expand All @@ -35,4 +39,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).

## Info

Template from [here](https://rust-github.github.io/)
Template from [here](https://rust-github.github.io/)
49 changes: 44 additions & 5 deletions src/conventional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use git_cliff_core::{
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::fs::read_to_string;
use std::path::PathBuf;

use super::git::{
Expand Down Expand Up @@ -263,6 +264,30 @@ fn generate_changelog(
String::from_utf8(changelog_output).unwrap_or_default()
}

/// Prepend changelog output
fn prepend_generate_changelog(
commits: &Vec<GitCommit>,
config: &Config,
changelog_content: &String,
version: Option<String>,
) -> String {
let releases = Release {
version,
commits: commits.to_vec().to_owned(),
..Release::default()
};

let changelog = Changelog::new(vec![releases], config);
let mut changelog_output = Vec::new();

changelog
.unwrap()
.prepend(changelog_content.to_string(), &mut changelog_output)
.unwrap();

String::from_utf8(changelog_output).unwrap_or_default()
}

/// Give info about commits in a package, generate changelog output
pub fn get_conventional_for_package(
package_info: &PackageInfo,
Expand All @@ -275,6 +300,9 @@ pub fn get_conventional_for_package(
None => get_project_root_path(None).unwrap(),
};

let changelog_dir =
PathBuf::from(package_info.package_path.to_string()).join(String::from("CHANGELOG.md"));

if no_fetch_all.is_some() {
git_fetch_all(Some(current_working_dir.to_string()), no_fetch_all).expect("Fetch all");
}
Expand Down Expand Up @@ -349,11 +377,22 @@ pub fn get_conventional_for_package(

let conventional_commits = process_commits(&commits_since, &conventional_config.git);

let changelog = generate_changelog(
&conventional_commits,
&conventional_config,
conventional_default_options.version,
);
let changelog = match changelog_dir.exists() {
true => {
let changelog_content = read_to_string(&changelog_dir).unwrap();
prepend_generate_changelog(
&conventional_commits,
&conventional_config,
&changelog_content,
conventional_default_options.version,
)
}
false => generate_changelog(
&conventional_commits,
&conventional_config,
conventional_default_options.version,
),
};

let changelog_output = &changelog.to_string();
conventional_package.changelog_output = changelog_output.to_string();
Expand Down
6 changes: 5 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Commit {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize)]
/// A struct that represents a commit information
pub struct Commit {
pub hash: String,
pub author_name: String,
Expand All @@ -51,6 +52,7 @@ pub struct RemoteTags {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize)]
/// A struct that represents a remote tag information
pub struct RemoteTags {
pub hash: String,
pub tag: String,
Expand All @@ -67,12 +69,14 @@ pub struct PublishTagInfo {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize)]
/// A struct that represents a publish tag information
pub struct PublishTagInfo {
pub hash: String,
pub tag: String,
pub package: String,
}

/// Fetch everything from origin including tags
pub fn git_fetch_all(
cwd: Option<String>,
fetch_tags: Option<bool>,
Expand Down Expand Up @@ -421,7 +425,7 @@ pub fn git_all_files_changed_since_sha(sha: String, cwd: Option<String>) -> Vec<
.arg("--no-pager")
.arg("diff")
.arg("--name-only")
.arg(format!("{}..", sha));
.arg(format!("{}", sha));
command.current_dir(&current_working_dir);

command.stdout(Stdio::piped());
Expand Down
1 change: 1 addition & 0 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum PackageManager {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Copy, PartialEq)]
/// Package manager used in the monorepo.
pub enum PackageManager {
Npm,
Yarn,
Expand Down
9 changes: 9 additions & 0 deletions src/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use super::manager::{detect_package_manager, PackageManager};
use super::paths::get_project_root_path;

#[derive(Debug, Deserialize, Serialize)]
/// A struct that represents a pnpm workspace.
struct PnpmInfo {
pub name: String,
pub path: String,
pub private: bool,
}

#[derive(Debug, Deserialize, Serialize)]
/// A struct that represents a yarn workspace.
struct PkgJson {
pub workspaces: Vec<String>,
}
Expand All @@ -49,6 +51,7 @@ pub struct PackageInfo {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
/// A struct that represents a package in the monorepo.
pub struct PackageInfo {
pub name: String,
pub private: bool,
Expand All @@ -74,21 +77,25 @@ pub struct PackageRepositoryInfo {

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
/// A struct that represents the repository information of a package.
pub struct PackageRepositoryInfo {
pub domain: String,
pub orga: String,
pub project: String,
}

impl PackageInfo {
/// Pushes a changed file to the list of changed files.
pub fn push_changed_file(&mut self, file: String) {
self.changed_files.push(file);
}

/// Returns the list of changed files.
pub fn get_changed_files(&self) -> Vec<String> {
self.changed_files.to_vec()
}

/// Extends the list of changed files with the provided list.
pub fn extend_changed_files(&mut self, files: Vec<String>) {
let founded_files = files
.iter()
Expand All @@ -99,12 +106,14 @@ impl PackageInfo {
self.changed_files.extend(founded_files);
}

/// Updates the version of the package.
pub fn update_version(&mut self, version: String) {
self.version = version.to_string();
self.pkg_json["version"] = Value::String(version.to_string());
}
}

/// Returns package info domain, scope and repository name.
fn get_package_repository_info(url: &String) -> PackageRepositoryInfo {
let regex = Regex::new(
r"(?m)((?<protocol>[a-z]+)://)((?<domain>[^/]*)/)(?<org>([^/]*)/)(?<project>(.*))(\.git)?",
Expand Down
5 changes: 4 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(dead_code)]

use regex::Regex;
use serde::{Deserialize, Serialize};

#[cfg(test)]
use std::path::Path;
Expand Down Expand Up @@ -29,7 +30,8 @@ use std::process::Command;
#[cfg(test)]
use std::process::Stdio;

#[derive(Debug)]
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Package scope metadata extracted from a package name.
pub struct PackageScopeMetadata {
pub full: String,
pub name: String,
Expand Down Expand Up @@ -57,6 +59,7 @@ pub(crate) fn package_scope_name_version(pkg_name: &str) -> Option<PackageScopeM
None
}

/// Strips the trailing newline from a string.
pub(crate) fn strip_trailing_newline(input: &String) -> String {
input
.strip_suffix("\r\n")
Expand Down

0 comments on commit ab908e0

Please sign in to comment.