Skip to content

Commit

Permalink
リリースコマンドを改善
Browse files Browse the repository at this point in the history
- バージョンを指定する仕様に変更(bin/cake create release 5.1.1)
- 生成するzipファイルにバージョン番号が入るようにした
- インストール時に composer update ではなく、composer install コマンドでインストールできるように lock ファイルを更新するようにした
  • Loading branch information
ryuring committed Sep 1, 2024
1 parent c58e146 commit 4e0ac0e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer_installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function command($phpPath)
exec($command, $out, $code);
if ($code !== 0) throw new Exception('composer のアップデートに失敗しました。(' . $command . ')');

$command = "cd " . ROOT_DIR . "; export HOME={$composerDir} ; yes | {$phpPath} {$composerDir}composer.phar update --ignore-platform-req=ext-xdebug";
$command = "cd " . ROOT_DIR . "; export HOME={$composerDir} ; yes | {$phpPath} {$composerDir}composer.phar install --ignore-platform-req=ext-xdebug";
exec($command, $out, $code);
if ($code !== 0) throw new Exception('ライブラリのインストールに失敗しました。<br>コマンド実行をお試しください<br>' . $command);

Expand Down
6 changes: 0 additions & 6 deletions plugins/baser-core/config/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@
*/
'coreReleaseUrl' => 'https://packagist.org/feeds/package.baserproject/baser-core.rss',

/**
* インストール時に composer.json にセットするバージョン
* @see \BaserCore\Utility\BcComposer::setupComposerForDistribution()
*/
'setupVersion' => '5.1.*',

