Skip to content

Commit 65985fb

Browse files
authored
Keep unreleased folder (#38)
* Add a gitkeep file to the test data folder * Keep the .gitkeep file when releasing a new version * Add a changelog:install command * Update changelog and documentation
1 parent 1e814d8 commit 65985fb

File tree

9 files changed

+130
-22
lines changed

9 files changed

+130
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased](https://github.com/markwalet/laravel-changelog/compare/v1.6.1...master)
44

5+
## Added
6+
- Make sure the unreleased folder stays in the filesystem for better diff comparisons ([#4](https://github.com/markwalet/laravel-changelog/issues/4))
7+
- Add a `changelog:install` artisan command.
8+
59
## [v1.6.1 (2022-03-27)](https://github.com/markwalet/laravel-changelog/compare/v1.6.0...v1.6.1)
610

711
### Fixed
@@ -10,7 +14,7 @@
1014
## [v1.6.0 (2021-12-30)](https://github.com/markwalet/laravel-changelog/compare/v1.5.0...v1.6.0)
1115

1216
### Added
13-
- Added Laravel 9 support.
17+
- Added Laravel 9 support
1418

1519
## [v1.5.0 (2021-12-30)](https://github.com/markwalet/laravel-changelog/compare/v1.4.1...v1.5.0)
1620

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Laravel >=5.5 uses Package auto-discovery, so you don't have to register the ser
2424
MarkWalet\Changelog\ChangelogServiceProvider::class
2525
```
2626

27-
After installation, you should create a folder where all the changes will be stored in. This defaults to `base_path('.changes')`.
27+
After installation, verify and change the config for your specific needs and run `php artisan changelog:install`. This creates a folder where all the changes will be stored in. This defaults to `base_path('.changes')`.
2828

2929
## Usage
3030

src/Adapters/XmlReleaseAdapter.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MarkWalet\Changelog\Adapters;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Facades\File;
67
use MarkWalet\Changelog\Concerns\CanSortReleases;
78
use MarkWalet\Changelog\Exceptions\DirectoryNotFoundException;
@@ -24,7 +25,7 @@ class XmlReleaseAdapter implements ReleaseAdapter
2425
*/
2526
public function __construct()
2627
{
27-
$this->featureAdapter = new XmlFeatureAdapter;
28+
$this->featureAdapter = new XmlFeatureAdapter();
2829
}
2930

3031
/**
@@ -36,14 +37,11 @@ public function __construct()
3637
*/
3738
public function read(string $path, string $version): Release
3839
{
39-
$fullPath = realpath($path.DIRECTORY_SEPARATOR.$version);
40-
4140
if ($this->exists($path, $version) === false) {
42-
throw new FileNotFoundException($fullPath);
41+
throw new FileNotFoundException(realpath($path.DIRECTORY_SEPARATOR.$version));
4342
}
4443

45-
$files = collect(File::allFiles($fullPath))
46-
->filter(fn (SplFileInfo $file) => $file->getExtension() === 'xml')
44+
$files = $this->filesForRelease($path, $version)
4745
->map(fn (SplFileInfo $file) => $file->getPathname());
4846

4947
$release = new Release($version);
@@ -63,15 +61,23 @@ public function read(string $path, string $version): Release
6361
*/
6462
public function release(string $path, string $version): void
6563
{
66-
$old = $path.DIRECTORY_SEPARATOR.'unreleased';
67-
$new = $path.DIRECTORY_SEPARATOR.$version;
68-
6964
// Prevent release if the given version already exists.
7065
if ($this->exists($path, $version)) {
7166
throw new VersionAlreadyExistsException($version);
7267
}
7368

74-
rename($old, $new);
69+
$old = $path.DIRECTORY_SEPARATOR.'unreleased';
70+
$new = $path.DIRECTORY_SEPARATOR.$version;
71+
72+
File::makeDirectory($new);
73+
collect(File::glob($old.DIRECTORY_SEPARATOR.'**'))
74+
->map(fn (string $path) => substr($path, strlen($old)))
75+
->each(function (string $file) use ($old, $new) {
76+
$source = $old.$file;
77+
$target = $new.$file;
78+
79+
File::move($source, $target);
80+
});
7581
}
7682

7783
/**
@@ -113,4 +119,17 @@ public function exists(string $path, string $version): bool
113119

114120
return file_exists($fullPath) && is_dir($fullPath);
115121
}
122+
123+
/**
124+
* Get all xml files for a given release.
125+
*
126+
* @param string $path
127+
* @param string $version
128+
* @return Collection<SplFileInfo>
129+
*/
130+
private function filesForRelease(string $path, string $version): Collection
131+
{
132+
return collect(File::allFiles($path.DIRECTORY_SEPARATOR.$version))
133+
->filter(fn (SplFileInfo $file) => $file->getExtension() === 'xml');
134+
}
116135
}

src/ChangelogServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use MarkWalet\Changelog\Adapters\XmlReleaseAdapter;
1111
use MarkWalet\Changelog\Commands\ChangelogAddCommand;
1212
use MarkWalet\Changelog\Commands\ChangelogGenerateCommand;
13+
use MarkWalet\Changelog\Commands\ChangelogInstallCommand;
1314
use MarkWalet\Changelog\Commands\ChangelogListCommand;
1415
use MarkWalet\Changelog\Commands\ChangelogReleaseCommand;
1516
use MarkWalet\Changelog\Commands\ChangelogUnreleasedCommand;
@@ -69,6 +70,7 @@ private function bootCommands(): void
6970
{
7071
if ($this->app->runningInConsole()) {
7172
$this->commands([
73+
ChangelogInstallCommand::class,
7274
ChangelogAddCommand::class,
7375
ChangelogListCommand::class,
7476
ChangelogUnreleasedCommand::class,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
8+
class ChangelogInstallCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'changelog:install';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a folder for the changelog files';
23+
24+
/**
25+
* Execute the command.
26+
*/
27+
public function handle()
28+
{
29+
$path = $this->folder().DIRECTORY_SEPARATOR.'.gitkeep';
30+
31+
if (File::exists($path)) {
32+
$this->warn('.gitkeep file already exists.');
33+
34+
return;
35+
}
36+
37+
File::makeDirectory($this->folder(), 0755, true);
38+
File::put($this->folder().DIRECTORY_SEPARATOR.'.gitkeep', '');
39+
40+
$this->info("Created $path");
41+
}
42+
43+
/**
44+
* Get the folder where the gitkeep file should be written to.
45+
*
46+
* @return string
47+
*/
48+
public function folder(): string
49+
{
50+
return config('changelog.path').DIRECTORY_SEPARATOR.'unreleased';
51+
}
52+
}

tests/Adapters/XmlReleaseAdapterTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class XmlReleaseAdapterTest extends LaravelTestCase
1616
*/
1717
public function adapter(): XmlReleaseAdapter
1818
{
19-
return new XmlReleaseAdapter;
19+
return new XmlReleaseAdapter();
2020
}
2121

2222
/** @test */
@@ -25,17 +25,20 @@ public function it_can_execute_a_release()
2525
$adapter = $this->adapter();
2626
$path = __DIR__.'/../test-data/release-write-test';
2727

28-
$unreleasedBefore = $adapter->exists($path, 'unreleased');
29-
$versionBefore = $adapter->exists($path, 'v1.0.2');
28+
$this->assertFileExists($path.'/unreleased/.gitkeep');
29+
$this->assertFileExists($path.'/unreleased/nested/feature-1.xml');
30+
$this->assertFileExists($path.'/unreleased/feature-2.xml');
31+
$this->assertFileDoesNotExist($path.'/v1.0.2');
32+
3033
$adapter->release($path, 'v1.0.2');
31-
$unreleasedAfter = $adapter->exists($path, 'unreleased');
32-
$versionAfter = $adapter->exists($path, 'v1.0.2');
3334

34-
$this->assertTrue($unreleasedBefore);
35-
$this->assertFalse($versionBefore);
36-
$this->assertFalse($unreleasedAfter);
37-
$this->assertTrue($versionAfter);
35+
$this->assertFileExists($path.'/unreleased/.gitkeep');
36+
$this->assertFileExists($path.'/v1.0.2/nested/feature-1.xml');
37+
$this->assertFileExists($path.'/v1.0.2/feature-2.xml');
38+
$this->assertFileDoesNotExist($path.'/unreleased/nested');
3839

39-
rename(__DIR__.'/../test-data/release-write-test/v1.0.2', __DIR__.'/../test-data/release-write-test/unreleased');
40+
rename(__DIR__.'/../test-data/release-write-test/v1.0.2/nested', __DIR__.'/../test-data/release-write-test/unreleased/nested');
41+
rename(__DIR__.'/../test-data/release-write-test/v1.0.2/feature-2.xml', __DIR__.'/../test-data/release-write-test/unreleased/feature-2.xml');
42+
rmdir(__DIR__.'/../test-data/release-write-test/v1.0.2');
4043
}
4144
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Tests\Commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use MarkWalet\Changelog\Tests\LaravelTestCase;
7+
8+
class ChangelogInstallCommandTest extends LaravelTestCase
9+
{
10+
/** @test */
11+
public function it_creates_a_gitkeep_file_in_the_configured_folder_once()
12+
{
13+
$this->app['config']['changelog.path'] = __DIR__.'/.changes';
14+
15+
$this->artisan('changelog:install');
16+
17+
$this->assertFileExists(__DIR__.'/.changes/unreleased/.gitkeep');
18+
19+
$this->artisan('changelog:install');
20+
21+
$this->assertFileExists(__DIR__.'/.changes/unreleased/.gitkeep');
22+
23+
File::deleteDirectory(__DIR__.'/.changes');
24+
}
25+
}

tests/test-data/release-write-test/unreleased/.gitkeep

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<feature>
3+
</feature>

0 commit comments

Comments
 (0)