Skip to content

Add skeleton commands #715

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions box.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"directories": [
"config",
"src",
"vendor/nette",
"vendor/psr",
"vendor/laravel/prompts",
"vendor/illuminate",
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ext-mbstring": "*",
"ext-zlib": "*",
"laravel/prompts": "^0.1.12",
"nette/php-generator": "^4.1",
"symfony/console": "^5.4 || ^6 || ^7",
"zhamao/logger": "^1.0"
},
Expand Down Expand Up @@ -44,7 +45,7 @@
],
"scripts": {
"analyse": "phpstan analyse --memory-limit 300M",
"cs-fix": "php-cs-fixer fix",
"cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix",
"test": "vendor/bin/phpunit tests/ --no-coverage",
"build:phar": "vendor/bin/box compile"
},
Expand Down
159 changes: 157 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/SPC/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
use SPC\command\BuildPHPCommand;
use SPC\command\DeleteDownloadCommand;
use SPC\command\dev\AllExtCommand;
use SPC\command\dev\ExtSkeletonCommand;
use SPC\command\dev\ExtVerCommand;
use SPC\command\dev\GenerateExtDepDocsCommand;
use SPC\command\dev\GenerateExtDocCommand;
use SPC\command\dev\GenerateLibDepDocsCommand;
use SPC\command\dev\LibSkeletonCommand;
use SPC\command\dev\LibVerCommand;
use SPC\command\dev\PackLibCommand;
use SPC\command\dev\PhpVerCommand;
use SPC\command\dev\SortConfigCommand;
use SPC\command\dev\SourceSkeletonCommand;
use SPC\command\DoctorCommand;
use SPC\command\DownloadCommand;
use SPC\command\DumpExtensionsCommand;
Expand All @@ -32,7 +35,7 @@
*/
final class ConsoleApplication extends Application
{
public const VERSION = '2.5.2';
public const VERSION = '2.5.3';

public function __construct()
{
Expand Down Expand Up @@ -67,6 +70,9 @@ public function __construct()
new GenerateExtDepDocsCommand(),
new GenerateLibDepDocsCommand(),
new PackLibCommand(),
new ExtSkeletonCommand(),
new LibSkeletonCommand(),
new SourceSkeletonCommand(),
]
);
}
Expand Down
122 changes: 122 additions & 0 deletions src/SPC/command/dev/ExtSkeletonCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace SPC\command\dev;

use SPC\exception\FileSystemException;
use SPC\store\Config;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

#[AsCommand('dev:ext-skel', 'Generate extension skeleton', ['ext-skel'])]
class ExtSkeletonCommand extends SkeletonCommand
{
/**
* @throws FileSystemException
*/
public function initialize(InputInterface $input, OutputInterface $output): void
{
$name = $input->getArgument('name');
if ($name !== null && is_string($r = $this->validateExtName($name))) {
throw new InvalidArgumentException($r);
}
}

/**
* @throws ExceptionInterface|FileSystemException
*/
public function handle(): int
{
$result = ['type' => 'external'];
// Get extension name
$ext_name = $this->input->getArgument('name');

// apply source name
$result['source'] = $ext_name;

// Select extension support
$ext_support = multiselect('Please select extension support for [' . $ext_name . '].', [
'Linux' => 'Linux',
'Darwin' => 'MacOS',
'Windows' => 'Windows',
], default: ['Linux', 'Darwin'], required: true, hint: 'Use the space bar to select options, press enter to the next step');

// $input->setArgument('name', $ext_name);
$a = new ArrayInput(['name' => $ext_name, '--is-middle-step' => true]);
$this->getApplication()->find('dev:source-skel')->run($a, $this->output);

// check if extension depends on other extensions
$ext_depends = confirm('Does this extension depend on other extensions?', default: false) ? multiselect('Please select extension dependencies', array_keys(Config::getExts()), hint: 'Use the space bar to select options, press enter to the next step') : [];
if ($ext_depends) {
$result['ext-depends'] = $ext_depends;
}

// check if extension suggests other extensions
$ext_suggests = confirm('Does this extension suggest other extensions?', default: false) ? multiselect('Please select extension suggestions', array_keys(Config::getExts()), hint: 'Use the space bar to select options, press enter to the next step') : [];
if ($ext_suggests) {
$result['ext-suggests'] = $ext_suggests;
}

// select extension build arg type (--enable-xxx, --with-xxx, with-xxx=PATH, custom)
if (in_array('Linux', $ext_support) || in_array('Darwin', $ext_support)) {
$ext_build_args_unix = select('Please select *nix (Linux, macOS) extension build arg type', [
'enable' => '--enable-' . strtolower($ext_name),
'with' => '--with-' . strtolower($ext_name),
'with-xxx=' => '--with-' . strtolower($ext_name) . '={buildroot}',
'custom' => 'custom',
], default: 'enable');
}
if (in_array('Windows', $ext_support)) {
$ext_build_args_windows = select('Please select Windows extension build arg type', [
'enable' => '--enable-' . strtolower($ext_name),
'with' => '--with-' . strtolower($ext_name),
'with-xxx=' => '--with-' . strtolower($ext_name) . '={buildroot}',
'custom' => 'custom',
], default: 'enable');
}
$ext_build_args_unix ??= null;
$ext_build_args_windows ??= $ext_build_args_unix;
if ($ext_build_args_windows === $ext_build_args_unix) {
$result['arg-type'] = $ext_build_args_windows;
} else {
$result['arg-type-unix'] = $ext_build_args_unix;
$result['arg-type-windows'] = $ext_build_args_windows;
}

// check if extension depends on other libraries
if (confirm('Does this extension depend on other libraries?', default: false)) {
// Select library dependencies, or create a new library skeleton
if (select('You can select existing libraries or create a new library skeleton', ['Create a new library skeleton', 'Select existing libraries'], default: 'Select existing libraries') === 'Create a new library skeleton') {
$lib_name = text('Please input new library name', required: true, validate: [$this, 'validateLibName']);
$lib_name = strtolower($lib_name);
$input = new ArrayInput(['name' => $lib_name, '--is-middle-step' => true]);
$this->getApplication()->find('dev:lib-skel')->run($input, $this->output);
} else {
// Select existing libraries
$ext_libs = multiselect('Please select library dependencies', array_keys(Config::getLibs()), hint: 'Use the space bar to select options, press enter to the next step');
}
} else {
$ext_libs = [];
}
if (!empty($ext_libs)) {
$result['lib-depends'] = $ext_libs;
}
$this->output->writeln('<info>Extension config generated!</info>');
$this->output->writeln(sprintf('<info>%s</info>', json_encode($result, JSON_PRETTY_PRINT)));
SkeletonCommand::$cache['ext'][$ext_name] = $result;
if (!$this->getOption('is-middle-step')) {
$this->generateAll();
}
return static::SUCCESS;
}
}
Loading