@Override
  protected boolean handleDirectory(File directory, int depth, Collection<VcmFileData> results)
      throws IOException {
    try {
      if (directory.getCanonicalPath().equals(outputRootFile.getCanonicalPath())) {
        log.debug(String.format("Ignoring %s", directory.getCanonicalPath()));
        return false;
      }

      log.info(String.format("Indexing %s", directory.getCanonicalPath()));
      String relativeParentPaths =
          StringUtils.remove(
              directory.getCanonicalPath(), new File(componentRootPath).getCanonicalPath());
      String outputPath = outputRootPath + relativeParentPaths;
      File outputDirectory = new File(outputPath);
      if (!outputDirectory.exists()) {
        if (!outputDirectory.mkdir()) {
          throw new CommandExecutionException(
              2, String.format("Could not create the %s directory", outputDirectory));
        }
      }

      VelocityContext velocityContext = new VelocityContext(velocityToolManager.createContext());
      velocityContext.put("baseURL", baseUrl);
      velocityContext.put(
          "folders",
          directory.listFiles(
              (FileFilter)
                  FileFilterUtils.and(
                      FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE)));
      velocityContext.put("parentFolders", getParentFolders(relativeParentPaths));
      List<VcmFileData> files = getFiles(directory, outputPath);
      velocityContext.put("files", files);
      if (depth == 0) {
        velocityContext.put("rootPath", ".");
      } else {
        velocityContext.put("rootPath", StringUtils.repeat("../", depth));
      }
      velocityContext.put("depth", depth);

      OutputStreamWriter writer = null;
      try {
        writer =
            new OutputStreamWriter(
                new FileOutputStream(new File(outputDirectory, "index.htm")), "UTF-8");
        velocityTemplate.merge(velocityContext, writer);
      } finally {
        IOUtils.closeQuietly(writer);
      }

      results.addAll(files);
      return true;
    } catch (ZipException e) {
      throw new CommandExecutionException(3, e);
    }
  }
  /**
   * Suffix <br>
   * Created on: Nov 28, 2012 1:50:50 PM
   *
   * @param filePath
   * @param nameFix
   * @param newerTime
   * @param isOlder true : all file ,false : newer file
   * @return
   */
  public static Collection<File> getSuffixAndNewerFiles(
      String filePath, String nameFix, long newerTime, boolean isOlder) {

    IOFileFilter wildcardFileFilter = new SuffixFileFilter(nameFix);
    IOFileFilter ageFileFilter = new AgeFileFilter(newerTime, isOlder);
    IOFileFilter andFilter = FileFilterUtils.and(wildcardFileFilter, ageFileFilter);

    NameFileComparator comp = new NameFileComparator();
    File[] fileArr =
        comp.sort(
            FileUtils.convertFileCollectionToFileArray(
                FileUtils.listFiles((new File(filePath)), andFilter, TrueFileFilter.INSTANCE)));
    comp = null;

    return new LinkedList<File>(Arrays.asList(fileArr));
  }
