PackageClass.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace DistributeHelper;
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. include_once "Utility.php";
  5. include_once "WscriptShell.php";
  6. class PackageClass
  7. {
  8. private static $config = null; //全局配置信息
  9. private static $app = null; //当前安装包composer.json
  10. //当所有包都安装完毕后
  11. public static function postUpdate($vendorDir,$rootPkg,$vendorPkgs)
  12. {
  13. //检查管理员权限
  14. $priv = new WinAdminPriv();
  15. if(! $priv->check() ){
  16. self::setProgressFailed('NO_ADMIN_PRIVILEGE');
  17. return false;
  18. }
  19. //初始化
  20. if(! self::initialize($vendorDir)){
  21. self::setProgressFailed('INITIALIZE_FAILED');
  22. return false;
  23. }
  24. logInfo("distribute mode: ",self::$config['distribute-mode']);
  25. //预先系统准备操作
  26. if(! self::prepare($vendorDir,$rootPkg) ){
  27. self::setProgressFailed('PREPARE_FAILED');
  28. return false;
  29. }
  30. //分发模式
  31. if( self::$config['distribute-mode'] == 'install'){
  32. self::install($vendorDir,$rootPkg,$vendorPkgs);
  33. }
  34. else{
  35. self::upgrade($vendorDir,$rootPkg,$vendorPkgs);
  36. }
  37. return true;
  38. }
  39. private static function prepare($vendorDir,$pkg)
  40. {
  41. //获取包信息
  42. $repoDir = dirname($vendorDir);
  43. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  44. //关闭进程
  45. if(! self::removeProcess($packageInfo) ){
  46. return false;
  47. }
  48. return true;
  49. }
  50. private static function install($vendorDir,$rootPkg,$vendorPkgs){
  51. //依赖库都已下载,默认先给个进度
  52. self::setProgressStep1(100);
  53. //逐个依赖仓库操作
  54. if(! self::postUpdateVendorPkgs($vendorDir,$vendorPkgs) ){
  55. return false;
  56. }
  57. //顶层仓库操作
  58. if(! self::postUpdateRootPkg($vendorDir,$rootPkg) ){
  59. return false;
  60. }
  61. //全部操作完毕
  62. self::setProgressOK(true);
  63. return true;
  64. }
  65. private static function upgrade($vendorDir,$rootPkg,$vendorPkgs){
  66. //依赖库都已下载,默认先给个进度
  67. self::setProgressStep1(100);
  68. //逐个依赖仓库操作
  69. if(! self::postUpdateVendorPkgs($vendorDir,$vendorPkgs) ){
  70. return false;
  71. }
  72. //顶层仓库操作
  73. if(! self::postUpdateRootPkg($vendorDir,$rootPkg) ){
  74. return false;
  75. }
  76. //全部操作完毕
  77. self::setProgressOK(true);
  78. return true;
  79. }
  80. /*
  81. private static function createModuleIni($vendorDir,$vendorPkgs)
  82. {
  83. //[MODULE]节点内容
  84. $content = '[MODULE]' . PHP_EOL;
  85. foreach($vendorPkgs as $i=>$pkg){
  86. if( in_array($pkg->getName(),['a/distribute-helper']) )
  87. continue;
  88. $cfgPath = pathJoin($vendorDir,$pkg->getName(),'composer.json');
  89. $data = json_decode(file_get_contents($cfgPath), true);
  90. $moduleName = $data["distribute-helper"]["module-name"];
  91. if(!$moduleName){
  92. logError('bad format. no module-name node exists.',$cfgPath);
  93. return false;
  94. }
  95. $content .= $moduleName . '=' . $pkg->getName() . ',' . $pkg->getPrettyVersion() . PHP_EOL;
  96. }
  97. //[CONFIG]节点内容
  98. //获取包信息
  99. $repoDir = dirname($vendorDir);
  100. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  101. $moduleConfig = $packageInfo['helper']['module-config'] ? : array();
  102. $content = '[CONFIG]' . PHP_EOL;
  103. foreach($moduleConfig as $i=>$cfg){
  104. $content .= $cfg['name'] . '=' . $cfg['value'] . PHP_EOL;
  105. }
  106. //保存文件
  107. file_put_contents(MODULEINI_PATH,$content);
  108. return true;
  109. }*/
  110. private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
  111. {
  112. $count = count($vendorPkgs);
  113. foreach($vendorPkgs as $i=>$pkg){
  114. //获取包信息
  115. $repoDir = pathJoin($vendorDir,$pkg->getName());
  116. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  117. //获取当前包的module文件
  118. if(! self::fetchModuleFiles($packageInfo) ){
  119. self::setProgressFailed('FETCH_MODULE_FAILED');
  120. return false;
  121. }
  122. self::setProgressStep2($count,$i,1);
  123. //获取当前包的common文件
  124. if(! self::fetchCommonFiles($packageInfo) ){
  125. self::setProgressFailed('FETCH_COMMON_FAILED');
  126. return false;
  127. }
  128. self::setProgressStep2($count,$i,2);
  129. //拷贝当前包的config文件
  130. self::setProgressStep2($count,$i,3);
  131. }
  132. return true;
  133. }
  134. private static function postUpdateRootPkg($vendorDir,$pkg)
  135. {
  136. //获取包信息
  137. $repoDir = dirname($vendorDir);
  138. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  139. //拷贝copy-files文件
  140. if(! self::copyFiles($packageInfo) ){
  141. self::setProgressFailed('COPY_FILES_FAILED');
  142. return false;
  143. }
  144. //替换文件内的宏变量
  145. if(! self::macoFiles($packageInfo) ){
  146. self::setProgressFailed('MACRO_FILES_FAILED');
  147. return false;
  148. }
  149. //注册全局路径
  150. if(! self::registerPath($packageInfo) ){
  151. self::setProgressFailed('REGISTER_PATH_FAILED');
  152. return false;
  153. }
  154. //注册自启动程序
  155. if(! self::registerAutorun($packageInfo) ){
  156. self::setProgressFailed('REGISTER_AUTORUN_FAILED');
  157. return false;
  158. }
  159. //注册桌面快捷方式
  160. if(! self::registerShortcut($packageInfo) ){
  161. self::setProgressFailed('REGISTER_SHORTCUT_FAILED');
  162. return false;
  163. }
  164. return true;
  165. }
  166. //初始化
  167. private static function initialize($vendorDir)
  168. {
  169. //创建下载包缓存目录
  170. $cacheDir = getCacheDir();
  171. if( !file_exists($cacheDir) ){
  172. createDirectory($cacheDir);
  173. }
  174. setHiddenAttrib($cacheDir);
  175. if(! defined('CACHE_DIR')){
  176. define('CACHE_DIR',$cacheDir);
  177. }
  178. //创建.distribute目录
  179. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  180. if( !file_exists($distributeDir) ){
  181. createDirectory($distributeDir);
  182. }
  183. if(! defined('DIST_DIR')){
  184. define('DIST_DIR',$distributeDir);
  185. }
  186. //设置日志文件路径
  187. if(! defined('LOG_PATH')){
  188. $logPath = pathJoin($distributeDir,'log.txt');
  189. define('LOG_PATH',$logPath);
  190. if(file_exists($logPath))
  191. unlink($logPath);
  192. }
  193. //设置进度文件路径
  194. if(! defined('PROGRESS_PATH')){
  195. $progressPath = pathJoin($distributeDir,'progress.txt');
  196. define('PROGRESS_PATH',$progressPath);
  197. if(file_exists($progressPath))
  198. unlink($progressPath);
  199. }
  200. //创建module目录
  201. $moduleDir = pathJoin(dirname($vendorDir),'module');
  202. if( !file_exists($moduleDir) ){
  203. createDirectory($moduleDir);
  204. }
  205. //解析安装包信息
  206. $composerJson = pathJoin(dirname($vendorDir),'composer.json');
  207. self::$app = json_decode(file_get_contents($composerJson), true);
  208. if(! self::$app ){
  209. logError("failed to decode " . $composerJson);
  210. return false;
  211. }
  212. //获取部署配置信息
  213. $cfgPath = pathJoin($distributeDir,'config.json');
  214. if(! file_exists($cfgPath) ){
  215. logInfo(".distribute/config.json file not exists,use default config. ");
  216. self::$config = defaultConfig();
  217. }
  218. else{
  219. self::$config = loadConfig($cfgPath);
  220. if(!self::$config){
  221. logError("Failed to load distribute config",$cfgPath);
  222. return false;
  223. }
  224. logInfo("succeeded to decode config file -- .distribute/config.json");
  225. }
  226. //如果检测到在SIS内网,替换成内网IP
  227. $clientip = getClientIp();
  228. if($clientip === false){
  229. return false;
  230. }
  231. if( $clientip == '39.171.62.20'){
  232. logInfo("sis intranet detected. oss host http://192.168.3.10:8996");
  233. self::$config['oss-host'] = 'http://192.168.3.10:8996';
  234. }else{
  235. logInfo("internet detected. oss host http://192.168.3.10:8996");
  236. self::$config['oss-host'] = 'http://distribute-helper.oss-cn-hangzhou.aliyuncs.com';
  237. }
  238. return true;
  239. }
  240. private static function setProgressStep1($percent)
  241. {
  242. $progress = 0.1 * $percent;
  243. file_put_contents(PROGRESS_PATH,strval($progress));
  244. }
  245. private static function setProgressStep2($total,$index,$step)
  246. {
  247. $index = $index + $step/3.0;
  248. $progress = 10 + intval(90.0 * $index / $total);
  249. file_put_contents(PROGRESS_PATH,strval($progress));
  250. }
  251. private static function setProgressFailed($msg)
  252. {
  253. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  254. }
  255. private static function setProgressOK($restart)
  256. {
  257. if($restart){
  258. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  259. }
  260. else{
  261. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  262. }
  263. }
  264. //获取包相关信息
  265. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  266. {
  267. $config = self::$config;
  268. $app = self::$app;
  269. $repoName = $pkg->getName();
  270. $module_alias = $app["distribute-root"]["module-alias"];
  271. $cfgPath = pathJoin($repoDir,'composer.json');
  272. $data = json_decode(file_get_contents($cfgPath), true);
  273. $moduleName = $module_alias[$repoName] ? : $repoName;
  274. //获取安装目录
  275. $rootDir = dirname($vendorDir);
  276. $commonDir = pathJoin($rootDir,'common');
  277. $configDir = pathJoin($rootDir,'config',$moduleName);
  278. $moduleDir = pathJoin($rootDir,'module',$moduleName);
  279. //获取缓存目录
  280. $cacheDir = pathJoin(CACHE_DIR,str_replace('/','-',$pkg->getName()));
  281. //返回包的相关信息
  282. return array(
  283. "repoName" => $repoName,
  284. "version" => $pkg->getPrettyVersion(),
  285. "rootDir" => $rootDir,
  286. "cacheDir" => $cacheDir,
  287. "toolDir" => $config['tool-dir'],
  288. "ossHost" => $config['oss-host'],
  289. "moduleName" => $moduleName,
  290. "commonDir" => $commonDir,
  291. "configDir" => $configDir,
  292. "moduleDir" => $moduleDir,
  293. "helper" => $data["distribute-helper"],
  294. );
  295. }
  296. //下载包module文件
  297. private static function fetchModuleFiles($packageInfo)
  298. {
  299. $toolDir = $packageInfo['toolDir'];
  300. $cacheDir = $packageInfo['cacheDir'];
  301. $repoName = $packageInfo['repoName'];
  302. $version = $packageInfo['version'];
  303. $moduleDir = $packageInfo['moduleDir'];
  304. $ossHost = $packageInfo['ossHost'];
  305. $moduleFiles = $packageInfo['helper']['module-files'] ? : array();
  306. foreach($moduleFiles as $fileInfo){
  307. $fileName = $fileInfo['name'];
  308. //下载文件到缓存区
  309. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  310. if(! $filePath ){
  311. logError($repoName,"fetch module file failed",$fileName);
  312. return false;
  313. }
  314. logInfo("succeeded to fetch module file --",$repoName,$fileName);
  315. // 解压文件
  316. if( isZipFile($fileName) ){
  317. if(! unzipFile($toolDir,$filePath,$moduleDir)) {
  318. logError($repoName,"unzip module file failed",$fileName);
  319. return false;
  320. }
  321. }
  322. }
  323. return true;
  324. }
  325. //下载包common文件
  326. private static function fetchCommonFiles($packageInfo)
  327. {
  328. $toolDir = $packageInfo['toolDir'];
  329. $cacheDir = $packageInfo['cacheDir'];
  330. $repoName = $packageInfo['repoName'];
  331. $version = $packageInfo['version'];
  332. $commonDir = $packageInfo['commonDir'];
  333. $ossHost = $packageInfo['ossHost'];
  334. $commonFiles = $packageInfo['helper']['common-files']? : array();
  335. foreach($commonFiles as $fileInfo){
  336. $fileName = $fileInfo['name'];
  337. $target = $fileInfo['target'];
  338. //下载文件到缓存区
  339. $filePath = downloadToDir($toolDir,$ossHost,$repoName,$fileName,$cacheDir);
  340. if(! $filePath ){
  341. logError($repoName,"fetch common file failed",$fileName);
  342. return false;
  343. }
  344. logInfo("succeeded to fetch common file --",$repoName,$fileName);
  345. // 解压文件
  346. if( isZipFile($fileName) ){
  347. $targetDir = pathJoin($commonDir,$target);
  348. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  349. logError($repoName,"unzip common file failed",$fileName);
  350. return false;
  351. }
  352. }
  353. }
  354. return true;
  355. }
  356. //拷贝config文件
  357. /*
  358. private static function copyConfigFiles($packageInfo)
  359. {
  360. $cacheDir = $packageInfo['cacheDir'];
  361. $repoName = $packageInfo['repoName'];
  362. $version = $packageInfo['version'];
  363. $configDir = $packageInfo['configDir'];
  364. $moduleDir = $packageInfo['moduleDir'];
  365. $configFiles = $packageInfo['helper']['config-files'] ? : array();
  366. foreach($configFiles as $fileInfo){
  367. $fileName = $fileInfo['name'];
  368. $targetName = $fileInfo['target'];
  369. $filePath = pathJoin($moduleDir,$fileName);
  370. if(! file_exists($filePath) ){
  371. logError($repoName,"config file not exists",$filePath);
  372. return false;
  373. }
  374. if(!$fileInfo['override'] && file_exists($targetPath)){
  375. logInfo($repoName,"skip copy config file",$targetPath);
  376. continue;
  377. }
  378. $targetPath = pathJoin($configDir,$targetName);
  379. if(! fileCopy($filePath,$targetPath) ){
  380. logError($repoName,"config file copy failed",$filePath);
  381. return false;
  382. }
  383. }
  384. return true;
  385. }*/
  386. private static function removeProcess($packageInfo)
  387. {
  388. $app = self::$app;
  389. $cacheDir = $packageInfo['cacheDir'];
  390. $repoName = $packageInfo['repoName'];
  391. $version = $packageInfo['version'];
  392. $rootDir = $packageInfo['rootDir'];
  393. $removeProcess = $app["distribute-root"]["remove-process"] ? : array();
  394. $proc = new WinProcess(DIST_DIR);
  395. foreach($removeProcess as $process){
  396. $name = $process['name'];
  397. //获取进程PIDs
  398. $pids = $proc->find($name);
  399. if( $pids === false ){
  400. logError("failed to find process, action failed.",$name);
  401. return false;
  402. }
  403. if( count($pids) === 0 ){
  404. logInfo("process not exists.no need to remove process",$name);
  405. continue;
  406. }
  407. //移除进程
  408. $ret = $proc->remove($name);
  409. if( $ret === false ){
  410. logError("failed to remove process, remove action failed.",$name);
  411. return false;
  412. }
  413. else{
  414. logInfo("succeeded to remove process",$name);
  415. }
  416. }
  417. return true;
  418. }
  419. //拷贝copy-files文件
  420. private static function copyFiles($packageInfo)
  421. {
  422. $app = self::$app;
  423. $cacheDir = $packageInfo['cacheDir'];
  424. $repoName = $packageInfo['repoName'];
  425. $version = $packageInfo['version'];
  426. $rootDir = $packageInfo['rootDir'];
  427. $copyFiles = $app["distribute-root"]["copy-files"] ? : array();
  428. foreach($copyFiles as $fileInfo){
  429. $fileName = $fileInfo['path'];
  430. $targetName = $fileInfo['target'];
  431. $filePath = pathJoin($rootDir,$fileName);
  432. $targetPath = pathJoin($rootDir,$targetName);
  433. if(! file_exists($filePath) ){
  434. logError("failed to copy file, file not exists.",$filePath);
  435. return false;
  436. }
  437. if(!$fileInfo['override'] && file_exists($targetPath)){
  438. logInfo("skip copy file, file already exists",$targetPath);
  439. continue;
  440. }
  441. if( filedirCopy($filePath,$targetPath) ){
  442. logInfo("succeeded to copy file",$fileName," -> ",$targetName);
  443. }
  444. else{
  445. logError("failed to copy file, copy action failed.",$filePath);
  446. return false;
  447. }
  448. }
  449. return true;
  450. }
  451. private static function macoFiles($packageInfo)
  452. {
  453. $app = self::$app;
  454. $cacheDir = $packageInfo['cacheDir'];
  455. $repoName = $packageInfo['repoName'];
  456. $version = $packageInfo['version'];
  457. $rootDir = $packageInfo['rootDir'];
  458. $macroFiles = $app["distribute-root"]["macro-files"] ? : array();
  459. $appDir = str_replace('\\','/',$rootDir);
  460. $rootDirUp2 = dirname(dirname($rootDir));
  461. $appdataDir = pathJoin($rootDirUp2,'appdata');
  462. $tmpdataDir = pathJoin($rootDirUp2,'tmpdata');
  463. $logDir = getLysis3LogDir();
  464. $macros = array(
  465. '{sis31.appdir}' => $appDir,
  466. '{sis31.appdata}' => $appdataDir,
  467. '{sis31.tmpdata}' => $tmpdataDir,
  468. '{sis31.logdir}' => $logDir,
  469. );
  470. foreach($macroFiles as $fileInfo){
  471. $fileName = $fileInfo['path'];
  472. $filePath = pathJoin($rootDir,$fileName);
  473. if(! file_exists($filePath) ){
  474. logError("failed to replace macro file, file not exists.",$filePath);
  475. return false;
  476. }
  477. $targetName = $fileInfo['target'];
  478. $targetPath = pathJoin($rootDir,$targetName);
  479. if( replaceMacro($filePath,$targetPath,$macros) ){
  480. logInfo("succeeded to replace macro file",$fileName);
  481. }
  482. else{
  483. logError("failed to replace macro file",$filePath);
  484. return false;
  485. }
  486. }
  487. return true;
  488. }
  489. private static function registerPath($packageInfo)
  490. {
  491. $app = self::$app;
  492. $rootDir = $packageInfo['rootDir'];
  493. $removePath = $app["distribute-root"]["remove-path"] ? : array();
  494. $registerPath = $app["distribute-root"]["register-path"] ? : array();
  495. $arrRemovePattern = array();
  496. foreach($removePath as $item){
  497. $pattern = pathJoin($rootDir,$item['pattern']);
  498. array_push($arrRemovePattern,$pattern);
  499. }
  500. $arrAddPath = array();
  501. foreach($registerPath as $item){
  502. $path = pathJoin($rootDir,$item['path']);
  503. array_push($arrAddPath,$path);
  504. }
  505. $removePathes = array();
  506. $env = new WinEnvPath();
  507. if(! $env->updatePath($arrRemovePattern,$arrAddPath,$removePathes) ){
  508. logError('failed to register path');
  509. return false;
  510. }
  511. foreach($removePathes as $path){
  512. logInfo('succeeded to remove path',$path);
  513. }
  514. foreach($arrAddPath as $path){
  515. logInfo('succeeded to add path',$path);
  516. }
  517. return true;
  518. }
  519. private static function registerAutorun($packageInfo)
  520. {
  521. $app = self::$app;
  522. $rootDir = $packageInfo['rootDir'];
  523. $removeAutorun = $app["distribute-root"]["remove-autorun"] ? : array();
  524. $regAutorun = $app["distribute-root"]["register-autorun"] ? : array();
  525. $auto = new WinAutorun();
  526. foreach($removeAutorun as $item){
  527. if(! $auto->remove($item['name']) ){
  528. logError('failed to remove autorun',$item['name']);
  529. return false;
  530. }
  531. logInfo('succeeded to remove autorun',$item['name']);
  532. }
  533. foreach($regAutorun as $item){
  534. $cmd = pathJoin($rootDir,$item['path']);
  535. if(! $auto->register($item['name'],$cmd) ){
  536. logError('failed to register autorun',$item['name']);
  537. return false;
  538. }
  539. logInfo('succeeded to register autorun',$item['name']);
  540. }
  541. return true;
  542. }
  543. private static function registerShortcut($packageInfo)
  544. {
  545. $app = self::$app;
  546. $rootDir = $packageInfo['rootDir'];
  547. $removeShortcut = $app["distribute-root"]["remove-shortcut"] ? : array();
  548. $regShortcut = $app["distribute-root"]["register-shortcut"] ? : array();
  549. $sc = new WinDesktopShortcut();
  550. foreach($removeShortcut as $item){
  551. if(! $sc->remove($item['name']) ){
  552. logError('failed to remove shortcut',$item['name']);
  553. return false;
  554. }
  555. logInfo('succeeded to remove shortcut',$item['name']);
  556. }
  557. foreach($regShortcut as $item){
  558. $path = pathJoin($rootDir,$item['path']);
  559. if(! $sc->register($item['name'],$path,$item['desc']) ){
  560. logError('failed to register shortcut',$item['name']);
  561. return false;
  562. }
  563. logInfo('succeeded to register shortcut',$item['name']);
  564. }
  565. return true;
  566. }
  567. }