-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce `--dirty` for Git projects * Fix path case * Correct docblocks * Removes extra line on .gitignore * Applies minor coding style changes * Updates description * Use `app` instead of `resolve` * Ignores excluded files * Includes new files * Removes `cut` and avoids styling deleted files * Filter deleted and renamed files * Update DefaultCommand.php * Update DefaultCommand.php Co-authored-by: Jason McCreary <jason@pureconcepts.net> Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
a93c2ed
commit f2ce5f7
Showing
13 changed files
with
211 additions
and
32 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 |
---|---|---|
|
@@ -2,4 +2,3 @@ | |
/vendor | ||
composer.lock | ||
.phpunit.result.cache | ||
|
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,13 @@ | ||
<?php | ||
|
||
namespace App\Contracts; | ||
|
||
interface PathsRepository | ||
{ | ||
/** | ||
* Determine the "dirty" files. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function dirty(); | ||
} |
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
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,49 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use App\Contracts\PathsRepository; | ||
|
||
class Project | ||
{ | ||
/** | ||
* Determine the project paths to apply the code style based on the options and arguments passed. | ||
* | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @return array<int, string> | ||
*/ | ||
public static function paths($input) | ||
{ | ||
if ($input->getOption('dirty')) { | ||
return static::resolveDirtyPaths(); | ||
} | ||
|
||
return $input->getArgument('path'); | ||
} | ||
|
||
/** | ||
* The project being analysed path. | ||
* | ||
* @return string | ||
*/ | ||
public static function path() | ||
{ | ||
return getcwd(); | ||
} | ||
|
||
/** | ||
* Resolves the dirty paths, if any. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public static function resolveDirtyPaths() | ||
{ | ||
$files = app(PathsRepository::class)->dirty(); | ||
|
||
if (empty($files)) { | ||
abort(1, 'No dirty files found.'); | ||
} | ||
|
||
return $files; | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
use App\Contracts\PathsRepository; | ||
use App\Factories\ConfigurationFactory; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Process\Process; | ||
|
||
class GitPathsRepository implements PathsRepository | ||
{ | ||
/** | ||
* The project path. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
/** | ||
* Creates a new Paths Repository instance. | ||
* | ||
* @param string $path | ||
*/ | ||
public function __construct($path) | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dirty() | ||
{ | ||
$process = tap(new Process(['git', 'status', '--short', '--', '*.php']))->run(); | ||
|
||
if (! $process->isSuccessful()) { | ||
abort(1, 'The [--dirty] option is only available when using Git.'); | ||
} | ||
|
||
$dirtyFiles = collect(preg_split('/\R+/', $process->getOutput(), flags: PREG_SPLIT_NO_EMPTY)) | ||
->mapWithKeys(fn ($file) => [substr($file, 3) => trim(substr($file, 0, 3))]) | ||
->reject(fn ($status) => $status === 'D') | ||
->map(fn ($status, $file) => $status === 'R' ? Str::after($file, ' -> ') : $file) | ||
->map(fn ($file) => $this->path.DIRECTORY_SEPARATOR.$file) | ||
->values() | ||
->all(); | ||
|
||
$files = array_values(array_map(function ($splFile) { | ||
return $splFile->getPathname(); | ||
}, iterator_to_array(ConfigurationFactory::finder() | ||
->in($this->path) | ||
->files() | ||
))); | ||
|
||
return array_values(array_intersect($files, $dirtyFiles)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
* | ||
!.gitignore |
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,57 @@ | ||
<?php | ||
|
||
use App\Contracts\PathsRepository; | ||
|
||
it('determines dirty files', function () { | ||
$paths = Mockery::mock(PathsRepository::class); | ||
|
||
$paths | ||
->shouldReceive('dirty') | ||
->once() | ||
->andReturn([ | ||
base_path('tests/Fixtures/without-issues/file.php'), | ||
]); | ||
|
||
$this->swap(PathsRepository::class, $paths); | ||
|
||
[$statusCode, $output] = run('default', ['--dirty' => true]); | ||
|
||
expect($statusCode)->toBe(0) | ||
->and($output) | ||
->toContain('── Laravel', ' 1 file'); | ||
}); | ||
|
||
it('ignores the path argument', function () { | ||
$paths = Mockery::mock(PathsRepository::class); | ||
|
||
$paths | ||
->shouldReceive('dirty') | ||
->once() | ||
->andReturn([ | ||
base_path('tests/Fixtures/without-issues/file.php'), | ||
]); | ||
|
||
$this->swap(PathsRepository::class, $paths); | ||
|
||
[$statusCode, $output] = run('default', [ | ||
'--dirty' => true, | ||
'path' => base_path(), | ||
]); | ||
|
||
expect($statusCode)->toBe(0) | ||
->and($output) | ||
->toContain('── Laravel', ' 1 file'); | ||
}); | ||
|
||
it('aborts when there are no dirty files', function () { | ||
$paths = Mockery::mock(PathsRepository::class); | ||
|
||
$paths | ||
->shouldReceive('dirty') | ||
->once() | ||
->andReturn([]); | ||
|
||
$this->swap(PathsRepository::class, $paths); | ||
|
||
run('default', ['--dirty' => true]); | ||
})->throws(Exception::class, 'No dirty files found.'); |
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