Skip to content

Commit

Permalink
feat: split types and presets configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cesarato committed Jan 21, 2021
1 parent 1795ffb commit 4239ec0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 39 deletions.
60 changes: 42 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ Create a file named `.changelog` on the root of your project or on the working d

> **Note:** If you don't need to customize some settings just omit it from the configuration file
> **Note:** The default excluded types are: `build`, `chore`, `ci`, `docs`, `refactor`, `revert`, `style`, `test`
> **Note:** The default ignored types are: `build`, `chore`, `ci`, `docs`, `refactor`, `revert`, `style`, `test`
> **Note:** To allow all types just keep empty `types` and set empty `ignoreTypes`
Create your configuration settings with the help of the following example.

Expand All @@ -65,10 +67,12 @@ Create your configuration settings with the help of the following example.
<?php
return [
// File changelog (relative to the working dir)
'path' => 'docs/CHANGELOG.md',
'path' => 'docs/CHANGELOG.md', // You can specify a different folder
'headerTitle' => 'My changelog',
'headerDescription' => 'This is my changelog file.',
'types' => [
// Types allowed on changelog
'types' => ['feat', 'fix', 'pref'], // These could overwrite ignored types
'preset' => [
// Add improvements type (deprecated type)
'improvements' => [
'label' => 'Improvements',
Expand Down Expand Up @@ -98,39 +102,59 @@ The changelog generator will generate a log of changes from the date of the last
and it will put all commit logs in the latest version just created (at the moment it doesn't generate the entire git commit version release history).
By default, will be added one to the patch semver part *(Example, if the last version is `1.0.2` the newer, if not specified the identity of the release, will be `1.0.3`)*.

- To generate your changelog for your first release:
---

To generate your changelog for your first release:

> **Note:** If the version code (`--ver`) isn't specified it will be automatically `1.0.0`
> **Note:** If the version code (`--ver`) isn't specified it will be automatically `1.0.0`
`php vendor/bin/conventional-changelog --first-release`
```shell
php vendor/bin/conventional-changelog --first-release
```

---

- To generate your changelog without committing files:

`php vendor/bin/conventional-changelog`
To generate your changelog without committing files:

```shell
php vendor/bin/conventional-changelog
```

---

- To generate your changelog with auto commit and auto version tagging:
To generate your changelog with auto commit and auto version tagging:

`php vendor/bin/conventional-changelog --commit`
```shell
php vendor/bin/conventional-changelog --commit
```

---

- To generate your changelog from a specified date to another specified date:
To generate your changelog from a specified date to another specified date:

`php vendor/bin/conventional-changelog --from-date="2020-12-01" --to-date="2021-01-01"`
```shell
php vendor/bin/conventional-changelog --from-date="2020-12-01" --to-date="2021-01-01"
```

---

- To generate your changelog with a specific version code:
To generate your changelog with a specific version code:

`php vendor/bin/conventional-changelog --ver="2.0.1"`
```shell
php vendor/bin/conventional-changelog --ver="2.0.1"
```

---

- To generate your changelog with the entire history of changes of all releases:
To generate your changelog with the entire history of changes of all releases:

> **Warn:** This operation will overwrite the `CHANGELOG.md` file if it already exists
> **Warn:** This operation will overwrite the `CHANGELOG.md` file if it already exists
`php vendor/bin/conventional-changelog --history`
```shell
php vendor/bin/conventional-changelog --history
```

---

### Commands List

Expand Down
4 changes: 2 additions & 2 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ public function generate(InputInterface $input, SymfonyStyle $output): int

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

// Group all changes to lists by type
$types = $this->config->getTypes();
$types = $this->config->getAllowedTypes();
foreach ($commits as $commit) {
if (in_array($commit->getType(), $types)) {
$itemKey = $this->getItemKey($commit->getDescription());
Expand Down
65 changes: 46 additions & 19 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Configuration
* @var string[][]
*/
public $preset = [
'breaking_changes' => ['label' => '⚠ BREAKING CHANGES', 'description' => 'Code changes that potentially causes other components to fail'],
'feat' => ['label' => 'Features', 'description' => 'New features'],
'perf' => ['label' => 'Performance Improvements', 'description' => 'Code changes that improves performance'],
'fix' => ['label' => 'Bug Fixes', 'description' => 'Issues resolution'],
Expand All @@ -46,6 +45,15 @@ class Configuration
'revert' => ['label' => 'Reverts', 'description' => 'Reverts a previous commit'],
];

/**
* Preset of breaking changes.
*
* @var string[][]
*/
public $breakingPreset = [
'breaking_changes' => ['label' => '⚠ BREAKING CHANGES', 'description' => 'Code changes that potentially causes other components to fail'],
];

/**
* Types allowed on changelog.
*
Expand Down Expand Up @@ -85,37 +93,50 @@ public function fromArray(array $array)
'headerTitle' => $this->headerTitle,
'headerDescription' => $this->headerDescription,
'path' => $this->path,
'types' => $this->preset,
'preset' => array_merge($this->breakingPreset, $this->preset),
'types' => [],
'ignoreTypes' => ['build', 'chore', 'ci', 'docs', 'refactor', 'revert', 'style', 'test'],
];

$params = array_replace_recursive($defaults, $array);

// Overwrite ignored types
// Ignore Types
if (!empty($array['ignoreTypes'])) {
$params['ignoreTypes'] = $array['ignoreTypes'];
$params['ignoreTypes'] = $array['ignoreTypes']; // Overwrite ignored types
}

// Unset excluded types
if (is_array($params['ignoreTypes'])) {
foreach ($params['ignoreTypes'] as $type) {
unset($params['types'][$type]);
unset($params['preset'][$type]); // Unset excluded types
}
}

// Ignore patterns
// Ignore Patterns
if (!empty($array['ignorePatterns'])) {
$params['ignorePatterns'] = array_merge($this->ignorePatterns, $array['ignorePatterns']);
}

// Check ignore patterns
foreach ($params['ignorePatterns'] as $key => $pattern) {
if (!$this->isRegex($pattern)) {
if (!$this->isRegex($pattern)) { // Check ignore patterns
$params['ignorePatterns'][$key] = '#' . preg_quote($pattern, '#') . '#i';
}
}

$this->setTypes($params['types']);
// Set Types (overwrite ignored types)
if (!empty($array['types'])) {
foreach ($this->preset as $type => $value) {
if (!in_array($type, $array['types'])) {
if (isset($params['preset'][$type])) {
unset($params['preset'][$type]);
}
} else {
$params['preset'][$type] = $value;
}
}
}

// Add breaking changes
$params['preset'] = array_merge($this->breakingPreset, $params['preset']);

$this->setTypes($params['preset']);
$this->setHeaderTitle($params['headerTitle']);
$this->setHeaderDescription($params['headerDescription']);
$this->setPath($params['path']);
Expand All @@ -135,22 +156,20 @@ protected function isRegex(string $pattern)
}

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

/**
* @return string[]
*/
public function getTypes(bool $breaking = false): array
public function getAllowedTypes(): array
{
$types = $this->types;
if (!$breaking) {
unset($types['breaking_changes']);
}
unset($types['breaking_changes']);

return array_keys($types);
}
Expand All @@ -163,6 +182,14 @@ public function getTypeLabel(string $type): string
return $this->types[$type]['label'];
}

/**
* @param $type
*/
public function getTypeDescription(string $type): string
{
return isset($this->types[$type]['description']) ? $this->types[$type]['description'] : '';
}

/**
* @param string[][] $types
*/
Expand Down

0 comments on commit 4239ec0

Please sign in to comment.