elviss 2 vuotta sitten
vanhempi
commit
4b9d81ed97
3 muutettua tiedostoa jossa 132 lisäystä ja 137 poistoa
  1. 3 7
      src/HelperClass.php
  2. 93 87
      src/PackageClass.php
  3. 36 43
      src/Utility.php

+ 3 - 7
src/HelperClass.php

@@ -12,9 +12,7 @@ class HelperClass
     public static function postUpdate(Event $event)
     {
         $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
-		$devMode = (getenv("COMPOSER_DEV_MODE") == "1");
-		PackageClass::setDevMode($devMode);
-		PackageClass::postUpdate($vendorDir, "");
+		PackageClass::index($vendorDir,"");
 		print("---postUpdate finished---");
     }
 
@@ -29,16 +27,14 @@ class HelperClass
     {      
 		$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
 		$package = $event->getOperation()->getPackage();
-		PackageClass::setDevMode( $event->isDevMode() );
-		PackageClass::postPackageInstall($vendorDir, $package->getName());
+		PackageClass::index($vendorDir, $package->getName());
     }
 
     public static function postPackageUpdate(PackageEvent $event)
     {
 		$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
 		$package = $event->getOperation()->getPackage();
-		PackageClass::setDevMode( $event->isDevMode() );
-		PackageClass::postPackageUpdate($vendorDir, $package->getName());
+		PackageClass::index($vendorDir, $package->getName());
     }
 	
 }

+ 93 - 87
src/PackageClass.php

