PackageClass.php 15 KB

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