add docker init to InitCommand

This commit is contained in:
sunxyw 2022-12-16 21:58:19 +08:00
parent 4bf328ef67
commit a430cf59c4
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
3 changed files with 96 additions and 27 deletions

View File

@ -2,11 +2,13 @@ FROM alpine:latest
MAINTAINER sunxyw <me@sunxyw.xyz>
RUN apk add --no-cache curl && \
mkdir -p /bot && \
curl -L -o /bot/walle-q https://github.com/onebot-walle/walle-q/releases/latest/download/walle-q-i686-linux-musl && \
chmod +x /bot/walle-q
RUN apk add --no-cache curl
RUN mkdir -p /bot && \
curl -fsSL https://github.com/onebot-walle/walle-q/releases/latest/download/walle-q-i686-linux-musl.tar.gz -O && \
tar -zxvf walle-q-i686-linux-musl.tar.gz -C /bot && \
rm -rf walle-q-i686-linux-musl.tar.gz && \
chmod +x /bot/walle-q-i686-linux-musl
ENV TZ=Asia/Shanghai
ENTRYPOINT [ "/bot/walle-q" ]
ENTRYPOINT [ "/bot/walle-q-i686-linux-musl" ]

View File

@ -31,6 +31,9 @@ const ZM_ERR_METHOD_NOT_FOUND = 1; // 找不到方法
const ZM_ERR_ROUTE_NOT_FOUND = 2; // 找不到路由
const ZM_ERR_ROUTE_METHOD_NOT_ALLOWED = 3; // 路由方法不允许
const LOAD_MODE_VENDOR = 0; // 从 vendor 加载
const LOAD_MODE_SRC = 1; // 从 src 加载
/* 定义工作目录 */
define('WORKING_DIR', getcwd());
@ -48,7 +51,7 @@ if (DIRECTORY_SEPARATOR === '\\') {
}
/* 定义启动模式,这里指的是框架本身的源码目录是通过 composer 加入 vendor 加载的还是直接放到 src 目录加载的,前者为 1后者为 0 */
define('LOAD_MODE', is_dir(zm_dir(SOURCE_ROOT_DIR . '/src/ZM')) ? 0 : 1);
define('LOAD_MODE', is_dir(zm_dir(SOURCE_ROOT_DIR . '/src/ZM')) ? LOAD_MODE_VENDOR : LOAD_MODE_SRC);
/* 定义框架本身所处的根目录,此处如果 LOAD_MODE 为 1 的话,框架自身的根目录在 vendor/zhamao/framework 子目录下 */
if (Phar::running() !== '') {

View File

@ -6,6 +6,7 @@ namespace ZM\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use ZM\Exception\InitException;
class InitCommand extends Command
@ -19,9 +20,10 @@ class InitCommand extends Command
protected function configure(): void
{
$this->setDescription('Initialize framework starter | 初始化框架运行的基础文件');
$this->setDescription('初始化框架运行的基础文件');
$this->setDefinition([
new InputOption('force', 'F', null, '强制重制,覆盖现有文件'),
new InputOption('force', 'f', InputOption::VALUE_NONE, '覆盖现有文件'),
new InputOption('docker', null, InputOption::VALUE_NONE, '启用 Docker 支持'),
]);
$this->setHelp('提取框架的基础文件到当前目录,以便于快速开始开发。');
}
@ -32,25 +34,10 @@ class InitCommand extends Command
$this->force = $this->input->getOption('force');
$this->section('提取框架基础文件', function (ConsoleSectionOutput $section) {
foreach ($this->getExtractFiles() as $file) {
$section->write("<fg=gray>提取 {$file} ... </>");
if ($this->shouldExtractFile($file)) {
try {
$this->extractFile($file);
$section->write('<info>完成</info>');
} catch (InitException $e) {
$section->write('<error>失败</error>');
throw $e;
} finally {
$section->writeln('');
}
} else {
$section->writeln('<comment>跳过(已存在)</comment>');
}
}
$this->extractFiles($this->getExtractFiles(), $section);
});
if (LOAD_MODE === 1) {
if (LOAD_MODE === LOAD_MODE_SRC) {
$this->section('应用自动加载配置', function (ConsoleSectionOutput $section) {
$autoload = [
'psr-4' => [
@ -62,6 +49,8 @@ class InitCommand extends Command
],
];
$section->write('<fg=gray>更新 composer.json ... </>');
if (!file_exists($this->base_path . '/composer.json')) {
throw new InitException('未找到 composer.json 文件', '请检查当前目录是否为项目根目录', 41);
}
@ -84,13 +73,39 @@ class InitCommand extends Command
throw new InitException('写入 composer.json 文件失败', '', 0, $e);
}
$section->writeln('<fg=gray>执行 composer dump-autoload ...</>');
$section->writeln('<info>完成</info>');
$section->write('<fg=gray>执行 composer dump-autoload ... </>');
exec('composer dump-autoload');
$section->writeln('<info>完成</info>');
});
}
if ($this->input->getOption('docker')) {
$this->section('应用 Docker 支持', function (ConsoleSectionOutput $section) {
$files = $this->getFilesFromPatterns([
'/docker/*/Dockerfile',
'/docker/environment.env.example',
'/docker-compose.yml',
]);
$this->extractFiles($files, $section);
// 生成 env 文件
if ($this->shouldExtractFile('/docker/environment.env')) {
$section->write('<fg=gray>生成环境变量文件 ... </>');
$env = file_get_contents($this->base_path . '/docker/environment.env.example');
foreach ($this->getEnvVariables() as $key => $value) {
$env = $this->injectEnv($env, $key, $value);
}
file_put_contents($this->base_path . '/docker/environment.env', $env);
$section->writeln('<info>完成</info>');
} else {
$section->writeln('<fg=gray>生成环境变量文件 ... </><comment>跳过(已存在)</comment>');
}
});
}
// 将命令行入口标记为可执行
chmod($this->base_path . '/zhamao', 0755);
return 0;
@ -105,10 +120,15 @@ class InitCommand extends Command
'/src/Globals/*.php',
];
return $this->getFilesFromPatterns($patterns);
}
private function getFilesFromPatterns(array $patterns): array
{
$files = [];
foreach ($patterns as $pattern) {
// TODO: 优化代码,避免在循环中使用 array_merge 以减少资源消耗
$files = array_merge($files, glob($this->getVendorPath($pattern)));
$files = array_merge($files, glob($this->getVendorPath($pattern), GLOB_BRACE));
}
return array_map(function ($file) {
return str_replace($this->getVendorPath(''), '', $file);
@ -173,4 +193,48 @@ class InitCommand extends Command
}
return $this->base_path . '/vendor/' . $package_name . $file;
}
private function extractFiles(array $files, OutputInterface $output): void
{
foreach ($files as $file) {
$output->write("<fg=gray>提取 {$file} ... </>");
if ($this->shouldExtractFile($file)) {
try {
$this->extractFile($file);
$output->write('<info>完成</info>');
} catch (InitException $e) {
$output->write('<error>失败</error>');
throw $e;
} finally {
$output->writeln('');
}
} else {
$output->writeln('<comment>跳过(已存在)</comment>');
}
}
}
private function injectEnv(string $env, string $key, string $value): string
{
$pattern = "/^{$key}=.+$/m";
if (preg_match($pattern, $env)) {
return preg_replace($pattern, "{$key}={$value}", $env);
}
return $env . PHP_EOL . "{$key}={$value}";
}
private function getEnvVariables(): array
{
return [
'REDIS_PASSWORD' => bin2hex(random_bytes(8)),
'POSTGRES_USER' => 'root',
'POSTGRES_PASSWORD' => bin2hex(random_bytes(8)),
'POSTGRES_APPLICATION_DATABASE' => 'zhamao',
'POSTGRES_APPLICATION_USER' => 'zhamao',
'POSTGRES_APPLICATION_USER_PASSWORD' => bin2hex(random_bytes(8)),
];
}
}