PackageClass.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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::setUpdateFailed('INITIALIZE_FAILED');
  14. return false;
  15. }
  16. //依赖库都已下载,默认先给个进度
  17. self::updateProgress(0,100);
  18. //逐个依赖仓库操作
  19. self::postUpdateVendorPkgs($vendorDir,$vendorPkgs);
  20. //顶层仓库操作
  21. self::postUpdateRootPkg($vendorDir,$rootPkg);
  22. //全部操作完毕
  23. self::setUpdateOK(true);
  24. return true;
  25. }
  26. private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
  27. {
  28. foreach($vendorPkgs as $i=>$pkg){
  29. //获取包信息
  30. $repoDir = pathJoin($vendorDir,$pkg->getName());
  31. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  32. //获取当前包的module文件
  33. self::fetchModuleFiles($packageInfo);
  34. //获取当前包的common文件
  35. self::fetchCommonFiles($packageInfo);
  36. //获取当前包的config文件
  37. self::fetchConfigFiles($packageInfo);
  38. //更新部署进度
  39. $percent = intval(100.0 * ($i+1) / count(vendorPkgs));
  40. self::updateProgress(1,$percent);
  41. }
  42. }
  43. private static function postUpdateRootPkg($vendorDir,$pkg)
  44. {
  45. //获取包信息
  46. $repoDir = dirname($vendorDir);
  47. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  48. //获取当前包的module文件
  49. self::fetchModuleFiles($packageInfo);
  50. //获取当前包的common文件
  51. self::fetchCommonFiles($packageInfo);
  52. //获取当前包的config文件
  53. self::fetchConfigFiles($packageInfo);
  54. //注册全局路径
  55. self::registerPath($packageInfo);
  56. //注册自启动程序
  57. self::registerAutorun($packageInfo);
  58. }
  59. //初始化
  60. private static function initialize($vendorDir)
  61. {
  62. //创建.distribute目录
  63. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  64. if( !file_exists($distributeDir) ){
  65. mkdir($distributeDir,0777,true);
  66. }
  67. //创建module目录
  68. $moduleDir = pathJoin(dirname($vendorDir),'module');
  69. if( !file_exists($moduleDir) ){
  70. mkdir($moduleDir,0777,true);
  71. }
  72. //设置日志文件路径
  73. if(! defined(LOG_PATH)){
  74. $logPath = pathJoin($distributeDir,'log.txt');
  75. define('LOG_PATH',$logPath);
  76. }
  77. //设置进度文件路径
  78. if(! defined(PROGRESS_PATH)){
  79. $progressPath = pathJoin($distributeDir,'progress.txt');
  80. define('PROGRESS_PATH',$progressPath);
  81. }
  82. //获取部署配置信息
  83. $cfgPath = pathJoin($distributeDir,'config.json');
  84. $config = loadConfig($cfgPath);
  85. if(!$config){
  86. printf("Failed to load distribute config -- %s\n",$cfgPath);
  87. return false;
  88. }
  89. self::$config = $config;
  90. return true;
  91. }
  92. private static function updateProgress($step,$percent)
  93. {
  94. file_put_contents(PROGRESS_PATH,strval($percent));
  95. }
  96. private static function setUpdateFailed($msg)
  97. {
  98. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  99. }
  100. private static function setUpdateOK($restart)
  101. {
  102. if($restart){
  103. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  104. }
  105. else{
  106. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  107. }
  108. }
  109. //获取包相关信息
  110. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  111. {
  112. $config = self::$config;
  113. $cfgPath = pathJoin($repoDir,'composer.json');
  114. $data = json_decode(file_get_contents($cfgPath), true);
  115. $moduleName = $data["distribute-helper"]["module-name"];
  116. //获取安装目录
  117. $rootDir = dirname($vendorDir);
  118. $commonDir = pathJoin($rootDir,'siscom');
  119. $configDir = pathJoin($rootDir,'config',$moduleName);
  120. $moduleDir = pathJoin($rootDir,'module',$moduleName . '-' . $pkg->getPrettyVersion());
  121. //获取缓存目录
  122. $cacheName = str_replace('/','-',$pkg->getName()) . '-' . $pkg->getPrettyVersion();
  123. $cacheDir = pathJoin($config['cache-dir'],$cacheName);
  124. //返回包的相关信息
  125. return array(
  126. "repoName" => $pkg->getName(),
  127. "version" => $pkg->getPrettyVersion(),
  128. "cacheDir" => $cacheDir,
  129. "ossHost" => $config['oss-host'],
  130. "moduleName" => $moduleName,
  131. "commonDir" => $commonDir,
  132. "configDir" => $configDir,
  133. "moduleDir" => $moduleDir,
  134. "helper" => $data["distribute-helper"],
  135. );
  136. }
  137. //下载包module文件
  138. private static function fetchModuleFiles($packageInfo)
  139. {
  140. $cacheDir = $packageInfo['cacheDir'];
  141. $repoName = $packageInfo['repoName'];
  142. $version = $packageInfo['version'];
  143. $moduleDir = $packageInfo['moduleDir'];
  144. $ossHost = $packageInfo['ossHost'];
  145. $moduleFiles = $packageInfo['helper']['module-files'];
  146. if(! $moduleFiles){
  147. logInfo($repoName,'no module files defined. skip.');
  148. return;
  149. }
  150. foreach($moduleFiles as $fileInfo){
  151. $fileName = $fileInfo['name'];
  152. printf("[%s] fetching file -- %s\n",$repoName,$fileName);
  153. //下载文件到缓存区
  154. $filePath = downloadToDir($ossHost,$repoName,$version,$fileName,$cacheDir);
  155. if(! $filePath ){
  156. printf("[%s] fetch file failed -- %s\n",$repoName,$fileName);
  157. continue;
  158. }
  159. // 解压文件
  160. if( isZipFile($fileName) ){
  161. if(! unzipFile($filePath,$moduleDir)) {
  162. printf("[%s] unzip file failed -- %s\n ",$repoName,$fileName);
  163. unlink($filePath);
  164. }
  165. }
  166. }
  167. }
  168. //下载包common文件
  169. private static function fetchCommonFiles($packageInfo)
  170. {
  171. $cacheDir = $packageInfo['cacheDir'];
  172. $repoName = $packageInfo['repoName'];
  173. $version = $packageInfo['version'];
  174. $commonDir = $packageInfo['commonDir'];
  175. $ossHost = $packageInfo['ossHost'];
  176. $commonFiles = $packageInfo['helper']['common-files'];
  177. if(! $commonFiles){
  178. logInfo($repoName,'no common files defined. skip.');
  179. return;
  180. }
  181. foreach($commonFiles as $fileInfo){
  182. $fileName = $fileInfo['name'];
  183. printf("[%s] fetching common file -- %s\n",$repoName,$fileName);
  184. //下载文件到缓存区
  185. $filePath = downloadToDir($ossHost,$repoName,$version,$fileName,$cacheDir);
  186. if(! $filePath ){
  187. printf("[%s] fetch common file failed -- %s\n",$repoName,$fileName);
  188. continue;
  189. }
  190. // 解压文件
  191. if( isZipFile($fileName) ){
  192. if(! unzipFile($filePath,$commonDir)) {
  193. printf("[%s] unzip common file failed -- %s\n ",$repoName,$fileName);
  194. unlink($filePath);
  195. }
  196. }
  197. }
  198. }
  199. //下载包config文件
  200. private static function fetchConfigFiles($packageInfo)
  201. {
  202. $cacheDir = $packageInfo['cacheDir'];
  203. $repoName = $packageInfo['repoName'];
  204. $version = $packageInfo['version'];
  205. $configDir = $packageInfo['configDir'];
  206. $ossHost = $packageInfo['ossHost'];
  207. $configFiles = $packageInfo['helper']['config-files'];
  208. if(! $configFiles){
  209. logInfo($repoName,'no config files defined. skip.');
  210. return;
  211. }
  212. foreach($configFiles as $fileInfo){
  213. $fileName = $fileInfo['name'];
  214. printf("[%s] fetching config file -- %s\n",$repoName,$fileName);
  215. //下载文件到缓存区
  216. $filePath = downloadToDir($ossHost,$repoName,$version,$fileName,$cacheDir);
  217. if(! $filePath ){
  218. printf("[%s] fetch config file failed -- %s\n",$repoName,$fileName);
  219. continue;
  220. }
  221. // 解压文件
  222. if( isZipFile($fileName) ){
  223. if(! unzipFile($filePath,$configDir)) {
  224. printf("[%s] unzip config file failed -- %s\n ",$repoName,$fileName);
  225. unlink($filePath);
  226. }
  227. }
  228. }
  229. }
  230. private static function registerPath($packageInfo)
  231. {
  232. }
  233. private static function registerAutorun($packageInfo)
  234. {
  235. }
  236. }