public void addDirectory()

in src/java/io/bazel/rulesscala/jar/JarCreator.java [113:160]


  public void addDirectory(Path directory) {
    if (!Files.exists(directory)) {
      throw new IllegalArgumentException("directory does not exist: " + directory);
    }
    try {
      Files.walkFileTree(
          directory,
          new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs)
                throws IOException {
              if (!path.equals(directory)) {
                // For consistency with legacy behaviour, include entries for directories except for
                // the root.
                addEntry(path, /* isDirectory= */ true);
              }
              return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
                throws IOException {
              addEntry(path, /* isDirectory= */ false);
              return FileVisitResult.CONTINUE;
            }

            void addEntry(Path path, boolean isDirectory) {
              StringBuilder sb = new StringBuilder();
              boolean first = true;
              for (Path entry : directory.relativize(path)) {
                if (!first) {
                  // use `/` as the directory separator for jar paths, even on Windows
                  sb.append('/');
                }
                sb.append(entry.getFileName());
                first = false;
              }
              if (isDirectory) {
                sb.append('/');
              }
              jarEntries.put(sb.toString(), path);
            }
          });
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }