Exemplo n.º 1
0
  /**
   * Construct the command line for parallel execution.
   *
   * @param srcFiles The filenames to add to the commandline
   * @param baseDir filenames are relative to this dir
   */
  protected String[] getCommandline(String[] srcFiles, File baseDir) {
    if (targetFilePos == null) {
      return super.getCommandline(srcFiles, baseDir);
    }

    Vector targets = new Vector();
    Hashtable addedFiles = new Hashtable();
    for (int i = 0; i < srcFiles.length; i++) {
      String[] subTargets = mapper.mapFileName(srcFiles[i]);
      if (subTargets != null) {
        for (int j = 0; j < subTargets.length; j++) {
          String name = (new File(destDir, subTargets[j])).getAbsolutePath();
          if (!addedFiles.contains(name)) {
            targets.addElement(name);
            addedFiles.put(name, name);
          }
        }
      }
    }
    String[] targetFiles = new String[targets.size()];
    targets.copyInto(targetFiles);

    String[] orig = cmdl.getCommandline();
    String[] result = new String[orig.length + srcFiles.length + targetFiles.length];

    int srcIndex = orig.length;
    if (srcFilePos != null) {
      srcIndex = srcFilePos.getPosition();
    }
    int targetIndex = targetFilePos.getPosition();

    if (srcIndex < targetIndex || (srcIndex == targetIndex && srcIsFirst)) {
      // 0 --> srcIndex
      System.arraycopy(orig, 0, result, 0, srcIndex);

      // srcIndex --> targetIndex
      System.arraycopy(orig, srcIndex, result, srcIndex + srcFiles.length, targetIndex - srcIndex);

      // targets are already absolute file names
      System.arraycopy(targetFiles, 0, result, targetIndex + srcFiles.length, targetFiles.length);

      // targetIndex --> end
      System.arraycopy(
          orig,
          targetIndex,
          result,
          targetIndex + srcFiles.length + targetFiles.length,
          orig.length - targetIndex);
    } else {
      // 0 --> targetIndex
      System.arraycopy(orig, 0, result, 0, targetIndex);

      // targets are already absolute file names
      System.arraycopy(targetFiles, 0, result, targetIndex, targetFiles.length);

      // targetIndex --> srcIndex
      System.arraycopy(
          orig, targetIndex, result, targetIndex + targetFiles.length, srcIndex - targetIndex);

      // srcIndex --> end
      System.arraycopy(
          orig,
          srcIndex,
          result,
          srcIndex + srcFiles.length + targetFiles.length,
          orig.length - srcIndex);
      srcIndex += targetFiles.length;
    }

    for (int i = 0; i < srcFiles.length; i++) {
      result[srcIndex + i] = (new File(baseDir, srcFiles[i])).getAbsolutePath();
    }
    return result;
  }
Exemplo n.º 2
0
  /**
   * Tells which sources should be reprocessed based on the last modification date of targets.
   *
   * @param logTo where to send (more or less) interesting output.
   * @param source ResourceCollection.
   * @param mapper filename mapper indicating how to find the target Resources.
   * @param targets object able to map a relative path as a Resource.
   * @param granularity The number of milliseconds leeway to give before deciding a target is out of
   *     date.
   * @return ResourceCollection.
   * @since Ant 1.7
   */
  public static ResourceCollection selectOutOfDateSources(
      ProjectComponent logTo,
      ResourceCollection source,
      FileNameMapper mapper,
      ResourceFactory targets,
      long granularity) {
    if (source.size() == 0) {
      logTo.log("No sources found.", Project.MSG_VERBOSE);
      return Resources.NONE;
    }
    source = Union.getInstance(source);
    logFuture(logTo, source, granularity);

    Union result = new Union();
    for (Iterator iter = source.iterator(); iter.hasNext(); ) {
      Resource sr = (Resource) iter.next();
      String srName = sr.getName();
      srName = srName == null ? srName : srName.replace('/', File.separatorChar);

      String[] targetnames = null;
      try {
        targetnames = mapper.mapFileName(srName);
      } catch (Exception e) {
        logTo.log("Caught " + e + " mapping resource " + sr, Project.MSG_VERBOSE);
      }
      if (targetnames == null || targetnames.length == 0) {
        logTo.log(sr + " skipped - don\'t know how to handle it", Project.MSG_VERBOSE);
        continue;
      }
      Union targetColl = new Union();
      for (int i = 0; i < targetnames.length; i++) {
        targetColl.add(targets.getResource(targetnames[i].replace(File.separatorChar, '/')));
      }
      // find the out-of-date targets:
      Restrict r = new Restrict();
      r.add(
          new And(
              new ResourceSelector[] {
                Type.FILE,
                new Or(new ResourceSelector[] {NOT_EXISTS, new Outdated(sr, granularity)})
              }));
      r.add(targetColl);
      if (r.size() > 0) {
        result.add(sr);
        Resource t = (Resource) (r.iterator().next());
        logTo.log(
            sr.getName()
                + " added as "
                + t.getName()
                + (t.isExists() ? " is outdated." : " doesn\'t exist."),
            Project.MSG_VERBOSE);
        continue;
      }
      // log uptodateness of all targets:
      logTo.log(
          sr.getName()
              + " omitted as "
              + targetColl.toString()
              + (targetColl.size() == 1 ? " is" : " are ")
              + " up to date.",
          Project.MSG_VERBOSE);
    }
    return result;
  }