feat(installer): 基本实现了安装器的逻辑

This commit is contained in:
李东云
2023-04-10 17:26:05 +08:00
parent ed22d562c7
commit 98b7e6e466
3 changed files with 100 additions and 64 deletions

View File

@@ -104,6 +104,7 @@ class OptionalPackages
/** @var int */
private int $port;
private string $description = '清锋的又一个软件项目';
public function __construct(IOInterface $io, Composer $composer, string $projectRoot = null)
{
@@ -141,9 +142,11 @@ class OptionalPackages
public function installHyperfScript(): void
{
$default_timezone = ini_get('TIME_ZONE');
$default_timezone = empty($default_timezone) ? 'Asia/Shanghai': $default_timezone;
$ask[] = "\n <question>What time zone do you want to set up ?</question>\n";
$ask[] = " [<comment>n</comment>] Default time zone for php.ini\n";
$ask[] = "Make your selection or type a time zone name, like Asia/Shanghai (n):\n";
$ask[] = " Make your selection or type a time zone name, like Asia/Shanghai ($default_timezone):\n ";
$answer = $this->io->askAndValidate(
implode('', $ask),
function ($value) {
@@ -156,25 +159,24 @@ class OptionalPackages
return trim($value);
},
null,
'n'
$default_timezone
);
if ($answer != 'n') {
$content = file_get_contents($this->installerSource . '/resources/bin/hyperf.stub');
$content = str_replace('%TIME_ZONE%', $answer, $content);
file_put_contents($this->projectRoot . '/bin/hyperf.php', $content);
$target_path = $this->projectRoot . 'bin/hyperf.php';
if (!is_dir(dirname($target_path))) {
mkdir(dirname($target_path));
}
file_put_contents($this->projectRoot . 'bin/hyperf.php', $content);
}
}
public function setupProject(): void
{
$ask = <<<ECHO
<question>这个项目的项目名是什么?</question> (必填项,格式类似 lux-studio)
ECHO;
$this->projectName = $this->io->askAndValidate(
$ask,
"\n <question>这个项目的项目名是什么?</question> (必填项,格式类似 lux-studio)\n ",
function ($value) {
$pattern = '/^\s*lux(-([a-z0-9])+)+\s*$/';
preg_match($pattern, $value, $matches);
@@ -187,19 +189,22 @@ ECHO;
null,
''
);
$this->projectName = $this->io->askAndValidate(
"\n <question>一句话描述下这个项目吧</question>(默认:{$this->description}\n ",
function ($value) {
return trim($value);
},
null,
$this->description
);
$ports_map = require __DIR__ . '/portMap.php';
$available_ports = array_keys($ports_map);
$max_port = max(...$available_ports);
$default_port = intval($max_port/10) + 1 .'1';
$ask = <<<ECHO
<question>你想使用哪个端口监听这个项目?</question> (默认:$default_port)
ECHO;
$default_port = intval($max_port / 10) + 1 . '1';
$this->port = $this->io->askAndValidate(
$ask,
"\n <question>你想使用哪个端口监听这个项目?</question> (默认:" . $default_port . ") \n ",
function ($input) use ($available_ports, $ports_map) {
if (in_array($input, $available_ports)) {
$error = <<<'ERROR'
@@ -214,7 +219,6 @@ ERROR;
[:$port] $project_name 的 $type
ERROR;
}
throw new \InvalidArgumentException($error);
}
@@ -224,14 +228,10 @@ ERROR;
null,
$default_port
);
}
public function updateComposerJson(): void
{
$composer_project_name = sprintf('web-service/%s', $this->projectName);
$this->composerDefinition['name'] = $composer_project_name;
$this->composerJson->write($this->composerDefinition);
$this->composerDefinition['description'] = $this->description;
}
public function updateDockerEnv(): void
@@ -510,9 +510,11 @@ ERROR;
$this->composerDefinition['autoload']['psr-4']['Installer\\'],
$this->composerDefinition['autoload-dev']['psr-4']['InstallerTest\\'],
$this->composerDefinition['extra']['branch-alias'],
$this->composerDefinition['extra']['hyperf'],
$this->composerDefinition['extra']['optional-packages'],
$this->composerDefinition['scripts']['pre-update-cmd'],
$this->composerDefinition['scripts']['pre-install-cmd']
$this->composerDefinition['scripts']['pre-install-cmd'],
$this->composerDefinition['version']
);
}
@@ -546,6 +548,20 @@ ERROR;
file_put_contents($ignoreFile, $content);
}
/**
* Remove lines from string content containing words in array.
*/
public function removeLinesContainingStrings(array $entries, string $content): string
{
$entries = implode(
'|',
array_map(function ($word) {
return preg_quote($word, '/');
}, $entries)
);
return preg_replace('/^.*(?:' . $entries . ").*$(?:\r?\n)?/m", '', $content);
}
/**
* Clean up/remove installer classes and assets.
*
@@ -566,20 +582,6 @@ ERROR;
$this->recursiveRmdir($this->installerSource);
}
/**
* Remove lines from string content containing words in array.
*/
public function removeLinesContainingStrings(array $entries, string $content): string
{
$entries = implode(
'|',
array_map(function ($word) {
return preg_quote($word, '/');
}, $entries)
);
return preg_replace('/^.*(?:' . $entries . ").*$(?:\r?\n)?/m", '', $content);
}
/**
* Recursively remove a directory.
*

View File

@@ -24,13 +24,11 @@ class Script
$installer->setupRuntimeDir();
$installer->removeDevDependencies();
$installer->setupProject();
$installer->updateComposerJson();
$installer->installHyperfScript();
$installer->promptForOptionalPackages();
$installer->updateDockerEnv();
die;
// $installer->installHyperfScript();
// $installer->promptForOptionalPackages();
// $installer->updateRootPackage();
// $installer->removeInstallerFromDefinition();
// $installer->finalizePackage();
$installer->updateRootPackage();
$installer->removeInstallerFromDefinition();
$installer->finalizePackage();
}
}

View File

@@ -11,70 +11,106 @@ declare(strict_types=1);
*/
return [
'packages' => [
'singularity/hyperf-saml' => [
'version' => '^0.2.3',
],
'singularity/hdk-auth' => [
'version' => '^0.2.2',
],
'hyperf/amqp' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/async-queue' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/database' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/db-connection' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/model-cache' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/constants' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/json-rpc' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/redis' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/rpc' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/rpc-client' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/rpc-server' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/grpc-client' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/grpc-server' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/elasticsearch' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/config-apollo' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/config-aliyun-acm' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/config-etcd' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/config-nacos' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/tracer' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
'hyperf/service-governance' => [
'version' => '~3.0.0',
'version' => '^3.0.15',
],
],
'require-dev' => [
],
'questions' => [
'sso' => [
'question' => '你需要接入单点登录LuxAccount',
'default' => 'y',
'required' => false,
'custom-package' => false,
'options' => [
'y' => [
'name' => 'yes',
'packages' => [
'singularity/hyperf-saml',
],
'resources' => [],
],
],
],
'auth' => [
'question' => '你需要登录鉴权吗?',
'default' => 'y',
'required' => false,
'custom-package' => false,
'options' => [
'y' => [
'name' => 'yes',
'packages' => [
'singularity/hdk-auth',
],
'resources' => [],
],
],
],
'database' => [
'question' => 'Do you want to use Database (MySQL Client) ?',
'default' => 'y',