MAC环境下,使用Beyond Compare命令行生成两个文件夹差异的html,按目录递归生成。
#1. 创建compare
#2. 创建compare/old
#3. compare/new
#4. sh tree.sh ~/Desktop/compare
#5. 生成 ~/Desktop/report
#环境初始化
function init()
{
echo "#########环境初始化#########################"
WORK_DIR=$(cd $(dirname $0); pwd)
#echo $WORK_DIR
BasePath=$1
BasePathLength=${#BasePath}
#命令行是否/结尾问题
FINAL=${BasePath:0-1}
#echo "FINAL:"$FINAL
#echo "BasePath...:"$BasePath
if [ ${FINAL} == "/" ]; then
#命令行是/结尾
BasePath=${BasePath:0:$BasePathLength-1}
#echo "BasePath......:"$BasePath
BaseOldPath="$1old"
BaseNewPath="$1new"
echo "YES"
else
#命令行否/结尾
BaseOldPath="$1/old"
BaseNewPath="$1/new"
echo "NO"
fi
BaseReportPath=$WORK_DIR/report
#生成report目录
if [ ! -d $BaseReportPath ]; then
echo "新建目录"
mkdir $BaseReportPath
else
echo "目录存在,重新生成"
rm -rf $BaseReportPath
mkdir $BaseReportPath
fi
#检查目录
if [ -d $BaseReportPath ]; then
echo "report目录创建成功"
else
echo "report目录创建失败,退出"
exit
fi
BasePatchPath=$WORK_DIR/diff_to_html
echo "强制写入比对配置文件"
echo "text-report layout:side-by-side &
options:display-all,line-numbers,display-context &
output-to:%3 output-options:html-color %1 %2" > $BasePatchPath
if [ -f $BasePatchPath ]; then
echo "临时写入比对配置文件成功"
else
echo "临时写入比对配置文件失败,退出"
exit
fi
BaseEmptyPath=$WORK_DIR/empty
echo "" > $BaseEmptyPath
echo "##########路径参数###########"
echo "BasePath...............:$BasePath"
echo "BaseOldPath............:$BaseOldPath"
echo "BaseNewPath............:$BaseNewPath"
echo "BaseReportPath.........:$BaseReportPath"
echo "BasePatchPath..........:$BasePatchPath"
echo "BaseEmptyPath..........:$BaseEmptyPath"
echo "#####################"
echo "#########环境初始化 ok############################"
}
#目录检查
# BasePath...............:~/Desktop/compare
# BaseOldPath............:~/Desktop/compare/old
# BaseNewPath............:~/Desktop/compare/new
function checkCompareDir(){
echo "##############checkCompareDir start########################"
if [ ! -d $BasePath ]; then
echo $BasePath"目录不存在,请检查!"
exit 0
fi
if [ ! -d $BaseOldPath ]; then
echo $BaseOldPath"子目录不存在,请检查!"
exit 0
fi
if [ ! -d $BaseNewPath ]; then
echo $BaseNewPath"子目录不存在,请检查!"
exit 0
fi
echo "##############checkCompareDir end########################"
}
function listFiles()
{
#1st param, the dir name
for file in `ls $1`;
do
#echo "path....:$1/$file"
if [ -d "$1/$file" ]; then
# echo "$2$file"
# echo "$1/$file"
#递归
listFiles "$1/$file"
else
#比较文件A
compareFileAPath=$1/$file
#echo "compareFileAPath..............:$compareFileAPath"
if [[ "${compareFileAPath}" =~ $BaseNewPath ]]; then
#echo "compareFileAPath is new"
compareFileBPath=$compareFileAPath
compareFileAPath=${compareFileBPath/$BaseNewPath/$BaseOldPath}
if [ ! -d $compareFileAPath ]; then
echo "A path不存在"
fulldir=$1
#临时html路径,compareFileAPath替换BaseOldPath赋值BaseReportPath
tempHtmlPath=${compareFileBPath/$BaseNewPath/$BaseReportPath}
# echo "tempHtmlPath....................:$tempHtmlPath"
#html文件路径,tempHtmlPath拼接.html
htmlPath=$tempHtmlPath.html
# echo "htmlPath......................:$htmlPath"
#替换生成Html文件对应的目录
htmldir=${fulldir/$BaseNewPath/$BaseReportPath}
#echo "htmldir......................:$htmldir"
#如果Html文件夹不存在,创建对应文件夹
if [ ! -d $htmldir ]; then
#echo "create.....:$htmldir"
mkdir $htmldir
fi
fi
else
echo "#old############"
fulldir=$1
# echo "fulldir..............:$fulldir"
#比较文件B,compareFileAPath替换BaseOldPath赋值BaseNewPath
compareFileBPath=${compareFileAPath/$BaseOldPath/$BaseNewPath}
# echo "compareFileBPath..............:$compareFileBPath"
#临时html路径,compareFileAPath替换BaseOldPath赋值BaseReportPath
tempHtmlPath=${compareFileBPath/$BaseNewPath/$BaseReportPath}
# echo "tempHtmlPath....................:$tempHtmlPath"
#html文件路径,tempHtmlPath拼接.html
htmlPath=$tempHtmlPath.html
# echo "htmlPath......................:$htmlPath"
#替换生成Html文件对应的目录
htmldir=${fulldir/$BaseNewPath/$BaseReportPath}
# echo "htmldir......................:$htmldir"
#如果Html文件夹不存在,创建对应文件夹
if [ ! -d $htmldir ]; then
echo "create.....:$htmldir"
mkdir $htmldir
fi
fi
echo "#################file compare start#####################"
if [ -f $compareFileBPath ]; then
#echo "compare aciton...B存在:$compareFileBPath"
if [ ! -f $compareFileAPath ]; then
echo "#################NEW#####################"
bcompare -silent @$BasePatchPath $BaseEmptyPath $compareFileBPath $htmlPath
else
#A存在
echo "#################UPDATE#####################"
#echo "compare aciton...$compareFileAPath"
# echo "compareFileAPath.......:$compareFileAPath"
# echo "compareFileBPath.......:$compareFileBPath"
if [[ "${compareFileAPath}" =~ $BaseOldPath ]]; then
bcompare -silent @$BasePatchPath $compareFileAPath $compareFileBPath $htmlPath
fi
fi
fi
echo "###################file compare end#########################"
fi
done
}
#清理函数
function clear(){
#echo "删除比对配置文件"
rm -rf $BasePatchPath
rm -rf $BaseEmptyPath
}
#######main run
init $1
checkCompareDir
listFiles $BasePath
clear