package com.matt.utils.patch;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 根据SVN日志提交记录自动生成增量补丁文件
* @author matt0614@qq.com
* 根据idea gitLab 生成的.path文件生成补丁
*
*/
public class CreatePatchFileFromPatchFile {
private static final String patchFile ="D:\\workspace\\newpath.patch"; //补丁位置
private static final String projectPath="D:\\workspace\\idea\\ztsc\\zzxt\\";//项目文件 开发路径
private static final String classPath="D:\\workspace\\idea\\ztsc\\zzxt\\out\\artifacts\\zzxt_Web_exploded\\WEB-INF\\classes\\";//class文件路径
private static final String savePath="F:\\工作目录\\09.种子管理系统\\patchs\\zzxt20170417\\";//补丁文件包存放文件夹
private static final String WebContent = "WebContent";//WebRoot 名称
/**
* 得到文件列表新方法
* @return
* @throws IOException
*/
public static List<String> getFileList() throws IOException{
File file = new File(patchFile);
List<String> lines = FileUtils.readLines(file, "GBK");
List<String> fileList = new ArrayList<String>();
for(String filePath:lines){
if(filePath.startsWith("Index:")){
int s = filePath.indexOf(":");
String fileSrc = filePath.substring(s+1).trim();
if(!fileList.contains(fileSrc)){
fileSrc = fileSrc.replace("/", "\\");
if (fileSrc.lastIndexOf(".java") != -1) {
String classFile = fileSrc.replace(".java", ".class");
classFile = classFile.replace("src\\", "");
classFile = classPath + classFile;
fileList.add(classFile);
System.out.println("更新列表:" + classFile);
} else {
fileSrc = projectPath + fileSrc;
fileList.add(fileSrc);
System.out.println("更新列表:" + fileSrc);
}
}
}
}
return fileList;
}
private static void copyFilesList(List<String> list) {
String toFilePath = "";
for (String sourFile:list) {
if (sourFile.lastIndexOf(".java") != -1) {
//todo 如果需要复制java源文件,在这写相应代码
} else if (sourFile.lastIndexOf(".class") != -1) {
toFilePath = savePath + WebContent + sourFile.replace(classPath, "\\WEB-INF\\classe\\");
} else {
toFilePath = sourFile.replace(projectPath, "");
toFilePath = savePath + toFilePath;
}
File file = new File(toFilePath);
String fileName = file.getName();
toFilePath = toFilePath.replace(fileName, "");
copy(sourFile, toFilePath);
}
}
private static void copy(String sourFile, String toDir) {
try {
File file = new File(sourFile);
FileUtils.copyFileToDirectory(file, new File(toDir));
System.out.println("复制文件:" + file.getName()+" 到 "+toDir);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
List<String> list = getFileList();
copyFilesList(list);
}
}