Skip to content

Commit b9f777a

Browse files
authored
Merge pull request #72 from tighten/adc/add-support-dirty
Add support for `--dirty`
2 parents c203834 + 192c682 commit b9f777a

File tree

6 files changed

+44
-30
lines changed

6 files changed

+44
-30
lines changed

app/Commands/DusterCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ protected function configure(): void
6161
mode: InputOption::VALUE_REQUIRED,
6262
description: 'Lint/Fix using specified (comma separated) tools: tlint,phpcodesniffer,phpcsfixer,pint',
6363
),
64+
new InputOption(
65+
name: 'dirty',
66+
mode: InputOption::VALUE_NONE,
67+
description: 'Only fix files that have uncommitted changes'
68+
),
6469
new InputOption(
6570
name: 'github-actions',
6671
shortcut: 'g',

app/Providers/DusterServiceProvider.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function register()
2222
$this->app->singleton(DusterConfig::class, function () {
2323
$input = $this->app->get(InputInterface::class);
2424

25-
$dusterConfig = $this->getDusterConfig();
25+
$dusterConfig = DusterConfig::all();
2626

2727
return new DusterConfig([
28-
'paths' => $input->getArgument('path'),
28+
'paths' => Project::paths($input),
2929
'lint' => $input->getOption('lint'),
3030
'fix' => $input->getOption('fix'),
3131
'using' => $input->getOption('using'),
@@ -42,7 +42,7 @@ public function register()
4242

4343
$using = $input->getOption('using')
4444
? explode(',', $input->getOption('using'))
45-
: ['tlint', 'phpcs', 'php-cs-fixer', 'pint', ...array_keys($this->getDusterConfig()['scripts'][$mode] ?? [])];
45+
: ['tlint', 'phpcs', 'php-cs-fixer', 'pint', ...array_keys(DusterConfig::all()['scripts'][$mode] ?? [])];
4646

4747
$tools = collect($using)
4848
->map(fn ($using): Tool => match (trim($using)) {
@@ -65,25 +65,9 @@ public function register()
6565
});
6666
}
6767

68-
/**
69-
* @return array<string, mixed>
70-
*/
71-
private function getDusterConfig(): array
72-
{
73-
if (file_exists(Project::path() . '/duster.json')) {
74-
return tap(json_decode(file_get_contents(Project::path() . '/duster.json'), true, 512, JSON_THROW_ON_ERROR), function ($configuration) {
75-
if (! is_array($configuration)) {
76-
abort(1, 'The configuration file duster.json is not valid JSON.');
77-
}
78-
});
79-
}
80-
81-
return [];
82-
}
83-
8468
private function userScript(string $mode, string $scriptName): ?UserScript
8569
{
86-
$userScript = $this->getDusterConfig()['scripts'][$mode][$scriptName] ?? null;
70+
$userScript = DusterConfig::all()['scripts'][$mode][$scriptName] ?? null;
8771

8872
return $userScript
8973
? new UserScript($scriptName, $userScript, resolve(DusterConfig::class))

app/Providers/PintServiceProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use App\Actions\ElaborateSummary;
66
use App\Actions\FixCode;
77
use App\Commands\DefaultCommand;
8+
use App\Contracts\PathsRepository;
89
use App\Contracts\PintInputInterface;
910
use App\Output\ProgressOutput;
1011
use App\Output\SummaryOutput;
1112
use App\Project;
1213
use App\Repositories\ConfigurationJsonRepository;
14+
use App\Repositories\GitPathsRepository;
1315
use App\Repositories\PintConfigurationJsonRepository;
1416
use App\Support\DusterConfig;
1517
use Illuminate\Support\ServiceProvider;
@@ -31,7 +33,7 @@ public function register()
3133
$input = $this->app->get(InputInterface::class);
3234

3335
return new ArrayInput(
34-
['--test' => $input->getOption('lint'), 'path' => $input->getArgument('path')],
36+
['--test' => $input->getOption('lint'), 'path' => Project::paths($input)],
3537
resolve(DefaultCommand::class)->getDefinition()
3638
);
3739
});
@@ -66,7 +68,13 @@ public function register()
6668
base_path('standards/pint.json'),
6769
])->first(fn ($path) => file_exists($path));
6870

69-
return new PintConfigurationJsonRepository($config, null, resolve(DusterConfig::class));
71+
return new PintConfigurationJsonRepository($config, null, DusterConfig::all()['exclude'] ?? []);
72+
});
73+
74+
$this->app->singleton(PathsRepository::class, function () {
75+
return new GitPathsRepository(
76+
Project::path(),
77+
);
7078
});
7179
}
7280
}

app/Repositories/PintConfigurationJsonRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Repositories;
44

5-
use App\Support\DusterConfig;
6-
75
class PintConfigurationJsonRepository extends ConfigurationJsonRepository
86
{
7+
/**
8+
* @param array<string, array<int, string>|string> $exclude
9+
*/
910
public function __construct(
1011
protected $path,
1112
protected $preset,
12-
protected DusterConfig $dusterConfig)
13+
protected array $exclude)
1314
{
1415
}
1516

@@ -20,10 +21,9 @@ protected function get(): array
2021
{
2122
$config = $this->getPintConfig();
2223

23-
collect($this->dusterConfig->get('exclude', []))
24-
->each(function ($path) use (&$config) {
25-
$config = $this->addPathToConfig($path, $config);
26-
});
24+
collect($this->exclude)->each(function ($path) use (&$config) {
25+
$config = $this->addPathToConfig($path, $config);
26+
});
2727

2828
return $config;
2929
}

app/Support/DusterConfig.php

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

33
namespace App\Support;
44

5+
use App\Project;
56
use Illuminate\Support\Arr;
67

78
class DusterConfig
@@ -27,6 +28,22 @@ public function __construct(
2728
);
2829
}
2930

31+
/**
32+
* @return array<string, mixed>
33+
*/
34+
public static function all(): array
35+
{
36+
if (file_exists(Project::path() . '/duster.json')) {
37+
return tap(json_decode(file_get_contents(Project::path() . '/duster.json'), true, 512, JSON_THROW_ON_ERROR), function ($configuration) {
38+
if (! is_array($configuration)) {
39+
abort(1, 'The configuration file duster.json is not valid JSON.');
40+
}
41+
});
42+
}
43+
44+
return [];
45+
}
46+
3047
public function get(string $key, mixed $default = null): mixed
3148
{
3249
return Arr::get($this->config, $key, $default);

app/Support/PhpCodeSniffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ private function process(string $tool, array $params = []): int
4949
'--standard=' . $this->getConfigFile(),
5050
...$ignore,
5151
...$params,
52-
...$this->dusterConfig->get('include', []),
5352
];
5453

5554
$this->resetConfig($tool);
@@ -122,6 +121,7 @@ private function getDefaultDirectories(): array
122121
Project::path() . '/resources',
123122
Project::path() . '/routes',
124123
Project::path() . '/tests',
124+
...$this->dusterConfig->get('include', []),
125125
],
126126
fn ($dir) => is_dir($dir)
127127
) ?: [Project::path()];

0 commit comments

Comments
 (0)