+++ /dev/null
-/*\r
- * Copyright 2011 gitblit.com.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package com.gitblit.build;\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileWriter;\r
-import java.io.FilenameFilter;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.RandomAccessFile;\r
-import java.net.URL;\r
-import java.text.MessageFormat;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-import java.util.Properties;\r
-\r
-import com.gitblit.Constants;\r
-import com.gitblit.utils.StringUtils;\r
-\r
-/**\r
- * The Build class downloads runtime and compile-time jar files from the Apache\r
- * or Eclipse Maven repositories.\r
- * \r
- * It also generates the Keys class from the gitblit.properties file.\r
- * \r
- * Its important that this class have minimal compile dependencies since its\r
- * called very early in the build script.\r
- * \r
- * @author James Moger\r
- * \r
- */\r
-public class Build {\r
-\r
- private static final String osName = System.getProperty("os.name");\r
-\r
- public interface DownloadListener {\r
- public void downloading(String name);\r
- }\r
-\r
- /**\r
- * BuildType enumeration representing compile-time or runtime. This is used\r
- * to download dependencies either for Gitblit GO runtime or for setting up\r
- * a development environment.\r
- */\r
- public static enum BuildType {\r
- RUNTIME, COMPILETIME;\r
- }\r
-\r
- private static DownloadListener downloadListener;\r
-\r
- public static void main(String... args) {\r
- runtime();\r
- compiletime();\r
- buildSettingKeys();\r
- delete(\r
- "bcmail-jdk16-1.46.jar",\r
- "bcprov-jdk16-1.46.jar",\r
- "src/bcmail-jdk16-1.46-sources.jar",\r
- "src/bcprov-jdk16-1.46-sources.jar");\r
- }\r
-\r
- public static void runtime() {\r
- downloadFromApache(MavenObject.JCOMMANDER, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JETTY, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JETTY_AJP, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SERVLET, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4JAPI, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.WICKET, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.WICKET_EXT, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.WICKET_AUTH_ROLES, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.WICKET_GOOGLE_CHARTS, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_MAIL, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_PKIX, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JSCH, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.ROME, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JDOM, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.GSON, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.MAIL, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.GROOVY, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_HIGHLIGHTER, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_MEMORY, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_QUERIES, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JAKARTA_REGEXP, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.UNBOUND_ID, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.IVY, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JCALENDAR, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.COMMONS_COMPRESS, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.XZ, BuildType.RUNTIME);\r
-\r
- //needed for selenium ui tests\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_API, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_FF, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_JAVA, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_REMOTE, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_SUPPORT, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.GUAVA, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.JSON, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.COMMONS_EXEC, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPCLIENT, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPCORE, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPMIME, BuildType.RUNTIME);\r
- downloadFromApacheToExtSelenium(MavenObject.COMMONS_LOGGING, BuildType.RUNTIME);\r
- \r
- downloadFromEclipse(MavenObject.JGIT, BuildType.RUNTIME);\r
- downloadFromEclipse(MavenObject.JGIT_HTTP, BuildType.RUNTIME);\r
- }\r
-\r
- public static void compiletime() {\r
- downloadFromApache(MavenObject.JUNIT, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.HAMCREST, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JCOMMANDER, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JETTY, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JETTY_AJP, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.SERVLET, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.SLF4JAPI, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.SLF4LOG4J, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.LOG4J, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.WICKET, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.WICKET_EXT, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.WICKET_AUTH_ROLES, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.WICKET_GOOGLE_CHARTS, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.MARKDOWNPAPERS, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_MAIL, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_PKIX, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JSCH, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.ROME, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JDOM, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.GSON, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.MAIL, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.GROOVY, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.LUCENE, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.LUCENE_HIGHLIGHTER, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.LUCENE_MEMORY, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.LUCENE_QUERIES, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JAKARTA_REGEXP, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.UNBOUND_ID, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.IVY, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.JCALENDAR, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.COMMONS_COMPRESS, BuildType.COMPILETIME);\r
- downloadFromApache(MavenObject.XZ, BuildType.COMPILETIME);\r
-\r
- //needed for selenium ui tests\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_API, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_FF, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_JAVA, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_REMOTE, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.SEL_SUPPORT, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.GUAVA, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.JSON, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.COMMONS_EXEC, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPCLIENT, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPCORE, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.HTTPMIME, BuildType.COMPILETIME);\r
- downloadFromApacheToExtSelenium(MavenObject.COMMONS_LOGGING, BuildType.COMPILETIME);\r
- \r
- downloadFromEclipse(MavenObject.JGIT, BuildType.COMPILETIME);\r
- downloadFromEclipse(MavenObject.JGIT_HTTP, BuildType.COMPILETIME);\r
-\r
- // needed for site publishing\r
- downloadFromApache(MavenObject.COMMONSNET, BuildType.RUNTIME);\r
- }\r
- \r
- private static void delete(String... files) {\r
- for (String name : files) {\r
- File file = new File("ext", name);\r
- if (file.exists()) {\r
- file.delete();\r
- }\r
- }\r
- }\r
-\r
- public static void federationClient() {\r
- downloadFromApache(MavenObject.JCOMMANDER, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SERVLET, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.MAIL, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4JAPI, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.GSON, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JSCH, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_HIGHLIGHTER, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_MEMORY, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LUCENE_QUERIES, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JAKARTA_REGEXP, BuildType.RUNTIME);\r
-\r
- downloadFromEclipse(MavenObject.JGIT, BuildType.RUNTIME);\r
- }\r
-\r
- public static void manager(DownloadListener listener) {\r
- downloadListener = listener;\r
- downloadFromApache(MavenObject.GSON, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.ROME, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JDOM, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JSCH, BuildType.RUNTIME);\r
-\r
- downloadFromEclipse(MavenObject.JGIT, BuildType.RUNTIME);\r
- }\r
- \r
- public static void authority(DownloadListener listener) {\r
- downloadListener = listener;\r
- downloadFromApache(MavenObject.JCOMMANDER, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JSCH, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4JAPI, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.SLF4LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.LOG4J, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_MAIL, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.BOUNCYCASTLE_PKIX, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.JCALENDAR, BuildType.RUNTIME);\r
- downloadFromApache(MavenObject.MAIL, BuildType.RUNTIME);\r
-\r
- downloadFromEclipse(MavenObject.JGIT, BuildType.RUNTIME);\r
- }\r
-\r
- /**\r
- * Builds the Keys class based on the gitblit.properties file and inserts\r
- * the class source into the project source folder.\r
- */\r
- public static void buildSettingKeys() {\r
- // Load all keys\r
- Properties properties = new Properties();\r
- FileInputStream is = null;\r
- try {\r
- is = new FileInputStream(new File("distrib", Constants.PROPERTIES_FILE));\r
- properties.load(is);\r
- } catch (Throwable t) {\r
- t.printStackTrace();\r
- } finally {\r
- if (is != null) {\r
- try {\r
- is.close();\r
- } catch (Throwable t) {\r
- // IGNORE\r
- }\r
- }\r
- }\r
- List<String> keys = new ArrayList<String>(properties.stringPropertyNames());\r
- Collections.sort(keys);\r
-\r
- KeyGroup root = new KeyGroup();\r
- for (String key : keys) {\r
- root.addKey(key);\r
- }\r
-\r
- // Save Keys class definition\r
- try {\r
- File file = new File("src/com/gitblit/Keys.java");\r
- FileWriter fw = new FileWriter(file, false);\r
- fw.write(root.generateClass("com.gitblit", "Keys"));\r
- fw.close();\r
- } catch (Throwable t) {\r
- t.printStackTrace();\r
- }\r
- }\r
- \r
- private static class KeyGroup {\r
- final KeyGroup parent;\r
- final String namespace;\r
- \r
- String name;\r
- List<KeyGroup> children; \r
- List<String> fields; \r
- \r
- KeyGroup() {\r
- this.parent = null;\r
- this.namespace = "";\r
- this.name = ""; \r
- }\r
- \r
- KeyGroup(String namespace, KeyGroup parent) {\r
- this.parent = parent;\r
- this.namespace = namespace;\r
- if (parent.children == null) {\r
- parent.children = new ArrayList<KeyGroup>();\r
- }\r
- parent.children.add(this);\r
- }\r
- \r
- void addKey(String key) {\r
- String keyspace = "";\r
- String field = key;\r
- if (key.indexOf('.') > -1) {\r
- keyspace = key.substring(0, key.lastIndexOf('.'));\r
- field = key.substring(key.lastIndexOf('.') + 1);\r
- KeyGroup group = addKeyGroup(keyspace);\r
- group.addKey(field);\r
- } else {\r
- if (fields == null) {\r
- fields = new ArrayList<String>();\r
- }\r
- fields.add(key);\r
- }\r
- }\r
- \r
- KeyGroup addKeyGroup(String keyspace) {\r
- KeyGroup parent = this;\r
- KeyGroup node = null; \r
- String [] space = keyspace.split("\\.");\r
- for (int i = 0; i < space.length; i++) {\r
- StringBuilder namespace = new StringBuilder();\r
- for (int j = 0; j <= i; j++) {\r
- namespace.append(space[j]);\r
- if (j < i) {\r
- namespace.append('.');\r
- }\r
- }\r
- if (parent.children != null) {\r
- for (KeyGroup child : parent.children) {\r
- if (child.name.equals(space[i])) {\r
- node = child; \r
- }\r
- }\r
- }\r
- if (node == null) {\r
- node = new KeyGroup(namespace.toString(), parent);\r
- node.name = space[i];\r
- }\r
- parent = node;\r
- node = null;\r
- }\r
- return parent;\r
- } \r
- \r
- String fullKey(String field) {\r
- if (namespace.equals("")) {\r
- return field;\r
- }\r
- return namespace + "." + field;\r
- }\r
- \r
- String generateClass(String packageName, String className) {\r
- StringBuilder sb = new StringBuilder();\r
- sb.append("package ").append(packageName).append(";\n");\r
- sb.append('\n');\r
- sb.append("/*\n");\r
- sb.append(" * This class is auto-generated from the properties file.\n");\r
- sb.append(" * Do not version control!\n");\r
- sb.append(" */\n");\r
- sb.append(MessageFormat.format("public final class {0} '{'\n\n", className));\r
- sb.append(generateClass(this, 0));\r
- sb.append("}\n");\r
- return sb.toString();\r
- }\r
- \r
- String generateClass(KeyGroup group, int level) {\r
- String classIndent = StringUtils.leftPad("", level, '\t');\r
- String fieldIndent = StringUtils.leftPad("", level + 1, '\t');\r
- \r
- // begin class\r
- StringBuilder sb = new StringBuilder();\r
- if (!group.namespace.equals("")) {\r
- sb.append(classIndent).append(MessageFormat.format("public static final class {0} '{'\n\n", group.name));\r
- sb.append(fieldIndent).append(MessageFormat.format("public static final String _ROOT = \"{0}\";\n\n", group.namespace));\r
- }\r
- \r
- if (group.fields != null) {\r
- // fields\r
- for (String field : group.fields) { \r
- sb.append(fieldIndent).append(MessageFormat.format("public static final String {0} = \"{1}\";\n\n", field, group.fullKey(field)));\r
- }\r
- }\r
- if (group.children != null) {\r
- // inner classes\r
- for (KeyGroup child : group.children) {\r
- sb.append(generateClass(child, level + 1));\r
- }\r
- }\r
- // end class\r
- if (!group.namespace.equals("")) {\r
- sb.append(classIndent).append("}\n\n");\r
- }\r
- return sb.toString(); \r
- }\r
- }\r
-\r
- /**\r
- * Download a file from the official Apache Maven repository.\r
- * \r
- * @param mo\r
- * the maven object to download.\r
- * @return\r
- */\r
- private static List<File> downloadFromApache(MavenObject mo, BuildType type) {\r
- return downloadFromMaven("http://repo1.maven.org/maven2/", mo, type);\r
- }\r
-\r
- /**\r
- * Download a file from the official Eclipse Maven repository.\r
- * \r
- * @param mo\r
- * the maven object to download.\r
- * @return\r
- */\r
- private static List<File> downloadFromEclipse(MavenObject mo, BuildType type) {\r
- return downloadFromMaven("http://download.eclipse.org/jgit/maven/", mo, type);\r
- }\r
-\r
- /**\r
- * Download a file from a Maven repository.\r
- * \r
- * @param mo\r
- * the maven object to download.\r
- * @return\r
- */\r
- private static List<File> downloadFromMaven(String mavenRoot, MavenObject mo, BuildType type, String targetFolder) {\r
- List<File> downloads = new ArrayList<File>();\r
- String[] jars = { "" };\r
- if (BuildType.RUNTIME.equals(type)) {\r
- jars = new String[] { "" };\r
- } else if (BuildType.COMPILETIME.equals(type)) {\r
- jars = new String[] { "-sources" };\r
- }\r
- for (String jar : jars) {\r
- File targetFile = mo.getLocalFile(targetFolder, jar);\r
- if ("-sources".equals(jar)) {\r
- File relocated = new File(targetFolder+"/src", targetFile.getName());\r
- if (targetFile.exists()) {\r
- // move -sources jar to ext/src folder\r
- targetFile.renameTo(relocated);\r
- }\r
- // -sources jars are located in ext/src\r
- targetFile = relocated;\r
- }\r
- \r
- if (targetFile.exists()) {\r
- downloads.add(targetFile);\r
- removeObsoleteArtifacts(mo, type, targetFile.getParentFile());\r
- continue;\r
- }\r
- String expectedSHA1 = mo.getSHA1(jar);\r
- if (expectedSHA1 == null) {\r
- // skip this jar\r
- continue;\r
- }\r
- float approximateLength = mo.getApproximateLength(jar);\r
- String mavenURL = mavenRoot + mo.getRepositoryPath(jar);\r
- if (!targetFile.getAbsoluteFile().getParentFile().exists()) {\r
- boolean success = targetFile.getAbsoluteFile().getParentFile().mkdirs();\r
- if (!success) {\r
- throw new RuntimeException("Failed to create destination folder structure!");\r
- }\r
- }\r
- if (downloadListener != null) {\r
- downloadListener.downloading(mo.name + "...");\r
- }\r
- ByteArrayOutputStream buff = new ByteArrayOutputStream();\r
- try {\r
- URL url = new URL(mavenURL);\r
- InputStream in = new BufferedInputStream(url.openStream());\r
- byte[] buffer = new byte[4096];\r
- int downloadedLen = 0;\r
- float lastProgress = 0f;\r
-\r
- updateDownload(0, targetFile);\r
- while (true) {\r
- int len = in.read(buffer);\r
- if (len < 0) {\r
- break;\r
- }\r
- downloadedLen += len;\r
- buff.write(buffer, 0, len);\r
- float progress = downloadedLen / approximateLength;\r
- if (progress - lastProgress >= 0.1f) {\r
- lastProgress = progress;\r
- updateDownload(progress, targetFile);\r
- if (downloadListener != null) {\r
- int percent = Math.min(100, Math.round(100 * progress));\r
- downloadListener.downloading(mo.name + " (" + percent + "%)");\r
- }\r
- }\r
- }\r
- in.close();\r
- updateDownload(1f, targetFile);\r
- if (downloadListener != null) {\r
- downloadListener.downloading(mo.name + " (100%)");\r
- }\r
-\r
- } catch (IOException e) {\r
- throw new RuntimeException("Error downloading " + mavenURL + " to " + targetFile, e);\r
- }\r
- byte[] data = buff.toByteArray();\r
- String calculatedSHA1 = StringUtils.getSHA1(data);\r
-\r
- System.out.println();\r
-\r
- if (expectedSHA1.length() == 0) {\r
- updateProgress(0, "sha: " + calculatedSHA1);\r
- System.out.println();\r
- } else {\r
- if (!calculatedSHA1.equals(expectedSHA1)) {\r
- throw new RuntimeException("SHA1 checksum mismatch; got: " + calculatedSHA1);\r
- }\r
- }\r
- try {\r
- RandomAccessFile ra = new RandomAccessFile(targetFile, "rw");\r
- ra.write(data);\r
- ra.setLength(data.length);\r
- ra.close();\r
- } catch (IOException e) {\r
- throw new RuntimeException("Error writing to file " + targetFile, e);\r
- }\r
- downloads.add(targetFile);\r
- \r
- removeObsoleteArtifacts(mo, type, targetFile.getParentFile());\r
- }\r
- return downloads;\r
- }\r
- \r
- /**\r
- * Download a file from the official Apache Maven repository.\r
- * \r
- * @param mo\r
- * the maven object to download.\r
- * @return\r
- */\r
- private static List<File> downloadFromApacheToExtSelenium(MavenObject mo,\r
- BuildType type) {\r
- return downloadFromMaven("http://repo1.maven.org/maven2/", mo, type,\r
- "ext/seleniumhq");\r
- }\r
- \r
- /**\r
- * Download a file from the official Apache Maven repository.\r
- * \r
- * @param mo\r
- * the maven object to download.\r
- * @return\r
- */\r
- private static List<File> downloadFromMaven(String mavenRoot,\r
- MavenObject mo, BuildType type) {\r
- return downloadFromMaven(mavenRoot, mo, type, "ext");\r
- }\r
- \r
- private static void removeObsoleteArtifacts(final MavenObject mo, final BuildType type, File folder) {\r
- File [] removals = folder.listFiles(new FilenameFilter() {\r
- @Override\r
- public boolean accept(File dir, String name) {\r
- String n = name.toLowerCase();\r
- String dep = mo.artifact.toLowerCase();\r
- if (n.startsWith(dep)) {\r
- String suffix = "-" + mo.version;\r
- if (type.equals(BuildType.COMPILETIME)) {\r
- suffix += "-sources.jar";\r
- } else {\r
- suffix += ".jar";\r
- }\r
- if (!n.endsWith(suffix)) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
- });\r
- \r
- // delete any matches\r
- if (removals != null) {\r
- for (File file : removals) {\r
- System.out.println("deleting " + file);\r
- file.delete();\r
- }\r
- }\r
- }\r
-\r
- private static void updateDownload(float progress, File file) {\r
- updateProgress(progress, "d/l: " + file.getName());\r
- }\r
-\r
- private static void updateProgress(float progress, String url) {\r
- boolean isWindows = osName.contains("Windows");\r
- String anim = "==========";\r
- int width = Math.round(anim.length() * progress);\r
- if (isWindows) System.out.print("\r");\r
- System.out.print("[");\r
- System.out.print(anim.substring(0, Math.min(width, anim.length())));\r
- for (int i = 0; i < anim.length() - width; i++) {\r
- System.out.print(' ');\r
- }\r
- System.out.print("] " + url);\r
- if (!isWindows) System.out.println();\r
- }\r
-\r
- /**\r
- * MavenObject represents a complete maven artifact (binary, sources, and\r
- * javadoc). MavenObjects can be downloaded and checksummed to confirm\r
- * authenticity.\r
- */\r
- private static class MavenObject {\r
-\r
- public static final MavenObject JCOMMANDER = new MavenObject(\r
- "jCommander", "com/beust", "jcommander", "1.17",\r
- 34000, 32000, 141000,\r
- "219a3540f3b27d7cc3b1d91d6ea046cd8723290e",\r
- "0bb50eec177acf0e94d58e0cf07262fe5164331d",\r
- "c7adc475ca40c288c93054e0f4fe58f3a98c0cb5");\r
-\r
- public static final MavenObject JETTY = new MavenObject(\r
- "Jetty", "org/eclipse/jetty/aggregate", "jetty-webapp", "7.6.8.v20121106",\r
- 1000000, 680000, 2720000,\r
- "6333969b4d509c4b681e05302ca7ebccb9c3efb5",\r
- "354f2752ed6544296bc0fc92e533d68a5b03045b",\r
- "");\r
-\r
- public static final MavenObject JETTY_AJP = new MavenObject(\r
- "Jetty-AJP", "org/eclipse/jetty", "jetty-ajp", "7.6.8.v20121106",\r
- 32000, 22000, 97000,\r
- "95bd1c89bb2afd4eeaabc6f4b0183a9f26a522d7",\r
- "e1fc2539202ebb240a87a080bc44a24c93d7318b",\r
- "");\r
- \r
- public static final MavenObject SERVLET = new MavenObject(\r
- "Servlet 3.0", "javax/servlet", "javax.servlet-api", "3.0.1",\r
- 84000, 211000, 0,\r
- "6bf0ebb7efd993e222fc1112377b5e92a13b38dd",\r
- "01952f91d84016a39e31346c9d18bd8c9c4a128a",\r
- null);\r
-\r
- public static final MavenObject SLF4JAPI = new MavenObject(\r
- "SLF4J API", "org/slf4j", "slf4j-api", "1.6.6",\r
- 25500, 45000, 182000,\r
- "ce53b0a0e2cfbb27e8a59d38f79a18a5c6a8d2b0",\r
- "bcd0e21b1572960cefd449f8a16efab5b6b8e644",\r
- "4253b52aabf1c5a5f20c191a261e6ada0fcf621d");\r
-\r
- public static final MavenObject SLF4LOG4J = new MavenObject(\r
- "SLF4J LOG4J", "org/slf4j", "slf4j-log4j12", "1.6.6",\r
- 9800, 9500, 52400,\r
- "5cd9b4fbc3ff6a97beaade3206137d76f65df805",\r
- "497bfac9a678118e7ff75d1f3b8c3bcc06dc9c8c",\r
- "69855e2a85d9249bb577df3c5076bc2cb34975d7");\r
-\r
- public static final MavenObject LOG4J = new MavenObject(\r
- "Apache LOG4J", "log4j", "log4j", "1.2.17",\r
- 481000, 471000, 1455000,\r
- "5af35056b4d257e4b64b9e8069c0746e8b08629f",\r
- "677abe279b68c5e7490d6d50c6951376238d7d3e",\r
- "c10c20168206896442f3192d5417815df7fcbf9a");\r
-\r
- public static final MavenObject WICKET = new MavenObject(\r
- "Apache Wicket", "org/apache/wicket", "wicket", "1.4.21",\r
- 1960000, 1906000, 6818000,\r
- "cce9dfd3088e18a3cdcf9be33b5b76caa48dc4c6",\r
- "e8c2bfe2c96a2da7a0eca947a2f60dc3242e7229",\r
- "");\r
-\r
- public static final MavenObject WICKET_EXT = new MavenObject(\r
- "Apache Wicket Extensions", "org/apache/wicket", "wicket-extensions", "1.4.21",\r
- 1180000, 1118000, 1458000,\r
- "fac510c7ee4399a29b927405ec3de40b67d105d8",\r
- "ee3409ce9ed64ad8cc8d69abbd7d63f07e10851a",\r
- "");\r
-\r
- public static final MavenObject WICKET_AUTH_ROLES = new MavenObject(\r
- "Apache Wicket Auth Roles", "org/apache/wicket", "wicket-auth-roles", "1.4.21",\r
- 44000, 45000, 166000,\r
- "e78df70ca942e2e15287c393f236b32fbe6f9a30",\r
- "47c301212cce43a70caa72f41a9a1aefcf26a533",\r
- "");\r
-\r
- public static final MavenObject WICKET_GOOGLE_CHARTS = new MavenObject(\r
- "Apache Wicket Google Charts Add-On", "org/wicketstuff", "googlecharts", "1.4.21",\r
- 34000, 18750, 161000,\r
- "73d7540267afc3a0e91ca6148d3073e050dba180",\r
- "627b125cc6029d4d5c59c3a910c1bef347384d97",\r
- "");\r
-\r
- public static final MavenObject JUNIT = new MavenObject(\r
- "JUnit", "junit", "junit", "4.10",\r
- 253000, 141000, 0, "e4f1766ce7404a08f45d859fb9c226fc9e41a861",\r
- "6c98d6766e72d5575f96c9479d1c1d3b865c6e25", "");\r
-\r
- public static final MavenObject HAMCREST = new MavenObject(\r
- "Hamcrest Core", "org/hamcrest", "hamcrest-core", "1.1",\r
- 77000, 0, 0,\r
- "860340562250678d1a344907ac75754e259cdb14",\r
- null,\r
- "");\r
-\r
- public static final MavenObject MARKDOWNPAPERS = new MavenObject(\r
- "MarkdownPapers", "org/tautua/markdownpapers", "markdownpapers-core", "1.3.2",\r
- 92000, 60000, 268000,\r
- "da22db6660e90b9a677bbdfc2c511c619ea5c249",\r
- "6a7228280a229144afe6c01351a8f44675d8524d",\r
- "");\r
-\r
- public static final MavenObject BOUNCYCASTLE = new MavenObject(\r
- "BouncyCastle", "org/bouncycastle", "bcprov-jdk15on", "1.47",\r
- 1900000, 1400000, 4670000,\r
- "b6f5d9926b0afbde9f4dbe3db88c5247be7794bb",\r
- "85e6e1ad449d5a3f09624bf4038fc8d2b02de81c",\r
- "");\r
-\r
- public static final MavenObject BOUNCYCASTLE_MAIL = new MavenObject(\r
- "BouncyCastle Mail", "org/bouncycastle", "bcmail-jdk15on", "1.47",\r
- 502000, 420000, 482000,\r
- "a35ccec640177d0de5815568529021af5546d6a7",\r
- "f742330cfe1e7365dbdf773c24b92382172164a7",\r
- "");\r
-\r
- public static final MavenObject BOUNCYCASTLE_PKIX = new MavenObject(\r
- "BouncyCastle PKIX", "org/bouncycastle", "bcpkix-jdk15on", "1.47",\r
- 502000, 420000, 482000,\r
- "cd204e6f26d2bbf65ff3a30de8831d3a1344e851",\r
- "80e774a73d0e6a6b40ddf35fff613f9f30fe2a98",\r
- "");\r
-\r
- public static final MavenObject JGIT = new MavenObject(\r
- "JGit", "org/eclipse/jgit", "org.eclipse.jgit", "2.2.0.201212191850-r",\r
- 1600000, 1565000, 3460000,\r
- "97d0761b9dd618d1f9f6c16c35c3ddf045ba536c",\r
- "08dcf9546f4d61e1b8a50df5da5513006023b64b",\r
- "");\r
-\r
- public static final MavenObject JGIT_HTTP = new MavenObject(\r
- "JGit", "org/eclipse/jgit", "org.eclipse.jgit.http.server", "2.2.0.201212191850-r",\r
- 68000, 62000, 110000,\r
- "8ad4fc4fb9529d645249bb46ad7e54d98436cb65",\r
- "3385cf294957d1d34c1270b468853aea347b36ca",\r
- "");\r
-\r
- public static final MavenObject JSCH = new MavenObject(\r
- "JSch", "com/jcraft", "jsch", "0.1.44-1",\r
- 214000, 211000, 413000,\r
- "2e9ae08de5a71bd0e0d3ba2558598181bfa71d4e",\r
- "e528f593b19b04d500992606f58b87fcfded8883",\r
- "d0ffadd0a4ab909d94a577b5aad43c13b617ddcb");\r
-\r
- public static final MavenObject COMMONSNET = new MavenObject(\r
- "commons-net", "commons-net", "commons-net", "1.4.0",\r
- 181000, 0, 0,\r
- "eb47e8cad2dd7f92fd7e77df1d1529cae87361f7",\r
- "",\r
- "");\r
-\r
- public static final MavenObject ROME = new MavenObject(\r
- "rome", "rome", "rome", "0.9",\r
- 208000, 196000, 407000,\r
- "dee2705dd01e79a5a96a17225f5a1ae30470bb18",\r
- "226f851dc44fd94fe70b9c471881b71f88949cbf",\r
- "8d7d867b97eeb3a9196c3926da550ad042941c1b");\r
-\r
- public static final MavenObject JDOM = new MavenObject(\r
- "jdom", "jdom", "jdom", "1.0",\r
- 153000, 235000, 445000,\r
- "a2ac1cd690ab4c80defe7f9bce14d35934c35cec",\r
- "662abe0196cf554d4d7374f5d6382034171b652c",\r
- "");\r
-\r
- public static final MavenObject GSON = new MavenObject(\r
- "gson", "com/google/code/gson", "gson", "1.7.2",\r
- 174000, 142000, 247000,\r
- "112366d8158749e25532ebce162232c6e0fb20a5",\r
- "a6fe3006df46174a9c1c56b3c51357b9bfde5874",\r
- "537f729ac63b6132a795a3c1f2e13b327e872333");\r
-\r
- public static final MavenObject MAIL = new MavenObject(\r
- "javax.mail", "javax/mail", "mail", "1.4.3",\r
- 462000, 642000, 0,\r
- "8154bf8d666e6db154c548dc31a8d512c273f5ee",\r
- "5875e2729de83a4e46391f8f979ec8bd03810c10", null);\r
-\r
- public static final MavenObject GROOVY = new MavenObject(\r
- "groovy", "org/codehaus/groovy", "groovy-all", "1.8.8",\r
- 6143000, 2290000, 4608000,\r
- "98a489343d3c30da817d36cbea5de11ed07bef31",\r
- "5f847ed18009f8a034bad3906e39f771c01728c1", "");\r
-\r
- public static final MavenObject LUCENE = new MavenObject(\r
- "lucene", "org/apache/lucene", "lucene-core", "3.6.1",\r
- 1540000, 1431000, 3608000,\r
- "6ae2c83c77a1ffa5840b9151a271ab3f451f6e0c",\r
- "6925deb6b78e63bbcac382004f00b98133327057", "");\r
-\r
- public static final MavenObject LUCENE_HIGHLIGHTER = new MavenObject(\r
- "lucene highlighter", "org/apache/lucene", "lucene-highlighter", "3.6.1",\r
- 89200, 85000, 0,\r
- "2bd49695e9891697c5f290aa94c3412dfb95b096",\r
- "20ae81816ce9c27186ef0f2e92a57812c9ee3b6c", "");\r
-\r
- public static final MavenObject LUCENE_MEMORY = new MavenObject(\r
- "lucene memory", "org/apache/lucene", "lucene-memory", "3.6.1",\r
- 30000, 23000, 0,\r
- "8c7ca5572edea50973dc0d26cf75c27047eebe7e",\r
- "2e291e96d25132e002b1c8240e361d1272d113e1", "");\r
-\r
- public static final MavenObject LUCENE_QUERIES = new MavenObject(\r
- "lucene queries", "org/apache/lucene", "lucene-queries", "3.6.1",\r
- 47400, 48600, 0,\r
- "4ed6022dd4aa80b932a1546e7e39e3b8bbe7acb7",\r
- "dc425c75d988e4975d314772035a46b6a17dcc8d", "");\r
-\r
- public static final MavenObject JAKARTA_REGEXP = new MavenObject(\r
- "jakarta regexp", "jakarta-regexp", "jakarta-regexp", "1.4",\r
- 28500, 0, 0,\r
- "0ea514a179ac1dd7e81c7e6594468b9b9910d298",\r
- null, "");\r
- \r
- public static final MavenObject UNBOUND_ID = new MavenObject(\r
- "unbound id", "com/unboundid", "unboundid-ldapsdk", "2.3.0",\r
- 1383417, 1439721, 0,\r
- "6fde8d9fb4ee3e7e3d7e764e3ea57195971e2eb2",\r
- "5276d3d29630693dba99ab9f7ea54f4c471d3af1",\r
- "");\r
- \r
- public static final MavenObject IVY = new MavenObject(\r
- "ivy", "org/apache/ivy", "ivy", "2.2.0",\r
- 948000, 744000, 0,\r
- "f9d1e83e82fc085093510f7d2e77d81d52bc2081",\r
- "0312527950ad0e8fbab37228fbed3bf41a6fe0a1", "");\r
-\r
- public static final MavenObject JCALENDAR = new MavenObject(\r
- "jcalendar", "com/toedter", "jcalendar", "1.3.2",\r
- 127000, 0, 0,\r
- "323a672aeacb5f5f4461be3b7f7d9d3e4bda80d4",\r
- null, "");\r
-\r
- public static final MavenObject COMMONS_COMPRESS = new MavenObject(\r
- "commons-compress", "org/apache/commons", "commons-compress", "1.4.1",\r
- 242000, 265000, 0,\r
- "b02e84a993d88568417536240e970c4b809126fd",\r
- "277d39267403965a7a192474794a29bac6760a25", "");\r
-\r
- public static final MavenObject XZ = new MavenObject(\r
- "xz", "org/tukaani", "xz", "1.0",\r
- 95000, 120000, 0,\r
- "ecff5cb8b1189514c9d1d8d68eb77ac372e000c9",\r
- "f95e32a5d2dd8da643c4419814415b9704312993", "");\r
-\r
- public static final MavenObject SEL_JAVA = new MavenObject(\r
- "selenium-java", "org/seleniumhq/selenium", "selenium-java",\r
- "2.28.0", 984098, 0, 0,\r
- "7606286989ac9cb942cc206d975ffe187c18d605", "4ede08d293dc153989a337cd0d31d26421433af5", "");\r
-\r
- public static final MavenObject SEL_API = new MavenObject(\r
- "selenium-api", "org/seleniumhq/selenium", "selenium-api",\r
- "2.28.0", 984098, 0, 0,\r
- "c4044c40fff65cd25135a5f443638a2b1ccaeac5", "35fc6ec0804ae32b16a56627e69bdcb69995c515", "");\r
-\r
- public static final MavenObject SEL_REMOTE = new MavenObject(\r
- "selenium-remote-driver", "org/seleniumhq/selenium",\r
- "selenium-remote-driver", "2.28.0", 984098, 0, 0,\r
- "c67f97cd94e02afec92b0ac881844febb4fc90be", "51a9c30de3c8c203cb7a474a10842443005a5fb4", "");\r
- public static final MavenObject SEL_SUPPORT = new MavenObject(\r
- "selenium-support", "org/seleniumhq/selenium",\r
- "selenium-support", "2.28.0", 984098, 0, 0,\r
- "caf68d6310425f583bc592c08e43066b35eb94f6", "ce3831a601f5f50fda2f4604decde409b6c735a7", "");\r
- public static final MavenObject SEL_FF = new MavenObject(\r
- "selenium-firefox-driver", "org/seleniumhq/selenium",\r
- "selenium-firefox-driver", "2.28.0", 984098, 0, 0,\r
- "a7c34e45dba39e65467b900aa67611aaa039692d", "aa8cd5fb49ca75a53d5b143406ea3d81ab3eddfd", "");\r
-\r
- public static final MavenObject GUAVA = new MavenObject("guava",\r
- "com/google/guava", "guava", "12.0", 984098, 0, 0,\r
- "5bc66dd95b79db1e437eb08adba124a3e4088dc0", "f8b98e61865bed3c39b978ee3bf5c7fb990c4032", "");\r
-\r
- public static final MavenObject JSON = new MavenObject("json",\r
- "org/json", "json", "20080701", 984098, 0, 0,\r
- "d652f102185530c93b66158b1859f35d45687258", "71bd54221e701df9d112bf9ba2918e13b0671f3a", "");\r
-\r
- public static final MavenObject COMMONS_EXEC = new MavenObject(\r
- "commons-exec", "org/apache/commons", "commons-exec", "1.1",\r
- 984098, 0, 0, "07dfdf16fade726000564386825ed6d911a44ba1", "f60bea898e18b308099862e8634d589b06a8b0be",\r
- "");\r
-\r
- public static final MavenObject HTTPCORE = new MavenObject("httpcore",\r
- "org/apache/httpcomponents", "httpcore", "4.2.1", 984098, 0, 0,\r
- "2d503272bf0a8b5f92d64db78b4ba9abbaccc6fd", "3f6caf5334fa83607b82e2f32dd128a9d8a0ea5e", "");\r
- \r
- public static final MavenObject HTTPMIME = new MavenObject("httpmime",\r
- "org/apache/httpcomponents", "httpmime", "4.2.1", 984098, 0, 0,\r
- "7c772bace9aa31a728c39a88c6ff66a7cd177e89", "", "4e453843ae47f1c2d70e2eb2c13c037de4b614c4");\r
- \r
- public static final MavenObject HTTPCLIENT = new MavenObject(\r
- "httpclient", "org/apache/httpcomponents", "httpclient",\r
- "4.2.1", 984098, 0, 0,\r
- "b69bd03af60bf487b3ae1209a644ecac587bf6fc", "6b27312b9c28b59aaeb6c21f3490045690c703d3", "");\r
- public static final MavenObject COMMONS_LOGGING = new MavenObject(\r
- "commons-logging", "commons-logging", "commons-logging",\r
- "1.1.1", 984098, 0, 0,\r
- "5043bfebc3db072ed80fbd362e7caf00e885d8ae", "f3f156cbff0e0fb0d64bfce31a352cce4a33bc19", "");\r
- \r
- public final String name;\r
- public final String group;\r
- public final String artifact;\r
- public final String version;\r
- public final int approxLibraryLen;\r
- public final int approxSourcesLen;\r
- public final int approxJavadocLen;\r
- public final String librarySHA1;\r
- public final String sourcesSHA1;\r
- public final String javadocSHA1;\r
-\r
- private MavenObject(String name, String group, String artifact, String version,\r
- int approxLibraryLen, int approxSourcesLen, int approxJavadocLen,\r
- String librarySHA1, String sourcesSHA1, String javadocSHA1) {\r
- this.name = name;\r
- this.group = group;\r
- this.artifact = artifact;\r
- this.version = version;\r
- this.approxLibraryLen = approxLibraryLen;\r
- this.approxSourcesLen = approxSourcesLen;\r
- this.approxJavadocLen = approxJavadocLen;\r
- this.librarySHA1 = librarySHA1;\r
- this.sourcesSHA1 = sourcesSHA1;\r
- this.javadocSHA1 = javadocSHA1;\r
- }\r
-\r
- private String getRepositoryPath(String jar) {\r
- return group + "/" + artifact + "/" + version + "/" + artifact + "-" + version + jar\r
- + ".jar";\r
- }\r
-\r
- private File getLocalFile(String basePath, String jar) {\r
- return new File(basePath, artifact + "-" + version + jar + ".jar");\r
- }\r
-\r
- private String getSHA1(String jar) {\r
- if (jar.equals("")) {\r
- return librarySHA1;\r
- } else if (jar.equals("-sources")) {\r
- return sourcesSHA1;\r
- } else if (jar.equals("-javadoc")) {\r
- return javadocSHA1;\r
- }\r
- return librarySHA1;\r
- }\r
-\r
- private int getApproximateLength(String jar) {\r
- if (jar.equals("")) {\r
- return approxLibraryLen;\r
- } else if (jar.equals("-sources")) {\r
- return approxSourcesLen;\r
- } else if (jar.equals("-javadoc")) {\r
- return approxJavadocLen;\r
- }\r
- return approxLibraryLen;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return name;\r
- }\r
- }\r
-}\r
+++ /dev/null
-/*\r
- * Copyright 2011 gitblit.com.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package com.gitblit.build;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileOutputStream;\r
-import java.io.FileReader;\r
-import java.io.FilenameFilter;\r
-import java.io.OutputStreamWriter;\r
-import java.nio.charset.Charset;\r
-import java.text.MessageFormat;\r
-import java.text.ParseException;\r
-import java.text.SimpleDateFormat;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Date;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-\r
-import com.beust.jcommander.JCommander;\r
-import com.beust.jcommander.Parameter;\r
-import com.beust.jcommander.ParameterException;\r
-import com.beust.jcommander.Parameters;\r
-import com.gitblit.Constants;\r
-import com.gitblit.utils.FileUtils;\r
-import com.gitblit.utils.MarkdownUtils;\r
-import com.gitblit.utils.StringUtils;\r
-\r
-/**\r
- * Builds the web site or deployment documentation from Markdown source files.\r
- * \r
- * All Markdown source files must have the .mkd extension.\r
- * \r
- * Natural string sort order of the Markdown source filenames is the order of\r
- * page links. "##_" prefixes are used to control the sort order.\r
- * \r
- * @author James Moger\r
- * \r
- */\r
-public class BuildSite {\r
-\r
- private static final String SPACE_DELIMITED = "SPACE-DELIMITED";\r
-\r
- private static final String CASE_SENSITIVE = "CASE-SENSITIVE";\r
-\r
- private static final String RESTART_REQUIRED = "RESTART REQUIRED";\r
-\r
- private static final String SINCE = "SINCE";\r
-\r
- public static void main(String... args) {\r
- Params params = new Params();\r
- JCommander jc = new JCommander(params);\r
- try {\r
- jc.parse(args);\r
- } catch (ParameterException t) {\r
- usage(jc, t);\r
- }\r
-\r
- File sourceFolder = new File(params.sourceFolder);\r
- File destinationFolder = new File(params.outputFolder);\r
- File[] markdownFiles = sourceFolder.listFiles(new FilenameFilter() {\r
-\r
- @Override\r
- public boolean accept(File dir, String name) {\r
- return name.toLowerCase().endsWith(".mkd");\r
- }\r
- });\r
- Arrays.sort(markdownFiles);\r
-\r
- Map<String, String> aliasMap = new HashMap<String, String>();\r
- for (String alias : params.aliases) {\r
- String[] values = alias.split("=");\r
- aliasMap.put(values[0], values[1]);\r
- }\r
-\r
- System.out.println(MessageFormat.format("Generating site from {0} Markdown Docs in {1} ",\r
- markdownFiles.length, sourceFolder.getAbsolutePath()));\r
-\r
- String htmlHeader = FileUtils.readContent(new File(params.pageHeader), "\n");\r
-\r
- String htmlAdSnippet = null;\r
- if (!StringUtils.isEmpty(params.adSnippet)) {\r
- File snippet = new File(params.adSnippet);\r
- if (snippet.exists()) {\r
- htmlAdSnippet = FileUtils.readContent(snippet, "\n");\r
- }\r
- }\r
- String htmlFooter = FileUtils.readContent(new File(params.pageFooter), "\n");\r
- final String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());\r
- final String footer = MessageFormat.format(htmlFooter, "generated " + date);\r
- for (File file : markdownFiles) {\r
- String documentName = getDocumentName(file);\r
- if (params.skips.contains(documentName)) {\r
- continue;\r
- }\r
- try {\r
- String links = createLinks(file, markdownFiles, aliasMap, params.skips);\r
- String header = MessageFormat.format(htmlHeader, Constants.FULL_NAME, links);\r
- if (!StringUtils.isEmpty(params.analyticsSnippet)) {\r
- File snippet = new File(params.analyticsSnippet);\r
- if (snippet.exists()) {\r
- String htmlSnippet = FileUtils.readContent(snippet, "\n");\r
- header = header.replace("<!-- ANALYTICS -->", htmlSnippet);\r
- }\r
- }\r
-\r
- String fileName = documentName + ".html";\r
- System.out.println(MessageFormat.format(" {0} => {1}", file.getName(), fileName));\r
- String rawContent = FileUtils.readContent(file, "\n");\r
- String markdownContent = rawContent;\r
-\r
- Map<String, List<String>> nomarkdownMap = new HashMap<String, List<String>>();\r
-\r
- // extract sections marked as no-markdown\r
- int nmd = 0;\r
- for (String token : params.nomarkdown) {\r
- StringBuilder strippedContent = new StringBuilder();\r
-\r
- String nomarkdownKey = "%NOMARKDOWN" + nmd + "%";\r
- String[] kv = token.split(":", 2);\r
- String beginToken = kv[0];\r
- String endToken = kv[1];\r
-\r
- // strip nomarkdown chunks from markdown and cache them\r
- List<String> chunks = new Vector<String>();\r
- int beginCode = 0;\r
- int endCode = 0;\r
- while ((beginCode = markdownContent.indexOf(beginToken, endCode)) > -1) {\r
- if (endCode == 0) {\r
- strippedContent.append(markdownContent.substring(0, beginCode));\r
- } else {\r
- strippedContent.append(markdownContent.substring(endCode, beginCode));\r
- }\r
- strippedContent.append(nomarkdownKey);\r
- endCode = markdownContent.indexOf(endToken, beginCode);\r
- chunks.add(markdownContent.substring(beginCode, endCode));\r
- nomarkdownMap.put(nomarkdownKey, chunks);\r
- }\r
-\r
- // get remainder of text\r
- if (endCode < markdownContent.length()) {\r
- strippedContent.append(markdownContent.substring(endCode,\r
- markdownContent.length()));\r
- }\r
- markdownContent = strippedContent.toString();\r
- nmd++;\r
- }\r
-\r
- // transform markdown to html\r
- String content = transformMarkdown(markdownContent.toString());\r
-\r
- // reinsert nomarkdown chunks\r
- for (Map.Entry<String, List<String>> nomarkdown : nomarkdownMap.entrySet()) {\r
- for (String chunk : nomarkdown.getValue()) {\r
- content = content.replaceFirst(nomarkdown.getKey(), chunk);\r
- }\r
- }\r
-\r
- for (String token : params.substitutions) {\r
- String[] kv = token.split("=", 2);\r
- content = content.replace(kv[0], kv[1]);\r
- }\r
- for (String token : params.regex) {\r
- String[] kv = token.split("!!!", 2);\r
- content = content.replaceAll(kv[0], kv[1]);\r
- }\r
- for (String alias : params.properties) {\r
- String[] kv = alias.split("=", 2);\r
- String loadedContent = generatePropertiesContent(new File(kv[1]));\r
- content = content.replace(kv[0], loadedContent);\r
- }\r
- for (String alias : params.loads) {\r
- String[] kv = alias.split("=", 2);\r
- String loadedContent = FileUtils.readContent(new File(kv[1]), "\n");\r
- loadedContent = StringUtils.escapeForHtml(loadedContent, false);\r
- loadedContent = StringUtils.breakLinesForHtml(loadedContent);\r
- content = content.replace(kv[0], loadedContent);\r
- }\r
- OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(\r
- destinationFolder, fileName)), Charset.forName("UTF-8"));\r
- writer.write(header);\r
- if (!StringUtils.isEmpty(htmlAdSnippet)) {\r
- writer.write(htmlAdSnippet);\r
- }\r
- writer.write(content);\r
- writer.write(footer);\r
- writer.close();\r
- } catch (Throwable t) {\r
- System.err.println("Failed to transform " + file.getName());\r
- t.printStackTrace();\r
- }\r
- }\r
- }\r
-\r
- private static String getDocumentName(File file) {\r
- String displayName = file.getName().substring(0, file.getName().lastIndexOf('.'))\r
- .toLowerCase();\r
- int underscore = displayName.indexOf('_') + 1;\r
- if (underscore > -1) {\r
- // trim leading ##_ which is to control display order\r
- return displayName.substring(underscore);\r
- }\r
- return displayName;\r
- }\r
-\r
- private static String createLinks(File currentFile, File[] markdownFiles,\r
- Map<String, String> aliasMap, List<String> skips) {\r
- String linkPattern = "<li><a href=''{0}''>{1}</a></li>";\r
- String currentLinkPattern = "<li class=''active''><a href=''{0}''>{1}</a></li>";\r
- StringBuilder sb = new StringBuilder();\r
- for (File file : markdownFiles) {\r
- String documentName = getDocumentName(file);\r
- if (!skips.contains(documentName)) {\r
- String displayName = documentName;\r
- if (aliasMap.containsKey(documentName)) {\r
- displayName = aliasMap.get(documentName);\r
- } else {\r
- displayName = displayName.replace('_', ' ');\r
- }\r
- String fileName = documentName + ".html";\r
- if (currentFile.getName().equals(file.getName())) {\r
- sb.append(MessageFormat.format(currentLinkPattern, fileName, displayName));\r
- } else {\r
- sb.append(MessageFormat.format(linkPattern, fileName, displayName));\r
- }\r
- }\r
- }\r
- sb.setLength(sb.length() - 3);\r
- sb.trimToSize();\r
- return sb.toString();\r
- }\r
-\r
- private static String generatePropertiesContent(File propertiesFile) throws Exception {\r
- // Read the current Gitblit properties\r
- BufferedReader propertiesReader = new BufferedReader(new FileReader(propertiesFile));\r
-\r
- Vector<Setting> settings = new Vector<Setting>();\r
- List<String> comments = new ArrayList<String>();\r
- String line = null;\r
- while ((line = propertiesReader.readLine()) != null) {\r
- if (line.length() == 0) {\r
- Setting s = new Setting("", "", comments);\r
- settings.add(s);\r
- comments.clear();\r
- } else {\r
- if (line.charAt(0) == '#') {\r
- comments.add(line.substring(1).trim());\r
- } else {\r
- String[] kvp = line.split("=", 2);\r
- String key = kvp[0].trim();\r
- Setting s = new Setting(key, kvp[1].trim(), comments);\r
- settings.add(s);\r
- comments.clear();\r
- }\r
- }\r
- }\r
- propertiesReader.close();\r
-\r
- StringBuilder sb = new StringBuilder();\r
- for (Setting setting : settings) {\r
- for (String comment : setting.comments) {\r
- if (comment.contains(SINCE) || comment.contains(RESTART_REQUIRED)\r
- || comment.contains(CASE_SENSITIVE) || comment.contains(SPACE_DELIMITED)) {\r
- sb.append(MessageFormat.format(\r
- "<span style=\"color:#004000;\"># <i>{0}</i></span>",\r
- transformMarkdown(comment)));\r
- } else {\r
- sb.append(MessageFormat.format("<span style=\"color:#004000;\"># {0}</span>",\r
- transformMarkdown(comment)));\r
- }\r
- sb.append("<br/>\n");\r
- }\r
- if (!StringUtils.isEmpty(setting.name)) {\r
- sb.append(MessageFormat\r
- .format("<span style=\"color:#000080;\">{0}</span> = <span style=\"color:#800000;\">{1}</span>",\r
- setting.name, StringUtils.escapeForHtml(setting.value, false)));\r
- }\r
- sb.append("<br/>\n");\r
- }\r
-\r
- return sb.toString();\r
- }\r
-\r
- private static String transformMarkdown(String comment) throws ParseException {\r
- String md = MarkdownUtils.transformMarkdown(comment);\r
- if (md.startsWith("<p>")) {\r
- md = md.substring(3);\r
- }\r
- if (md.endsWith("</p>")) {\r
- md = md.substring(0, md.length() - 4);\r
- }\r
- return md;\r
- }\r
-\r
- private static void usage(JCommander jc, ParameterException t) {\r
- System.out.println(Constants.getGitBlitVersion());\r
- System.out.println();\r
- if (t != null) {\r
- System.out.println(t.getMessage());\r
- System.out.println();\r
- }\r
- if (jc != null) {\r
- jc.usage();\r
- }\r
- System.exit(0);\r
- }\r
-\r
- /**\r
- * Setting represents a setting with its comments from the properties file.\r
- */\r
- private static class Setting {\r
- final String name;\r
- final String value;\r
- final List<String> comments;\r
-\r
- Setting(String name, String value, List<String> comments) {\r
- this.name = name;\r
- this.value = value;\r
- this.comments = new ArrayList<String>(comments);\r
- }\r
- }\r
-\r
- /**\r
- * JCommander Parameters class for BuildSite.\r
- */\r
- @Parameters(separators = " ")\r
- private static class Params {\r
-\r
- @Parameter(names = { "--sourceFolder" }, description = "Markdown Source Folder", required = true)\r
- public String sourceFolder;\r
-\r
- @Parameter(names = { "--outputFolder" }, description = "HTML Ouptut Folder", required = true)\r
- public String outputFolder;\r
-\r
- @Parameter(names = { "--pageHeader" }, description = "Page Header HTML Snippet", required = true)\r
- public String pageHeader;\r
-\r
- @Parameter(names = { "--pageFooter" }, description = "Page Footer HTML Snippet", required = true)\r
- public String pageFooter;\r
-\r
- @Parameter(names = { "--adSnippet" }, description = "Ad HTML Snippet", required = false)\r
- public String adSnippet;\r
-\r
- @Parameter(names = { "--analyticsSnippet" }, description = "Analytics HTML Snippet", required = false)\r
- public String analyticsSnippet;\r
-\r
- @Parameter(names = { "--skip" }, description = "Filename to skip", required = false)\r
- public List<String> skips = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--alias" }, description = "Filename=Linkname aliases", required = false)\r
- public List<String> aliases = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--substitute" }, description = "%TOKEN%=value", required = false)\r
- public List<String> substitutions = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--load" }, description = "%TOKEN%=filename", required = false)\r
- public List<String> loads = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--properties" }, description = "%TOKEN%=filename", required = false)\r
- public List<String> properties = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--nomarkdown" }, description = "%STARTTOKEN%:%ENDTOKEN%", required = false)\r
- public List<String> nomarkdown = new ArrayList<String>();\r
-\r
- @Parameter(names = { "--regex" }, description = "searchPattern!!!replacePattern", required = false)\r
- public List<String> regex = new ArrayList<String>();\r
-\r
- }\r
-}\r