Beispiel #3
0
    @Override
    protected Object[] doInBackground() throws Exception {
      IOFileFilter pdfFilter = FileFilterUtils.asFileFilter(this);
      IOFileFilter suffixFilter = FileFilterUtils.notFileFilter(new SuffixFileFilter(".fo"));
      IOFileFilter sheetFilter =
          FileFilterUtils.prefixFileFilter(Constants.CHARACTER_TEMPLATE_PREFIX);
      IOFileFilter fileFilter = FileFilterUtils.and(pdfFilter, suffixFilter, sheetFilter);

      IOFileFilter dirFilter = FileFilterUtils.makeSVNAware(TrueFileFilter.INSTANCE);
      File dir = new File(ConfigurationSettings.getOutputSheetsDir());
      Collection<File> files = FileUtils.listFiles(dir, fileFilter, dirFilter);
      URI osPath = new File(ConfigurationSettings.getOutputSheetsDir()).toURI();
      Object[] uriList = new Object[files.size()];
      int i = 0;
      for (File file : files) {
        uriList[i] = osPath.relativize(file.toURI());
        i++;
      }
      return uriList;
    }
  private void copyLocalNativeLibraries(
      final File localNativeLibrariesDirectory, final File destinationDirectory)
      throws MojoExecutionException {
    getLog().debug("Copying existing native libraries from " + localNativeLibrariesDirectory);
    try {

      IOFileFilter libSuffixFilter = FileFilterUtils.suffixFileFilter(".so");

      IOFileFilter gdbserverNameFilter = FileFilterUtils.nameFileFilter("gdbserver");
      IOFileFilter orFilter = FileFilterUtils.or(libSuffixFilter, gdbserverNameFilter);

      IOFileFilter libFiles = FileFilterUtils.and(FileFileFilter.FILE, orFilter);
      FileFilter filter = FileFilterUtils.or(DirectoryFileFilter.DIRECTORY, libFiles);
      org.apache.commons.io.FileUtils.copyDirectory(
          localNativeLibrariesDirectory, destinationDirectory, filter);

    } catch (IOException e) {
      getLog().error("Could not copy native libraries: " + e.getMessage(), e);
      throw new MojoExecutionException("Could not copy native dependency.", e);
    }
  }
  @Override
  public boolean publish(ProblemQuery problems) throws IOException {
    boolean ok;
    boolean subsetGoog = true;

    final String intermediateDirPath = outputFolder.getPath();
    final File intermediateDir = new File(intermediateDirPath);
    File srcDir = new File(configuration.getTargetFile());
    srcDir = srcDir.getParentFile();

    final String projectName = FilenameUtils.getBaseName(configuration.getTargetFile());
    final String outputFileName = projectName + "." + JSSharedData.OUTPUT_EXTENSION;

    File releaseDir = new File(outputParentFolder, FLEXJS_RELEASE_DIR_NAME);
    final String releaseDirPath = releaseDir.getPath();

    if (!isMarmotinniRun) {
      if (releaseDir.exists()) org.apache.commons.io.FileUtils.deleteQuietly(releaseDir);

      releaseDir.mkdirs();
    }

    // If the closure-lib parameter is empty we'll try to find the resources
    // in the classpath, dump its content to the output directory and use this
    // as closure-lib parameter.
    final String closureLibDirPath;
    if (((JSGoogConfiguration) configuration).isClosureLibSet()) {
      closureLibDirPath = ((JSGoogConfiguration) configuration).getClosureLib();
    } else {
      // Check if the "goog/deps.js" is available in the classpath.
      URL resource = Thread.currentThread().getContextClassLoader().getResource("goog/deps.js");
      if (resource != null) {
        File closureLibDir = new File(intermediateDir.getParent(), "closure");

        // Only create and dump the content, if the directory does not exists.
        if (!closureLibDir.exists()) {
          if (!closureLibDir.mkdirs()) {
            throw new IOException(
                "Unable to create directory for closure-lib at " + closureLibDir.getAbsolutePath());
          }

          // Strip the url of the parts we don't need.
          // Unless we are not using some insanely complex setup
          // the resource will always be on the same machine.
          String resourceJarPath = resource.getFile();
          if (resourceJarPath.contains(":")) {
            resourceJarPath = resourceJarPath.substring(resourceJarPath.lastIndexOf(":") + 1);
          }
          if (resourceJarPath.contains("!")) {
            resourceJarPath = resourceJarPath.substring(0, resourceJarPath.indexOf("!"));
          }
          File resourceJar = new File(resourceJarPath);

          // Dump the closure lib from classpath.
          dumpJar(resourceJar, closureLibDir);
        }
        // The compiler automatically adds a "closure" to the lib dir path,
        // so we omit this here.
        closureLibDirPath = intermediateDir.getParentFile().getPath();
      }
      // Fallback to the default.
      else {
        closureLibDirPath = ((JSGoogConfiguration) configuration).getClosureLib();
      }
    }

    final String closureGoogSrcLibDirPath = closureLibDirPath + "/closure/goog/";
    final String closureGoogTgtLibDirPath = intermediateDirPath + "/library/closure/goog";
    final String depsSrcFilePath = intermediateDirPath + "/library/closure/goog/deps.js";
    final String depsTgtFilePath = intermediateDirPath + "/deps.js";
    final String projectIntermediateJSFilePath =
        intermediateDirPath + File.separator + outputFileName;
    final String projectReleaseJSFilePath = releaseDirPath + File.separator + outputFileName;

    appendExportSymbol(projectIntermediateJSFilePath, projectName);
    appendEncodedCSS(projectIntermediateJSFilePath, projectName);

    if (!subsetGoog) {
      // (erikdebruin) We need to leave the 'goog' files and dependencies well
      //               enough alone. We copy the entire library over so the
      //               'goog' dependencies will resolve without our help.
      FileUtils.copyDirectory(
          new File(closureGoogSrcLibDirPath), new File(closureGoogTgtLibDirPath));
    }

    VF2JSClosureCompilerWrapper compilerWrapper = new VF2JSClosureCompilerWrapper();

    VF2JSDepsWriter gdw =
        new VF2JSDepsWriter(intermediateDir, projectName, (JSGoogConfiguration) configuration);
    try {
      StringBuilder depsFileData = new StringBuilder();
      ok = gdw.generateDeps(problems, depsFileData);
      if (!subsetGoog) {
        writeFile(depsTgtFilePath, depsFileData.toString(), false);
      } else {
        String s = depsFileData.toString();
        int c = s.indexOf("'goog.");
        ArrayList<String> googreqs = new ArrayList<String>();
        while (c != -1) {
          int c2 = s.indexOf("'", c + 1);
          String googreq = s.substring(c, c2 + 1);
          googreqs.add(googreq);
          c = s.indexOf("'goog.", c2);
        }
        HashMap<String, DependencyRecord> defmap = new HashMap<String, DependencyRecord>();
        // read in goog's deps.js
        FileInputStream fis = new FileInputStream(closureGoogSrcLibDirPath + "/deps.js");
        Scanner scanner = new Scanner(fis, "UTF-8");
        String addDependency = "goog.addDependency('";
        int currentLine = 0;
        while (scanner.hasNextLine()) {
          String googline = scanner.nextLine();
          if (googline.indexOf(addDependency) == 0) {
            int c1 = googline.indexOf("'", addDependency.length() + 1);
            String googpath = googline.substring(addDependency.length(), c1);
            String googdefs = googline.substring(googline.indexOf("[") + 1, googline.indexOf("]"));
            String googdeps =
                googline.substring(googline.lastIndexOf("[") + 1, googline.lastIndexOf("]"));
            String[] thedefs = googdefs.split(",");
            DependencyRecord deprec = new DependencyRecord();
            deprec.path = googpath;
            deprec.deps = googdeps;
            deprec.line = googline;
            deprec.lineNumber = currentLine;
            for (String def : thedefs) {
              def = def.trim();
              defmap.put(def, deprec);
            }
          }
          currentLine++;
        }
        // (erikdebruin) Prevent 'Resource leak' warning on line 212:
        scanner.close();
        ArrayList<DependencyRecord> subsetdeps = new ArrayList<DependencyRecord>();
        HashMap<String, String> gotgoog = new HashMap<String, String>();
        for (String req : googreqs) {
          DependencyRecord deprec = defmap.get(req);
          // if we've already processed this file, skip
          if (!gotgoog.containsKey(deprec.path)) {
            gotgoog.put(deprec.path, null);
            subsetdeps.add(deprec);
            addDeps(subsetdeps, gotgoog, defmap, deprec.deps);
          }
        }
        // now we should have the subset of files we need in the order needed
        StringBuilder sb = new StringBuilder();
        sb.append("goog.addDependency('base.js', ['goog'], []);\n");
        File file = new File(closureGoogSrcLibDirPath + "/base.js");
        FileUtils.copyFileToDirectory(file, new File(closureGoogTgtLibDirPath));
        compilerWrapper.addJSSourceFile(file.getCanonicalPath());
        Collections.sort(subsetdeps, new DependencyLineComparator());
        for (DependencyRecord subsetdeprec : subsetdeps) {
          sb.append(subsetdeprec.line).append("\n");
        }
        writeFile(depsTgtFilePath, sb.toString() + depsFileData.toString(), false);
        // copy the required files
        for (String googfn : gotgoog.keySet()) {
          file = new File(closureGoogSrcLibDirPath + File.separator + googfn);
          String dir = closureGoogTgtLibDirPath;
          if (googfn.contains("/")) {
            dir += File.separator + googfn.substring(0, googfn.lastIndexOf("/"));
          }
          FileUtils.copyFileToDirectory(file, new File(dir));
          compilerWrapper.addJSSourceFile(file.getCanonicalPath());
        }
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
      return false;
    }

    IOFileFilter pngSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".png"));
    IOFileFilter gifSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".gif"));
    IOFileFilter jpgSuffixFilter =
        FileFilterUtils.and(FileFileFilter.FILE, FileFilterUtils.suffixFileFilter(".jpg"));
    IOFileFilter assetFiles = FileFilterUtils.or(pngSuffixFilter, jpgSuffixFilter, gifSuffixFilter);

    FileUtils.copyDirectory(srcDir, intermediateDir, assetFiles);
    FileUtils.copyDirectory(srcDir, releaseDir, assetFiles);

    File srcDeps = new File(depsSrcFilePath);

    // ToDo (erikdebruin): yeah, right, hard coded the path, nice!
    File sdkDepsFile =
        new File("/Users/erik/Documents/ApacheFlex/git/flex-asjs/vf2js/frameworks/js/sdk-deps.js");
    if (sdkDepsFile.exists())
      FileUtils.copyFile(
          sdkDepsFile, new File(intermediateDirPath + File.separator + "sdk-deps.js"));

    writeHTML("intermediate", projectName, intermediateDirPath, gdw.additionalHTML);
    writeHTML("release", projectName, releaseDirPath, gdw.additionalHTML);
    writeCSS(projectName, intermediateDirPath);
    writeCSS(projectName, releaseDirPath);

    if (!subsetGoog) {
      // (erikdebruin) add 'goog' files
      Collection<File> files =
          org.apache.commons.io.FileUtils.listFiles(
              new File(closureGoogTgtLibDirPath),
              new RegexFileFilter("^.*(\\.js)"),
              DirectoryFileFilter.DIRECTORY);
      for (File file : files) {
        compilerWrapper.addJSSourceFile(file.getCanonicalPath());
      }
    }

    // (erikdebruin) add project files
    for (String filePath : gdw.filePathsInOrder) {
      compilerWrapper.addJSSourceFile(new File(filePath).getCanonicalPath());
    }

    compilerWrapper.setOptions(projectReleaseJSFilePath, useStrictPublishing);

    // (erikdebruin) Include the 'goog' deps to allow the compiler to resolve
    //               dependencies.
    compilerWrapper.addJSSourceFile(closureGoogSrcLibDirPath + File.separator + "deps.js");

    List<String> externs = ((JSGoogConfiguration) configuration).getExternalJSLib();
    for (String extern : externs) {
      compilerWrapper.addJSExternsFile(extern);
    }

    compilerWrapper.targetFilePath = projectReleaseJSFilePath;
    compilerWrapper.compile();

    appendSourceMapLocation(projectReleaseJSFilePath, projectName);

    if (!isMarmotinniRun) {
      String allDeps = "";
      if (!subsetGoog) allDeps += FileUtils.readFileToString(srcDeps);
      allDeps += FileUtils.readFileToString(new File(depsTgtFilePath));

      FileUtils.writeStringToFile(srcDeps, allDeps);

      org.apache.commons.io.FileUtils.deleteQuietly(new File(depsTgtFilePath));
    }

    if (ok)
      System.out.println(
          "The project '" + projectName + "' has been successfully compiled and optimized.");

    return true;
  }
 public GenerateWebCatalogCommand() {
   super(FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE), -1);
 }