PackageClass.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. //预先系统准备操作
  25. if(! self::prepare($vendorDir,$rootPkg) ){
  26. self::setProgressFailed('PREPARE_FAILED');
  27. return false;
  28. }
  29. //分发模式
  30. $dist_mode = self::$config['distribute-mode'];
  31. logInfo("distribute mode: ",$dist_mode);
  32. if( $dist_mode == 'install'){ //在线安装
  33. return self::install($vendorDir,$rootPkg,$vendorPkgs);
  34. }
  35. elseif( $dist_mode == 'upgrade'){ //在线升级
  36. return self::upgrade($vendorDir,$rootPkg,$vendorPkgs);
  37. }
  38. elseif( $dist_mode == 'offpkg'){ //生成离线包
  39. return self::offpkg($vendorDir,$rootPkg,$vendorPkgs);
  40. }
  41. elseif( $dist_mode == 'offinst'){ //离线安装
  42. return self::offinst($vendorDir,$rootPkg,$vendorPkgs);
  43. }
  44. elseif( $dist_mode == 'offupgd'){ //离线升级
  45. return self::offupgd($vendorDir,$rootPkg,$vendorPkgs);
  46. }
  47. else{
  48. self::setProgressFailed('INVALID_DIST_MODE');
  49. return false;
  50. }
  51. }
  52. private static function prepare($vendorDir,$pkg)
  53. {
  54. //获取包信息
  55. $repoDir = dirname($vendorDir);
  56. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  57. //关闭进程
  58. if(! self::removeProcess($packageInfo) ){
  59. return false;
  60. }
  61. return true;
  62. }
  63. private static function install($vendorDir,$rootPkg,$vendorPkgs){
  64. //依赖库都已下载,默认先给个进度
  65. self::setProgressStep1(100);
  66. //逐个依赖仓库操作
  67. if(! self::postUpdateVendorPkgs($vendorDir,$vendorPkgs) ){
  68. return false;
  69. }
  70. //顶层仓库操作
  71. if(! self::postUpdateRootPkg($vendorDir,$rootPkg) ){
  72. return false;
  73. }
  74. //全部操作完毕
  75. self::setProgressOK(true);
  76. return true;
  77. }
  78. private static function upgrade($vendorDir,$rootPkg,$vendorPkgs){
  79. //依赖库都已下载,默认先给个进度
  80. self::setProgressStep1(100);
  81. //逐个依赖仓库操作
  82. if(! self::postUpdateVendorPkgs($vendorDir,$vendorPkgs) ){
  83. return false;
  84. }
  85. //顶层仓库操作
  86. if(! self::postUpdateRootPkg($vendorDir,$rootPkg) ){
  87. return false;
  88. }
  89. //全部操作完毕
  90. self::setProgressOK(true);
  91. return true;
  92. }
  93. private static function offpkg($vendorDir,$rootPkg,$vendorPkgs){
  94. //依赖库都已下载,默认先给个进度
  95. self::setProgressStep1(100);
  96. //逐个依赖仓库操作
  97. if(! self::postUpdateVendorPkgs($vendorDir,$vendorPkgs) ){
  98. return false;
  99. }
  100. //顶层仓库操作
  101. if(! self::postUpdateRootPkg($vendorDir,$rootPkg) ){
  102. return false;
  103. }
  104. //全部操作完毕
  105. self::setProgressOK(true);
  106. return true;
  107. }
  108. private static function offinst($vendorDir,$rootPkg,$vendorPkgs){
  109. return true;
  110. }
  111. private static function offupgd($vendorDir,$rootPkg,$vendorPkgs){
  112. return true;
  113. }
  114. /*
  115. private static function createModuleIni($vendorDir,$vendorPkgs)
  116. {
  117. //[MODULE]节点内容
  118. $content = '[MODULE]' . PHP_EOL;
  119. foreach($vendorPkgs as $i=>$pkg){
  120. if( in_array($pkg->getName(),['a/distribute-helper']) )
  121. continue;
  122. $cfgPath = pathJoin($vendorDir,$pkg->getName(),'composer.json');
  123. $data = json_decode(file_get_contents($cfgPath), true);
  124. $moduleName = $data["distribute-helper"]["module-name"];
  125. if(!$moduleName){
  126. logError('bad format. no module-name node exists.',$cfgPath);
  127. return false;
  128. }
  129. $content .= $moduleName . '=' . $pkg->getName() . ',' . $pkg->getPrettyVersion() . PHP_EOL;
  130. }
  131. //[CONFIG]节点内容
  132. //获取包信息
  133. $repoDir = dirname($vendorDir);
  134. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  135. $moduleConfig = $packageInfo['helper']['module-config'] ? : array();
  136. $content = '[CONFIG]' . PHP_EOL;
  137. foreach($moduleConfig as $i=>$cfg){
  138. $content .= $cfg['name'] . '=' . $cfg['value'] . PHP_EOL;
  139. }
  140. //保存文件
  141. file_put_contents(MODULEINI_PATH,$content);
  142. return true;
  143. }*/
  144. private static function postUpdateVendorPkgs($vendorDir,$vendorPkgs)
  145. {
  146. $count = count($vendorPkgs);
  147. foreach($vendorPkgs as $i=>$pkg){
  148. //获取包信息
  149. $repoDir = pathJoin($vendorDir,$pkg->getName());
  150. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  151. //获取当前包的module文件
  152. if(! self::fetchModuleFiles($packageInfo) ){
  153. self::setProgressFailed('FETCH_MODULE_FILES_FAILED');
  154. return false;
  155. }
  156. self::setProgressStep2($count,$i,1);
  157. //获取当前包的common文件
  158. if(! self::fetchCommonFiles($packageInfo) ){
  159. self::setProgressFailed('FETCH_COMMON_FILES_FAILED');
  160. return false;
  161. }
  162. self::setProgressStep2($count,$i,2);
  163. //拷贝当前包的vendor文件
  164. if(! self::fetchVendorFiles($packageInfo) ){
  165. self::setProgressFailed('FETCH_VENDOR_FILES_FAILED');
  166. return false;
  167. }
  168. self::setProgressStep2($count,$i,3);
  169. }
  170. return true;
  171. }
  172. private static function postUpdateRootPkg($vendorDir,$pkg)
  173. {
  174. //获取包信息
  175. $repoDir = dirname($vendorDir);
  176. $packageInfo = self::getPackageInfo($vendorDir,$repoDir,$pkg);
  177. //运行script-files文件
  178. if(! self::scriptFiles($packageInfo) ){
  179. self::setProgressFailed('RUN_SCRIPT_FILES_FAILED');
  180. return false;
  181. }
  182. //拷贝copy-files文件
  183. if(! self::copyFiles($packageInfo) ){
  184. self::setProgressFailed('COPY_FILES_FAILED');
  185. return false;
  186. }
  187. //替换文件内的宏变量
  188. if(! self::macoFiles($packageInfo) ){
  189. self::setProgressFailed('MACRO_FILES_FAILED');
  190. return false;
  191. }
  192. //注册全局路径
  193. if(! self::registerPath($packageInfo) ){
  194. self::setProgressFailed('REGISTER_PATH_FAILED');
  195. return false;
  196. }
  197. //注册自启动程序
  198. if(! self::registerAutorun($packageInfo) ){
  199. self::setProgressFailed('REGISTER_AUTORUN_FAILED');
  200. return false;
  201. }
  202. //注册服务程序
  203. if(! self::registerService($packageInfo) ){
  204. self::setProgressFailed('REGISTER_SERVICE_FAILED');
  205. return false;
  206. }
  207. //注册桌面快捷方式
  208. if(! self::registerShortcut($packageInfo) ){
  209. self::setProgressFailed('REGISTER_SHORTCUT_FAILED');
  210. return false;
  211. }
  212. return true;
  213. }
  214. //初始化
  215. private static function initialize($vendorDir)
  216. {
  217. //创建下载包缓存目录
  218. $cacheDir = getCacheDir();
  219. if( !file_exists($cacheDir) ){
  220. createDirectory($cacheDir);
  221. }
  222. setHiddenAttrib($cacheDir);
  223. if(! defined('CACHE_DIR')){
  224. define('CACHE_DIR',$cacheDir);
  225. }
  226. //创建.distribute目录
  227. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  228. if( !file_exists($distributeDir) ){
  229. createDirectory($distributeDir);
  230. }
  231. if(! defined('DIST_DIR')){
  232. define('DIST_DIR',$distributeDir);
  233. }
  234. //设置日志文件路径
  235. if(! defined('LOG_PATH')){
  236. $logPath = pathJoin($distributeDir,'log.txt');
  237. define('LOG_PATH',$logPath);
  238. if(file_exists($logPath))
  239. unlink($logPath);
  240. }
  241. //设置进度文件路径
  242. if(! defined('PROGRESS_PATH')){
  243. $progressPath = pathJoin($distributeDir,'progress.txt');
  244. define('PROGRESS_PATH',$progressPath);
  245. if(file_exists($progressPath))
  246. unlink($progressPath);
  247. }
  248. //创建module目录
  249. $moduleDir = pathJoin(dirname($vendorDir),'module');
  250. if( !file_exists($moduleDir) ){
  251. createDirectory($moduleDir);
  252. }
  253. //解析安装包信息
  254. $composerJson = pathJoin(dirname($vendorDir),'composer.json');
  255. self::$app = json_decode(file_get_contents($composerJson), true);
  256. if(! self::$app ){
  257. logError("failed to decode " . $composerJson);
  258. return false;
  259. }
  260. //获取部署配置信息
  261. $cfgPath = pathJoin($distributeDir,'config.json');
  262. if(! file_exists($cfgPath) ){
  263. logInfo(".distribute/config.json file not exists,use default config. ");
  264. self::$config = defaultConfig();
  265. }
  266. else{
  267. self::$config = loadConfig($cfgPath);
  268. if(!self::$config){
  269. logError("Failed to load distribute config",$cfgPath);
  270. return false;
  271. }
  272. logInfo("succeeded to decode config file -- .distribute/config.json");
  273. }
  274. //如果检测到在SIS内网,替换成内网IP
  275. $clientip = getClientIp();
  276. if($clientip === false){
  277. return false;
  278. }
  279. if( $clientip == '39.171.62.20'){
  280. logInfo("sis intranet detected. oss host http://192.168.3.10:8996");
  281. self::$config['oss-host'] = 'http://192.168.3.10:8996';
  282. }else{
  283. logInfo("internet detected. oss host http://192.168.3.10:8996");
  284. self::$config['oss-host'] = 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com';
  285. }
  286. //工具换成wget
  287. self::$config['down-client'] = 'wget';
  288. return true;
  289. }
  290. private static function setProgressStep1($percent)
  291. {
  292. $progress = 0.1 * $percent;
  293. file_put_contents(PROGRESS_PATH,strval($progress));
  294. }
  295. private static function setProgressStep2($total,$index,$step)
  296. {
  297. $index = $index + $step/3.0;
  298. $progress = 10 + intval(90.0 * $index / $total);
  299. file_put_contents(PROGRESS_PATH,strval($progress));
  300. }
  301. private static function setProgressFailed($msg)
  302. {
  303. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  304. }
  305. private static function setProgressOK($restart)
  306. {
  307. if($restart){
  308. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  309. }
  310. else{
  311. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  312. }
  313. }
  314. //获取包相关信息
  315. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  316. {
  317. $config = self::$config;
  318. $app = self::$app;
  319. $repoName = $pkg->getName();
  320. $module_alias = $app["distribute-root"]["module-alias"];
  321. $cfgPath = pathJoin($repoDir,'composer.json');
  322. $data = json_decode(file_get_contents($cfgPath), true);
  323. $moduleName = $module_alias[$repoName] ? : $repoName;
  324. //获取安装目录
  325. $rootDir = dirname($vendorDir);
  326. $commonDir = pathJoin($rootDir,'common');
  327. $configDir = pathJoin($rootDir,'config',$moduleName);
  328. $moduleDir = pathJoin($rootDir,'module',$moduleName);
  329. //获取缓存目录
  330. $cacheDir = pathJoin(CACHE_DIR,str_replace('/','-',$repoName));
  331. //返回包的相关信息
  332. return array(
  333. "repoName" => $repoName,
  334. "version" => $pkg->getPrettyVersion(),
  335. "rootDir" => $rootDir,
  336. "vendorDir" => $vendorDir,
  337. "cacheDir" => $cacheDir,
  338. "toolDir" => $config['tool-dir'],
  339. "downClient" => $config['down-client'],
  340. "ossHost" => $config['oss-host'],
  341. "moduleName" => $moduleName,
  342. "commonDir" => $commonDir,
  343. "configDir" => $configDir,
  344. "moduleDir" => $moduleDir,
  345. "helper" => $data["distribute-helper"],
  346. );
  347. }
  348. //运行脚本文件
  349. private static function scriptFiles($packageInfo)
  350. {
  351. $toolDir = $packageInfo['toolDir'];
  352. $cacheDir = $packageInfo['cacheDir'];
  353. $repoName = $packageInfo['repoName'];
  354. $rootDir = $packageInfo['rootDir'];
  355. $version = $packageInfo['version'];
  356. $vendorDir = $packageInfo['vendorDir'];
  357. $ossHost = $packageInfo['ossHost'];
  358. $fileMacros = array(
  359. '{vendor}' => $vendorDir,
  360. '{root}' => $rootDir,
  361. );
  362. $app = self::$app;
  363. $scriptFiles = $app["distribute-root"]["script-files"] ? : array();
  364. foreach($scriptFiles as $info){
  365. $name = $info['name'];
  366. $type = $info['type'];
  367. if($type == 'txt'){
  368. $filePath = pathJoin($rootDir,$name);
  369. if(! self::runScriptFile($filePath,$fileMacros) ){
  370. logError('failed to run script file',$filePath);
  371. return false;
  372. }
  373. }
  374. }
  375. return true;
  376. }
  377. private static function runScriptFile($filePath,$fileMacros){
  378. $fileLines = readFileLines($filePath);
  379. foreach($fileLines as $fileLine){
  380. $cmd = replaceTextMacro($fileLine,$fileMacros);
  381. logInfo("start to run script file cmd --",$cmd);
  382. system($cmd, $ret);
  383. if ($ret != 0) {
  384. logError("failed to run script file cmd --",$cmd);
  385. return false;
  386. }
  387. }
  388. return true;
  389. }
  390. //下载包module文件
  391. private static function fetchModuleFiles($packageInfo)
  392. {
  393. $toolDir = $packageInfo['toolDir'];
  394. $downClient = $packageInfo['downClient'];
  395. $cacheDir = $packageInfo['cacheDir'];
  396. $repoName = $packageInfo['repoName'];
  397. $version = $packageInfo['version'];
  398. $moduleDir = $packageInfo['moduleDir'];
  399. $ossHost = $packageInfo['ossHost'];
  400. $moduleFiles = $packageInfo['helper']['module-files'] ? : array();
  401. foreach($moduleFiles as $fileInfo){
  402. $fileName = $fileInfo['name'];
  403. $fileMd5 = $fileInfo['md5'];
  404. $fileSize = intval($fileInfo['size']);
  405. //下载文件到缓存区
  406. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  407. if(! $filePath ){
  408. logError($repoName,"fetch module file failed",$fileName);
  409. return false;
  410. }
  411. logInfo("succeeded to fetch module file --",$repoName,$fileName);
  412. //当前是生成离线包模式,同时拷贝到离线包目录
  413. if(self::$config['offpkg-dir']){
  414. $targetPath = pathJoin(self::$config['offpkg-dir'],str_replace('/','-',$repoName),$fileName);
  415. if(! fileCopy($filePath,$targetPath) ){
  416. logError($repoName,"offpkg file copy failed",$filePath,'-->',$targetPath);
  417. return false;
  418. }
  419. }
  420. // 解压或拷贝文件
  421. if( isZipFile($fileName) ){
  422. if(! unzipFile($toolDir,$filePath,$moduleDir)) {
  423. logError($repoName,"unzip module file failed",$fileName);
  424. return false;
  425. }
  426. }else{
  427. $targetPath = pathJoin($moduleDir,$fileName);
  428. if(! fileCopy($filePath,$targetPath) ){
  429. logError($repoName,"copy module file failed",$filePath,'-->',$targetPath);
  430. return false;
  431. }
  432. }
  433. }
  434. return true;
  435. }
  436. //下载包common文件
  437. private static function fetchCommonFiles($packageInfo)
  438. {
  439. $toolDir = $packageInfo['toolDir'];
  440. $downClient = $packageInfo['downClient'];
  441. $cacheDir = $packageInfo['cacheDir'];
  442. $repoName = $packageInfo['repoName'];
  443. $version = $packageInfo['version'];
  444. $commonDir = $packageInfo['commonDir'];
  445. $ossHost = $packageInfo['ossHost'];
  446. $commonFiles = $packageInfo['helper']['common-files']? : array();
  447. foreach($commonFiles as $fileInfo){
  448. $fileName = $fileInfo['name'];
  449. $fileMd5 = $fileInfo['md5'];
  450. $fileSize = intval($fileInfo['size']);
  451. $target = $fileInfo['target'];
  452. //下载文件到缓存区
  453. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  454. if(! $filePath ){
  455. logError($repoName,"fetch common file failed",$fileName);
  456. return false;
  457. }
  458. logInfo("succeeded to fetch common file --",$repoName,$fileName);
  459. // 解压或拷贝文件
  460. $targetDir = pathJoin($commonDir,$target);
  461. if( isZipFile($fileName) ){
  462. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  463. logError($repoName,"unzip common file failed",$fileName);
  464. return false;
  465. }
  466. }else{
  467. $targetPath = pathJoin($targetDir,$fileName);
  468. if(! fileCopy($filePath,$targetPath) ){
  469. logError($repoName,"copy common file failed",$filePath,'-->',$targetPath);
  470. return false;
  471. }
  472. }
  473. }
  474. return true;
  475. }
  476. //下载包vendor文件
  477. private static function fetchVendorFiles($packageInfo)
  478. {
  479. $toolDir = $packageInfo['toolDir'];
  480. $downClient = $packageInfo['downClient'];
  481. $cacheDir = $packageInfo['cacheDir'];
  482. $repoName = $packageInfo['repoName'];
  483. $version = $packageInfo['version'];
  484. $vendorDir = $packageInfo['vendorDir'];
  485. $ossHost = $packageInfo['ossHost'];
  486. $vendorFiles = $packageInfo['helper']['vendor-files']? : array();
  487. foreach($vendorFiles as $fileInfo){
  488. $fileName = $fileInfo['name'];
  489. $fileMd5 = $fileInfo['md5'];
  490. $fileSize = intval($fileInfo['size']);
  491. $target = $fileInfo['target'];
  492. //下载文件到缓存区
  493. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  494. if(! $filePath ){
  495. logError($repoName,"fetch vendor file failed",$fileName);
  496. return false;
  497. }
  498. logInfo("start to fetch vendor file --",$repoName,$fileName);
  499. // 解压或拷贝文件
  500. if( isZipFile($fileName) ){
  501. $targetDir = pathJoin($vendorDir,$target);
  502. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  503. logError($repoName,"unzip vendor file failed",$fileName);
  504. return false;
  505. }
  506. }else{
  507. $targetPath = pathJoin($vendorDir,$repoName,$fileName);
  508. if(! fileCopy($filePath,$targetPath) ){
  509. logError($repoName,"offpkg file copy failed",$filePath,'-->',$targetPath);
  510. return false;
  511. }
  512. }
  513. }
  514. return true;
  515. }
  516. //拷贝config文件
  517. /*
  518. private static function copyConfigFiles($packageInfo)
  519. {
  520. $cacheDir = $packageInfo['cacheDir'];
  521. $repoName = $packageInfo['repoName'];
  522. $version = $packageInfo['version'];
  523. $configDir = $packageInfo['configDir'];
  524. $moduleDir = $packageInfo['moduleDir'];
  525. $configFiles = $packageInfo['helper']['config-files'] ? : array();
  526. foreach($configFiles as $fileInfo){
  527. $fileName = $fileInfo['name'];
  528. $targetName = $fileInfo['target'];
  529. $filePath = pathJoin($moduleDir,$fileName);
  530. if(! file_exists($filePath) ){
  531. logError($repoName,"config file not exists",$filePath);
  532. return false;
  533. }
  534. if(!$fileInfo['override'] && file_exists($targetPath)){
  535. logInfo($repoName,"skip copy config file",$targetPath);
  536. continue;
  537. }
  538. $targetPath = pathJoin($configDir,$targetName);
  539. if(! fileCopy($filePath,$targetPath) ){
  540. logError($repoName,"config file copy failed",$filePath);
  541. return false;
  542. }
  543. }
  544. return true;
  545. }*/
  546. private static function removeProcess($packageInfo)
  547. {
  548. $app = self::$app;
  549. $cacheDir = $packageInfo['cacheDir'];
  550. $repoName = $packageInfo['repoName'];
  551. $version = $packageInfo['version'];
  552. $rootDir = $packageInfo['rootDir'];
  553. $removeProcess = $app["distribute-root"]["remove-process"] ? : array();
  554. $proc = new WinProcess(DIST_DIR);
  555. foreach($removeProcess as $process){
  556. $name = $process['name'];
  557. //获取进程PIDs
  558. $pids = $proc->find($name);
  559. if( $pids === false ){
  560. logError("failed to find process, action failed.",$name);
  561. return false;
  562. }
  563. if( count($pids) === 0 ){
  564. logInfo("process not exists.no need to remove process",$name);
  565. continue;
  566. }
  567. //移除进程
  568. $ret = $proc->remove($name);
  569. if( $ret === false ){
  570. logError("failed to remove process, remove action failed.",$name);
  571. return false;
  572. }
  573. else{
  574. logInfo("succeeded to remove process",$name);
  575. }
  576. }
  577. return true;
  578. }
  579. //拷贝copy-files文件
  580. private static function copyFiles($packageInfo)
  581. {
  582. $app = self::$app;
  583. $cacheDir = $packageInfo['cacheDir'];
  584. $repoName = $packageInfo['repoName'];
  585. $version = $packageInfo['version'];
  586. $rootDir = $packageInfo['rootDir'];
  587. $copyFiles = $app["distribute-root"]["copy-files"] ? : array();
  588. foreach($copyFiles as $fileInfo){
  589. $fileName = $fileInfo['path'];
  590. $targetName = $fileInfo['target'];
  591. $filePath = pathJoin($rootDir,$fileName);
  592. $targetPath = pathJoin($rootDir,$targetName);
  593. if(! file_exists($filePath) ){
  594. logError("failed to copy file, file not exists.",$filePath);
  595. return false;
  596. }
  597. if(!$fileInfo['override'] && file_exists($targetPath)){
  598. logInfo("skip copy file, file already exists",$targetPath);
  599. continue;
  600. }
  601. if( filedirCopy($filePath,$targetPath) ){
  602. logInfo("succeeded to copy file",$fileName," -> ",$targetName);
  603. }
  604. else{
  605. logError("failed to copy file, copy action failed.",$filePath);
  606. return false;
  607. }
  608. }
  609. return true;
  610. }
  611. private static function macoFiles($packageInfo)
  612. {
  613. $app = self::$app;
  614. $cacheDir = $packageInfo['cacheDir'];
  615. $repoName = $packageInfo['repoName'];
  616. $version = $packageInfo['version'];
  617. $rootDir = $packageInfo['rootDir'];
  618. $macroFiles = $app["distribute-root"]["macro-files"] ? : array();
  619. $appDir = str_replace('\\','/',$rootDir);
  620. $rootDirUp2 = dirname(dirname($rootDir));
  621. $appdataDir = pathJoin($rootDirUp2,'appdata');
  622. $tmpdataDir = pathJoin($rootDirUp2,'tmpdata');
  623. $logDir = getLysis3LogDir();
  624. $macros = array(
  625. '{sis31.appdir}' => $appDir,
  626. '{sis31.appdata}' => $appdataDir,
  627. '{sis31.tmpdata}' => $tmpdataDir,
  628. '{sis31.logdir}' => $logDir,
  629. );
  630. foreach($macroFiles as $fileInfo){
  631. $fileName = $fileInfo['path'];
  632. $filePath = pathJoin($rootDir,$fileName);
  633. if(! file_exists($filePath) ){
  634. logError("failed to replace macro file, file not exists.",$filePath);
  635. return false;
  636. }
  637. $targetName = $fileInfo['target'];
  638. $targetPath = pathJoin($rootDir,$targetName);
  639. if( replaceFileMacro($filePath,$targetPath,$macros) ){
  640. logInfo("succeeded to replace macro file",$fileName);
  641. }
  642. else{
  643. logError("failed to replace macro file",$filePath);
  644. return false;
  645. }
  646. }
  647. return true;
  648. }
  649. private static function registerPath($packageInfo)
  650. {
  651. $app = self::$app;
  652. $rootDir = $packageInfo['rootDir'];
  653. $removePath = $app["distribute-root"]["remove-path"] ? : array();
  654. $registerPath = $app["distribute-root"]["register-path"] ? : array();
  655. $arrRemovePattern = array();
  656. foreach($removePath as $item){
  657. $pattern = $item['pattern'];
  658. array_push($arrRemovePattern,$pattern);
  659. }
  660. $arrAddPath = array();
  661. foreach($registerPath as $item){
  662. $path = pathJoin($rootDir,$item['path']);
  663. array_push($arrAddPath,$path);
  664. }
  665. $removePathes = array();
  666. $env = new WinEnvPath();
  667. if(! $env->updatePath($arrRemovePattern,$arrAddPath,$removePathes) ){
  668. logError('failed to register path');
  669. return false;
  670. }
  671. foreach($removePathes as $path){
  672. logInfo('succeeded to remove path',$path);
  673. }
  674. foreach($arrAddPath as $path){
  675. logInfo('succeeded to add path',$path);
  676. }
  677. return true;
  678. }
  679. private static function registerAutorun($packageInfo)
  680. {
  681. $app = self::$app;
  682. $rootDir = $packageInfo['rootDir'];
  683. $removeAutorun = $app["distribute-root"]["remove-autorun"] ? : array();
  684. $regAutorun = $app["distribute-root"]["register-autorun"] ? : array();
  685. $auto = new WinAutorun();
  686. foreach($removeAutorun as $item){
  687. if(! $auto->remove($item['name']) ){
  688. logError('failed to remove autorun',$item['name']);
  689. return false;
  690. }
  691. logInfo('succeeded to remove autorun',$item['name']);
  692. }
  693. foreach($regAutorun as $item){
  694. $cmd = pathJoin($rootDir,$item['path']);
  695. $cmd = toOsSlashes($cmd);
  696. if(! $auto->register($item['name'],$cmd) ){
  697. logError('failed to register autorun',$item['name']);
  698. return false;
  699. }
  700. logInfo('succeeded to register autorun',$item['name']);
  701. }
  702. return true;
  703. }
  704. private static function registerService($packageInfo)
  705. {
  706. $app = self::$app;
  707. $rootDir = $packageInfo['rootDir'];
  708. $removeService = $app["distribute-root"]["remove-service"] ? : array();
  709. $regService = $app["distribute-root"]["register-service"] ? : array();
  710. $service = new WinService(DIST_DIR);
  711. foreach($removeService as $item){
  712. if(! $service->remove($item['name']) ){
  713. logError('failed to remove autorun',$item['name']);
  714. return false;
  715. }
  716. logInfo('succeeded to remove autorun',$item['name']);
  717. }
  718. foreach($regService as $item){
  719. $path = pathJoin($rootDir,$item['path']);
  720. $path = toOsSlashes($path);
  721. if(! $service->register($item['name'],$path) ){
  722. logError('failed to register autorun',$item['name']);
  723. return false;
  724. }
  725. logInfo('succeeded to register autorun',$item['name']);
  726. }
  727. return true;
  728. }
  729. private static function registerShortcut($packageInfo)
  730. {
  731. $app = self::$app;
  732. $rootDir = $packageInfo['rootDir'];
  733. $removeShortcut = $app["distribute-root"]["remove-shortcut"] ? : array();
  734. $regShortcut = $app["distribute-root"]["register-shortcut"] ? : array();
  735. $sc = new WinDesktopShortcut();
  736. foreach($removeShortcut as $item){
  737. if(! $sc->remove($item['name']) ){
  738. logError('failed to remove shortcut',$item['name']);
  739. return false;
  740. }
  741. logInfo('succeeded to remove shortcut',$item['name']);
  742. }
  743. foreach($regShortcut as $item){
  744. $path = pathJoin($rootDir,$item['path']);
  745. if(! $sc->register($item['name'],$path,$item['desc']) ){
  746. logError('failed to register shortcut',$item['name']);
  747. return false;
  748. }
  749. logInfo('succeeded to register shortcut',$item['name']);
  750. }
  751. return true;
  752. }
  753. }