123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- //记录信息日志
- function logInfo(){
- $content = '[INF] ';
- $args = func_get_args();
- $argc = func_num_args();
- for($i=0;$i<$argc;$i++){
- $content .= $args[$i] . ' ';
- }
- $content .= PHP_EOL;
- print($content);
- file_put_contents(LOG_PATH, $content, FILE_APPEND);
- }
- //记录错误日志
- function logError(){
- $content = '[ERR] ';
- $args = func_get_args();
- $argc = func_num_args();
- for($i=0;$i<$argc;$i++){
- $content .= $args[$i] . ' ';
- }
- $content .= PHP_EOL;
- print($content);
- file_put_contents(LOG_PATH, $content, FILE_APPEND);
- }
- //获取操作系统名称
- function getOsName(){
- if(substr(PHP_OS,0,3)==='WIN')
- return "win";
- else
- return "linux";
- }
- //判断是否键值对类型的数组
- function isAssocArray($array){
- if(! is_array($array) )
- return false;
- return array_keys($array) !== range(0, count($array) - 1);
- }
- //获取文件扩展名,小写
- function getFileExt($fileName){
- return strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
- }
- //是否压缩文件
- function isZipFile($fileName){
- $fileExt = getFileExt($fileName);
- return in_array($fileExt,array("zip","7z"));
- }
- //获取 gopeed.exe 路径
- function getGoPeedPath($toolDir){
- return pathJoin($toolDir,"gopeed.exe");
- }
- //获取 7z.exe 路径
- function get7ZipPath($toolDir){
- return pathJoin($toolDir,"7z.exe");
- }
- //两个参数的路径拼接
- function twoPathJoin($base, $path) {
- return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
- }
- //支持中文的递归创建目录
- //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
- //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
- function createDirectory($path,$encoding="GBK"){
- mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
- }
- //不定参数路径拼接
- function pathJoin($base, $path) {
- $newPath = twoPathJoin($base, $path);
- $args = func_get_args();
- $argc = func_num_args();
- for($i=2;$i<$argc;$i++){
- $newPath = twoPathJoin($newPath,$args[$i]);
- }
- return $newPath;
- }
- //拷贝文件到,自动创建目录,自动覆盖
- function fileCopy(string $filePath, string $targetPath){
- $targetDir = dirname($targetPath);
- if(!is_dir($targetDir)){
- createDirectory($targetDir);
- }
-
- if( file_exists($targetPath) ){
- unlink($targetPath);
- }
- copy($filePath,$targetPath);
- return true;
- }
- //枚举子目录
- 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 loadConfig($cfgPath){
- $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($toolDir,$ossHost,$repoName,$repoVersion,$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,$repoVersion,$fileName);
- $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
- 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;
- }
|