Skip to content

Commit 34186f5

Browse files
authored
Merge pull request #42 from markwalet/current-changes-command
Current changes command
2 parents 65985fb + be3036a commit 34186f5

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Added
66
- Make sure the unreleased folder stays in the filesystem for better diff comparisons ([#4](https://github.com/markwalet/laravel-changelog/issues/4))
77
- Add a `changelog:install` artisan command.
8+
- Add a `changelog:current` artisan command.
89

910
## [v1.6.1 (2022-03-27)](https://github.com/markwalet/laravel-changelog/compare/v1.6.0...v1.6.1)
1011

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ After installation, verify and change the config for your specific needs and run
2828

2929
## Usage
3030

31-
The main functionality of this package consists of 5 commands:
31+
The main functionality of this package consists of 6 commands:
3232

3333
- `php artisan changelog:add {--type=} {--message=}` (Add a change to the current feature entry)
3434
- `php artisan changelog:list` (Show a list of changes for all versions)
3535
- `php artisan changelog:unreleased` (Show a list of unreleased changes)
36+
- `php artisan changelog:current` (Show a list of unreleased changes for your current branch)
3637
- `php artisan changelog:release` (Move all unreleased changes to a new version)
3738
- `php artisan changelog:generate {--dry-run} {--path=}` (Generate a markdown file based on your changes. The path option can be empty)
3839

@@ -43,4 +44,4 @@ The default configuration is defined in `changelog.php`. If you want to edit thi
4344
php artisan vendor:publish --provider="MarkWalet\Changelog\ChangelogServiceProvider"
4445
```
4546

46-
When you publish these vendor assets, you can also edit the default template that is used when generating the changelog. The template file can be found in `resources/views/vendor/changelog/changelog.blade.php`.
47+
When you publish these vendor assets, you can also edit the default template that is used when generating the changelog markdown file. The template file can be found in `resources/views/vendor/changelog/changelog.blade.php`.

src/ChangelogServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MarkWalet\Changelog\Adapters\XmlFeatureAdapter;
1010
use MarkWalet\Changelog\Adapters\XmlReleaseAdapter;
1111
use MarkWalet\Changelog\Commands\ChangelogAddCommand;
12+
use MarkWalet\Changelog\Commands\ChangelogCurrentCommand;
1213
use MarkWalet\Changelog\Commands\ChangelogGenerateCommand;
1314
use MarkWalet\Changelog\Commands\ChangelogInstallCommand;
1415
use MarkWalet\Changelog\Commands\ChangelogListCommand;
@@ -71,6 +72,7 @@ private function bootCommands(): void
7172
if ($this->app->runningInConsole()) {
7273
$this->commands([
7374
ChangelogInstallCommand::class,
75+
ChangelogCurrentCommand::class,
7476
ChangelogAddCommand::class,
7577
ChangelogListCommand::class,
7678
ChangelogUnreleasedCommand::class,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use MarkWalet\Changelog\Adapters\FeatureAdapter;
7+
use MarkWalet\GitState\Drivers\GitDriver;
8+
9+
class ChangelogCurrentCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'changelog:current';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Show a list of unreleased changes for the current branch.';
24+
25+
/**
26+
* Execute the command.
27+
*
28+
* @param FeatureAdapter $adapter
29+
* @param GitDriver $gitState
30+
*/
31+
public function handle(FeatureAdapter $adapter, GitDriver $gitState)
32+
{
33+
$branch = $gitState->currentBranch();
34+
$path = config('changelog.path').DIRECTORY_SEPARATOR.'unreleased'.DIRECTORY_SEPARATOR.$branch.'.xml';
35+
36+
if (! $adapter->exists($path)) {
37+
$this->warn("No changes found for $branch");
38+
39+
return;
40+
}
41+
$feature = $adapter->read($path);
42+
$this->line("Changes for $branch:");
43+
44+
foreach ($feature->changes() as $change) {
45+
$type = ucfirst($change->type());
46+
$this->line(" - $type: {$change->message()}");
47+
}
48+
}
49+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Tests\Commands;
4+
5+
use MarkWalet\Changelog\Adapters\FeatureAdapter;
6+
use MarkWalet\Changelog\Change;
7+
use MarkWalet\Changelog\Feature;
8+
use MarkWalet\Changelog\Tests\LaravelTestCase;
9+
use MarkWalet\GitState\Drivers\GitDriver;
10+
use Mockery\MockInterface;
11+
12+
class ChangelogCurrentCommandTest extends LaravelTestCase
13+
{
14+
/**
15+
* Setup the test environment.
16+
*
17+
* @return void
18+
*/
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
config()->set('changelog.path', 'fake-path');
24+
$this->mock(GitDriver::class, function (MockInterface $git) {
25+
$git->expects('currentBranch')->once()->andReturn('test-branch');
26+
});
27+
}
28+
29+
/** @test */
30+
public function it_can_list_all_changes_for_all_releases()
31+
{
32+
$this->mock(FeatureAdapter::class, function (MockInterface $adapter) {
33+
$adapter->allows('exists')->andReturnUsing(function (string $branch) {
34+
return $branch === 'fake-path/unreleased/test-branch.xml';
35+
});
36+
37+
$adapter->allows('read')->andReturn(tap(new Feature(), function (Feature $feature) {
38+
$feature->add(new Change('added', 'Added a new feature.'));
39+
$feature->add(new Change('fixed', 'Fixed a bug.'));
40+
}));
41+
});
42+
43+
$this->artisan('changelog:current')
44+
->expectsOutput('Changes for test-branch:')
45+
->expectsOutput(' - Added: Added a new feature.')
46+
->expectsOutput(' - Fixed: Fixed a bug.');
47+
}
48+
49+
/** @test */
50+
public function it_shows_a_warning_when_there_are_no_unreleased_changes_found_yet(): void
51+
{
52+
config()->set('changelog.path', 'fake-path');
53+
$this->mock(FeatureAdapter::class, function (MockInterface $adapter) {
54+
$adapter->allows('exists')->andReturn(false);
55+
});
56+
57+
$this->artisan('changelog:current')
58+
->expectsOutput('No changes found for test-branch');
59+
}
60+
}

0 commit comments

Comments
 (0)