/**
* リリースパッケージに不要なファイル
* @see \BaserCore\Command\CreateReleaseCommand::deleteExcludeFiles()
Expand Down
25 changes: 20 additions & 5 deletions plugins/baser-core/src/Command/CreateReleaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class CreateReleaseCommand extends Command
*/
protected function buildOptionParser(\Cake\Console\ConsoleOptionParser $parser): \Cake\Console\ConsoleOptionParser
{
$parser->addArgument('version', [
'help' => __d('baser_core', 'リリースバージョン'),
'required' => true
]);
$parser->addArgument('branch', [
'help' => __d('baser_core', 'クローン対象ブランチ'),
'default' => 'master',
Expand All @@ -62,15 +66,26 @@ public function execute(Arguments $args, ConsoleIo $io)
if(is_dir($packagePath)) {
(new BcFolder($packagePath))->delete();
}
$version = $args->getArgument('version');

$io->out(__d('baser_core', 'リリースパッケージを作成します。', TMP));
$io->out();

$io->out(__d('baser_core', '- {0} にパッケージをクローンします。', TMP));
$this->clonePackage($packagePath, $args->getArgument('branch'));

$io->out(__d('baser_core', '- composer.json をセットアップします。'));
BcComposer::setupComposerForDistribution($packagePath);
$io->out(__d('baser_core', '- composer.json / composer.lock をセットアップします。'));
BcComposer::setup('', $packagePath);
$result = BcComposer::setupComposerForDistribution($version, true);
if($result['code'] === 0) {
$io->out(__d('baser_core', 'Composer による lock ファイルの更新に失敗アップデートが完了しました。'));
} else {
$message = __d('baser_core', 'Composer による lock ファイルの更新に失敗しました。ログを確認してください。');
$this->log($message);
$this->log(implode("\n", $result['out']));
$io->error($message);
$this->abort();
}

$io->out(__d('baser_core', '- プラグインを初期化します。'));
$this->deletePlugins($packagePath);
Expand All @@ -79,7 +94,7 @@ public function execute(Arguments $args, ConsoleIo $io)
$this->deleteExcludeFiles($packagePath);

$io->out(__d('baser_core', '- Zip ファイルを作成します。'));
$this->createZip($packagePath);
$this->createZip($packagePath, $version);

$io->out(__d('baser_core', '- クリーニング処理を実行します。'));
(new BcFolder($packagePath))->delete();
Expand Down Expand Up @@ -130,10 +145,10 @@ public function deletePlugins(string $packagePath)
* @noTodo
* @unitTest
*/
public function createZip(string $packagePath)
public function createZip(string $packagePath, string $version)
{
$zip = new ZipArchiver();
$zipFile = TMP . 'basercms.zip';
$zipFile = TMP . 'basercms-' . $version . '.zip';
if(file_exists($zipFile)) {
unlink($zipFile);
}
Expand Down
30 changes: 14 additions & 16 deletions plugins/baser-core/src/Utility/BcComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class BcComposer
public static function setup(string $php = '', $dir = '')
{
self::checkEnv();
if(!preg_match('/\/$/', $dir)) {
$dir .= '/';
}
self::$currentDir = $dir;
self::$cd = ($dir)? "cd " . $dir . ';': "cd " . ROOT . DS . ';';
self::$composerDir = ROOT . DS . 'composer' . DS;
Expand Down Expand Up @@ -246,27 +249,22 @@ public static function createCommand(string $command)

/**
* 配布用に composer.json をセットアップする
* @param string $packagePath
* @return void
* @param string $version
* @return array
* @noTodo
* @checked
* @unitTest
*/
public static function setupComposerForDistribution(string $packagePath)
public static function setupComposerForDistribution(string $version)
{
$composer = $packagePath . 'composer.json';
$file = new BcFile($composer);
$data = $file->read();
$regex = '/^(.+?) "replace": {.+?},\n(.+?)/s';
$data = preg_replace($regex, "$1$2", $data);
$regex = '/^(.+?"cakephp\/cakephp": ".+?",)(.+?)$/s';
$setupVersion = Configure::read('BcApp.setupVersion');
$replace = "$1\n \"baserproject/baser-core\": \"{$setupVersion}\",$2";
$data = preg_replace($regex, $replace, $data);
$file->write($data);
self::deleteReplace();
$result = self::require('baser-core', $version);
(new BcFolder(self::$currentDir . 'vendor'))->delete();
mkdir(self::$currentDir . 'vendor');
(new BcFile(self::$currentDir . 'vendor' . DS . '.gitkeep'))->create();
return $result;
}


/**
* changeMinimumStabilityToDev
*
Expand All @@ -276,7 +274,7 @@ public static function setupComposerForDistribution(string $packagePath)
*/
public static function changeMinimumStabilityToDev()
{
$file = new BcFile(self::$currentDir . DS . 'composer.json');
$file = new BcFile(self::$currentDir . 'composer.json');
$json = $file->read();

if(strpos($json, '"minimum-stability"') !== false) {
Expand All @@ -299,7 +297,7 @@ public static function changeMinimumStabilityToDev()
*/
public static function deleteReplace()
{
$file = new BcFile(self::$currentDir . DS . 'composer.json');
$file = new BcFile(self::$currentDir . 'composer.json');
$json = $file->read();
$json = preg_replace('/"replace"\s*:\s*?{[^}]+?},/', '', $json);
$file->write($json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function testExecute()
$this->assertErrorContains('Composer によるアップデートが失敗しました。update ログを確認してください。');

// composer実行(composer.json を配布用にセットアップ)
BcComposer::setupComposerForDistribution(ROOT . DS);
BcComposer::setup('', ROOT . DS);
BcComposer::setupComposerForDistribution('5.0.15');
$this->exec('composer 5.0.15');
$this->assertExitCode(Command::CODE_SUCCESS);
$this->assertOutputContains('Composer によるアップデートが完了しました。');
Expand Down Expand Up @@ -104,7 +105,8 @@ public function testExecuteOnUpdateTmp()
copy(ROOT . DS . 'composer.json', TMP . 'update' . DS . 'composer.json');
copy(ROOT . DS . 'composer.lock', TMP . 'update' . DS . 'composer.lock');
// composer.json を配布用にセットアップ
BcComposer::setupComposerForDistribution(TMP . 'update' . DS);
BcComposer::setup('', TMP . 'update' . DS);
BcComposer::setupComposerForDistribution('5.0.15');
// composer 実行
$this->exec('composer 5.0.15 --dir ' . TMP . 'update');
// バージョンを確認
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ public function test_get_core_update()
copy(ROOT . DS . 'composer.lock', ROOT . DS . 'composer.bak.lock');

// composer.json を配布用に更新
BcComposer::setupComposerForDistribution(ROOT . DS);
BcComposer::setup('', ROOT . DS);
BcComposer::setupComposerForDistribution('5.0.15');

$this->post('/baser/admin/baser-core/plugins/get_core_update', [
'targetVersion' => '5.0.15',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ public function testGetCoreUpdateAndUpdateCoreFiles()
copy(ROOT . DS . 'composer.lock', ROOT . DS . 'composer.bak.lock');

// composer.json を配布用に更新
BcComposer::setupComposerForDistribution(ROOT . DS);
BcComposer::setup('', ROOT . DS);
BcComposer::setupComposerForDistribution('5.0.15');

// getCoreUpdate 実行
$this->Plugins->getCoreUpdate('5.0.15', 'php');
Expand Down Expand Up @@ -615,7 +616,8 @@ public function testRollbackCore()
// composer.json をバックアップ
copy(ROOT . DS . 'composer.json', ROOT . DS . 'composer.bak.json');
// composer.json を配布用に更新
BcComposer::setupComposerForDistribution(ROOT . DS);
BcComposer::setup('', ROOT . DS);
BcComposer::setupComposerForDistribution('5.1.1');

// ロールバック
$this->Plugins->rollbackCore('5.0.15', 'php');
Expand Down
22 changes: 18 additions & 4 deletions plugins/baser-core/tests/TestCase/Utility/BcComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,32 @@ public function testInstall()
public function testSetupComposerForDistribution()
{
// composer.json をバックアップ
$composer = ROOT . DS . 'composer.json';
copy($composer, ROOT . DS . 'composer.json.bak');
$composer = TMP_TESTS . 'composer.json';
copy(ROOT . DS . 'composer.json', $composer);

// 実行
BcComposer::setupComposerForDistribution(ROOT . DS);
BcComposer::setup('', TMP_TESTS);

// 5.1.0 のテストの場合、5.1.1 との依存関係の問題があるためライブラリを調整
// このままでは、今後のリリースのタイミングでまた依存関係が変わる可能性があるため
// 特定のバージョンの composer.json を別途用意しておいた方が良さそう
// >>>
BcComposer::require('josegonzalez/dotenv', '^3.2');
sleep(1);
BcComposer::require('mobiledetect/mobiledetectlib', '^4.8.03');
// <<<

BcComposer::setupComposerForDistribution('5.1.0');
$file = new BcFile($composer);
$data = $file->read();
$this->assertNotFalse(strpos($data, '"baserproject/baser-core": '));
$this->assertFalse(strpos($data, '"replace": {'));
$this->assertFileExists(TMP_TESTS . 'composer.lock');

// バックアップをリストア
rename(ROOT . DS . 'composer.json.bak', ROOT . DS . 'composer.json');
unlink($composer);
unlink(TMP_TESTS . 'composer.lock');
(new BcFolder(TMP_TESTS . 'vendor'))->delete();
}

/**
Expand Down

0 comments on commit 4e0ac0e

Please sign in to comment.