Skip to content

Commit

Permalink
fix: uppercase first char of scope on stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Jan 19, 2021
1 parent 603f724 commit 4eaebe9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 107 deletions.
121 changes: 34 additions & 87 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ConventionalChangelog;

use ConventionalChangelog\Helper\Format;
use ConventionalChangelog\Helper\Git;
use ConventionalChangelog\Helper\SemanticVersion;
use DateTime;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -79,7 +82,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int

// Current Dates
$today = new DateTime();
$todayString = Utils::getDateString($today);
$todayString = Format::getDateString($today);

// First commit
$firstCommit = Git::getFirstCommit();
Expand All @@ -89,20 +92,20 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
$lastVersionCommit = Git::getLastTagCommit(); // Last version commit
$lastVersionDate = Git::getCommitDate($lastVersionCommit); // Last version date

$bumpRelease = SemanticVersion::RELEASE_PATCH;
$bumpRelease = SemanticVersion::PATCH;

if ($majorRelease) {
$bumpRelease = SemanticVersion::RELEASE_MAJOR;
$bumpRelease = SemanticVersion::MAJOR;
} elseif ($minorRelease) {
$bumpRelease = SemanticVersion::RELEASE_MINOR;
$bumpRelease = SemanticVersion::MINOR;
} elseif ($patchRelease) {
$bumpRelease = SemanticVersion::RELEASE_PATCH;
$bumpRelease = SemanticVersion::PATCH;
} elseif ($preRelease) {
$bumpRelease = SemanticVersion::RELEASE_RC;
$bumpRelease = SemanticVersion::RC;
} elseif ($betaRelease) {
$bumpRelease = SemanticVersion::RELEASE_BETA;
$bumpRelease = SemanticVersion::BETA;
} elseif ($alphaRelease) {
$bumpRelease = SemanticVersion::RELEASE_ALPHA;
$bumpRelease = SemanticVersion::ALPHA;
} else {
$autoBump = true;
}
Expand Down Expand Up @@ -174,7 +177,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
$time = strtotime($toDate);
$additionalParams .= ' --before="' . date('Y-m-d', $time) . '"';
$today->setTimestamp($time);
$todayString = Utils::getDateString($today);
$todayString = Format::getDateString($today);
}
}
$options[$lastVersion] = [
Expand All @@ -190,63 +193,42 @@ public function generate(InputInterface $input, SymfonyStyle $output): int

// Get all commits information
$commits = [];
foreach ($commitsRaw as $commit) {
$rows = explode("\n", $commit);
$count = count($rows);
// Commit info
$head = Utils::clean($rows[0]);
$sha = Utils::clean($rows[$count - 1]);
$message = '';
// Get message
foreach ($rows as $i => $row) {
if ($i !== 0 && $i !== $count - 1) {
$message .= $row . "\n";
}
foreach ($commitsRaw as $commitRaw) {
$commit = new Commit\Parser($commitRaw);

// Not a conventional commit
if (!$commit->isValid()) {
break;
}

// Check ignored commit
$ignore = false;
foreach ($this->config->getIgnorePatterns() as $pattern) {
if (preg_match($pattern, $head)) {
if (preg_match($pattern, $commit->getHeader())) {
$ignore = true;
break;
}
}
// Add commit
if (!empty($sha) && !$ignore) {
$commits[] = [
'sha' => $sha,
'head' => $head,
'message' => Utils::clean($message),
];
if (!$ignore) {
$commits[] = new Commit\Parser($commit);
}
}

// Changes groups
$changes = [];
foreach ($this->config->getTypes() as $key => $type) {
$changes[$key] = [];
}

// Group all changes to lists by type
$types = $this->config->getTypes();
foreach ($commits as $commit) {
foreach ($this->config->getTypes() as $name => $type) {
$head = Utils::clean($commit['head']);
$code = preg_quote($name, '/');
if (preg_match('/^' . $code . '(\(.*?\))?[:]?\\s/i', $head)) {
$parse = $this->parseCommitHead($head, $name);
$scope = $parse['scope'];
$description = $parse['description'];
$sha = $commit['sha'];
$short = substr($sha, 0, 6);
// List item
$itemKey = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '', $description));
$changes[$name][$scope][$itemKey][$sha] = [
'description' => $description,
'short' => $short,
'url' => $url,
'sha' => $sha,
];
}
if (in_array($commit->getType(), $types)) {
$itemKey = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '', $commit->getDescription()));
$type = (string)$commit->getType();
$scope = (string)$commit->getScope();
$hash = $commit->getHash();
$changes[$type][$scope][$itemKey][$hash] = [
'description' => ucfirst($commit->getDescription()),
'short' => $commit->getShortHash(),
'url' => $url,
'sha' => $hash,
];
}
}

Expand Down Expand Up @@ -328,39 +310,4 @@ protected function getMarkdownChanges(array $changes): string

return $changelog;
}

/**
* Parse conventional commit head.
*
* @param string $message
*/
protected function parseCommitHead(string $head, string $type): array
{
$parse = [
'scope' => null,
'description' => Utils::clean($head),
];

$descriptionType = preg_quote(substr($parse['description'], 0, strlen($type)), '/');
$parse['description'] = preg_replace('/^' . $descriptionType . '[:]?\s*/i', '', $parse['description']);
$parse['description'] = preg_replace('/^\((.*?)\)[!]?[:]?\s*/', '**$1**: ', Utils::clean($parse['description']));
$parse['description'] = preg_replace('/\s+/m', ' ', $parse['description']);

// Set scope
if (preg_match("/^\*\*(.*?)\*\*:(.*?)$/", $parse['description'], $match)) {
$parse['scope'] = Utils::clean($match[1]);
$parse['description'] = Utils::clean($match[2]);

// Unify scope labels
$parse['scope'] = preg_replace('/[_]+/m', ' ', $parse['scope']);
$parse['scope'] = ucfirst($parse['scope']);
$parse['scope'] = preg_replace('/((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))/u', ' $0', $parse['scope']);
$parse['scope'] = preg_replace('/\.(php|md|json|txt|csv|js)($|\s)/', '', $parse['scope']);
$parse['scope'] = Utils::clean($parse['scope']);
}

$parse['description'] = ucfirst($parse['description']);

return $parse;
}
}
3 changes: 1 addition & 2 deletions src/Commit/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public function __construct(string $content)

