Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run CI + add choice for types #9

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ jobs:
php --version
php -r 'foreach (get_loaded_extensions() as $extension) echo $extension . " " . phpversion($extension) . PHP_EOL;'

- name: Install Composer
run: composer install --optimize-autoloader --classmap-authoritative --no-interaction

- name: Run Linters
run: composer run-script lint

- name: Run Tests
run: composer run-script test

- name: "Install Composer: Library / Composer Normalize"
run: composer install --optimize-autoloader --classmap-authoritative --no-interaction
working-directory: _init/library/vendor-bin/c-norm
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/.php-cs-fixer.cache
/composer.lock
/vendor
/vendor-bin/*/composer.lock
/vendor-bin/*/vendor
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
1.4.0
=====

* (feature) Rename the call from `init-symfony` to `init symfony`.
* (feature) Rename the call from `init-library` to `init library`.
* (feature) Add BC layer for old commands.
* (feature) Add choice for when calling `init` without or with invalid type.
* (internal) Refactored whole implementation.
* (internal) Run CI on Janus itself.


1.3.0
=====

Expand Down
2 changes: 0 additions & 2 deletions bin/janus
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<?php

use Janus\Command\InitializeCommand;
use Janus\Command\LegacyCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -16,5 +15,4 @@ $allowedCommands = ["init-symfony", "init-library"];

$application = new Application("Janus");
$application->add(new InitializeCommand());
$application->add(new LegacyCommand());
$application->run();
35 changes: 28 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
{
"name": "21torr/janus",
"type": "lib",
"description": "Code style, encoded as rules for common tools.",
"homepage": "https://github.com/21TORR/janus-php",
"license": "BSD-3-Clause",
"type": "lib",
"authors": [
{
"name": "21TORR",
"homepage": "https://www.21torr.com/"
}
],
"homepage": "https://github.com/21TORR/janus-php",
"require": {
"php": ">= 8.3",
"21torr/cli": "^1.2",
"symfony/console": "^7.0",
"symfony/process": "^7.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
"roave/security-advisories": "dev-latest"
},
"bin": [
"bin/janus"
],
"autoload": {
"psr-4": {
"Janus\\": "src/"
}
},
"bin": [
"bin/janus"
],
"config": {
"sort-packages": true,
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
"extra": {
"bamarni-bin": {
"bin-links": false,
"forward-command": true
}
},
"scripts": {
"fix-lint": [
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --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 --no-interaction --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"
]
}
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- phpstan/lib.neon
63 changes: 35 additions & 28 deletions src/Command/InitializeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

final class InitializeCommand extends Command
{
private const array ALLOWED_TYPES = [
"symfony",
"library",
];
private const array LEGACY_COMMANDS = [
"init-symfony",
"init-library",
];

/**
*/
public function __construct ()
Expand All @@ -22,18 +31,17 @@ public function __construct ()
/**
* @inheritDoc
*/
protected function configure ()
protected function configure () : void
{
$this
->setDescription("Initializes a given command")
->setAliases(self::LEGACY_COMMANDS)
->addArgument(
"type",
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
"The project type to initialize",
suggestedValues: [
"symfony",
"library",
],
default: null,
suggestedValues: self::ALLOWED_TYPES,
);
}

Expand All @@ -45,39 +53,38 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
$io = new TorrStyle($input, $output);
$io->title("Janus: Initialize");


try
if (\in_array($input->getFirstArgument(), self::LEGACY_COMMANDS, true))
{
$type = $input->getArgument("type");
$io->caution("You are using a deprecated command. Use the `init` command instead.");
}

$initializer = match ($type)
{
"symfony" => new SymfonyInitializer(),
"library" => new LibraryInitializer(),
default => null,
};
$type = $input->getArgument("type");
\assert(null === $type || \is_string($type));

if (null === $initializer)
if (!\in_array($type, self::ALLOWED_TYPES, true))
{
if (null !== $type)
{
return $this->printError($io, $type);
$io->error("Used invalid type: {$type}");
}

return $initializer->initialize($io);
$type = $io->choice("Please select the type to initialize", self::ALLOWED_TYPES);
}

\assert(\is_string($type));

try
{
return match ($type)
{
"symfony" => (new SymfonyInitializer())->initialize($io),
"library" => (new LibraryInitializer())->initialize($io),
};
}
catch (\Throwable $exception)
{
$io->error("Running janus failed: {$exception->getMessage()}");
return 2;
}
}

/**
* Prints an error
*/
private function printError (TorrStyle $io, string $type) : int
{
$io->error("Unknown type: {$type}");

return self::FAILURE;
}
}
63 changes: 0 additions & 63 deletions src/Command/LegacyCommand.php

This file was deleted.

7 changes: 7 additions & 0 deletions src/Exception/InvalidCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Janus\Exception;

final class InvalidCallException extends \InvalidArgumentException implements JanusException
{
}
27 changes: 20 additions & 7 deletions src/Initializer/InitializeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct (
private TorrStyle $io,
)
{
$this->cwd = \getcwd();
$this->cwd = (string) \getcwd();
}

/**
Expand All @@ -39,6 +39,8 @@ public function copyFilesIntoProject (string $directory) : void

/**
* Add the given config to the projects composer.json
*
* @param array<array-key, mixed> $config
*/
public function addToProjectComposerJson (array $config) : void
{
Expand All @@ -58,13 +60,14 @@ public function addToProjectComposerJson (array $config) : void
* If there are multiple lines matching, all will be replaced.
* If there are no lines matching, the call will just be appended.
*
*
* @param string $key The scripts key to update.
* @param string $key The scripts key to update.
* @param array<string, string> $scripts The scripts to replace.
*/
function updateProjectComposerJsonScripts (string $key, array $scripts) : void
public function updateProjectComposerJsonScripts (string $key, array $scripts) : void
{
$jsonContent = $this->readProjectComposerJson();
\assert(!isset($jsonContent["scripts"]) || \is_array($jsonContent["scripts"]));

$existingScripts = $jsonContent["scripts"][$key] ?? [];
// keep existing scripts
$result = [];
Expand Down Expand Up @@ -94,6 +97,9 @@ function updateProjectComposerJsonScripts (string $key, array $scripts) : void
}

/**
* Writes the given config to the project composer.json
*
* @param array<array-key, mixed> $config
*/
private function writeProjectComposerJson (array $config) : void
{
Expand All @@ -109,21 +115,26 @@ private function writeProjectComposerJson (array $config) : void
}

/**
* @return array<array-key, mixed>
*/
private function readProjectComposerJson () : array
{
$filePath = "{$this->cwd}/composer.json";

return \json_decode(
\file_get_contents($filePath),
$result = \json_decode(
(string) \file_get_contents($filePath),
true,
flags: \JSON_THROW_ON_ERROR
flags: \JSON_THROW_ON_ERROR,
);
\assert(\is_array($result));
return $result;
}


/**
* Runs a composer command in the project
*
* @param string[] $cmd
*/
public function runComposerInProject (array $cmd) : void
{
Expand All @@ -143,6 +154,8 @@ public function runComposerInProject (array $cmd) : void

/**
* Runs the given command in the project directory
*
* @param string[] $cmd
*/
public function runProcessInProject (array $cmd) : void
{
Expand Down
11 changes: 11 additions & 0 deletions vendor-bin/c-norm/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"roave/security-advisories": "dev-latest"
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
}
}
}
6 changes: 6 additions & 0 deletions vendor-bin/cs-fixer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require-dev": {
"21torr/php-cs-fixer": "^1.0.2",
"roave/security-advisories": "dev-latest"
}
}
Loading