-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 21TORR/next
- Loading branch information
Showing
10 changed files
with
464 additions
and
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,281 +1,20 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Janus\Command\InitializeCommand; | ||
use Janus\Command\LegacyCommand; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Input\ArgvInput; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Process\ExecutableFinder; | ||
use Symfony\Component\Process\Process; | ||
|
||
require_once $_composer_autoload_path ?? (__DIR__ . '/../vendor/autoload.php'); | ||
|
||
$input = new ArgvInput(); | ||
$output = new ConsoleOutput(); | ||
|
||
$allowedCommands = [ | ||
"init-symfony" => null, | ||
]; | ||
$allowedCommands = ["init-symfony", "init-library"]; | ||
|
||
|
||
(new SingleCommandApplication()) | ||
->setName("Janus") | ||
->addArgument( | ||
"command", | ||
InputArgument::REQUIRED, | ||
suggestedValues: array_keys($allowedCommands), | ||
) | ||
->setCode( | ||
function (InputInterface $input, OutputInterface $output) : int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title("Janus"); | ||
$command = $input->getArgument("command"); | ||
|
||
try | ||
{ | ||
return match ($command) | ||
{ | ||
"init-symfony" => initializeSymfony($io), | ||
"init-library" => initializeLibrary($io), | ||
null => printUsage(), | ||
default => printError($io, "Unknown command: {$command}"), | ||
}; | ||
} | ||
catch (Throwable $exception) | ||
{ | ||
$io->error("Running janus failed: {$exception->getMessage()}"); | ||
return 2; | ||
} | ||
} | ||
) | ||
->run(); | ||
|
||
return; | ||
|
||
|
||
// region Commands | ||
/** | ||
*/ | ||
function initializeSymfony (SymfonyStyle $io) : int | ||
{ | ||
$io->writeln("• Copying config files to the project..."); | ||
copyFilesIntoProject($io, "symfony"); | ||
|
||
$io->writeln("• Updating <fg=yellow>composer.json</>..."); | ||
addToProjectComposerJson([ | ||
"config" => [ | ||
"allow-plugins" => [ | ||
"bamarni/composer-bin-plugin" => true, | ||
], | ||
"sort-packages" => true, | ||
], | ||
"extra" => [ | ||
"bamarni-bin" => [ | ||
"bin-links" => false, | ||
"forward-command" => true, | ||
], | ||
], | ||
"require-dev" => [ | ||
"bamarni/composer-bin-plugin" => "^1.8", | ||
"roave/security-advisories" => "dev-latest", | ||
], | ||
"scripts" => [ | ||
"fix-lint" => [ | ||
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi", | ||
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi", | ||
], | ||
"lint" => [ | ||
"bin/console lint:yaml config --parse-tags", | ||
"bin/console lint:twig templates", | ||
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", | ||
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --dry-run --no-interaction --ansi", | ||
], | ||
"test" => [ | ||
"vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi", | ||
], | ||
], | ||
]); | ||
|
||
$io->writeln("• Running <fg=blue>composer update</>..."); | ||
runComposerInProject($io, ["update"]); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
*/ | ||
function initializeLibrary (SymfonyStyle $io) : int | ||
{ | ||
$io->writeln("• Copying config files to the project..."); | ||
copyFilesIntoProject($io, "library"); | ||
|
||
$io->writeln("• Updating <fg=yellow>composer.json</>..."); | ||
addToProjectComposerJson([ | ||
"config" => [ | ||
"allow-plugins" => [ | ||
"bamarni/composer-bin-plugin" => true, | ||
], | ||
"sort-packages" => true, | ||
], | ||
"extra" => [ | ||
"bamarni-bin" => [ | ||
"bin-links" => false, | ||
"forward-command" => true, | ||
], | ||
], | ||
"require-dev" => [ | ||
"bamarni/composer-bin-plugin" => "^1.8", | ||
"roave/security-advisories" => "dev-latest", | ||
], | ||
"scripts" => [ | ||
"fix-lint" => [ | ||
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi", | ||
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi", | ||
], | ||
"lint" => [ | ||
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", | ||
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --dry-run --no-interaction --ansi", | ||
], | ||
"test" => [ | ||
"vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi", | ||
], | ||
], | ||
]); | ||
|
||
$io->writeln("• Running <fg=blue>composer update</>..."); | ||
runComposerInProject($io, ["update"]); | ||
|
||
return 0; | ||
} | ||
// endregion | ||
|
||
|
||
// region CommandHelpers | ||
function copyFilesIntoProject (SymfonyStyle $io, string $directory) : void | ||
{ | ||
$sourceDir = __DIR__ . "/../_init/{$directory}/."; | ||
|
||
runProcessInProject($io, [ | ||
"cp", | ||
"-a", | ||
$sourceDir, | ||
".", | ||
]); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
function runProcessInProject (SymfonyStyle $io, array $cmd) : void | ||
{ | ||
$io->writeln(sprintf( | ||
"$> Running command <fg=blue>%s</>", | ||
implode(" ", $cmd), | ||
)); | ||
|
||
$process = new Process( | ||
$cmd, | ||
cwd: getcwd(), | ||
); | ||
$process->mustRun(); | ||
|
||
$output = trim(sprintf("%s\n%s", $process->getErrorOutput(), $process->getOutput())); | ||
|
||
if ("" !== $output) | ||
{ | ||
$io->block( | ||
trim(sprintf("%s\n%s", $process->getErrorOutput(), $process->getOutput())), | ||
prefix: " │ ", | ||
); | ||
} | ||
} | ||
|
||
|
||
function runComposerInProject (SymfonyStyle $io, array $cmd) : void | ||
{ | ||
$finder = new ExecutableFinder(); | ||
$composer = $finder->find("composer"); | ||
|
||
if (null === $composer) | ||
{ | ||
throw new Exception("Could not find locally installed composer"); | ||
} | ||
|
||
array_unshift($cmd, $composer); | ||
$cmd[] = "--ansi"; | ||
runProcessInProject($io, $cmd); | ||
} | ||
|
||
/** | ||
*/ | ||
function addToProjectComposerJson (array $config) : void | ||
{ | ||
$filePath = getcwd() . "/composer.json"; | ||
|
||
$jsonContent = json_decode( | ||
file_get_contents($filePath), | ||
true, | ||
flags: JSON_THROW_ON_ERROR | ||
); | ||
|
||
$jsonContent = array_replace_recursive($jsonContent, $config); | ||
file_put_contents( | ||
$filePath, | ||
json_encode( | ||
$jsonContent, | ||
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES, | ||
JSON_THROW_ON_ERROR | ||
), | ||
); | ||
} | ||
// endregion | ||
|
||
|
||
// region Output Helpers | ||
/** | ||
* Prints an error message + usage and returns an error status code | ||
*/ | ||
function printError (SymfonyStyle $io, string $message) : int | ||
{ | ||
global $allowedCommands; | ||
|
||
$io->error($message); | ||
$io->writeln("Allowed commands:"); | ||
$io->listing( | ||
array_map( | ||
static fn (string $command) => "<fg=yellow>{$command}</>", | ||
array_keys($allowedCommands), | ||
), | ||
); | ||
|
||
return 1; | ||
} | ||
|
||
|
||
/** | ||
* Prints the usage and returns a success status code | ||
*/ | ||
function printUsage () : int | ||
{ | ||
echo " ~~~~~~~\n"; | ||
echo " Janus \n"; | ||
echo " ~~~~~~~\n"; | ||
echo "\n"; | ||
echo "Usage:\n"; | ||
echo " janus [command] [argument]\n"; | ||
echo "\n"; | ||
echo "\n"; | ||
echo "Commands:\n"; | ||
echo "\n"; | ||
echo " init-symfony .. to initialize a symfony application\n"; | ||
echo " init-library .. to initialize a library\n"; | ||
echo "\n"; | ||
echo "\n"; | ||
|
||
return 0; | ||
} | ||
// endregion | ||
$application = new Application("Janus"); | ||
$application->add(new InitializeCommand()); | ||
$application->add(new LegacyCommand()); | ||
$application->run(); |
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
Oops, something went wrong.