Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanshukurlu committed May 27, 2023
0 parents commit 9107d33
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Orkhan Shukurlu <orkhandev@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "orkhanshukurlu/laravel-git-artisan",
"description": "Run multiple git commands in a single command",
"keywords": [
"orkhanshukurlu",
"laravel",
"git",
"artisan"
],
"homepage": "https://github.com/orkhanshukurlu/laravel-git-artisan",
"license": "MIT",
"authors": [
{
"name": "Orkhan Shukurlu",
"email": "orkhandev@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"illuminate/console": "^10.10",
"illuminate/process": "^10.10"
},
"autoload": {
"psr-4": {
"OrkhanShukurlu\\GitArtisan\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"OrkhanShukurlu\\GitArtisan\\GitArtisanServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
21 changes: 21 additions & 0 deletions src/GitArtisanServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace OrkhanShukurlu\GitArtisan;

use Illuminate\Support\ServiceProvider;

final class GitArtisanServiceProvider extends ServiceProvider
{
public function boot(): void
{
if (! $this->app->runningInConsole()) {
return;
}

$this->commands([
GitCommand::class,
]);
}
}
163 changes: 163 additions & 0 deletions src/GitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

declare(strict_types=1);

namespace OrkhanShukurlu\GitArtisan;

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

final class GitCommand extends Command
{
protected $description = 'Run multiple git commands in a single command';

protected $signature = 'git {message : Commit the changes with the message}
{--pull= : Pull changes to the branch}
{--push= : Push changes to the branch}
{--no-pull : Commit changes without pulling}
{--no-push : Commit changes without pushing}';

public function handle(): int
{
$this->status();
$this->add();
$this->commit();

if (! $this->option('no-pull')) {
$this->pull();
}

if (! $this->option('no-push')) {
$this->push();
}

return parent::SUCCESS;
}

private function add(): void
{
$process = $this->runAddProcess();

$this->writeInfo('add')->handleOutput($process);
}

private function commit(): void
{
$process = $this->runCommitProcess();

$this->writeInfo('commit')->handleOutput($process);
}

private function errorOutputOnFailure(ProcessResult $process): void
{
if (empty($output = $process->errorOutput())) {
return;
}

$this->error($output);
}

private function errorOutputOnSuccess(ProcessResult $process): void
{
if (empty($output = $process->errorOutput())) {
return;
}

$this->warn($output);
}

private function getCommitMessage(): string
{
return $this->argument('message');
}

private function getPullBranch(): string
{
return $this->option('pull') ?: 'master';
}

private function getPushBranch(): string
{
return $this->option('push') ?: 'master';
}

private function handleOutput(ProcessResult $process): void
{
if ($process->failed()) {
$this->errorOutputOnFailure($process);
exit;
}

$this->errorOutputOnSuccess($process);
$this->outputOnSuccess($process);
}

private function outputOnSuccess(ProcessResult $process): void
{
if (empty($output = $process->output())) {
return;
}

$this->info($output);
}

private function pull(): void
{
$process = $this->runPullProcess();

$this->writeInfo('pull')->handleOutput($process);
}

private function push(): void
{
$process = $this->runPushProcess();

$this->writeInfo('push')->handleOutput($process);
}

private function runAddProcess(): ProcessResult
{
return Process::run(['git', 'add', '.']);
}

private function runCommitProcess(): ProcessResult
{
$message = $this->getCommitMessage();

return Process::run(['git', 'commit', '-m', $message]);
}

private function runPullProcess(): ProcessResult
{
$branch = $this->getPullBranch();

return Process::run(['git', 'pull', 'origin', $branch]);
}

private function runPushProcess(): ProcessResult
{
$branch = $this->getPushBranch();

return Process::run(['git', 'push', 'origin', $branch]);
}

private function runStatusProcess(): ProcessResult
{
return Process::run(['git', 'status']);
}

private function status(): void
{
$process = $this->runStatusProcess();

$this->writeInfo('status')->handleOutput($process);
}

private function writeInfo(string $command): self
{
$this->line('<options=bold;bg=magenta>' . strtoupper($command) . '</>');

return $this;
}
}

0 comments on commit 9107d33

Please sign in to comment.