Skip to content

Commit

Permalink
Add a --commit option to automatically commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 28, 2023
1 parent 3c5b5f3 commit 34a1223
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
81 changes: 81 additions & 0 deletions app/Actions/GitCommitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Actions;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

use function Termwind\render;

class GitCommitter
{
/**
* Creates a new Git Committer instance.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function __construct(
protected $input,
protected $output,
) {
//
}

/**
* Commit the changes to Git.
*
* @param array<string, array{appliedFixers: array<int, string>, diff: string}> $changes
* @return int
*/
public function execute($changes)
{
$files = array_keys($changes);

if (empty($files)) {
render(<<<'HTML'
<div class="mx-2">
<span class="text-green-500">No changes to commit!</span>
</div>
HTML
);

return Command::SUCCESS;
}

return $this->makeCommit($files);
}

/**
* Make the Git commit.
*
* @param array<string> $files
* @return int
*/
protected function makeCommit(array $files)
{
$process = Process::run(sprintf('git commit -m "Apply style fixes from Laravel Pint" %s', implode(' ', $files)));

if ($process->failed()) {
render(<<<HTML
<div class="mx-2">
<span class="px-2 bg-red text-white uppercase font-bold mr-1">Error:</span>
<span>{$process->errorOutput()}</span>
</div>
HTML
);

return Command::FAILURE;
}

render(<<<'HTML'
<div class="mx-2">
<span class="text-green-500">Changes committed successfully!</span>
</div>
HTML
);

return Command::SUCCESS;
}
}
11 changes: 10 additions & 1 deletion app/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands;

use App\Actions\GitCommitter;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -40,6 +41,7 @@ protected function configure()
new InputOption('test', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them'),
new InputOption('dirty', '', InputOption::VALUE_NONE, 'Only fix files that have uncommitted changes'),
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format that should be used'),
new InputOption('commit', '', InputOption::VALUE_NONE, 'Automatically commit fixed files'),
]
);
}
Expand All @@ -55,6 +57,13 @@ public function handle($fixCode, $elaborateSummary)
{
[$totalFiles, $changes] = $fixCode->execute();

return $elaborateSummary->execute($totalFiles, $changes);
$exitCode = $elaborateSummary->execute($totalFiles, $changes);

if ($exitCode === \Illuminate\Console\Command::SUCCESS && $this->input->getOption('commit')) {
$commitCode = resolve(GitCommitter::class)->execute($changes);
$exitCode = max($exitCode, $commitCode);
}

return $exitCode;
}
}
8 changes: 8 additions & 0 deletions app/Providers/ActionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\ElaborateSummary;
use App\Actions\FixCode;
use App\Actions\GitCommitter;
use App\Output\ProgressOutput;
use App\Output\SummaryOutput;
use App\Repositories\ConfigurationJsonRepository;
Expand Down Expand Up @@ -59,5 +60,12 @@ public function register()
)
);
});

$this->app->singleton(GitCommitter::class, function () {
return new GitCommitter(
resolve(InputInterface::class),
resolve(OutputInterface::class),
);
});
}
}

0 comments on commit 34a1223

Please sign in to comment.