diff --git a/kpt/CmdPaserType.java b/kpt/CmdPaserType.java new file mode 100644 index 0000000000000000000000000000000000000000..951847e9cf50f73d912cc91cbececdb7962751fd --- /dev/null +++ b/kpt/CmdPaserType.java @@ -0,0 +1,6 @@ +public enum CmdPaserType { + VERSION, + CREATE_PACKAGE, + INSTALL_PACKAGE, + LIST_PACKAGE, +} diff --git a/kpt/FileUtils.java b/kpt/FileUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..41d4234c08ede7d0e0b031749ae4fac3380fead1 --- /dev/null +++ b/kpt/FileUtils.java @@ -0,0 +1,94 @@ +import java.io.*; +import java.nio.Buffer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class FileUtils { + + /** + * 获取文件扩展名 + * @param fullFileName 完整文件名 + * @return + */ + public static String getFileSuffix(String fullFileName) { + try { + return fullFileName.substring(fullFileName.lastIndexOf(".") + 1); + } catch (Exception e) { + return fullFileName; + } + } + + /** + * 获取文件名 + * @param fullFIleName 完整文件名 + * @return + */ + public static String getFileName(String fullFIleName) { + try { + return fullFIleName.substring(0 , fullFIleName.lastIndexOf(".")); + }catch (Exception exception) { + return fullFIleName; + } + } + + /** + * 解压并安装 + * @param zipFilePath + * @param destDirPath + * @param jarDirectory + * @return + */ + public static boolean unZip(String zipFilePath, String destDirPath, String jarDirectory) { + File destDir = new File(destDirPath); + if (!destDir.exists()) { + if(!destDir.mkdirs()) { + return false; + } + } + try (ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(Paths.get(zipFilePath)))) { + ZipEntry entry = zipIn.getNextEntry(); + while (entry != null) { + String filePath = destDirPath + File.separator + entry.getName(); + System.out.println("[INFO] INSTALL: " + entry.getName() + "."); + if (!entry.isDirectory()) { + if (entry.getName().equals("info.json")) { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipIn)); + StringBuilder sb = new StringBuilder(); + while (true) { + String line = bufferedReader.readLine(); + if (line == null) { + break; + } + sb.append(line); + } + FileWriter fw = new FileWriter(jarDirectory + "/../lib_info/" + getFileName(new File(zipFilePath).getName()) + ".json"); + fw.write(sb.toString()); + fw.flush(); + fw.close(); + entry = zipIn.getNextEntry(); + continue; + } + BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(Paths.get(filePath))); + byte[] bytesIn = new byte[4096]; + int read = 0; + while ((read = zipIn.read(bytesIn)) != -1) { + bos.write(bytesIn, 0, read); + } + } else { + if (!(new File(filePath).mkdir())) { + return false; + } + } + entry = zipIn.getNextEntry(); + } + System.out.println("[FINISH] Install OK."); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/kpt/KptMain.java b/kpt/KptMain.java index 9368b5b0bad8ea065b4fae736b8e7a4ae987ee5f..8ce5ab2bfd274a4686cb836bef8b8c83f109a72f 100644 --- a/kpt/KptMain.java +++ b/kpt/KptMain.java @@ -1,139 +1,112 @@ - - -import java.io.*; -import java.net.URISyntaxException; import java.io.File; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; +import java.io.FileWriter; +import java.net.URISyntaxException; public class KptMain { - public static String info = " Kpt:\n" + + + private static final String HELP_INFO = " Kpt:\n" + " -n [name] Create a new package.\n" + " -i [package path] Install a package on system.\n" + " -version Show the version information.\n" + " -list List all the install package."; - public static String version = "1.0"; - public static String jarDirectory = null; - static { + + private static final String KPT_VERSION = "1.0"; + + private static String JAR_DIRECTORY = null; + + public KptMain() { + // get jar directory try { - //获取jar包的位置 - jarDirectory = new File(KptMain.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent(); + JAR_DIRECTORY = new File(KptMain.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent(); } catch (URISyntaxException e) { - throw new RuntimeException(e); + throw new RuntimeException(e); } } - public static String getName(String filename) { - try { - return filename.substring(0 , filename.lastIndexOf(".")); - }catch (Exception exception) { - return filename; + + + + /** + * 解析命令模式参数 + * @param args 参数数组 + * @return 解析枚举 + */ + private static CmdPaserType cmdPaser(String[] args) { + if (args.length == 0) { + return null; } - } - public static String getLastName(String filename) { - try { - return filename.substring(filename.lastIndexOf(".")+1); - }catch (Exception e) { - return filename; + String cmdType = args[0]; + switch (cmdType) { + case "-version": + case "-v": + return CmdPaserType.VERSION; + case "-n": + return CmdPaserType.CREATE_PACKAGE; + case "-i": + return CmdPaserType.INSTALL_PACKAGE; + case "-list": + return CmdPaserType.LIST_PACKAGE; } + return null; } + public static void main(String[] args) { - try { - if (args[0].equals("-version")) { - System.out.println(version); + CmdPaserType paserType = cmdPaser(args); + if (paserType == CmdPaserType.VERSION) { + System.out.println(KPT_VERSION); + } else if (paserType == CmdPaserType.CREATE_PACKAGE) { + if (args.length < 2) { + System.out.println("ERR: Please input a package name."); } - else if (args[0].equals("-n")) { - String name = args[1]; - new File(name).mkdir(); - FileWriter fileWriter = new FileWriter(name+"/info.json"); - fileWriter.write("{\n" + - " \"name\" : \""+name+"\",\n" + + String packageName = args[1]; + // Because of the folder name rules, package name must mache it. + if (!packageName.matches("^[\\w\\s!@#$%^&()\\-_{},.~]+")) { + System.out.println("ERR: Please input a right package name. Do not use \\ or / in the package name."); + } + try { + if (!(new File(packageName).mkdir())) { + System.out.println("ERR: Please check your System Permissions"); + } + FileWriter fileWriter = new FileWriter(packageName + "/info.json"); + String packageInfoContext = "{\n" + + " \"name\" : \"" + packageName + "\",\n" + " \"author\": \"\",\n" + " \"time\": \"\",\n" + " \"depend\": null,\n" + " \"version\": 1.0\n" + - "}"); + "}"; + fileWriter.write(packageInfoContext); + fileWriter.flush(); fileWriter.close(); - System.out.println("Create Package: "+name); + } catch (Exception e) { +// e.printStackTrace(); + System.out.println("ERR: Please check your System Permissions"); } - else if (args[0].equals("-i")) { - unzip(args[1] , jarDirectory+"/../lib/"); + System.out.println("Create package: " + packageName); + } else if (paserType == CmdPaserType.INSTALL_PACKAGE) { + if (args.length < 2) { + System.out.println("ERR: Please input a package name."); } - else if (args[0].equals("-list")) { - System.out.println(" [Package] ==>"); - File[] files = new File(jarDirectory+"/../lib_info/").listFiles(); - StringBuilder stringBuilder = new StringBuilder(); - for (File i : files) { - if (getLastName(i.getName()).equals("json")) { - stringBuilder.append(" - "); - stringBuilder.append(getLastName(getName(i.getName()))); - stringBuilder.append("\n"); - } - } - System.out.println(stringBuilder.toString()); - } - }catch (Exception e) { - System.out.println(info); - } - } - /** - * 解压ZIP文件到指定目录 - * - * @param zipFilePath ZIP文件的路径 - * @param destDirPath 解压目标目录的路径 - */ - public static void unzip(String zipFilePath, String destDirPath) { - // 创建目标目录 - File destDir = new File(destDirPath); - if (!destDir.exists()) { - destDir.mkdirs(); - } - try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) { - ZipEntry entry = zipIn.getNextEntry(); - // 遍历ZIP文件中的每一个条目 - while (entry != null) { - String filePath = destDirPath + File.separator + entry.getName(); - System.out.println("[INFO] INSTALL: "+entry.getName()+"."); - if (!entry.isDirectory()) { - if (entry.getName().equals("info.json")) { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipIn)); - StringBuilder stringBuilder = new StringBuilder(); - while (true) { - String line = bufferedReader.readLine(); - if (line == null) { - break; - } - stringBuilder.append(line); + FileUtils.unZip(args[1], JAR_DIRECTORY + "/../lib/", JAR_DIRECTORY); + } else if (paserType == CmdPaserType.LIST_PACKAGE) { + System.out.println(" [Package] ==>"); + StringBuilder stringBuilder = new StringBuilder(); + try { + File[] files = new File(JAR_DIRECTORY + "/../lib_info/").listFiles(); + if (files != null) { + for (File i : files) { + if (FileUtils.getFileSuffix(i.getName()).equals("json")) { + stringBuilder.append(" - "); + stringBuilder.append(FileUtils.getFileSuffix(FileUtils.getFileName(i.getName()))); + stringBuilder.append("\n"); } - //bufferedReader.close(); - FileWriter fileWriter = new FileWriter(jarDirectory+"/../lib_info/"+getName(new File(zipFilePath).getName())+".json"); - fileWriter.write(stringBuilder.toString()); - fileWriter.close(); - entry = zipIn.getNextEntry(); - continue; } - // 解压文件 - extractFile(zipIn, filePath); - } else { - // 创建目录 - File dir1 = new File(filePath); - dir1.mkdirs(); } - entry = zipIn.getNextEntry(); - } - System.out.println("[FINISH] Install OK."); - } catch (IOException e) { - e.printStackTrace(); - } - } - // 解压文件到指定路径 - private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { - //System.out.println(filePath); - try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) { - byte[] bytesIn = new byte[4096]; - int read = 0; - while ((read = zipIn.read(bytesIn)) != -1) { - bos.write(bytesIn, 0, read); + } catch (Exception e) { + System.out.println("ERR: List packages error."); } + System.out.println(stringBuilder); + } else { + System.out.println(HELP_INFO); } } }