adjust git strategy

This commit is contained in:
crazywhalecc
2023-03-29 20:43:29 +08:00
committed by Jerry
parent b733781eff
commit d56f4b2343

View File

@@ -43,6 +43,7 @@ class GitStrategy extends PluginInstallStrategy
$composer = ZMUtil::getComposerMetadata($this->root_composer_path); $composer = ZMUtil::getComposerMetadata($this->root_composer_path);
$origin_composer = $composer; $origin_composer = $composer;
$already_has_repo = false; $already_has_repo = false;
// 不破坏原有队列,加入 GitHub 的 repo // 不破坏原有队列,加入 GitHub 的 repo
if (!isset($composer['repositories'])) { if (!isset($composer['repositories'])) {
$composer['repositories'] = []; $composer['repositories'] = [];
@@ -56,6 +57,8 @@ class GitStrategy extends PluginInstallStrategy
break; break;
} }
} }
// 缓存一下对应的 repositories 属于的插件名称
if (!$already_has_repo) { if (!$already_has_repo) {
$composer['repositories'][] = [ $composer['repositories'][] = [
'type' => $is_github ? 'github' : 'git', 'type' => $is_github ? 'github' : 'git',
@@ -63,32 +66,42 @@ class GitStrategy extends PluginInstallStrategy
'.belongs' => $plugin_name, '.belongs' => $plugin_name,
]; ];
} }
// 写入 composer.json // 写入 composer.json
if (ZMUtil::putComposerMetadata($this->root_composer_path, $composer) === false) { if (ZMUtil::putComposerMetadata($this->root_composer_path, $composer) === false) {
$this->error = '写入 composer.json 失败'; $this->error = '写入 composer.json 失败';
return false; return false;
} }
$env = getenv('COMPOSER_EXECUTABLE');
if ($env === false) { // 获取 Composer 命令行名称
$env = 'composer'; $env = ZMUtil::getComposerExecutable();
}
// 这里返回 null 表明没调用成功 GitHub API 拿 composer.json 的元信息
if ($plugin_name === null) { if ($plugin_name === null) {
$this->error = '没有从 Git 获取到插件的元信息,目前无法从 GitHub 以外的 Git 仓库下载插件,后续会更新!'; $this->error = '没有从 Git 获取到插件的元信息,目前无法从 GitHub 以外的 Git 仓库下载插件,后续会更新!';
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
return false; return false;
} }
// 这里为空字符串表明插件名称不对,获取了空的,说明元 composer.json 文件出错
if ($plugin_name === '') { if ($plugin_name === '') {
$this->error = '获取插件名称失败!'; $this->error = '获取插件名称失败!';
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
return false; return false;
} }
// Git 方式拉取插件,在 Linux 系统上监听一下 Ctrl+C这样即使用户想 Ctrl+C 断掉,也可以方便地恢复原来的 composer.json 文件内容。
if (function_exists('pcntl_signal')) { if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, function () use ($origin_composer) { pcntl_signal(SIGINT, function () use ($origin_composer) {
echo "强行中断,恢复 Composer 中\n"; echo "强行中断,恢复 Composer 中\n";
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
}); });
} }
// 引入
passthru("{$env} require {$plugin_name}", $code); passthru("{$env} require {$plugin_name}", $code);
// 恢复 SIGINT 信号
if (function_exists('pcntl_signal')) { if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, SIG_IGN); pcntl_signal(SIGINT, SIG_IGN);
} }