From 3ef91d3617e00f53b6a5ac01688a2cd9637e9a64 Mon Sep 17 00:00:00 2001 From: hide_d Date: Sun, 13 Mar 2022 18:56:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20install=EC=97=90=EC=84=9C=20composer=20?= =?UTF-8?q?update=EB=8F=84=20=EC=88=98=ED=96=89.=20update=20=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=20=EB=B3=80=EA=B2=BD=20-=20lock=20file=EC=9D=84=20git?= =?UTF-8?q?=EC=97=90=20=EC=B6=94=EA=B0=80=ED=95=98=EC=98=80=EC=9C=BC?= =?UTF-8?q?=EB=AF=80=EB=A1=9C,=20=EC=9D=B4=EC=97=90=20=EB=94=B0=EB=A6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- install.php | 101 +++++++++++++++++++++++++++++++++++--------- j_updateServer.php | 102 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 163 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 4d8e51a4..305d943f 100644 --- a/.gitignore +++ b/.gitignore @@ -57,12 +57,11 @@ test.* /pya /twe +/vendor phpinfo.php -vendor/ezyang/htmlpurifier/library/HTMLPurifier/DefinitionCache/* *.sqlite3 ingame/js/* ingame/css/* gateway/js/* gateway/css/* -vendor/phpunit/phpunit/.phpunit.result.cache diff --git a/install.php b/install.php index 825bf708..e4ec4ba7 100644 --- a/install.php +++ b/install.php @@ -3,27 +3,20 @@ namespace sammo; set_time_limit(600); -function tryNpmInstall() +function tryComposerInstall() { - $npmResultPath = 'npm_recent.json.log'; - $packageJsonPath = 'package.json'; - $packageJsonLockPath = 'package-lock.json'; + $resultPath = 'composer_result.json.log'; + $lockPath = 'composer.lock'; + $runCode = "php composer.phar install"; - $packageJsonHash = hash_file('sha512', $packageJsonPath); $timestamp = time(); - if (file_exists($npmResultPath) && file_exists($packageJsonLockPath)) { + if (file_exists($resultPath) && file_exists($lockPath)) { do { - $result = json_decode(file_get_contents($npmResultPath)); - $oldJsonHash = $result->packageJsonHash; - $oldTimestamp = $result->updateTimestamp; + $result = json_decode(file_get_contents($resultPath)); + $oldJsonHash = $result->lockHash; + $lockHash = hash_file('sha512', $lockPath); - //1. package.json 파일이 다르면 업데이트. - if ($packageJsonHash != $oldJsonHash) { - break; - } - - //2. package-lock.json 업데이트가 2주를 초과했다면 업데이트. - if($oldTimestamp + 60*60*24*14 < $timestamp){ + if ($lockHash != $oldJsonHash) { break; } @@ -32,7 +25,7 @@ function tryNpmInstall() } while (0); } - exec("npm install", $output, $result_code); + exec($runCode, $output, $result_code); if ($result_code != 0) { Json::die([ 'result' => false, @@ -40,12 +33,76 @@ function tryNpmInstall() ]); } - file_put_contents($npmResultPath, json_encode([ - 'packageJsonHash'=>$packageJsonHash, + if (!file_exists($lockPath)){ + Json::die([ + 'result' => false, + 'reason' => "no lockfile: {$lockPath}" + ]); + } + $lockHash = hash_file('sha512', $lockPath); + + + file_put_contents($resultPath, json_encode([ + 'lockHash'=>$lockHash, 'updateTimestamp'=>$timestamp, ])); return true; } + + +function tryNpmInstall() +{ + $resultPath = 'npm_recent.json.log'; + $lockPath = 'package-lock.json'; + $runCode = "npm ci"; + $runAltCode = "npm install"; + + $timestamp = time(); + if (file_exists($resultPath) && file_exists($lockPath)) { + do { + $result = json_decode(file_get_contents($resultPath)); + $oldJsonHash = $result->lockHash; + $lockHash = hash_file('sha512', $lockPath); + + if ($lockHash != $oldJsonHash) { + break; + } + + //그것도 아니라면 업데이트하지 않겠다. + return false; + } while (0); + } + + if(file_exists($lockPath)){ + exec($runCode, $output, $result_code); + } + else{ + exec($runAltCode, $output, $result_code); + } + + if ($result_code != 0) { + Json::die([ + 'result' => false, + 'reason' => $output + ]); + } + + if (!file_exists($lockPath)){ + Json::die([ + 'result' => false, + 'reason' => "no lockfile: {$lockPath}" + ]); + } + $lockHash = hash_file('sha512', $lockPath); + + + file_put_contents($resultPath, json_encode([ + 'lockHash'=>$lockHash, + 'updateTimestamp'=>$timestamp, + ])); + return true; +} + function genJS() { $command = "./node_modules/.bin/webpack build"; @@ -58,6 +115,12 @@ function genJS() ]); } } + +if(tryComposerInstall()){ + header('location:install.php'); + die(); +} + if(tryNpmInstall()){ genJS(); } diff --git a/j_updateServer.php b/j_updateServer.php index 8542a83e..49fbbb00 100644 --- a/j_updateServer.php +++ b/j_updateServer.php @@ -66,27 +66,20 @@ function genJS($server) } } -function tryNpmInstall() +function tryComposerInstall() { - $npmResultPath = './npm_recent.json.log'; - $packageJsonPath = './package.json'; - $packageJsonLockPath = './package-lock.json'; + $resultPath = 'composer_result.json.log'; + $lockPath = 'composer.lock'; + $runCode = "php composer.phar install"; - $packageJsonHash = hash_file('sha512', $packageJsonPath); $timestamp = time(); - if (file_exists($npmResultPath) && file_exists($packageJsonLockPath)) { + if (file_exists($resultPath) && file_exists($lockPath)) { do { - $result = json_decode(file_get_contents($npmResultPath)); - $oldJsonHash = $result->packageJsonHash; - $oldTimestamp = $result->updateTimestamp; + $result = json_decode(file_get_contents($resultPath)); + $oldJsonHash = $result->lockHash; + $lockHash = hash_file('sha512', $lockPath); - //1. package.json 파일이 다르면 업데이트. - if ($packageJsonHash != $oldJsonHash) { - break; - } - - //2. package-lock.json 업데이트가 2주를 초과했다면 업데이트. - if ($oldTimestamp + 60 * 60 * 24 * 14 < $timestamp) { + if ($lockHash != $oldJsonHash) { break; } @@ -95,7 +88,7 @@ function tryNpmInstall() } while (0); } - exec("npm install", $output, $result_code); + exec($runCode, $output, $result_code); if ($result_code != 0) { Json::die([ 'result' => false, @@ -103,16 +96,80 @@ function tryNpmInstall() ]); } - file_put_contents($npmResultPath, json_encode([ - 'packageJsonHash' => $packageJsonHash, - 'updateTimestamp' => $timestamp, + if (!file_exists($lockPath)){ + Json::die([ + 'result' => false, + 'reason' => "no lockfile: {$lockPath}" + ]); + } + $lockHash = hash_file('sha512', $lockPath); + + + file_put_contents($resultPath, json_encode([ + 'lockHash'=>$lockHash, + 'updateTimestamp'=>$timestamp, ])); return true; } -//묻고 따지지 않고 일단 npm install은 시도한다. + +function tryNpmInstall() +{ + $resultPath = 'npm_recent.json.log'; + $lockPath = 'package-lock.json'; + $runCode = "npm ci"; + $runAltCode = "npm install"; + + $timestamp = time(); + if (file_exists($resultPath) && file_exists($lockPath)) { + do { + $result = json_decode(file_get_contents($resultPath)); + $oldJsonHash = $result->lockHash; + $lockHash = hash_file('sha512', $lockPath); + + if ($lockHash != $oldJsonHash) { + break; + } + + //그것도 아니라면 업데이트하지 않겠다. + return false; + } while (0); + } + + if(file_exists($lockPath)){ + exec($runCode, $output, $result_code); + } + else{ + exec($runAltCode, $output, $result_code); + } + + if ($result_code != 0) { + Json::die([ + 'result' => false, + 'reason' => $output + ]); + } + + if (!file_exists($lockPath)){ + Json::die([ + 'result' => false, + 'reason' => "no lockfile: {$lockPath}" + ]); + } + $lockHash = hash_file('sha512', $lockPath); + + + file_put_contents($resultPath, json_encode([ + 'lockHash'=>$lockHash, + 'updateTimestamp'=>$timestamp, + ])); + return true; +} + +//묻고 따지지 않고 일단 composer install, npm install은 시도한다. //hwe 업데이트인 경우에만 한번 더 부른다. +tryComposerInstall(); if (tryNpmInstall()) { genJS(Util::array_last_key(ServConfig::getServerList())); } @@ -268,7 +325,8 @@ if ($server == $baseServerName) { ])); } - //git 업데이트했는데, package.json이 바뀌면 곤란하니까 + //git 업데이트했는데, package가 바뀌면 곤란하니까 + tryComposerInstall(); tryNpmInstall(); genJS($server);