Skip to content

Commit d51a905

Browse files
[ Feat ] Use TTY mode on Process Facade (#72)
* Extract snippets into getter function * new runner using tty mode on process facade * add new snippet to use new run command, make default * Update deprecated GetRunCmd to automatically update hook snippets * add fallback to support non-tty interfaces * remove unnecessary shell script * remove unused 'scripts' command * remove empty test file * temporarily skip test * update assignment operator * refactor skipped test
1 parent ef39f3f commit d51a905

File tree

7 files changed

+101
-93
lines changed

7 files changed

+101
-93
lines changed

app/Commands/GetRunCmd.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,30 @@
22

33
namespace ProjektGopher\Whisky\Commands;
44

5-
use Illuminate\Support\Facades\File;
65
use LaravelZero\Framework\Commands\Command;
7-
use ProjektGopher\Whisky\Platform;
86
use ProjektGopher\Whisky\Whisky;
97

108
/**
11-
* This command is basically only needed to build the execution path
12-
* to the `run` bash script, or to skip the hooks once.
9+
* This command was only used to construct the execution path
10+
* to the `run-hook` bash script, which is now deprecated.
11+
* Now we can automatically update the hooks snippets.
1312
*/
1413
class GetRunCmd extends Command
1514
{
1615
protected $signature = 'get-run-cmd {hook}';
1716

1817
protected $description = 'Get the bash command to run a given hook';
1918

20-
public function handle(): int
19+
public function handle(): void
2120
{
22-
if (File::exists(Platform::cwd('.git/hooks/skip-once'))) {
23-
File::delete(Platform::cwd('.git/hooks/skip-once'));
24-
25-
return Command::SUCCESS;
26-
}
27-
28-
// Check if the hook is disabled in whisky.json
29-
if (in_array($this->argument('hook'), Whisky::readConfig('disabled'))) {
30-
return Command::SUCCESS;
31-
}
32-
3321
$bin = Whisky::bin_path();
34-
$this->line(Whisky::base_path("bin/run-hook {$this->argument('hook')} {$bin}"));
3522

36-
return Command::SUCCESS;
23+
$commands = collect([
24+
"echo 'The snippet in your hook is deprecated. Updating...'",
25+
"{$bin} update",
26+
"{$bin} run {$this->argument('hook')}",
27+
]);
28+
29+
$this->line($commands->implode(PHP_EOL));
3730
}
3831
}

app/Commands/Run.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ProjektGopher\Whisky\Commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Facades\Process;
7+
use LaravelZero\Framework\Commands\Command;
8+
use ProjektGopher\Whisky\Hook;
9+
use ProjektGopher\Whisky\Platform;
10+
use ProjektGopher\Whisky\Whisky;
11+
use Symfony\Component\Process\Process as SymfonyProcess;
12+
13+
class Run extends Command
14+
{
15+
protected $signature = 'run {hook}';
16+
17+
protected $description = 'Run the scripts for a given hook';
18+
19+
public function handle(): int
20+
{
21+
if (File::missing(Platform::cwd('whisky.json'))) {
22+
$this->error('Whisky has not been initialized in this project, aborting...');
23+
$this->line('Run `./vendor/bin/whisky install` to initialize Whisky in this project.');
24+
25+
return Command::FAILURE;
26+
}
27+
28+
if (File::exists(Platform::cwd('.git/hooks/skip-once'))) {
29+
File::delete(Platform::cwd('.git/hooks/skip-once'));
30+
31+
return Command::SUCCESS;
32+
}
33+
34+
// Check if the hook is disabled in whisky.json
35+
if (in_array($this->argument('hook'), Whisky::readConfig('disabled'))) {
36+
return Command::SUCCESS;
37+
}
38+
39+
$exitCode = Command::SUCCESS;
40+
41+
Hook::make($this->argument('hook'))
42+
->getScripts()
43+
->each(function (string $script) use (&$exitCode): void {
44+
$isTtySupported = SymfonyProcess::isTtySupported();
45+
46+
$result = $isTtySupported
47+
? Process::forever()->tty()->run($script)
48+
: Process::timeout(300)->run($script);
49+
50+
if ($result->failed() && ! $isTtySupported) {
51+
$this->line($result->errorOutput());
52+
$this->line($result->output());
53+
}
54+
55+
$exitCode |= $result->exitCode();
56+
});
57+
58+
return $exitCode;
59+
}
60+
}

app/Commands/Scripts.php

-31
This file was deleted.

app/Hook.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ public function uninstall(): bool
3737
}
3838

