Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to prepend command in hook #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/FileJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ protected function getSchemaValidation(): ?\stdClass
'properties' => [],
'additionalProperties' => false,
],
'prepend' => [
'type' => 'string',
],
],
'required' => ['hooks'],
'additionalProperties' => false,
Expand Down
25 changes: 20 additions & 5 deletions app/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function uninstall(): bool

$contents = File::get($path);
$commands = $this->getSnippets()
->map(fn (string $snippet): string => $snippet.PHP_EOL)
->map(fn (string $snippet): array => [$this->getPrepend().$snippet.PHP_EOL, $snippet.PHP_EOL])
->collapse()
->unique()
->values()
->toArray();

if (! Str::contains($contents, $commands)) {
Expand Down Expand Up @@ -113,7 +116,7 @@ public function install(): void
{
File::append(
Platform::cwd(".git/hooks/{$this->hook}"),
$this->getSnippets()->first().PHP_EOL,
$this->getPrepend().$this->getSnippets()->first().PHP_EOL,
);
}

Expand All @@ -130,6 +133,17 @@ public function getScripts(): Collection
return collect(Whisky::readConfig("hooks.{$this->hook}"));
}

public function getPrepend(): string
{
$prepend = Whisky::readConfig('prepend');

if (empty($prepend)) {
return '';
}

return trim($prepend, ' ').' ';
}

/**
* Collect the bash snippet history for calling the Whisky bin.
* The current version of the snippet should always be first.
Expand Down Expand Up @@ -172,9 +186,10 @@ public static function all(int $flags = self::FROM_CONFIG): Collection
$result = collect();

if ($flags & self::FROM_GIT) {
$result->push(...collect(File::files(Platform::cwd('.git/hooks')))
->map(fn (SplFileInfo $file) => $file->getFilename())
->filter(fn (string $filename) => ! str_ends_with($filename, 'sample'))
$result->push(
...collect(File::files(Platform::cwd('.git/hooks')))
->map(fn (SplFileInfo $file) => $file->getFilename())
->filter(fn (string $filename) => ! str_ends_with($filename, 'sample'))
);
}

Expand Down
3 changes: 2 additions & 1 deletion stubs/whisky.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"pre-push": [
"composer test"
]
}
},
"prepend": ""
}
1 change: 1 addition & 0 deletions tests/Unit/ValidationFileJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'{ "hooks": { } }',
'{ "hooks": { "pre-commit": [ "composer lint -- --test" ] } }',
'{ "disabled": [ "pre-commit" ], "hooks": { "pre-commit": [ "composer lint -- --test" ] } }',
'{ "prepend": "vendor/bin/sail php", "disabled": [ "pre-commit" ], "hooks": { "pre-commit": [ "composer lint -- --test" ] } }',
]);

it('accepts valid json with invalid schema when validation skipped', function ($test_json) {
Expand Down