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

[ Task ] Derive path for binary #29

Merged
merged 1 commit into from
Jul 21, 2023
Merged
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: 2 additions & 1 deletion app/Commands/GetRunCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function handle(): int
return Command::SUCCESS;
}

$this->line(Whisky::base_path("bin/run-hook {$this->argument('hook')}"));
$bin = Whisky::bin();
$this->line(Whisky::base_path("bin/run-hook {$this->argument('hook')} {$bin}"));

return Command::SUCCESS;
}
Expand Down
11 changes: 9 additions & 2 deletions app/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ private function getHooks(): Collection

private function hookIsInstalled(string $hook): bool
{
$bin = Whisky::bin();

return Str::contains(
File::get(Whisky::cwd(".git/hooks/{$hook}")),
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"",
[
"eval \"$({$bin} get-run-cmd {$hook})\"",
// TODO: legacy - handle upgrade somehow
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"",
],
);
}

Expand All @@ -87,9 +93,10 @@ private function installHook(string $hook): void
return;
}

$bin = Whisky::bin();
File::append(
Whisky::cwd(".git/hooks/{$hook}"),
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL,
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
);

if ($this->option('verbose')) {
Expand Down
12 changes: 9 additions & 3 deletions app/Commands/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProjektGopher\Whisky\Commands;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use ProjektGopher\Whisky\Whisky;

Expand All @@ -19,17 +20,22 @@ public function handle(): int
)->filter(
fn ($file) => ! str_ends_with($file->getFilename(), 'sample')
)->each(function ($file): void {
$bin = Whisky::bin();
$path = $file->getPathname();
$hook = $file->getFilename();
$contents = File::get($path);
$command = "eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL;
$commands = [
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
// TODO: legacy - handle upgrade somehow
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL,
];

if (! str_contains($contents, $command)) {
if (! Str::contains($contents, $commands)) {
return;
}

$contents = str_replace(
$command,
$commands,
'',
File::get($path),
);
Expand Down
10 changes: 7 additions & 3 deletions app/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private function uninstall(): void
$path = $file->getPathname();
$hook = $file->getFilename();
$contents = File::get($path);
$command = "eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL;
$bin = Whisky::bin();
$command = "eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL;

if (! str_contains($contents, $command)) {
return;
Expand All @@ -66,9 +67,11 @@ private function getHooks(): Collection

private function hookIsInstalled(string $hook): bool
{
$bin = Whisky::bin();

return Str::contains(
File::get(Whisky::cwd(".git/hooks/{$hook}")),
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"",
"eval \"$({$bin} get-run-cmd {$hook})\"",
);
}

Expand All @@ -89,9 +92,10 @@ private function installHook(string $hook): void
return;
}

$bin = Whisky::bin();
File::append(
Whisky::cwd(".git/hooks/{$hook}"),
"eval \"$(./vendor/bin/whisky get-run-cmd {$hook})\"".PHP_EOL,
"eval \"$({$bin} get-run-cmd {$hook})\"".PHP_EOL,
);

if ($this->option('verbose')) {
Expand Down
20 changes: 20 additions & 0 deletions app/Whisky.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ public static function cwd(string $path = ''): string
return getcwd();
}

public static function bin(): string
{
return match (true) {
self::dogfooding() => self::cwd('whisky'),
self::isRunningGlobally() => '/usr/local/bin/whisky', // TODO
default => self::cwd('vendor/bin/whisky'),
};
}

public static function dogfooding(): bool
{
return self::cwd() === self::base_path();
}

// TODO
public static function isRunningGlobally(): bool
{
return false;
}

public static function base_path(string $path = ''): string
{
return Phar::running()
Expand Down
7 changes: 5 additions & 2 deletions bin/run-hook
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
# DO NOT EDIT THIS FILE #
#########################

# Get the hook name from the first and only argument
# Get the hook name from the first argument
hook=$1

# Get the path to the Whisky binary from the second argument
bin=$2

# Get a list of scripts to run sequencially for this hook
SCRIPTS=$(./vendor/bin/whisky scripts "$hook")
SCRIPTS=$($bin scripts "$hook")

# Make newlines the only separator
IFS=$'\n'
Expand Down
Loading