PackageClass.php 23 KB

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