123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace DistributeHelper;
- error_reporting(E_ALL & ~E_NOTICE);
- include "Utility.php";
- class PackageClass
- {
- private static $config = null; //配置信息
- //当所有包都安装完毕后
- public static function postUpdate($vendorDir,$rootPkg,$vendorPkgs)
- {
- //初始化
- if(! self::initialize($vendorDir)){
- self::setProgressFailed('INITIALIZE_FAILED');
- return false;
- }
-
- //分发模式
- if( self::$config['distribute-mode'] == 'install'){
- self::install($vendorDir,$rootPkg,$vendorPkgs);
- }
- else{
- self::upgrade($vendorDir,$rootPkg,$vendorPkgs);
- }
- return true;
- }
-
- private static function install($vendorDir,$rootPkg,$vendorPkgs){
- //依赖库都已下载,默认先给个进度
- self::setProgressStep1(100);
-
- //生成module.ini文件
- self::createModuleIni($vendorDir,$vendorPkgs);
-
- //逐个依赖仓库操作
- self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
-
- //顶层仓库操作
- self::postUpdateRootPkg($vendorDir,$rootPkg);
-
- //全部操作完毕
- self::setProgressOK(true);
- }
- private static function upgrade($vendorDir,$rootPkg,$vendorPkgs){
- //依赖库都已下载,默认先给个进度
- self::setProgressStep1(100);
-
- //生成module.ini文件
- self::createModuleIni($vendorDir,$vendorPkgs);
-
- //逐个依赖仓库操作
- self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
-
- //顶层仓库操作
- self::postUpdateRootPkg($vendorDir,$rootPkg);
-
- //全部操作完毕
- self::setProgressOK(true);
- }
- private static function createModuleIni($vendorDir,$vendorPkgs)
- {
- $content = '[MODULE]' . PHP_EOL;
- foreach($vendorPkgs as $i=>$pkg){
- if( in_array($pkg->getName(),['util/distribute-helper']) )
- continue;
- $cfgPath = pathJoin($vendorDir,$pkg->getName(),'composer.json');
- $data = json_decode(file_get_contents($cfgPath), true);
- $moduleName = $data["distribute-helper"]["module-name"];
- if(!$moduleName){
- logError('bad format. no module-name node exists.',$cfgPath);
- return false;
- }
- $content .= $moduleName . '=' . $pkg->getName() . ',' . $pkg->getPrettyVersion() . PHP_EOL;
- }
- file_put_contents(MODULEINI_PATH,$content);
- return true;
- }
- private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
- {
- $count = count($vendorPkgs);
- foreach($vendorPkgs as $i=>$pkg){
- //获取包信息
- $repoDir = pathJoin($vendorDir,$pkg->getName());
- $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
-
- //获取当前包的module文件
- self::fetchModuleFiles($packageInfo);
- self::setProgressStep2($count,$i,1);
-
- //获取当前包的common文件
- self::fetchCommonFiles($packageInfo);
- self::setProgressStep2($count,$i,2);
- //拷贝当前包的config文件
- self::copyConfigFiles($packageInfo);
- self::setProgressStep2($count,$i,3);
- }
- }
- private static function postUpdateRootPkg($vendorDir,$pkg)
- {
- //获取包信息
- $repoDir = dirname($vendorDir);
- $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
-
- //注册全局路径
- self::registerPath($packageInfo);
-
- //注册自启动程序
- self::registerAutorun($packageInfo);
- }
- //初始化
- private static function initialize($vendorDir)
- {
- //创建.distribute目录
- $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
- if( !file_exists($distributeDir) ){
- createDirectory($distributeDir);
- }
-
- //设置日志文件路径
- if(! defined(LOG_PATH)){
- $logPath = pathJoin($distributeDir,'log.txt');
- define('LOG_PATH',$logPath);
- if(file_exists($logPath))
- unlink($logPath);
- }
-
- //设置进度文件路径
- if(! defined(PROGRESS_PATH)){
- $progressPath = pathJoin($distributeDir,'progress.txt');
- define('PROGRESS_PATH',$progressPath);
- if(file_exists($progressPath))
- unlink($progressPath);
- }
-
- //创建module目录
- $moduleDir = pathJoin(dirname($vendorDir),'module');
- if( !file_exists($moduleDir) ){
- createDirectory($moduleDir);
- }
-
- //设置 module.ini 路径
- if(! defined(MODULEINI_PATH)){
- $moduleiniPath = pathJoin($moduleDir,'module.ini');
- define('MODULEINI_PATH',$moduleiniPath);
- if(file_exists($moduleiniPath))
- unlink($moduleiniPath);
- }
-
- //获取部署配置信息
- $cfgPath = pathJoin($distributeDir,'config.json');
- if(! file_exists($cfgPath) ){
- logInfo(".distribute/config.json file not exists,use default config. ");
- self::$config = defaultConfig();
- }
- else{
- self::$config = loadConfig($cfgPath);
- if(!self::$config){
- logError("Failed to load distribute config",$cfgPath);
- return false;
- }
- logInfo("succeeded to decode config file -- .distribute/config.json");
- }
- return true;
- }
-
- private static function setProgressStep1($percent)
- {
- $progress = 0.1 * $percent;
- file_put_contents(PROGRESS_PATH,strval($progress));
- }
- private static function setProgressStep2($total,$index,$step)
- {
- $index = $index + $step/3.0;
- $progress = 10 + intval(90.0 * $index / $total);
- file_put_contents(PROGRESS_PATH,strval($progress));
- }
- private static function setProgressFailed($msg)
- {
- file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
- }
- private static function setProgressOK($restart)
- {
- if($restart){
- file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
- }
- else{
- file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
- }
- }
- //获取包相关信息
- private static function getPackageInfo($vendorDir,$repoDir,$pkg)
- {
- $config = self::$config;
-
- $cfgPath = pathJoin($repoDir,'composer.json');
- $data = json_decode(file_get_contents($cfgPath), true);
- $moduleName = $data["distribute-helper"]["module-name"];
-
- //获取安装目录
- $rootDir = dirname($vendorDir);
- $commonDir = pathJoin($rootDir,'siscom');
- $configDir = pathJoin($rootDir,'config',$moduleName);
- $moduleDir = pathJoin($rootDir,'module',$moduleName);
-
- //获取缓存目录
- $cacheDir = pathJoin($config['cache-dir'],str_replace('/','-',$pkg->getName()));
-
- //返回包的相关信息
- return array(
- "repoName" => $pkg->getName(),
- "version" => $pkg->getPrettyVersion(),
- "cacheDir" => $cacheDir,
- "toolDir" => $config['tool-dir'],
- "ossHost" => $config['oss-host'],
- "moduleName" => $moduleName,
- "commonDir" => $commonDir,
- "configDir" => $configDir,
- "moduleDir" => $moduleDir,
- "helper" => $data["distribute-helper"],
- );
- }
-
- //下载包module文件
- private static function fetchModuleFiles($packageInfo)
- {
- $toolDir = $packageInfo['toolDir'];
- $cacheDir = $packageInfo['cacheDir'];
- $repoName = $packageInfo['repoName'];
- $version = $packageInfo['version'];
- $moduleDir = $packageInfo['moduleDir'];
- $ossHost = $packageInfo['ossHost'];
-
- $moduleFiles = $packageInfo['helper']['module-files'];
- if(! $moduleFiles){
- return;
- }
-
- foreach($moduleFiles as $fileInfo){
- $fileName = $fileInfo['name'];
- //下载文件到缓存区
- $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
- if(! $filePath ){
- logError($repoName,"fetch module file failed",$fileName);
- return false;
- }
- logInfo("succeeded to fetch module file --",$repoName,$fileName);
- // 解压文件
- if( isZipFile($fileName) ){
- if(! unzipFile($toolDir,$filePath,$moduleDir)) {
- logError($repoName,"unzip module file failed",$fileName);
- return false;
- }
- }
- }
- return true;
- }
-
- //下载包common文件
- private static function fetchCommonFiles($packageInfo)
- {
- $toolDir = $packageInfo['toolDir'];
- $cacheDir = $packageInfo['cacheDir'];
- $repoName = $packageInfo['repoName'];
- $version = $packageInfo['version'];
- $commonDir = $packageInfo['commonDir'];
- $ossHost = $packageInfo['ossHost'];
- $commonFiles = $packageInfo['helper']['common-files'];
- if(! $commonFiles){
- return;
- }
- foreach($commonFiles as $fileInfo){
- $fileName = $fileInfo['name'];
- //下载文件到缓存区
- $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
- if(! $filePath ){
- logError($repoName,"fetch common file failed",$fileName);
- return false;
- }
- logInfo("succeeded to fetch common file --",$repoName,$fileName);
- // 解压文件
- if( isZipFile($fileName) ){
- if(! unzipFile($toolDir,$filePath,$commonDir)) {
- logError($repoName,"unzip common file failed",$fileName);
- return false;
- }
- }
- }
- return true;
- }
-
- //拷贝config文件
- private static function copyConfigFiles($packageInfo)
- {
- $cacheDir = $packageInfo['cacheDir'];
- $repoName = $packageInfo['repoName'];
- $version = $packageInfo['version'];
- $configDir = $packageInfo['configDir'];
- $moduleDir = $packageInfo['moduleDir'];
- $configFiles = $packageInfo['helper']['config-files'];
- if(! $configFiles){
- return;
- }
- foreach($configFiles as $fileInfo){
- $fileName = $fileInfo['name'];
- $filePath = pathJoin($moduleDir,$fileName);
- if(! file_exists($filePath) ){
- logError($repoName,"config file not exists",$filePath);
- return false;
- }
- if(!$fileInfo['override'] && file_exists($targetPath)){
- logInfo($repoName,"skip copy config file",$targetPath);
- }
- $targetPath = pathJoin($configDir,$fileName);
- fileCopy($filePath,$targetPath);
- }
- return true;
- }
-
- private static function registerPath($packageInfo)
- {
- }
-
- private static function registerAutorun($packageInfo)
- {
- }
- }
|