-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into task/gitpod-optimize
- Loading branch information
Showing
7 changed files
with
221 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ochorocho\Tdk\Scripts; | ||
|
||
use Composer\Script\Event; | ||
use Symfony\Component\Filesystem\Exception\IOException; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
class HookScript extends BaseScript | ||
{ | ||
public static function enable(Event $event) | ||
{ | ||
$questions = [ | ||
[ | ||
'method' => 'enableCommitMessage', | ||
'message' => 'Setup Commit Message Hook? [<fg=cyan;options=bold>y</>/n] ', | ||
'default' => true | ||
], | ||
[ | ||
'method' => 'enablePreCommit', | ||
'message' => 'Setup Pre Commit Hook? [<fg=cyan;options=bold>y</>/n] ', | ||
'default' => true | ||
], | ||
]; | ||
|
||
$force = (bool)(GitScript::getArguments($event->getArguments())['force'] ?? getenv('TDK_HOOK_FORCE_CREATE') ?? false); | ||
foreach ($questions as $question) { | ||
if ($force) { | ||
$answer = true; | ||
} else { | ||
$answer = $event->getIO()->askConfirmation($question['message'], $question['default']); | ||
} | ||
|
||
if ($answer) { | ||
$method = $question['method']; | ||
static::$method($event); | ||
} | ||
} | ||
} | ||
|
||
public static function remove(Event $event) | ||
{ | ||
$filesystem = new Filesystem(); | ||
$filesystem->remove([ | ||
self::$coreDevFolder . '/.git/hooks/pre-commit', | ||
self::$coreDevFolder . '/.git/hooks/commit-msg', | ||
]); | ||
} | ||
|
||
private static function enableCommitMessage(Event $event) | ||
{ | ||
$filesystem = new Filesystem(); | ||
|
||
try { | ||
$targetCommitMsg = self::$coreDevFolder . '/.git/hooks/commit-msg'; | ||
$filesystem->copy(self::$coreDevFolder . '/Build/git-hooks/commit-msg', $targetCommitMsg); | ||
|
||
if (!is_executable($targetCommitMsg)) { | ||
$filesystem->chmod($targetCommitMsg, 0755); | ||
} | ||
|
||
$event->getIO()->write('<info>Created Commit Message Hook</info>'); | ||
} catch (IOException $e) { | ||
$event->getIO()->writeError('<warning>Exception:enableCommitMessageHook:' . $e->getMessage() . '</warning>'); | ||
} | ||
} | ||
|
||
private static function enablePreCommit(Event $event) | ||
{ | ||
if (DIRECTORY_SEPARATOR === '\\') { | ||
return; | ||
} | ||
$filesystem = new Filesystem(); | ||
try { | ||
$targetPreCommit = self::$coreDevFolder . '/.git/hooks/pre-commit'; | ||
$filesystem->copy(self::$coreDevFolder . '/Build/git-hooks/unix+mac/pre-commit', $targetPreCommit); | ||
|
||
if (!is_executable($targetPreCommit)) { | ||
$filesystem->chmod($targetPreCommit, 0755); | ||
} | ||
|
||
$event->getIO()->write('<info>Created Pre Commit Hook</info>'); | ||
} catch (IOException $e) { | ||
$event->getIO()->writeError('<warning>Exception:enablePreCommitHook:' . $e->getMessage() . '</warning>'); | ||
} | ||
} | ||
} |
Oops, something went wrong.