Skip to content

Commit

Permalink
Merge pull request #11 from whitecube/pre-commit-hooks
Browse files Browse the repository at this point in the history
Added pre commit hooks to run pint automatically
  • Loading branch information
voidgraphics authored Apr 12, 2023
2 parents 0e09857 + dc50b9a commit c6b00d2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/Codestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Whitecube\LaravelPreset;

use Illuminate\Support\Facades\File;
use Laravel\Ui\UiCommand;

class Codestyle
Expand All @@ -11,7 +12,15 @@ public static function install(UiCommand $command)
{
$command->info('Installing code style fixer...');

static::copyStubs();
}

public static function copyStubs()
{
copy(__DIR__.'/stubs/pint.json', base_path('pint.json'));

File::ensureDirectoryExists('.githooks');
copy(__DIR__.'/stubs/pre-commit', base_path('.githooks/pre-commit'));
}

}
24 changes: 0 additions & 24 deletions src/ComposerEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,11 @@ public static function install(UiCommand $command)
{
$command->info('Installing composer env...');
static::copyStub();
static::addComposerScripts();
}

public static function copyStub()
{
File::ensureDirectoryExists('app/Console/Commands');
copy(__DIR__ . '/stubs/ComposerEnv.stub', app_path('Console/Commands/ComposerEnv.php'));
}

public static function addComposerScripts()
{
$composer = app()->make(\Whitecube\LaravelPreset\Support\Composer::class);

$composer->run([
'config',
'scripts.pre-install-cmd',
'App\\Console\\Commands\\ComposerEnv::loadLocalRepositories'
]);

$composer->run([
'config',
'scripts.pre-update-cmd',
'App\\Console\\Commands\\ComposerEnv::loadLocalRepositories'
]);

$composer->run([
'config',
'scripts.fix-style',
'./vendor/bin/pint --preset laravel'
]);
}
}
22 changes: 21 additions & 1 deletion src/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Preset extends LaravelPreset
{
public static function install(UiCommand $command)
{
static::updatePackages($command);
static::updatePackages();
static::addComposerScripts();
Codestyle::install($command);
Gitignore::install($command);
Mix::install($command);
Expand Down Expand Up @@ -51,4 +52,23 @@ public static function updatePackageArray($packages)
])
);
}

public static function addComposerScripts()
{
$composer = app()->make(\Whitecube\LaravelPreset\Support\Composer::class);

$composer->run([
'config',
'scripts.pre-install-cmd',
'App\\Console\\Commands\\ComposerEnv::loadLocalRepositories',
'git config core.hooksPath .githooks'
]);

$composer->run([
'config',
'scripts.pre-update-cmd',
'App\\Console\\Commands\\ComposerEnv::loadLocalRepositories',
'git config core.hooksPath .githooks'
]);
}
}
10 changes: 10 additions & 0 deletions src/stubs/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# store list of staged files to variable except deleted file since it won't work at re-add
files=$(git diff --name-only --cached --diff-filter=ACMR -- '*.php')

if [ -n "$files" ]; then
# run pint, this will unstage any files that pint will fix
./vendor/bin/pint $files

# re-add unstaged files caused by pint back to staging
printf $files | xargs git add
fi

0 comments on commit c6b00d2

Please sign in to comment.