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::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::registerShortcut($packageInfo) ){
  204. self::setProgressFailed('REGISTER_SHORTCUT_FAILED');
  205. return false;
  206. }
  207. return true;
  208. }
  209. //初始化
  210. private static function initialize($vendorDir)
  211. {
  212. //创建下载包缓存目录
  213. $cacheDir = getCacheDir();
  214. if( !file_exists($cacheDir) ){
  215. createDirectory($cacheDir);
  216. }
  217. setHiddenAttrib($cacheDir);
  218. if(! defined('CACHE_DIR')){
  219. define('CACHE_DIR',$cacheDir);
  220. }
  221. //创建.distribute目录
  222. $distributeDir = pathJoin(dirname($vendorDir),'.distribute');
  223. if( !file_exists($distributeDir) ){
  224. createDirectory($distributeDir);
  225. }
  226. if(! defined('DIST_DIR')){
  227. define('DIST_DIR',$distributeDir);
  228. }
  229. //设置日志文件路径
  230. if(! defined('LOG_PATH')){
  231. $logPath = pathJoin($distributeDir,'log.txt');
  232. define('LOG_PATH',$logPath);
  233. if(file_exists($logPath))
  234. unlink($logPath);
  235. }
  236. //设置进度文件路径
  237. if(! defined('PROGRESS_PATH')){
  238. $progressPath = pathJoin($distributeDir,'progress.txt');
  239. define('PROGRESS_PATH',$progressPath);
  240. if(file_exists($progressPath))
  241. unlink($progressPath);
  242. }
  243. //创建module目录
  244. $moduleDir = pathJoin(dirname($vendorDir),'module');
  245. if( !file_exists($moduleDir) ){
  246. createDirectory($moduleDir);
  247. }
  248. //解析安装包信息
  249. $composerJson = pathJoin(dirname($vendorDir),'composer.json');
  250. self::$app = json_decode(file_get_contents($composerJson), true);
  251. if(! self::$app ){
  252. logError("failed to decode " . $composerJson);
  253. return false;
  254. }
  255. //获取部署配置信息
  256. $cfgPath = pathJoin($distributeDir,'config.json');
  257. if(! file_exists($cfgPath) ){
  258. logInfo(".distribute/config.json file not exists,use default config. ");
  259. self::$config = defaultConfig();
  260. }
  261. else{
  262. self::$config = loadConfig($cfgPath);
  263. if(!self::$config){
  264. logError("Failed to load distribute config",$cfgPath);
  265. return false;
  266. }
  267. logInfo("succeeded to decode config file -- .distribute/config.json");
  268. }
  269. //如果检测到在SIS内网,替换成内网IP
  270. $clientip = getClientIp();
  271. if($clientip === false){
  272. return false;
  273. }
  274. if( $clientip == '39.171.62.20'){
  275. logInfo("sis intranet detected. oss host http://192.168.3.10:8996");
  276. self::$config['oss-host'] = 'http://192.168.3.10:8996';
  277. }else{
  278. logInfo("internet detected. oss host http://192.168.3.10:8996");
  279. self::$config['oss-host'] = 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com';
  280. }
  281. //工具换成wget
  282. self::$config['down-client'] = 'wget';
  283. return true;
  284. }
  285. private static function setProgressStep1($percent)
  286. {
  287. $progress = 0.1 * $percent;
  288. file_put_contents(PROGRESS_PATH,strval($progress));
  289. }
  290. private static function setProgressStep2($total,$index,$step)
  291. {
  292. $index = $index + $step/3.0;
  293. $progress = 10 + intval(90.0 * $index / $total);
  294. file_put_contents(PROGRESS_PATH,strval($progress));
  295. }
  296. private static function setProgressFailed($msg)
  297. {
  298. file_put_contents(PROGRESS_PATH,"FAIL:" . $msg);
  299. }
  300. private static function setProgressOK($restart)
  301. {
  302. if($restart){
  303. file_put_contents(PROGRESS_PATH,"OK:NEED_RESTART");
  304. }
  305. else{
  306. file_put_contents(PROGRESS_PATH,"OK:NO_RESTART");
  307. }
  308. }
  309. //获取包相关信息
  310. private static function getPackageInfo($vendorDir,$repoDir,$pkg)
  311. {
  312. $config = self::$config;
  313. $app = self::$app;
  314. $repoName = $pkg->getName();
  315. $module_alias = $app["distribute-root"]["module-alias"];
  316. $cfgPath = pathJoin($repoDir,'composer.json');
  317. $data = json_decode(file_get_contents($cfgPath), true);
  318. $moduleName = $module_alias[$repoName] ? : $repoName;
  319. //获取安装目录
  320. $rootDir = dirname($vendorDir);
  321. $commonDir = pathJoin($rootDir,'common');
  322. $configDir = pathJoin($rootDir,'config',$moduleName);
  323. $moduleDir = pathJoin($rootDir,'module',$moduleName);
  324. //获取缓存目录
  325. $cacheDir = pathJoin(CACHE_DIR,str_replace('/','-',$repoName));
  326. //返回包的相关信息
  327. return array(
  328. "repoName" => $repoName,
  329. "version" => $pkg->getPrettyVersion(),
  330. "rootDir" => $rootDir,
  331. "vendorDir" => $vendorDir,
  332. "cacheDir" => $cacheDir,
  333. "toolDir" => $config['tool-dir'],
  334. "downClient" => $config['down-client'],
  335. "ossHost" => $config['oss-host'],
  336. "moduleName" => $moduleName,
  337. "commonDir" => $commonDir,
  338. "configDir" => $configDir,
  339. "moduleDir" => $moduleDir,
  340. "helper" => $data["distribute-helper"],
  341. );
  342. }
  343. //运行脚本文件
  344. private static function scriptFiles($packageInfo)
  345. {
  346. $toolDir = $packageInfo['toolDir'];
  347. $cacheDir = $packageInfo['cacheDir'];
  348. $repoName = $packageInfo['repoName'];
  349. $rootDir = $packageInfo['rootDir'];
  350. $version = $packageInfo['version'];
  351. $vendorDir = $packageInfo['vendorDir'];
  352. $ossHost = $packageInfo['ossHost'];
  353. $fileMacros = array(
  354. '{vendor}' => $vendorDir,
  355. '{root}' => $rootDir,
  356. );
  357. $app = self::$app;
  358. $scriptFiles = $app["distribute-root"]["script-files"] ? : array();
  359. foreach($scriptFiles as $info){
  360. $name = $info['name'];
  361. $type = $info['type'];
  362. if($type == 'txt'){
  363. $filePath = pathJoin($rootDir,$name);
  364. if(! self::runScriptFile($filePath,$fileMacros) ){
  365. logError('failed to run script file',$filePath);
  366. return false;
  367. }
  368. }
  369. }
  370. return true;
  371. }
  372. private static function runScriptFile($filePath,$fileMacros){
  373. $fileLines = readFileLines($filePath);
  374. foreach($fileLines as $fileLine){
  375. $cmd = replaceTextMacro($fileLine,$fileMacros);
  376. logInfo("start to run script file cmd --",$cmd);
  377. system($cmd, $ret);
  378. if ($ret != 0) {
  379. logError("failed to run script file cmd --",$cmd);
  380. return false;
  381. }
  382. }
  383. return true;
  384. }
  385. //下载包module文件
  386. private static function fetchModuleFiles($packageInfo)
  387. {
  388. $toolDir = $packageInfo['toolDir'];
  389. $downClient = $packageInfo['downClient'];
  390. $cacheDir = $packageInfo['cacheDir'];
  391. $repoName = $packageInfo['repoName'];
  392. $version = $packageInfo['version'];
  393. $moduleDir = $packageInfo['moduleDir'];
  394. $ossHost = $packageInfo['ossHost'];
  395. $moduleFiles = $packageInfo['helper']['module-files'] ? : array();
  396. foreach($moduleFiles as $fileInfo){
  397. $fileName = $fileInfo['name'];
  398. $fileMd5 = $fileInfo['md5'];
  399. $fileSize = int($fileInfo['size']);
  400. //下载文件到缓存区
  401. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  402. if(! $filePath ){
  403. logError($repoName,"fetch module file failed",$fileName);
  404. return false;
  405. }
  406. logInfo("succeeded to fetch module file --",$repoName,$fileName);
  407. //当前是生成离线包模式,同时拷贝到离线包目录
  408. if(self::$config['offpkg-dir']){
  409. $targetPath = pathJoin(self::$config['offpkg-dir'],str_replace('/','-',$repoName),$fileName);
  410. if(! fileCopy($filePath,$targetPath) ){
  411. logError($repoName,"offpkg file copy failed",$filePath,'-->',$targetPath);
  412. return false;
  413. }
  414. }
  415. // 解压或拷贝文件
  416. if( isZipFile($fileName) ){
  417. if(! unzipFile($toolDir,$filePath,$moduleDir)) {
  418. logError($repoName,"unzip module file failed",$fileName);
  419. return false;
  420. }
  421. }else{
  422. $targetPath = pathJoin($moduleDir,$fileName);
  423. if(! fileCopy($filePath,$targetPath) ){
  424. logError($repoName,"copy module file failed",$filePath,'-->',$targetPath);
  425. return false;
  426. }
  427. }
  428. }
  429. return true;
  430. }
  431. //下载包common文件
  432. private static function fetchCommonFiles($packageInfo)
  433. {
  434. $toolDir = $packageInfo['toolDir'];
  435. $downClient = $packageInfo['downClient'];
  436. $cacheDir = $packageInfo['cacheDir'];
  437. $repoName = $packageInfo['repoName'];
  438. $version = $packageInfo['version'];
  439. $commonDir = $packageInfo['commonDir'];
  440. $ossHost = $packageInfo['ossHost'];
  441. $commonFiles = $packageInfo['helper']['common-files']? : array();
  442. foreach($commonFiles as $fileInfo){
  443. $fileName = $fileInfo['name'];
  444. $fileMd5 = $fileInfo['md5'];
  445. $fileSize = int($fileInfo['size']);
  446. $target = $fileInfo['target'];
  447. //下载文件到缓存区
  448. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  449. if(! $filePath ){
  450. logError($repoName,"fetch common file failed",$fileName);
  451. return false;
  452. }
  453. logInfo("succeeded to fetch common file --",$repoName,$fileName);
  454. // 解压或拷贝文件
  455. $targetDir = pathJoin($commonDir,$target);
  456. if( isZipFile($fileName) ){
  457. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  458. logError($repoName,"unzip common file failed",$fileName);
  459. return false;
  460. }
  461. }else{
  462. $targetPath = pathJoin($targetDir,$fileName);
  463. if(! fileCopy($filePath,$targetPath) ){
  464. logError($repoName,"copy common file failed",$filePath,'-->',$targetPath);
  465. return false;
  466. }
  467. }
  468. }
  469. return true;
  470. }
  471. //下载包vendor文件
  472. private static function fetchVendorFiles($packageInfo)
  473. {
  474. $toolDir = $packageInfo['toolDir'];
  475. $downClient = $packageInfo['downClient'];
  476. $cacheDir = $packageInfo['cacheDir'];
  477. $repoName = $packageInfo['repoName'];
  478. $version = $packageInfo['version'];
  479. $vendorDir = $packageInfo['vendorDir'];
  480. $ossHost = $packageInfo['ossHost'];
  481. $vendorFiles = $packageInfo['helper']['vendor-files']? : array();
  482. foreach($vendorFiles as $fileInfo){
  483. $fileName = $fileInfo['name'];
  484. $fileMd5 = $fileInfo['md5'];
  485. $fileSize = int($fileInfo['size']);
  486. $target = $fileInfo['target'];
  487. //下载文件到缓存区
  488. $filePath = downloadToDir($toolDir,$downClient,$ossHost,$repoName,$fileName,$fileMd5,$fileSize,$cacheDir);
  489. if(! $filePath ){
  490. logError($repoName,"fetch vendor file failed",$fileName);
  491. return false;
  492. }
  493. logInfo("start to fetch vendor file --",$repoName,$fileName);
  494. // 解压或拷贝文件
  495. if( isZipFile($fileName) ){
  496. $targetDir = pathJoin($vendorDir,$target);
  497. if(! unzipFile($toolDir,$filePath,$targetDir)) {
  498. logError($repoName,"unzip vendor file failed",$fileName);
  499. return false;
  500. }
  501. }else{
  502. $targetPath = pathJoin($vendorDir,$repoName,$fileName);
  503. if(! fileCopy($filePath,$targetPath) ){
  504. logError($repoName,"offpkg file copy failed",$filePath,'-->',$targetPath);
  505. return false;
  506. }
  507. }
  508. }
  509. return true;
  510. }
  511. //拷贝config文件
  512. /*
  513. private static function copyConfigFiles($packageInfo)
  514. {
  515. $cacheDir = $packageInfo['cacheDir'];
  516. $repoName = $packageInfo['repoName'];
  517. $version = $packageInfo['version'];
  518. $configDir = $packageInfo['configDir'];
  519. $moduleDir = $packageInfo['moduleDir'];
  520. $configFiles = $packageInfo['helper']['config-files'] ? : array();
  521. foreach($configFiles as $fileInfo){
  522. $fileName = $fileInfo['name'];
  523. $targetName = $fileInfo['target'];
  524. $filePath = pathJoin($moduleDir,$fileName);
  525. if(! file_exists($filePath) ){
  526. logError($repoName,"config file not exists",$filePath);
  527. return false;
  528. }
  529. if(!$fileInfo['override'] && file_exists($targetPath)){
  530. logInfo($repoName,"skip copy config file",$targetPath);
  531. continue;
  532. }
  533. $targetPath = pathJoin($configDir,$targetName);
  534. if(! fileCopy($filePath,$targetPath) ){
  535. logError($repoName,"config file copy failed",$filePath);
  536. return false;
  537. }
  538. }
  539. return true;
  540. }*/
  541. private static function removeProcess($packageInfo)
  542. {
  543. $app = self::$app;
  544. $cacheDir = $packageInfo['cacheDir'];
  545. $repoName = $packageInfo['repoName'];
  546. $version = $packageInfo['version'];
  547. $rootDir = $packageInfo['rootDir'];
  548. $removeProcess = $app["distribute-root"]["remove-process"] ? : array();
  549. $proc = new WinProcess(DIST_DIR);
  550. foreach($removeProcess as $process){
  551. $name = $process['name'];
  552. //获取进程PIDs
  553. $pids = $proc->find($name);
  554. if( $pids === false ){
  555. logError("failed to find process, action failed.",$name);
  556. return false;
  557. }
  558. if( count($pids) === 0 ){
  559. logInfo("process not exists.no need to remove process",$name);
  560. continue;
  561. }
  562. //移除进程
  563. $ret = $proc->remove($name);
  564. if( $ret === false ){
  565. logError("failed to remove process, remove action failed.",$name);
  566. return false;
  567. }
  568. else{
  569. logInfo("succeeded to remove process",$name);
  570. }
  571. }
  572. return true;
  573. }
  574. //拷贝copy-files文件
  575. private static function copyFiles($packageInfo)
  576. {
  577. $app = self::$app;
  578. $cacheDir = $packageInfo['cacheDir'];
  579. $repoName = $packageInfo['repoName'];
  580. $version = $packageInfo['version'];
  581. $rootDir = $packageInfo['rootDir'];
  582. $copyFiles = $app["distribute-root"]["copy-files"] ? : array();
  583. foreach($copyFiles as $fileInfo){
  584. $fileName = $fileInfo['path'];
  585. $targetName = $fileInfo['target'];
  586. $filePath = pathJoin($rootDir,$fileName);
  587. $targetPath = pathJoin($rootDir,$targetName);
  588. if(! file_exists($filePath) ){
  589. logError("failed to copy file, file not exists.",$filePath);
  590. return false;
  591. }
  592. if(!$fileInfo['override'] && file_exists($targetPath)){
  593. logInfo("skip copy file, file already exists",$targetPath);
  594. continue;
  595. }
  596. if( filedirCopy($filePath,$targetPath) ){
  597. logInfo("succeeded to copy file",$fileName," -> ",$targetName);
  598. }
  599. else{
  600. logError("failed to copy file, copy action failed.",$filePath);
  601. return false;
  602. }
  603. }
  604. return true;
  605. }
  606. private static function macoFiles($packageInfo)
  607. {
  608. $app = self::$app;
  609. $cacheDir = $packageInfo['cacheDir'];
  610. $repoName = $packageInfo['repoName'];
  611. $version = $packageInfo['version'];
  612. $rootDir = $packageInfo['rootDir'];
  613. $macroFiles = $app["distribute-root"]["macro-files"] ? : array();
  614. $appDir = str_replace('\\','/',$rootDir);
  615. $rootDirUp2 = dirname(dirname($rootDir));
  616. $appdataDir = pathJoin($rootDirUp2,'appdata');
  617. $tmpdataDir = pathJoin($rootDirUp2,'tmpdata');
  618. $logDir = getLysis3LogDir();
  619. $macros = array(
  620. '{sis31.appdir}' => $appDir,
  621. '{sis31.appdata}' => $appdataDir,
  622. '{sis31.tmpdata}' => $tmpdataDir,
  623. '{sis31.logdir}' => $logDir,
  624. );
  625. foreach($macroFiles as $fileInfo){
  626. $fileName = $fileInfo['path'];
  627. $filePath = pathJoin($rootDir,$fileName);
  628. if(! file_exists($filePath) ){
  629. logError("failed to replace macro file, file not exists.",$filePath);
  630. return false;
  631. }
  632. $targetName = $fileInfo['target'];
  633. $targetPath = pathJoin($rootDir,$targetName);
  634. if( replaceFileMacro($filePath,$targetPath,$macros) ){
  635. logInfo("succeeded to replace macro file",$fileName);
  636. }
  637. else{
  638. logError("failed to replace macro file",$filePath);
  639. return false;
  640. }
  641. }
  642. return true;
  643. }
  644. private static function registerPath($packageInfo)
  645. {
  646. $app = self::$app;
  647. $rootDir = $packageInfo['rootDir'];
  648. $removePath = $app["distribute-root"]["remove-path"] ? : array();
  649. $registerPath = $app["distribute-root"]["register-path"] ? : array();
  650. $arrRemovePattern = array();
  651. foreach($removePath as $item){
  652. $pattern = pathJoin($rootDir,$item['pattern']);
  653. array_push($arrRemovePattern,$pattern);
  654. }
  655. $arrAddPath = array();
  656. foreach($registerPath as $item){
  657. $path = pathJoin($rootDir,$item['path']);
  658. array_push($arrAddPath,$path);
  659. }
  660. $removePathes = array();
  661. $env = new WinEnvPath();
  662. if(! $env->updatePath($arrRemovePattern,$arrAddPath,$removePathes) ){
  663. logError('failed to register path');
  664. return false;
  665. }
  666. foreach($removePathes as $path){
  667. logInfo('succeeded to remove path',$path);
  668. }
  669. foreach($arrAddPath as $path){
  670. logInfo('succeeded to add path',$path);
  671. }
  672. return true;
  673. }
  674. private static function registerAutorun($packageInfo)
  675. {
  676. $app = self::$app;
  677. $rootDir = $packageInfo['rootDir'];
  678. $removeAutorun = $app["distribute-root"]["remove-autorun"] ? : array();
  679. $regAutorun = $app["distribute-root"]["register-autorun"] ? : array();
  680. $auto = new WinAutorun();
  681. foreach($removeAutorun as $item){
  682. if(! $auto->remove($item['name']) ){
  683. logError('failed to remove autorun',$item['name']);
  684. return false;
  685. }
  686. logInfo('succeeded to remove autorun',$item['name']);
  687. }
  688. foreach($regAutorun as $item){
  689. $cmd = pathJoin($rootDir,$item['path']);
  690. $cmd = str_replace('/','\\',$cmd);
  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. }