示例#1
0
 @Override
 public PatternFilterable setIncludes(Iterable<String> arg0) {
   return patternSet.setIncludes(arg0);
 }
  /**
   * Perform the source replacement task.
   *
   * @throws IOException
   */
  @TaskAction
  public void run() throws IOException {
    final PatternSet patternSet = new PatternSet();
    patternSet.setIncludes(this.input.getIncludes());
    patternSet.setExcludes(this.input.getExcludes());

    if (this.output.exists()) {
      // Remove the output directory if it exists to prevent any possible conflicts
      FileUtil.deleteDirectory(this.output);
    }

    this.output.mkdirs();
    this.output = this.output.getCanonicalFile();

    // Resolve global and by-file replacements
    final Map<String, String> globalReplacements = this.resolveReplacementsGlobal();
    final Multimap<String, Map<String, String>> fileReplacements = this.resolveReplacementsByFile();

    for (final DirectoryTree dirTree : this.input.getSrcDirTrees()) {
      File dir = dirTree.getDir();

      // handle non-existent source directories
      if (!dir.exists() || !dir.isDirectory()) {
        continue;
      } else {
        dir = dir.getCanonicalFile();
      }

      // this could be written as .matching(source), but it doesn't actually work
      // because later on gradle casts it directly to PatternSet and crashes
      final FileTree tree =
          this.getProject().fileTree(dir).matching(this.input.getFilter()).matching(patternSet);

      for (final File file : tree) {
        final File destination = getDestination(file, dir, this.output);
        destination.getParentFile().mkdirs();
        destination.createNewFile();

        boolean wasChanged = false;
        String text = Files.toString(file, Charsets.UTF_8);

        if (this.isIncluded(file)) {
          for (Map.Entry<String, String> entry : globalReplacements.entrySet()) {
            text = text.replaceAll(entry.getKey(), entry.getValue());
          }

          wasChanged = true;
        }

        final String path = this.getFilePath(file);
        Collection<Map<String, String>> collection = fileReplacements.get(path);
        if (collection != null && !collection.isEmpty()) {
          for (Map<String, String> map : collection) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
              text = text.replaceAll(entry.getKey(), entry.getValue());
            }
          }

          wasChanged = true;
        }

        if (wasChanged) {
          Files.write(text, destination, Charsets.UTF_8);
        } else {
          Files.copy(file, destination);
        }
      }
    }
  }
示例#3
0
 public CopySpec setIncludes(Iterable<String> includes) {
   patternSet.setIncludes(includes);
   return this;
 }
 public DefaultConfigurableFileTree setIncludes(Iterable<String> includes) {
   patternSet.setIncludes(includes);
   return this;
 }