PackageClass.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace DistributeHelper;
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. include "Utility.php";
  5. class PackageClass
  6. {
  7. private static $config = null; //配置信息
  8. //当所有包都安装完毕后
  9. public static function postUpdate($vendorDir,$rootPkg,$vendorPkgs)
  10. {
  11. //初始化
  12. if(! self::initialize($vendorDir)){
  13. self::setProgressFailed('INITIALIZE_FAILED');
  14. return false;
  15. }
  16. //分发模式
  17. if( self::$config['distribute-mode'] == 'install'){
  18. self::install($vendorDir,$rootPkg,$vendorPkgs);
  19. }
  20. else{
  21. self::upgrade($vendorDir,$rootPkg,$vendorPkgs);
  22. }
  23. return true;
  24. }
  25. private static function install($vendorDir,$rootPkg,$vendorPkgs){
  26. //依赖库都已下载,默认先给个进度
  27. self::setProgressStep1(100);
  28. //生成module.ini文件
  29. self::createModuleIni($vendorDir,$vendorPkgs);
  30. //逐个依赖仓库操作
  31. self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
  32. //顶层仓库操作
  33. self::postUpdateRootPkg($vendorDir,$rootPkg);
  34. //全部操作完毕
  35. self::setProgressOK(true);
  36. }
  37. private static function upgrade($vendorDir,$rootPkg,$vendorPkgs){
  38. //依赖库都已下载,默认先给个进度
  39. self::setProgressStep1(100);
  40. //生成module.ini文件
  41. self::createModuleIni($vendorDir,$vendorPkgs);
  42. //逐个依赖仓库操作
  43. self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
  44. //顶层仓库操作
  45. self::postUpdateRootPkg($vendorDir,$rootPkg);
  46. //全部操作完毕
  47. self::setProgressOK(true);
  48. }
  49. private static function createModuleIni($vendorDir,$vendorPkgs)
  50. {
  51. $content = '[MODULE]' . PHP_EOL;
  52. foreach($vendorPkgs as $i=>$pkg){
  53. if( in_array($pkg->getName(),['util/distribute-helper']) )
  54. continue;
  55. $cfgPath = pathJoin($vendorDir,$pkg->getName(),'composer.json');
  56. $data = json_decode(file_get_contents($cfgPath), true);
  57. $moduleName = $data["distribute-helper"]["module-name"];
  58. if(!$moduleName){
  59. logError('bad format. no module-name node exists.',$cfgPath);
  60. return false;
  61. }
  62. $content .= $moduleName . '=' . $pkg->getName() . ',' . $pkg->getPrettyVersion() . PHP_EOL;
  63. }
  64. file_put_contents(MODULEINI_PATH,$content);
  65. return true;
  66. }
  67. private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
  68. {
  69. $count = count($vendorPkgs);
  70. foreach($vendorPkgs as $i=>$pkg){
  71. //获取包信息
  72. $repoDir = pathJoin($vendorDir,$pkg->getName());
  73. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  74. //获取当前包的module文件
  75. self::fetchModuleFiles($packageInfo);
  76. self::setProgressStep2($count,$i,1);
  77. //获取当前包的common文件
  78. self::fetchCommonFiles($packageInfo);
  79. self::setProgressStep2($count,$i,2);
  80. //拷贝当前包的config文件
  81. self::copyConfigFiles($packageInfo);
  82. self::setProgressStep2($count,$i,3);
  83. }
  84. }
  85. private static function postUpdateRootPkg($vendorDir,$pkg)
  86. {
  87. //获取包信息
  88. $repoDir = dirname($vendorDir);
  89. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  90. //注册全局路径
  91. self::registerPath($packageInfo);
  92. //注册自启动程序
  93. self::registerAutorun($packageInfo);
  94. }
  95. //初始化
  96. private static function initialize($vendorDir)
  97. {
  98. //创建.distribute目录
  99. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  100. if( !file_exists($distributeDir) ){
  101. createDirectory($distributeDir);
  102. }
  103. //设置日志文件路径
  104. if(! defined(LOG_PATH)){
  105. $logPath = pathJoin($distributeDir,'log.txt');
  106. define('LOG_PATH',$logPath);
  107. if(file_exists($logPath))
  108. unlink($logPath);
  109. }
  110. //设置进度文件路径
  111. if(! defined(PROGRESS_PATH)){
  112. $progressPath = pathJoin($distributeDir,'progress.txt');
  113. define('PROGRESS_PATH',$progressPath);
  114. if(file_exists($progressPath))
  115. unlink($progressPath);
  116. }
  117. //创建module目录
  118. $moduleDir = pathJoin(dirname($vendorDir),'module');
  119. if( !file_exists($moduleDir) ){
  120. createDirectory($moduleDir);
  121. }
  122. //设置 module.ini 路径
  123. if(! defined(MODULEINI_PATH)){
  124. $moduleiniPath = pathJoin($moduleDir,'module.ini');
  125. define('MODULEINI_PATH',$moduleiniPath);
  126. if(file_exists($moduleiniPath))
  127. unlink($moduleiniPath);
  128. }
  129. //获取部署配置信息
  130. $cfgPath = pathJoin($distributeDir,'config.json');
  131. if(! file_exists($cfgPath) ){
  132. logInfo(".distribute/config.json file not exists,use default config. ");
  133. self::$config = defaultConfig();
  134. }
  135. else{
  136. self::$config = loadConfig($cfgPath);
  137. if(!self::$config){
  138. logError("Failed to load distribute config",$cfgPath);
  139. return false;
  140. }
  141. logInfo("succeeded to decode config file -- .distribute/config.json");
  142. }
  143. return true;
  144. }
  145. private static function setProgressStep1($percent)
  146. {
  147. $progress = 0.1 * $percent;
  148. file_put_contents(PROGRESS_PATH,strval($progress));
  149. }
  150. private static function setProgressStep2($total,$index,$step)
  151. {
  152. $index = $index + $step/3.0;
  153. $progress = 10 + intval(90.0 * $index / $total);
  154. file_put_contents(PROGRESS_PATH,strval($progress));
  155. }
  156. private static function setProgressFailed($msg)
  157. {
  158. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  159. }
  160. private static function setProgressOK($restart)
  161. {
  162. if($restart){
  163. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  164. }
  165. else{
  166. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  167. }
  168. }
  169. //获取包相关信息
  170. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  171. {
  172. $config = self::$config;
  173. $cfgPath = pathJoin($repoDir,'composer.json');
  174. $data = json_decode(file_get_contents($cfgPath), true);
  175. $moduleName = $data["distribute-helper"]["module-name"];
  176. //获取安装目录
  177. $rootDir = dirname($vendorDir);
  178. $commonDir = pathJoin($rootDir,'siscom');
  179. $configDir = pathJoin($rootDir,'config',$moduleName);
  180. $moduleDir = pathJoin($rootDir,'module',$moduleName);
  181. //获取缓存目录
  182. $cacheDir = pathJoin($config['cache-dir'],str_replace('/','-',$pkg->getName()));
  183. //返回包的相关信息
  184. return array(
  185. "repoName" => $pkg->getName(),
  186. "version" => $pkg->getPrettyVersion(),
  187. "cacheDir" => $cacheDir,
  188. "toolDir" => $config['tool-dir'],
  189. "ossHost" => $config['oss-host'],
  190. "moduleName" => $moduleName,
  191. "commonDir" => $commonDir,
  192. "configDir" => $configDir,
  193. "moduleDir" => $moduleDir,
  194. "helper" => $data["distribute-helper"],
  195. );
  196. }
  197. //下载包module文件
  198. private static function fetchModuleFiles($packageInfo)
  199. {
  200. $toolDir = $packageInfo['toolDir'];
  201. $cacheDir = $packageInfo['cacheDir'];
  202. $repoName = $packageInfo['repoName'];
  203. $version = $packageInfo['version'];
  204. $moduleDir = $packageInfo['moduleDir'];
  205. $ossHost = $packageInfo['ossHost'];
  206. $moduleFiles = $packageInfo['helper']['module-files'];
  207. if(! $moduleFiles){
  208. return;
  209. }
  210. foreach($moduleFiles as $fileInfo){
  211. $fileName = $fileInfo['name'];
  212. //下载文件到缓存区
  213. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  214. if(! $filePath ){
  215. logError($repoName,"fetch module file failed",$fileName);
  216. return false;
  217. }
  218. logInfo("succeeded to fetch module file --",$repoName,$fileName);
  219. // 解压文件
  220. if( isZipFile($fileName) ){
  221. if(! unzipFile($toolDir,$filePath,$moduleDir)) {
  222. logError($repoName,"unzip module file failed",$fileName);
  223. return false;
  224. }
  225. }
  226. }
  227. return true;
  228. }
  229. //下载包common文件
  230. private static function fetchCommonFiles($packageInfo)
  231. {
  232. $toolDir = $packageInfo['toolDir'];
  233. $cacheDir = $packageInfo['cacheDir'];
  234. $repoName = $packageInfo['repoName'];
  235. $version = $packageInfo['version'];
  236. $commonDir = $packageInfo['commonDir'];
  237. $ossHost = $packageInfo['ossHost'];
  238. $commonFiles = $packageInfo['helper']['common-files'];
  239. if(! $commonFiles){
  240. return;
  241. }
  242. foreach($commonFiles as $fileInfo){
  243. $fileName = $fileInfo['name'];
  244. //下载文件到缓存区
  245. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  246. if(! $filePath ){
  247. logError($repoName,"fetch common file failed",$fileName);
  248. return false;
  249. }
  250. logInfo("succeeded to fetch common file --",$repoName,$fileName);
  251. // 解压文件
  252. if( isZipFile($fileName) ){
  253. if(! unzipFile($toolDir,$filePath,$commonDir)) {
  254. logError($repoName,"unzip common file failed",$fileName);
  255. return false;
  256. }
  257. }
  258. }
  259. return true;
  260. }
  261. //拷贝config文件
  262. private static function copyConfigFiles($packageInfo)
  263. {
  264. $cacheDir = $packageInfo['cacheDir'];
  265. $repoName = $packageInfo['repoName'];
  266. $version = $packageInfo['version'];
  267. $configDir = $packageInfo['configDir'];
  268. $moduleDir = $packageInfo['moduleDir'];
  269. $configFiles = $packageInfo['helper']['config-files'];
  270. if(! $configFiles){
  271. return;
  272. }
  273. foreach($configFiles as $fileInfo){
  274. $fileName = $fileInfo['name'];
  275. $filePath = pathJoin($moduleDir,$fileName);
  276. if(! file_exists($filePath) ){
  277. logError($repoName,"config file not exists",$filePath);
  278. return false;
  279. }
  280. if(!$fileInfo['override'] && file_exists($targetPath)){
  281. logInfo($repoName,"skip copy config file",$targetPath);
  282. }
  283. $targetPath = pathJoin($configDir,$fileName);
  284. fileCopy($filePath,$targetPath);
  285. }
  286. return true;
  287. }
  288. private static function registerPath($packageInfo)
  289. {
  290. }
  291. private static function registerAutorun($packageInfo)
  292. {
  293. }
  294. }