@@ -6,97 +6,95 @@ include "Utility.php";
 class PackageClass
 {
 	//当所有包都安装完毕后
-	public static function postUpdate($vendorDir, $repoName){
-		//获取配置信息
-		$cfgPath = getDistributeConfigPath($vendorDir);
-		$config = loadDistributeConfig($cfgPath);
-		if(!$config){
-			printf("Failed to load distribute config -- %s\n",$cfgPath);
-			exit();
+	public static function index($vendorDir,$repoName){
+		//设置日志文件路径
+		if(! defined(LOG_PATH)){
+			define('LOG_PATH',getLogPath($vendorDir))
 		}
-		
-		//获取当前包信息
-		$packageInfo = self::getPackageInfo($config,$vendorDir, $repoName);
-		
-		//下载当前包的add文件
-		$packageFiles = getPackageFiles($packageInfo['configPath']);
-        self::fetchAddFiles($packageInfo,$packageFiles['add-files']);
-		
-		//下载当前包的bin文件
-		self::fetchBinFiles($packageInfo,$packageFiles['bin-files']);
-
-		//下载当前包的copy文件
-		self::fetchCopyFiles($packageInfo,$packageFiles['copy-files']);
-	}
-	//当某个依赖包安装完毕后
-	public static function postPackageInstall($vendorDir, $repoName){
 		//获取配置信息
-		$cfgPath = getDistributeConfigPath($vendorDir);
-		$config = loadDistributeConfig($cfgPath);
+		$cfgPath = getConfigPath($vendorDir);
+		$config = loadConfig($cfgPath);
 		if(!$config){
 			printf("Failed to load distribute config -- %s\n",$cfgPath);
 			exit();
 		}
 		
 		//获取当前包信息
-		$packageInfo = self::getPackageInfo($config,$vendorDir, $repoName);
+		$packageInfo = self::getPackageInfo($config,$vendorDir,$repoName);
 		
-		//下载当前包的add文件
-		$packageFiles = getPackageFiles($packageInfo['configPath']);
-        self::fetchAddFiles($packageInfo,$packageFiles['add-files']);
+		//获取当前包的module文件
+        self::fetchModuleFiles($packageInfo);
 		
-		//下载当前包的bin文件
-		self::fetchBinFiles($packageInfo,$packageFiles['bin-files']);
+		//获取当前包的common文件
+		self::fetchCommonFiles($packageInfo);
 
-		//下载当前包的copy文件
-		self::fetchCopyFiles($packageInfo,$packageFiles['copy-files']);
-	}
-	public static function postPackageUpdate($vendorDir, $repoName){
-	}
-	public static function setDevMode($devMode){
+		//获取当前包的config文件
+		self::fetchConfigFiles($packageInfo);
+		
+		//顶层包的额外工作
+		if($repoName == ""){
+			//注册全局路径
+			self::registerPath($packageInfo);
+			//注册自启动程序
+			self::registerAutorun($packageInfo);
+		}
 	}
+
 	//获取包相关信息
 	private static function getPackageInfo($config,$vendorDir, $repoName){
-		if($repoName == ""){ //最上层包的信息
-			$packageDir = dirname($vendorDir);
-			$json = file_get_contents($packageDir . '/composer.json');
-			$data = json_decode($json, true);
-			$repoName = $data["name"];
+		$rootDir = dirname($vendorDir);
+		if($repoName == ""){ 
+			$repoDir = $rootDir; //最上层包所在目录
 		}
-		else{ //其他依赖包的信息
-			$packageDir = $vendorDir . '/' . $repoName;
+		else{
+			$repoDir = pathJoin($vendorDir,$repoName);//其他依赖包所在目录
 		}
-		$cacheDir = pathJoin($config['cache-dir'],$repoName);
 		
-		//获取最上层包配置的 bin-dir
-		$cfgPath = dirname($vendorDir)."/composer.json";
-		$binDir = getBinDir($cfgPath);
+		//解析composer.json
+		$cfgPath = pathJoin($repoDir,'composer.json');
+		$data = json_decode(file_get_contents($cfgPath), true);
+		$moduleName = $data["distribute-helper"]["module-name"];
+		
+		//获取安装目录
+		$commonDir = pathJoin($rootDir,'common');
+		$configDir = pathJoin($rootDir,'config',$moduleName);
+		$moduleDir = pathJoin($rootDir,'module',$moduleName);
+		
+		//获取缓存目录
+		$cacheDir = pathJoin($config['cache-dir'],$data["name"]);
 		
-		//
+		//返回包的相关信息
 		return array(
-			"repoName" => $repoName,
-			"packageDir" => $packageDir,
-			"configPath" => $packageDir."/composer.json",
+			"repoName" => $data["name"],
+			"repoDir"  => $repoDir,
 			"cacheDir" => $cacheDir,
-			"binDir" => $binDir,
 			"ossHost" => $config['oss-host'],
+			"moduleName" => $moduleName,
+			"commonDir" => $commonDir,
+			"configDir" => $configDir,
+			"moduleDir" => $moduleDir,
+			"helper" => $data["distribute-helper"],
 		);
 	}
 	
-	//下载包add文件
-	private static function fetchAddFiles($packageInfo,$files){
+	//下载包module文件
+	private static function fetchModuleFiles($packageInfo){
 		$cacheDir = $packageInfo['cacheDir'];
 		$repoName = $packageInfo['repoName'];
-		$packageDir = $packageInfo['packageDir'];
-		$ossUrl = $packageInfo['ossUrl'];
-		if(! $files)
+		$moduleDir = $packageInfo['moduleDir'];
+		$ossHost = $packageInfo['ossHost'];
+		
+		$moduleFiles = $packageInfo['helper']['module-files'];
+		if(! $moduleFiles){
+			logInfo($repoName,'no module files defined. skip.');
 			return;
+		}
 		
-		foreach($files as $fileName){
+		foreach($moduleFiles as $fileName){
 			printf("[%s] fetching file -- %s\n",$repoName,$fileName);
 
 			//下载文件到缓存区
-			$filePath = downloadToDir($ossUrl,$repoName,$fileName,$cacheDir);
+			$filePath = downloadToDir($ossHost,$repoName,$fileName,$cacheDir);
 			if(! $filePath ){
 				printf("[%s] fetch file failed -- %s\n",$repoName,$fileName);
 				continue;
@@ -104,7 +102,7 @@ class PackageClass
 
 			// 解压文件
 			if( isZipFile($fileName) ){
-				if(! unzipFile($filePath,$packageDir)) {
+				if(! unzipFile($filePath,$moduleDir)) {
 					printf("[%s] unzip file failed -- %s\n ",$repoName,$fileName);
 					unlink($filePath);
 				}
@@ -112,61 +110,69 @@ class PackageClass
 		}
 	}
 	
-	//下载包bin文件
-	private static function fetchBinFiles($packageInfo,$binFiles){
+	//下载包common文件
+	private static function fetchCommonFiles($packageInfo){
 		$cacheDir = $packageInfo['cacheDir'];
 		$repoName = $packageInfo['repoName'];
-		$binDir = $packageInfo['binDir'];
-		$ossUrl = $packageInfo['ossUrl'];
-		if(! $binDir)
-			return;
-		if(! $binFiles)
+		$commonDir = $packageInfo['commonDir'];
+		$ossHost = $packageInfo['ossHost'];
+		$commonFiles = $packageInfo['helper']['common-files'];
+		if(! $commonFiles){
+			logInfo($repoName,'no common files defined. skip.');
 			return;
-		foreach($binFiles as $fileName){
-			printf("[%s] fetching binfile -- %s\n",$repoName,$fileName);
+		}
+		foreach($commonFiles as $fileName){
+			printf("[%s] fetching common file -- %s\n",$repoName,$fileName);
 			//下载文件到缓存区
-			$filePath = downloadToDir($ossUrl,$repoName,$fileName,$cacheDir);
+			$filePath = downloadToDir($ossHost,$repoName,$fileName,$cacheDir);
 			if(! $filePath ){
-				printf("[%s] fetch binfile failed -- %s\n",$repoName,$fileName);
+				printf("[%s] fetch common file failed -- %s\n",$repoName,$fileName);
 				continue;
 			}
 
 			// 解压文件
 			if( isZipFile($fileName) ){
-				if(! unzipFile($filePath,$binDir)) {
-					printf("[%s] unzip binfile failed -- %s\n ",$repoName,$fileName);
+				if(! unzipFile($filePath,$commonDir)) {
+					printf("[%s] unzip common file failed -- %s\n ",$repoName,$fileName);
 					unlink($filePath);
 				}
 			}
 		}
 	}
 	
-	//下载包copy文件
-	private static function fetchCopyFiles($packageInfo,$binFiles){
+	//下载包config文件
+	private static function fetchConfigFiles($packageInfo){
 		$cacheDir = $packageInfo['cacheDir'];
 		$repoName = $packageInfo['repoName'];
-		$binDir = $packageInfo['binDir'];
-		$ossUrl = $packageInfo['ossUrl'];
-		if(! $binDir)
-			return;
-		if(! $binFiles)
+		$configDir = $packageInfo['configDir'];
+		$ossHost = $packageInfo['ossHost'];
+		$configFiles = $packageInfo['helper']['config-files'];
+		if(! $configFiles){
+			logInfo($repoName,'no config files defined. skip.');
 			return;
-		foreach($binFiles as $fileName){
-			printf("[%s] fetching binfile -- %s\n",$repoName,$fileName);
+		}
+		foreach($configFiles as $fileName){
+			printf("[%s] fetching config file -- %s\n",$repoName,$fileName);
 			//下载文件到缓存区
-			$filePath = downloadToDir($ossUrl,$repoName,$fileName,$cacheDir);
+			$filePath = downloadToDir($ossHost,$repoName,$fileName,$cacheDir);
 			if(! $filePath ){
-				printf("[%s] fetch binfile failed -- %s\n",$repoName,$fileName);
+				printf("[%s] fetch config file failed -- %s\n",$repoName,$fileName);
 				continue;
 			}
 
 			// 解压文件
 			if( isZipFile($fileName) ){
-				if(! unzipFile($filePath,$binDir)) {
-					printf("[%s] unzip binfile failed -- %s\n ",$repoName,$fileName);
+				if(! unzipFile($filePath,$configDir)) {
+					printf("[%s] unzip config file failed -- %s\n ",$repoName,$fileName);
 					unlink($filePath);
 				}
 			}
 		}
 	}
+	
+	private static function registerPath($packageInfo){
+	}
+	
+	private static function registerAutorun($packageInfo){
+	}	
 }

+ 36 - 43
src/Utility.php

@@ -1,5 +1,12 @@
 <?php
-
+//记录普通日志
+function logInfo($content){
+	file_put_contents(LOG_PATH, $content.PHP_EOL, FILE_APPEND);
+}
+//记录错误日志
+function logError($content){
+	file_put_contents(LOG_PATH, $content.PHP_EOL, FILE_APPEND);
+}
 //获取操作系统名称
 function getOsName(){
 	if(substr(PHP_OS,0,3)==='WIN')
@@ -48,21 +55,43 @@ function pathJoin($base, $path) {
 	}
     return $newPath;
 }
-
+//获取部署日志文件
+function getLogPath($vendorDir){
+	return pathJoin(dirname($vendorDir),'.distribute/distribute.log');
+}
 //获取部署配置文件
-function getDistributeConfigPath($vendorDir){
+function getConfigPath($vendorDir){
 	return pathJoin(dirname($vendorDir),'.distribute/config.json')
 }
 //解析部署配置文件
-function loadDistributeConfig($cfgPath){
-	$data = json_decode(file_get_contents($cfgPath), true);
-	return $data;
+function loadConfig($cfgPath){
+	if(! file_exists($cfgPath) ){
+		logError("config file not exists",$cfgPath);
+		return false;
+	}
+	$config = json_decode(file_get_contents($cfgPath), true);
+	if(!$config){
+		logError("failed to decode config file",$cfgPath);
+		return false;
+	}
+	logInfo("succeeded to decode config file",$cfgPath);
+	
+	if(! $config['distribute-mode']){
+		logError("distribute-mode node not exists");
+		return false;
+	}
+	if(! $config['cache-dir'] ){
+		logError("cache-dir node not exists");
+		return false;
+	}
+	return $config;
 }
 
 //多线程下载文件
 function downloadToDir($ossHost,$repoName,$fileName,$targetDir){
-	if (!file_exists($targetDir))
+	if (!file_exists($targetDir)){
 		mkdir($targetDir,0777,true);
+	}
 
 	$targetPath = pathJoin($targetDir,$fileName);
 	if( file_exists($targetPath) ) {
@@ -105,39 +134,3 @@ function unzipFile($zipFile, $targetDir){
 	}
 	return false;
 }
-
-//获取最上层包配置的bin-dir路径
-function  getBinDir($cfgPath) {
-	$data = json_decode(file_get_contents($cfgPath), true);
-	$binDir = $data["distribute-helper"]["bin-dir"];
-	if(! $binDir){
-		return "";
-	}
-	$osname = getOsName();
-	if(isAssocArray($binDir))
-		$binDir = $binDir[$osname];
-	if($binDir){
-		return dirname($cfgPath) . '/' . $binDir;
-	}else{
-		return "";
-	}
-}
-//获取依赖包的待下载的文件列表
-function getPackageFiles($cfgPath)
-{
-	// 解析 composer.json
-	if (!file_exists($cfgPath)) {
-		return false;
-	}
-	$data = json_decode(file_get_contents($cfgPath), true);
-	if (!$data) {
-		print("bad json format - " . $cfgPath . PHP_EOL);
-		return false;
-	}
-	
-	//过滤并组合得到待下载文件列表
-	return array(
-		'files'=>filterFiles($data["distribute-helper"]["files"]),
-		'bin-files'=>filterFiles($data["distribute-helper"]["bin-files"])
-	);
-}