|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2010 java2script.org and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Zhou Renjian - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package net.sf.j2s.lib.build; |
| 13 | + |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author zhou renjian |
| 23 | + * |
| 24 | + * 2010-4-18 |
| 25 | + */ |
| 26 | +public class J2SVersionDelta { |
| 27 | + |
| 28 | + private static String readFileContent(File file) { |
| 29 | + FileInputStream res = null; |
| 30 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 31 | + byte[] buf = new byte[1024]; |
| 32 | + int read = 0; |
| 33 | + try { |
| 34 | + res = new FileInputStream(file); |
| 35 | + while ((read = res.read(buf)) != -1) { |
| 36 | + baos.write(buf, 0, read); |
| 37 | + } |
| 38 | + } catch (IOException e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } finally { |
| 41 | + if (res != null) { |
| 42 | + try { |
| 43 | + res.close(); |
| 44 | + } catch (IOException e) { |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return baos.toString(); |
| 50 | + } |
| 51 | + |
| 52 | + private static void traverseFolder(File file, String baseFolder, String oldVersion, List deltas) { |
| 53 | + File[] files = file.listFiles(); |
| 54 | + if (files != null && files.length > 0) { |
| 55 | + for (int i = 0; i < files.length; i++) { |
| 56 | + File f = files[i]; |
| 57 | + if (f.isDirectory()) { |
| 58 | + traverseFolder(f, baseFolder, oldVersion, deltas); |
| 59 | + } else { |
| 60 | + String absPath = f.getAbsolutePath(); |
| 61 | + String relativePath = absPath.substring(baseFolder.length() + 1); |
| 62 | + int idx = relativePath.indexOf(File.separator); |
| 63 | + relativePath = relativePath.substring(idx + 1); |
| 64 | + String oldPath = baseFolder + File.separator + oldVersion + File.separator + relativePath; |
| 65 | + File oldFile = new File(oldPath); |
| 66 | + |
| 67 | + if (!oldFile.exists() || oldFile.length() != f.length()) { |
| 68 | + deltas.add(relativePath); |
| 69 | + } else { |
| 70 | + String content = readFileContent(f); |
| 71 | + String oldContent = readFileContent(oldFile); |
| 72 | + if (!content.equals(oldContent)) { |
| 73 | + deltas.add(relativePath); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + if (args == null || args.length != 3) { |
| 83 | + System.out.println("Usage:\r\nJ2SVersionDelta <base folder> <original version alias> <updated version alias>"); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + String oldVersion = args[1]; |
| 88 | + String newVersion = args[2]; |
| 89 | + File originalFolder = new File(args[0], oldVersion); |
| 90 | + File updatedFolder = new File(args[0], newVersion); |
| 91 | + if (!originalFolder.exists()) { |
| 92 | + System.out.println("Folder " + oldVersion + " does not exist!"); |
| 93 | + return; |
| 94 | + } |
| 95 | + if (!updatedFolder.exists()) { |
| 96 | + System.out.println("Folder " + newVersion + " does not exist!"); |
| 97 | + return; |
| 98 | + } |
| 99 | + String basePath = new File(args[0]).getAbsolutePath(); |
| 100 | + ArrayList deltas = new ArrayList(); |
| 101 | + traverseFolder(updatedFolder, basePath, oldVersion, deltas); |
| 102 | + int size = deltas.size(); |
| 103 | + if (size > 0) { |
| 104 | + System.out.println("window[\"j2s.update.delta\"] = ["); |
| 105 | + for (int i = 0; i < size; i++) { |
| 106 | + String path = (String) deltas.get(i); |
| 107 | + if (i > 0) { |
| 108 | + System.out.print("\t\"$\", \"$\", "); |
| 109 | + } else { |
| 110 | + System.out.print("\t\"" + oldVersion + "\", "); |
| 111 | + System.out.print("\"" + newVersion + "\", "); |
| 112 | + } |
| 113 | + System.out.print("\"" + path.replace('\\', '/') + "\""); |
| 114 | + if (i == size - 1) { |
| 115 | + System.out.println(); |
| 116 | + } else { |
| 117 | + System.out.println(","); |
| 118 | + } |
| 119 | + } |
| 120 | + System.out.println("];"); |
| 121 | + } else { |
| 122 | + System.out.println("There is no updates!"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments