Skip to content

Commit

Permalink
Bump v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
obstschale committed Jul 31, 2019
1 parent f85b3a2 commit 41e67d6
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
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
14 changes: 0 additions & 14 deletions app/Commands/AddChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Commands;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -158,17 +157,4 @@ private function getAuthor() : string

return $author;
}


/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
public function schedule(Schedule $schedule) : void
{
// $schedule->command(static::class)->everyMinute();
}
}
124 changes: 124 additions & 0 deletions app/Commands/BuildChangelog.php
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");
}
}
25 changes: 25 additions & 0 deletions app/Types.php
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 removed changelogs/unreleased/.gitkeep
Empty file.
3 changes: 0 additions & 3 deletions changelogs/unreleased/master.yml

This file was deleted.

0 comments on commit 41e67d6

Please sign in to comment.