-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f85b3a2
commit 41e67d6
Showing
6 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## [v0.0.1] - 2019-07-31 | ||
|
||
### New features (2 changes) | ||
|
||
* Add command to create new changelogs | ||
* Build command to generate changelog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace App\Commands; | ||
|
||
use App\Types; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\File; | ||
use LaravelZero\Framework\Commands\Command; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class BuildChangelog extends Command | ||
{ | ||
|
||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'build {tag : Version or tag name}'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Build new changelog from unreleased logs'; | ||
|
||
/** | ||
* Path to changelog directory | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
|
||
/** | ||
* BuildChangelog constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->path = getcwd() . '/changelogs/unreleased'; | ||
} | ||
|
||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$this->initFolder(); | ||
$allFiles = File::allFiles($this->path); | ||
|
||
if (empty($allFiles)) { | ||
$this->build('No changes.'); | ||
exit(); | ||
} | ||
|
||
$changes = collect(); | ||
foreach ($allFiles as $file) { | ||
$changes->push(Yaml::parse($file->getContents())); | ||
} | ||
|
||
$content = $this->generateContent($changes); | ||
$this->build($content); | ||
|
||
$this->info("Changelog for {$this->argument('tag')} created"); | ||
File::delete($allFiles); | ||
} | ||
|
||
|
||
private function initFolder() : void | ||
{ | ||
if ( ! File::exists($this->path)) { | ||
File::makeDirectory($this->path, 0755, true); | ||
} | ||
} | ||
|
||
|
||
private function build(string $string) : void | ||
{ | ||
$tag = $this->argument('tag'); | ||
$today = Carbon::now()->format('Y-m-d'); | ||
|
||
$content = <<<CONTENT | ||
## [$tag] - $today | ||
$string | ||
\n | ||
CONTENT; | ||
|
||
File::prepend(getcwd() . '/CHANGELOG.md', $content); | ||
} | ||
|
||
|
||
private function generateContent(Collection $changes) : string | ||
{ | ||
$changes = $changes->groupBy('type'); | ||
|
||
return $changes->map(function (Collection $logType, $key) { | ||
$header = Types::getName($key); | ||
$count = $logType->count(); | ||
$changes = sprintf('%d %s', $count, $count === 1 ? 'change' : 'changes'); | ||
$content = "### {$header} ({$changes})\n\n"; | ||
|
||
$content .= $logType->map(function (array $log) { | ||
$changeEntry = "* {$log['title']}"; | ||
|
||
if ( ! empty($log['author'])) { | ||
$changeEntry .= " (props {$log['author']})"; | ||
} | ||
|
||
return $changeEntry; | ||
})->implode("\n"); | ||
|
||
$content .= "\n"; | ||
|
||
return $content; | ||
})->implode("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Types | ||
{ | ||
|
||
private static $types = [ | ||
'New feature' => 'added', | ||
'Bug fix' => 'fixed', | ||
'Feature change' => 'changed', | ||
'New deprecation' => 'deprecated', | ||
'Feature removal' => 'removed', | ||
'Security fix' => 'security', | ||
'Performance improvement' => 'performance', | ||
'Other' => 'other', | ||
]; | ||
|
||
public static function getName(string $key) : string | ||
{ | ||
return collect(self::$types)->filter(function ($type) use ($key) { | ||
return $type === $key; | ||
})->keys()->first(); | ||
} | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.