elviss 2 anni fa
parent
commit
ce7983a397
2 ha cambiato i file con 46 aggiunte e 1 eliminazioni
  1. 37 1
      src/PackageClass.php
  2. 9 0
      src/Utility.php

+ 37 - 1
src/PackageClass.php

@@ -113,11 +113,15 @@ class PackageClass
 		//拷贝copy-files文件
 		self::copyFiles($packageInfo);
 		
+		//替换文件内的宏变量
+		self::macoFiles($packageInfo);
+		
 		//注册全局路径
 		self::registerPath($packageInfo);
 		
 		//注册自启动程序
 		self::registerAutorun($packageInfo);
+		
 	}
 	//初始化
 	private static function initialize($vendorDir)
@@ -345,7 +349,7 @@ class PackageClass
 		$rootDir = $packageInfo['rootDir'];
 		$copyFiles = $app["distribute-root"]["copy-files"] ? : array();
 		foreach($copyFiles as $fileInfo){
-			$fileName = $fileInfo['name'];
+			$fileName = $fileInfo['path'];
 			$targetName = $fileInfo['target'];
 			$filePath = pathJoin($rootDir,$fileName);
 			if(! file_exists($filePath) ){
@@ -366,6 +370,38 @@ class PackageClass
 		}
 		return true;
 	}	
+	private static function macoFiles($packageInfo)
+	{
+		$app = self::$app;	
+		$cacheDir = $packageInfo['cacheDir'];
+		$repoName = $packageInfo['repoName'];
+		$version = $packageInfo['version'];
+		$rootDir = $packageInfo['rootDir'];
+		$macroFiles = $app["distribute-root"]["macro-files"] ? : array();
+		
+		$macros = array(
+			'{root}' => $rootDir
+		);
+		foreach($macroFiles as $fileInfo){
+			$fileName = $fileInfo['path'];
+			$filePath = pathJoin($rootDir,$fileName);
+			if(! file_exists($filePath) ){
+				logError("failed to replace macro file, file not exists.",$filePath);
+				return false;
+			}
+			$targetName = $fileInfo['target'];
+			$targetPath = pathJoin($rootDir,$targetName);
+			if( replaceMacro($filePath,$targetPath,$macros) ){
+				logInfo("succeeded to replace macro file",$fileName);
+			}
+			else{
+				logError("failed to replace macro file",$filePath);
+				return false;
+			}
+		}
+		return true;
+	}
+
 	private static function registerPath($packageInfo)
 	{
 	}

+ 9 - 0
src/Utility.php

@@ -143,6 +143,15 @@ function filedirCopy(string $sourcePath, string $targetPath){
 	}
 }
 
+//替换文件中的宏变量
+function replaceMacro(string $filePath,string $targetPath,array $macros){
+	$content = file_get_contents($filePath);
+	foreach($macros as $key=>$value){
+		$content = str_replace($content,$key,$value);
+	}
+	file_put_contents($targetPath,$content);
+}
+
 //枚举子目录
 function scanSubDir($dir,$exclude_names = array()){
 	$filelist = scandir($dir);