Skip to content

Commit 8c59f12

Browse files
joetannenbaumshaedrichtaylorotwellStyleCIBot
authored
Add artisan dev command (#60412)
* scaffolded * default command, except, only * clear * Update DevCommands.php * first real pass at vendor prevention * Update DevCommands.php * Update DevCommands.php * doc blocks * Update DevCommands.php * small fixes * fixes * better method names * Update NodePackageManager.php * tests * Update DevCommand.php * pint * Update FoundationDevCommandsTest.php * Update SupportNodePackageManagerTest.php * colors -> enum * Update DevCommand.php * strstr instead of collect * Update src/Illuminate/Foundation/DevCommand.php Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> * Update src/Illuminate/Foundation/DevCommands.php Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> * use new color enum * Update DevCommand.php * Update DevCommand.php * formatting * Apply fixes from StyleCI * fix columns --------- Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> Co-authored-by: Taylor Otwell <taylor@laravel.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent a1cb21e commit 8c59f12

14 files changed

Lines changed: 1430 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Console\Prohibitable;
7+
use Illuminate\Foundation\DevCommands;
8+
use Illuminate\Support\NodePackageManager;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
11+
use function Termwind\terminal;
12+
13+
#[AsCommand(name: 'dev')]
14+
class DevCommand extends Command
15+
{
16+
use Prohibitable;
17+
18+
/**
19+
* The console command name.
20+
*
21+
* @var string
22+
*/
23+
protected $name = 'dev';
24+
25+
/**
26+
* The console command description.
27+
*
28+
* @var string
29+
*/
30+
protected $description = 'Run the dev processes';
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return int
36+
*
37+
* @throws \Exception
38+
*/
39+
public function handle(NodePackageManager $packageManager)
40+
{
41+
if ($this->isProhibited()) {
42+
return self::FAILURE;
43+
}
44+
45+
$devCommands = DevCommands::commands();
46+
47+
$commands = array_column($devCommands, 'command');
48+
$colors = array_column($devCommands, 'color');
49+
$names = array_column($devCommands, 'name');
50+
51+
$longestName = max(array_map(strlen(...), $names));
52+
53+
$columns = getenv('COLUMNS');
54+
55+
putenv('COLUMNS='.max(terminal()->width() - $longestName - 4, 1));
56+
57+
$this->line('');
58+
59+
foreach ($devCommands as $devCommand) {
60+
$this->line(
61+
sprintf(
62+
'<fg=%s>[%s]</>%s<fg=#888888>%s</>',
63+
$devCommand['color'],
64+
$devCommand['name'],
65+
str_repeat(' ', ($longestName - strlen($devCommand['name'])) + 1),
66+
$devCommand['command'],
67+
),
68+
);
69+
}
70+
71+
$this->line('');
72+
73+
$command = $packageManager->getExecCommand(sprintf(
74+
'concurrently -c "%s" "%s" --names=%s --kill-others',
75+
implode(',', $colors),
76+
implode('" "', $commands),
77+
implode(',', $names)
78+
));
79+
80+
if (extension_loaded('pcntl')) {
81+
pcntl_exec('/usr/bin/env', ['sh', '-c', $command]);
82+
}
83+
84+
passthru($command, $exitCode);
85+
86+
$columns === false ? putenv('COLUMNS') : putenv("COLUMNS={$columns}");
87+
88+
return $exitCode;
89+
}
90+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation;
4+
5+
class DevCommand
6+
{
7+
/**
8+
* Color of the command when output to the console.
9+
*
10+
* @var string|null
11+
*/
12+
protected ?string $color = null;
13+
14+
/**
15+
* Create a new DevCommand instance.
16+
*
17+
* @param string $command
18+
* @param string|null $name
19+
* @return void
20+
*/
21+
public function __construct(protected string $command, protected ?string $name = null)
22+
{
23+
$this->name ??= strstr($command, ' ', true);
24+
}
25+
26+
/**
27+
* Get the command name.
28+
*
29+
* @return string
30+
*/
31+
public function name(): string
32+
{
33+
return $this->name;
34+
}
35+
36+
/**
37+
* Set the command color.
38+
*
39+
* @param string $color
40+
* @return self
41+
*/
42+
public function color(string $color): self
43+
{
44+
$this->color = $color;
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Set the command color to blue.
51+
*
52+
* @return self
53+
*/
54+
public function blue(): self
55+
{
56+
return $this->color(DevCommandColor::BLUE->value);
57+
}
58+
59+
/**
60+
* Set the command color to purple.
61+
*
62+
* @return self
63+
*/
64+
public function purple(): self
65+
{
66+
return $this->color(DevCommandColor::PURPLE->value);
67+
}
68+
69+
/**
70+
* Set the command color to pink.
71+
*
72+
* @return self
73+
*/
74+
public function pink(): self
75+
{
76+
return $this->color(DevCommandColor::PINK->value);
77+
}
78+
79+
/**
80+
* Set the command color to orange.
81+
*
82+
* @return self
83+
*/
84+
public function orange(): self
85+
{
86+
return $this->color(DevCommandColor::ORANGE->value);
87+
}
88+
89+
/**
90+
* Set the command color to green.
91+
*
92+
* @return self
93+
*/
94+
public function green(): self
95+
{
96+
return $this->color(DevCommandColor::GREEN->value);
97+
}
98+
99+
/**
100+
* Set the command color to yellow.
101+
*
102+
* @return self
103+
*/
104+
public function yellow(): self
105+
{
106+
return $this->color(DevCommandColor::YELLOW->value);
107+
}
108+
109+
/**
110+
* Get the command as an array.
111+
*
112+
* @return array{command: string, name: string, color: string|null}
113+
*/
114+
public function toArray(): array
115+
{
116+
return [
117+
'command' => $this->command,
118+
'name' => $this->name,
119+
'color' => $this->color,
120+
];
121+
}
122+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation;
4+
5+
enum DevCommandColor: string
6+
{
7+
case BLUE = '#93c5fd';
8+
case PURPLE = '#c4b5fd';
9+
case PINK = '#fb7185';
10+
case ORANGE = '#fdba74';
11+
case GREEN = '#86efac';
12+
case YELLOW = '#fcd34d';
13+
}

0 commit comments

Comments
 (0)