$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) { } }