Skip to content

Commit

Permalink
アップデータにて、利用可能なバージョンに関わらず、composer.json の内容でダウンロードするオプションを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Jun 22, 2024
1 parent b7947ab commit 017b299
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 260 deletions.
13 changes: 12 additions & 1 deletion plugins/baser-core/src/Command/ComposerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ protected function buildOptionParser(\Cake\Console\ConsoleOptionParser $parser):
'help' => __d('baser_core', '実行対象ディレクトリ'),
'default' => ''
]);
$parser->addOption('force', [
'help' => __d('baser_core', '指定したバージョンを設定せず composer.json の内容で update する'),
'default' => ''
]);
return $parser;
}

Expand All @@ -72,8 +76,15 @@ public function execute(Arguments $args, ConsoleIo $io)
$io->error($message);
$this->abort();
}

BcComposer::clearCache();
$result = BcComposer::require('baser-core', $args->getArgument('version'));

if($args->getOption('force')) {
$result = BcComposer::update();
} else {
$result = BcComposer::require('baser-core', $args->getArgument('version'));
}

if($result['code'] === 0) {
$io->out(__d('baser_core', 'Composer によるアップデートが完了しました。'));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function update(PluginsAdminServiceInterface $service, $name = '')
try {
$service->rollbackCore(
$request->getData('currentVersion'),
$request->getData('php'),
$request->getData('php')
);
$this->BcMessage->setError(__d('baser_core', 'コアファイルを元に戻しました。'));
} catch (\Throwable $e) {
Expand Down Expand Up @@ -183,6 +183,7 @@ public function get_core_update(PluginsAdminServiceInterface $service)
$service->getCoreUpdate(
$request->getData('targetVersion')?? '',
$request->getData('php')?? 'php',
$request->getData('force'),
);
} catch (\Throwable $e) {
$this->BcMessage->setError($e->getMessage());
Expand Down
19 changes: 12 additions & 7 deletions plugins/baser-core/src/Service/Admin/PluginsAdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ public function getViewVarsForUpdate(EntityInterface $entity): array
$isWritableVendor = is_writable(ROOT . DS . 'vendor');
$isWritableComposerJson = is_writable(ROOT . DS . 'composer.json');
$isWritableComposerLock = is_writable(ROOT . DS . 'composer.lock');

$requireUpdate = $this->isRequireUpdate(
$programVersion,
$dbVersion,
$availableVersion
);
if($entity->name === 'BaserCore') {
$isUpdatable = ($requireUpdate && $isWritableVendor && $isWritableComposerJson && $isWritableComposerLock);
} else {
$isUpdatable = $requireUpdate;
}
return [
'plugin' => $entity,
'scriptNum' => $scriptNum,
Expand All @@ -92,17 +101,13 @@ public function getViewVarsForUpdate(EntityInterface $entity): array
'programVerPoint' => $programVerPoint,
'availableVersion' => $availableVersion,
'log' => $this->getUpdateLog(),
'requireUpdate' => $this->isRequireUpdate(
$programVersion,
$dbVersion,
$availableVersion
),
'coreDownloaded' => Cache::read('coreDownloaded', '_bc_update_'),
'php' => $this->whichPhp(),
'isCore' => $entity->name === 'BaserCore',
'isWritableVendor' => $isWritableVendor,
'isWritableComposerJson' => $isWritableComposerJson,
'isWritableComposerLock' => $isWritableComposerLock,
'isWritablePackage' => ($isWritableVendor && $isWritableComposerJson && $isWritableComposerLock)
'isUpdatable' => $isUpdatable
];
}

Expand Down
54 changes: 29 additions & 25 deletions plugins/baser-core/src/Service/PluginsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function install($name, bool $permission = true, $connection = 'default')
{
$dbInit = false;
$config = $this->Plugins->getPluginConfig($name);
if($config) {
if ($config) {
$dbInit = $config->db_init;
}
$options = [
Expand Down Expand Up @@ -188,7 +188,7 @@ public function update(string $name, string $connection = 'default'): ?bool
unlink(LOGS . 'update.log');
}

$ids = [];
$ids = [];
if ($name === 'BaserCore') {
$pluginNames = array_merge(['BaserCore'], Configure::read('BcApp.corePlugins'));
$ids = $this->detachAll();
Expand All @@ -212,16 +212,16 @@ public function update(string $name, string $connection = 'default'): ?bool
$plugin = $pluginCollection->create($pluginName);
$migrate = false;
if (method_exists($plugin, 'migrate')) {
try {
$plugin->migrate($options);
} catch (\Throwable $e) {
if($ids) $this->attachAllFromIds($ids);
BcUpdateLog::set(__d('baser_core', 'アップデート処理が途中で失敗しました。'));
BcUpdateLog::set($e->getMessage());
BcUtil::clearAllCache();
BcUpdateLog::save();
return false;
}
try {
$plugin->migrate($options);
} catch (\Throwable $e) {
if ($ids) $this->attachAllFromIds($ids);
BcUpdateLog::set(__d('baser_core', 'アップデート処理が途中で失敗しました。'));
BcUpdateLog::set($e->getMessage());
BcUtil::clearAllCache();
BcUpdateLog::save();
return false;
}
$migrate = true;
}
$plugins[$pluginName] = [
Expand All @@ -231,7 +231,7 @@ public function update(string $name, string $connection = 'default'): ?bool
];
}

if(!$plugins) {
if (!$plugins) {
BcUpdateLog::set(__d('baser_core', '登録済のプラグインが見つかりませんでした。先にインストールを実行してください。'));
BcUpdateLog::save();
return false;
Expand All @@ -250,7 +250,7 @@ public function update(string $name, string $connection = 'default'): ?bool
$plugin['instance']->migrations->rollback($options);
}
}
if($ids) $this->attachAllFromIds($ids);
if ($ids) $this->attachAllFromIds($ids);
BcUpdateLog::set(__d('baser_core', 'アップデート処理が途中で失敗しました。'));
BcUpdateLog::set($e->getMessage());
BcUtil::clearAllCache();
Expand All @@ -271,7 +271,7 @@ public function update(string $name, string $connection = 'default'): ?bool
$plugin['instance']->migrations->rollback($options);
}
}
if($ids) $this->attachAllFromIds($ids);
if ($ids) $this->attachAllFromIds($ids);
BcUpdateLog::set(__d('baser_core', 'アップデート処理が途中で失敗しました。'));
BcUpdateLog::set($e->getMessage());
BcUtil::clearAllCache();
Expand All @@ -283,7 +283,7 @@ public function update(string $name, string $connection = 'default'): ?bool

BcUtil::clearAllCache();
BcUpdateLog::save();
if($ids) $this->attachAllFromIds($ids);
if ($ids) $this->attachAllFromIds($ids);

return true;
}
Expand Down Expand Up @@ -317,7 +317,7 @@ public function rollbackCore(string $currentVersion, string $php): void
*/
public function updateCoreFiles()
{
if(!is_dir(TMP . 'update' . DS . 'vendor')) {
if (!is_dir(TMP . 'update' . DS . 'vendor')) {
throw new BcException(__d('baser_core', 'ダウンロードした最新版が見つかりませんでした。'));
}

Expand All @@ -329,7 +329,7 @@ public function updateCoreFiles()
(new Folder())->delete(ROOT . DS . 'vendor');

// 最新版に更新
if(!(new Folder(TMP . 'update' . DS . 'vendor'))->copy(ROOT . DS . 'vendor')) {
if (!(new Folder(TMP . 'update' . DS . 'vendor'))->copy(ROOT . DS . 'vendor')) {
$zip->extract(TMP . 'update' . DS . 'vendor.zip', ROOT . DS . 'vendor');
throw new BcException(__d('baser_core', '最新版のファイルをコピーできませんでした。'));
}
Expand Down Expand Up @@ -718,7 +718,7 @@ public function add(array $postData)
*/
public function getAvailableCoreVersionInfo()
{
if(!BcSiteConfig::get('use_update_notice')) return [];
if (!BcSiteConfig::get('use_update_notice')) return [];

$coreReleaseInfo = Cache::read('coreReleaseInfo', '_bc_update_');
if (!$coreReleaseInfo) {
Expand Down Expand Up @@ -753,11 +753,11 @@ public function getAvailableCoreVersionInfo()
$currentVerPoint = BcUtil::verpoint($currentVersion);
$latestVerPoint = BcUtil::verpoint($latest);
// 現在のパッケージが開発版の場合は無視
if($currentVerPoint === false) break;
if ($currentVerPoint === false) break;
// アップデートバージョンが開発版の場合は無視
if($latestVerPoint === false) continue;
if ($latestVerPoint === false) continue;
// アップデートバージョンが現在のパッケージのバージョンより小さい場合は無視
if($currentVerPoint > $latestVerPoint) break;
if ($currentVerPoint > $latestVerPoint) break;

if ($currentVersion === $version) break;
$versions[] = $version;
Expand Down Expand Up @@ -786,7 +786,7 @@ public function getAvailableCoreVersionInfo()
* @noTodo
* @unitTest
*/
public function getCoreUpdate(string $targetVersion, string $php)
public function getCoreUpdate(string $targetVersion, string $php, bool $force = false)
{
if (function_exists('ini_set')) {
ini_set('max_execution_time', 0);
Expand All @@ -796,10 +796,10 @@ public function getCoreUpdate(string $targetVersion, string $php)
unlink(LOGS . 'update.log');
}

if(!is_dir(TMP . 'update')) {
if (!is_dir(TMP . 'update')) {
mkdir(TMP . 'update', 0777);
}
if(!is_dir(TMP . 'update' . DS . 'vendor')) {
if (!is_dir(TMP . 'update' . DS . 'vendor')) {
$folder = new Folder(ROOT . DS . 'vendor');
$folder->copy(TMP . 'update' . DS . 'vendor');
}
Expand All @@ -808,6 +808,10 @@ public function getCoreUpdate(string $targetVersion, string $php)

// Composer 実行
$command = $php . ' ' . ROOT . DS . 'bin' . DS . 'cake.php composer ' . $targetVersion . ' --php ' . $php . ' --dir ' . TMP . 'update';
if ($force) {
$command .= ' --force true';
}

exec($command, $out, $code);
if ($code !== 0) throw new BcException(__d('baser_core', '最新版のダウンロードに失敗しました。ログを確認してください。'));

Expand Down
13 changes: 12 additions & 1 deletion plugins/baser-core/src/Utility/BcComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public static function require(string $package, string $version)
return self::execCommand("require baserproject/{$package}:{$version} --with-all-dependencies --ignore-platform-req=ext-xdebug");
}

/**
* composer update 実行
* @return array
* @checked
* @noTodo
*/
public static function update()
{
return self::execCommand('update --with-all-dependencies --ignore-platform-req=ext-xdebug');
}

/**
* composer install 実行
*
Expand All @@ -164,7 +175,7 @@ public static function require(string $package, string $version)
*/
public static function install()
{
return self::execCommand('install');
return self::execCommand('install --with-all-dependencies --ignore-platform-req=ext-xdebug');
}

/**
Expand Down
82 changes: 50 additions & 32 deletions plugins/bc-admin-third/src/js/admin/plugins/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ const updateForm = {
*/
isWritablePackage: false,

/**
* アップデートできるかどうか
*/
isUpdatable: false,

/**
* 起動処理
*/
mounted() {
const script = $("#AdminPluginsUpdateScript");
this.plugin = script.attr('data-plugin');
this.isWritablePackage = script.attr('data-isWritablePackage');
this.isUpdatable = script.attr('data-isUpdatable');
if(this.isUpdatable === undefined) this.isUpdatable = false;
this.registerEvents();
this.toggleUpdate();
},
Expand All @@ -49,35 +55,35 @@ const updateForm = {
*/
update() {
if (confirm(bcI18n.confirmMessage1)) {
if(updateForm.plugin !== 'BaserCore') {
if (updateForm.plugin !== 'BaserCore') {
$.bcUtil.showLoader();
return true;
}
$.bcToken.check(function() {
$.bcToken.check(function () {
$.bcUtil.showLoader();
$.bcUtil.hideMessage();
axios.post($.bcUtil.apiAdminBaseUrl + 'baser-core/plugins/update_core_files.json', {}, {
headers: {
'X-CSRF-Token': $.bcToken.key
}
})
.then(response => {
let message = response.data.message + bcI18n.updateMessage1;
$.bcUtil.showNoticeMessage(message);
$(window).scrollTop(0);
$.bcUtil.showLoader();
// フォーム送信
$("#PluginUpdateForm").submit();
})
.catch(error => {
if (error.response.status === 500) {
$.bcUtil.showAlertMessage(error.response.data.message);
} else {
$.bcUtil.showAlertMessage('予期せぬエラーが発生しました。システム管理者に連絡してください。');
}
$.bcUtil.hideLoader();
$(window).scrollTop(0);
});
.then(response => {
let message = response.data.message + bcI18n.updateMessage1;
$.bcUtil.showNoticeMessage(message);
$(window).scrollTop(0);
$.bcUtil.showLoader();
// フォーム送信
$("#PluginUpdateForm").submit();
})
.catch(error => {
if (error.response.status === 500) {
$.bcUtil.showAlertMessage(error.response.data.message);
} else {
$.bcUtil.showAlertMessage('予期せぬエラーが発生しました。システム管理者に連絡してください。');
}
$.bcUtil.hideLoader();
$(window).scrollTop(0);
});
}, {hideLoader: false});
}
return false;
Expand All @@ -90,19 +96,31 @@ const updateForm = {
const $btnUpdate = $("#BtnUpdate");
const $btnDownload = $("#BtnDownload");
const $phpNotice = $(".php-notice");
if(updateForm.plugin !== 'BaserCore') return;
if($("#php").val()) {
if($btnUpdate.length) $btnUpdate.removeAttr('disabled');
if($btnDownload.length) $btnDownload.removeAttr('disabled');
$phpNotice.hide();
const $inputPhp = $("#php");

if (updateForm.plugin === 'BaserCore') {
if ($inputPhp.val() !== ''){
if(updateForm.isUpdatable) {
$btnUpdate.removeAttr('disabled');
}
$btnDownload.removeAttr('disabled');
} else {
$btnUpdate.attr('disabled', 'disabled');
$btnDownload.attr('disabled', 'disabled');
}
if ($inputPhp.val()) {
$phpNotice.hide();
} else {
$phpNotice.show();
}
} else {
if($btnUpdate.length) $btnUpdate.attr('disabled', 'disabled');
if($btnDownload.length) $btnDownload.attr('disabled', 'disabled');
$phpNotice.show();
}
if(!updateForm.isWritablePackage) {
if($btnUpdate.length) $btnUpdate.attr('disabled', 'disabled');
if($btnDownload.length) $btnDownload.attr('disabled', 'disabled');
if (updateForm.isUpdatable) {
$btnUpdate.removeAttr('disabled');
$btnDownload.removeAttr('disabled');
} else {
$btnUpdate.attr('disabled', 'disabled');
$btnDownload.attr('disabled', 'disabled');
}
}
}

Expand Down
Loading

0 comments on commit 017b299

Please sign in to comment.