3939
$contents = File::get($path);
40-
$commands = [
41-
"eval \"$({$this->bin} get-run-cmd {$this->hook})\"".PHP_EOL,
42-
// TODO: legacy - handle upgrade somehow
43-
"eval \"$(./vendor/bin/whisky get-run-cmd {$this->hook})\"".PHP_EOL,
44-
];
40+
$commands = $this->getSnippets()
41+
->map(fn (string $snippet): string => $snippet.PHP_EOL)
42+
->toArray();
4543

4644
if (! Str::contains($contents, $commands)) {
4745
return false;
@@ -94,11 +92,7 @@ public function isInstalled(): bool
9492
{
9593
return Str::contains(
9694
File::get(Platform::cwd(".git/hooks/{$this->hook}")),
97-
[
98-
"eval \"$({$this->bin} get-run-cmd {$this->hook})\"",
99-
// TODO: legacy - handle upgrade somehow
100-
"eval \"$(./vendor/bin/whisky get-run-cmd {$this->hook})\"",
101-
],
95+
$this->getSnippets()->toArray(),
10296
);
10397
}
10498

@@ -109,7 +103,7 @@ public function install(): void
109103
{
110104
File::append(
111105
Platform::cwd(".git/hooks/{$this->hook}"),
112-
"eval \"$({$this->bin} get-run-cmd {$this->hook})\"".PHP_EOL,
106+
$this->getSnippets()->first().PHP_EOL,
113107
);
114108
}
115109

@@ -126,6 +120,23 @@ public function getScripts(): Collection
126120
return collect(Whisky::readConfig("hooks.{$this->hook}"));
127121
}
128122

123+
/**
124+
* Collect the bash snippet history for calling the Whisky bin.
125+
* The current version of the snippet should always be first.
126+
* We keep this history to make updating our hooks easier.
127+
*
128+
* @return Collection<int, string>
129+
*/
130+
public function getSnippets(): Collection
131+
{
132+
return collect([
133+
"{$this->bin} run {$this->hook}",
134+
// Legacy Snippets.
135+
"eval \"$({$this->bin} get-run-cmd {$this->hook})\"",
136+
"eval \"$(./vendor/bin/whisky get-run-cmd {$this->hook})\"",
137+
]);
138+
}
139+
129140
////////////////////////////////////////
130141
//// Static methods ////
131142
////////////////////////////////////////

bin/run-hook

-28
This file was deleted.

tests/Feature/RunTest.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
use Illuminate\Support\Facades\File;
44
use ProjektGopher\Whisky\Platform;
55

6-
it('deletes skip-once file if exists and outputs nothing', function () {
6+
it('deletes skip-once file if exists as long as whisky.json exists', function () {
7+
File::shouldReceive('missing')
8+
->once()
9+
->with(Platform::cwd('whisky.json'))
10+
->andReturnFalse();
11+
712
File::shouldReceive('exists')
813
->once()
914
->with(Platform::cwd('.git/hooks/skip-once'))
@@ -14,9 +19,7 @@
1419
->with(Platform::cwd('.git/hooks/skip-once'))
1520
->andReturnTrue();
1621

17-
$this->artisan('get-run-cmd pre-commit')
22+
$this->artisan('run pre-commit')
1823
->doesntExpectOutputToContain('run-hook')
1924
->assertExitCode(0);
20-
});
21-
22-
it('points correctly to the run-hook script');
25+
})->skip('Needs to be refactored so that the hooks don\'t actually run');

tests/Feature/ScriptsTest.php

Whitespace-only changes.

0 commit comments

Comments
 (0)