Skip to content

Commit

Permalink
add possibility to execute array of commands, add silent mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Gaal committed Aug 10, 2022
1 parent df29f85 commit d66f400
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
7 changes: 6 additions & 1 deletion aliases.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"update": "composer update"
"about": [
"echo 'A PHP CLI application that helps you organize your aliases.'"
],
"update": [
"composer update"
]
}
25 changes: 18 additions & 7 deletions app/Commands/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ExecuteCommand extends Command
{
protected $signature = 'execute {alias?}';
protected $signature = 'execute {alias?} {-q|--quiet}';

protected $description = 'Executes an alias.';

Expand All @@ -17,21 +17,32 @@ class ExecuteCommand extends Command
public function handle()
{
$alias = $this->argument('alias');
$quiet = $this->option('quiet') ?? false;

try {
$this->getProjectAliases();

if ($alias && ! in_array($alias, array_keys($this->aliases))) {
if ($alias && !in_array($alias, array_keys($this->aliases))) {
$this->components->error("Alias $alias not found.");
}

if (! $alias) {
$alias = $this->choice('Please select alias to execute', $this->aliases);
if (!$alias) {
$alias = $this->choice('Please select alias to execute', array_keys($this->aliases));
}

$this->components->info("Executing: $alias");

shell_exec($this->aliases[$alias]);
if (is_array($this->aliases[$alias]) === false) {
$this->aliases[$alias] = [$this->aliases[$alias]];
}

foreach ($this->aliases[$alias] as $task) {
if ($quiet) {
@shell_exec($task);
} else {
shell_exec($task);
}
}

$this->newLine();
$this->components->info('Finished.');
Expand All @@ -43,8 +54,8 @@ public function handle()
protected function getProjectAliases(): array
{
$file = match (true) {
File::exists($path = getcwd().'/aliases.dev.json') => File::get($path),
File::exists($path = getcwd().'/aliases.json') => File::get($path),
File::exists($path = getcwd() . '/aliases.dev.json') => File::get($path),
File::exists($path = getcwd() . '/aliases.json') => File::get($path),
default => throw new FileNotFoundException('aliases.json or aliases.dev.json not found.')
};

Expand Down
Binary file modified builds/alias
Binary file not shown.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "gipfel-dev/alias",
"description": "A PHP CLI application that helps you organize your aliases.",
"version": "0.0.1",
"keywords": ["framework", "laravel", "laravel zero", "console", "cli"],
"homepage": "https://laravel-zero.com",
"type": "project",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/Feature/ExecuteCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

test('execute command test will show aliases without argument', function () {
$output = $this->artisan('execute');

$output
->expectsChoice('Please select alias to execute', 'about', ['about'])
->assertExitCode(0);
});

test('execute about will description', function () {
$output = $this->artisan('execute about');

/** @var \Illuminate\Testing\PendingCommand $output */
$output
->expectsOutputToContain('Executing: about.')
->expectsOutputToContain('A PHP CLI application that helps you organize your aliases.')
->assertExitCode(0);
});

0 comments on commit d66f400

Please sign in to comment.