public function __toString(): string
{
$string = $this->content;
$string = ucfirst($this->content);
$string = preg_replace('/[_]+/m', ' ', $string);
$string = ucfirst($string);
$string = preg_replace('/((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))/u', ' $0', $string);
$string = preg_replace('/\.(php|md|json|txt|csv|js)($|\s)/', '', $string);
$string = Format::clean($string);
Expand Down
10 changes: 9 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,19 @@ protected function isRegex(string $pattern)
/**
* @return string[][]
*/
public function getTypes(): array
public function getTypesInfo(): array
{
return $this->types;
}

/**
* @return string[]
*/
public function getTypes(): array
{
return array_keys($this->types);
}

/**
* @param $type
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Utils.php → src/Helper/Format.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace ConventionalChangelog;
namespace ConventionalChangelog\Helper;

use DateTime;

class Utils
class Format
{
/**
* Clean string removing double spaces and trim.
Expand All @@ -13,7 +13,7 @@ public static function clean(string $string): string
{
$string = trim($string);

return preg_replace('/[ ]+/m', ' ', $string);
return preg_replace('/[[:blank:]]+/m', ' ', $string);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Git.php → src/Helper/Git.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ConventionalChangelog;
namespace ConventionalChangelog\Helper;

use DateTime;

Expand All @@ -15,7 +15,7 @@ protected static function shellExec($string): string
{
$value = shell_exec($string);

return Utils::clean($value);
return Format::clean($value);
}

/**
Expand Down
23 changes: 11 additions & 12 deletions src/SemanticVersion.php → src/Helper/SemanticVersion.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace ConventionalChangelog;
namespace ConventionalChangelog\Helper;

class SemanticVersion
{
public const RELEASE_MAJOR = 'major';
public const RELEASE_MINOR = 'minor';
public const RELEASE_PATCH = 'patch';
public const RELEASE_RC = 'rc';
public const RELEASE_BETA = 'beta';
public const RELEASE_ALPHA = 'alpha';
public const MAJOR = 'major';
public const MINOR = 'minor';
public const PATCH = 'patch';
public const RC = 'rc';
public const BETA = 'beta';
public const ALPHA = 'alpha';

/**
* @var string
Expand Down Expand Up @@ -40,7 +40,7 @@ public function bump(string $release): string
$split = explode('-', $version, 2);
$extra = !empty($split[1]) ? $split[1] : '';

$extraReleases = [self::RELEASE_RC, self::RELEASE_BETA, self::RELEASE_ALPHA];
$extraReleases = [self::RC, self::BETA, self::ALPHA];

if (in_array($release, $extraReleases)) {
$partsExtra = explode('.', $extra);
Expand All @@ -60,17 +60,16 @@ public function bump(string $release): string
$newVersion[$key] = (int)$value;
}

if ($release === self::RELEASE_MAJOR) {
if ($release === self::MAJOR) {
$newVersion[0]++;
} elseif ($release === self::RELEASE_MINOR) {
} elseif ($release === self::MINOR) {
$newVersion[1]++;
} elseif ($release === self::RELEASE_PATCH) {
} elseif ($release === self::PATCH) {
$newVersion[2]++;
}

// Recompose semver
$version = implode('.', $newVersion) . (!empty($extra) ? '-' . $extra : '');

$this->setVersion($version);

return $version;
Expand Down

0 comments on commit 4eaebe9

Please sign in to comment.