',$targetPath); return false; } } else { copy($filePath,$targetPath); if(! file_exists($targetPath) ){ logError('failed to copy file',$filePath,'->',$targetPath); return false; } } } closedir($dir); return true; } //拷贝文件或目录 function filedirCopy(string $sourcePath, string $targetPath){ if(! file_exists($sourcePath)){ logError('failed to copy file',$sourcePath,'->',$targetPath); return false; } if( is_dir($sourcePath)){ return dirCopy($sourcePath,$targetPath); }else{ return fileCopy($sourcePath,$targetPath); } } //替换文件中的宏变量 function replaceMacro(string $filePath,string $targetPath,array $macros){ $content = file_get_contents($filePath); foreach($macros as $key=>$value){ $content = str_replace($key,$value,$content); } return file_put_contents($targetPath,$content); } //枚举子目录 function scanSubDir($dir,$exclude_names = array()){ $filelist = scandir($dir); $subdirs = array(); foreach($filelist as $filename){ //过滤掉当前目录和上级目录 if($filename == "." || $filename ==".."){ continue; } //过滤掉排查的名称 if(in_array($filename,$exclude_names)){ continue; } //判断是否是文件夹 $filepath = pathJoin($dir,$filename); if(is_dir($filepath) ){ array_push($subdirs,$filename); } } return $subdirs; } //枚举所有仓库名 function scanRepoNames($vendoDir){ $repos = array(); $users = scanSubDir($vendorDir); foreach($users as $user){ $userdir = pathJoin($vendorDir,$user); $names = scanSubDir($userdir); foreach($names as $name){ array_push($repos,$user . '/' . $name); } } array_push($repos,"."); //.名称表示顶层包,放最后 return $repos; } //解析所有仓库配置 function loadRepoConfigs($vendorDir,$repoNames){ $configs = array(); foreach($repoNames as $repoName){ if($repoName == '.') $cfgPath = pathJoin(dirname($vendorDir),'composer.json'); else $cfgPath = pathJoin($vendorDir,$repoName,'composer.json'); $data = json_decode(file_get_contents($cfgPath), true); if(!$data){ logError('invalid composer.json',$cfgPath); return false; } $configs[$repoName] = $data; } return $configs; } //默认配置信息 function defaultConfig(){ return array( 'distribute-mode' => 'install', 'cache-dir' => 'd:/dist-cache/', 'tool-dir' => '', //'oss-host' => 'http://distribute-helper.oss-cn-hangzhou.aliyuncs.com', 'oss-host' => 'http://192.168.3.10:8996', 'mysql-host'=> '', ); } //解析部署配置文件 function loadConfig($cfgPath){ $config = json_decode(file_get_contents($cfgPath), true); if(!$config){ logError("failed to decode config file",$cfgPath); return false; } 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($toolDir,$ossHost,$repoName,$fileName,$targetDir){ if (!file_exists($targetDir)){ mkdir($targetDir,0777,true); } $targetPath = pathJoin($targetDir,$fileName); if( file_exists($targetPath) ) { return $targetPath; } $gopeed = getGoPeedPath($toolDir); $remoteUrl = pathJoin($ossHost,$repoName,$fileName); $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl; logInfo("start to fetch common file --",$cmd); system($cmd, $ret); if ($ret != 0) { return false; } return $targetPath; } //解压缩文件 function unzipFile($toolDir,$zipFile, $targetDir){ $fileExt = getFileExt($zipFile); if($fileExt == "zip"){ $zip= new \ZipArchive; if($zip->open($zipFile)==true){ $zip->extractTo($targetDir); $zip->close(); return true; } else{ return false; } } if($fileExt == "7z"){ $sevenZip = get7ZipPath($toolDir); $logPath = $zipFile . '.log'; $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath; system($cmd, $ret); if ($ret != 0) { return false; } return true; } return false; }