Ejemplo n.º 1
0
  @Test
  public void shouldSetUpAPrivateLibraryIfGivenAMavenCoordWithoutDeps() throws Exception {
    resolver.resolve("com.example:no-deps:jar:1.0");

    Path groupDir = thirdParty.resolve("example");
    assertTrue(Files.exists(groupDir));

    Path original = repo.resolve("com/example/no-deps/1.0/no-deps-1.0.jar");
    HashCode expected = MorePaths.asByteSource(original).hash(Hashing.sha1());
    Path jarFile = groupDir.resolve("no-deps-1.0.jar");
    HashCode seen = MorePaths.asByteSource(jarFile).hash(Hashing.sha1());
    assertEquals(expected, seen);

    List<Map<String, Object>> rules = buildFileParser.getAll(groupDir.resolve("BUCK"));

    assertEquals(1, rules.size());
    Map<String, Object> rule = rules.get(0);
    // Name is derived from the project identifier
    assertEquals("no-deps", rule.get("name"));

    // The binary jar should be set
    assertEquals("no-deps-1.0.jar", rule.get("binaryJar"));

    // There was no source jar in the repo
    assertTrue(rule.containsKey("sourceJar"));
    assertNull(rule.get("sourceJar"));

    // Nothing depends on this, so it's not visible
    assertEquals(ImmutableList.of(), rule.get("visibility"));

    // And it doesn't depend on anything
    assertEquals(ImmutableList.of(), rule.get("deps"));
  }
Ejemplo n.º 2
0
  /**
   * If the source paths specified are all generated files, then our path to source tmp should be
   * absent.
   */
  @Test
  public void testGeneratedSourceFile() {
    Path pathToGenFile = GEN_PATH.resolve("GeneratedFile.java");
    assertTrue(MorePaths.isGeneratedFile(pathToGenFile));

    ImmutableSortedSet<Path> javaSrcs = ImmutableSortedSet.of(pathToGenFile);
    JavaLibrary javaLibrary =
        new FakeJavaLibrary(
                BuildTarget.builder("//foo", "bar").build(),
                new SourcePathResolver(new BuildRuleResolver()))
            .setJavaSrcs(javaSrcs);

    DefaultJavaPackageFinder defaultJavaPackageFinder = createMock(DefaultJavaPackageFinder.class);

    Object[] mocks = new Object[] {defaultJavaPackageFinder};
    replay(mocks);

    ImmutableSet<String> result =
        TestRunning.getPathToSourceFolders(
            javaLibrary, Optional.of(defaultJavaPackageFinder), new FakeProjectFilesystem());

    assertTrue(
        "No path should be returned if the library contains only generated files.",
        result.isEmpty());

    verify(mocks);
  }
Ejemplo n.º 3
0
  /**
   * If the source paths specified are from the new unified source tmp then we should return the
   * correct source tmp corresponding to the unified source path.
   */
  @Test
  public void testUnifiedSourceFile() {
    Path pathToNonGenFile = Paths.get("java/package/SourceFile1.java");
    assertFalse(MorePaths.isGeneratedFile(pathToNonGenFile));

    ImmutableSortedSet<Path> javaSrcs = ImmutableSortedSet.of(pathToNonGenFile);
    JavaLibrary javaLibrary =
        new FakeJavaLibrary(
                BuildTarget.builder("//foo", "bar").build(),
                new SourcePathResolver(new BuildRuleResolver()))
            .setJavaSrcs(javaSrcs);

    DefaultJavaPackageFinder defaultJavaPackageFinder = createMock(DefaultJavaPackageFinder.class);
    expect(defaultJavaPackageFinder.getPathsFromRoot()).andReturn(pathsFromRoot);

    Object[] mocks = new Object[] {defaultJavaPackageFinder};
    replay(mocks);

    ImmutableSet<String> result =
        TestRunning.getPathToSourceFolders(
            javaLibrary, Optional.of(defaultJavaPackageFinder), new FakeProjectFilesystem());

    assertEquals(
        "All non-generated source files are under one source tmp.",
        ImmutableSet.of("java/"),
        result);

    verify(mocks);
  }
Ejemplo n.º 4
0
 private Path normalizePathToProjectRoot(Path pathRelativeToProjectRoot)
     throws NoSuchFileException {
   if (!exists(pathRelativeToProjectRoot)) {
     throw new NoSuchFileException(pathRelativeToProjectRoot.toString());
   }
   return MorePaths.normalize(pathRelativeToProjectRoot);
 }
Ejemplo n.º 5
0
  private static boolean canMerge(
      IjFolder parent, IjFolder child, PackagePathCache packagePathCache) {
    Preconditions.checkArgument(child.getPath().startsWith(parent.getPath()));

    if (!child.canMergeWith(parent)) {
      return false;
    }

    if (parent.getWantsPackagePrefix() != child.getWantsPackagePrefix()) {
      return false;
    }

    if (parent.getWantsPackagePrefix()) {
      Optional<Path> parentPackage = packagePathCache.lookup(parent);
      if (!parentPackage.isPresent()) {
        return false;
      }
      Path childPackage = packagePathCache.lookup(child).get();

      int pathDifference = child.getPath().getNameCount() - parent.getPath().getNameCount();
      Preconditions.checkState(pathDifference == 1);
      if (childPackage.getNameCount() == 0) {
        return false;
      }
      if (!MorePaths.getParentOrEmpty(childPackage).equals(parentPackage.get())) {
        return false;
      }
    }
    return true;
  }
Ejemplo n.º 6
0
  /** Verify that owners are correctly detected: - one owner, multiple inputs */
  @Test
  public void verifyInputsWithOneOwnerAreCorrectlyReported()
      throws CmdLineException, IOException, InterruptedException {
    FakeProjectFilesystem filesystem =
        new FakeProjectFilesystem() {
          @Override
          public File getFileForRelativePath(String pathRelativeToProjectRoot) {
            return new ExistingFile(getRootPath(), pathRelativeToProjectRoot);
          }
        };

    ImmutableSet<String> inputs =
        ImmutableSet.of(
            "java/somefolder/badfolder/somefile.java",
            "java/somefolder/perfect.java",
            "com/test/subtest/random.java");
    ImmutableSet<Path> inputPaths = MorePaths.asPaths(inputs);

    BuildTarget target = BuildTargetFactory.newInstance("//base:name");
    TargetNode<?> targetNode = createTargetNode(target, inputPaths);

    CommandRunnerParams params = createAuditOwnerCommandRunnerParams(filesystem);
    AuditOwnerCommand.OwnersReport report =
        AuditOwnerCommand.generateOwnersReport(params, targetNode, inputs, false);
    assertTrue(report.nonFileInputs.isEmpty());
    assertTrue(report.nonExistentInputs.isEmpty());
    assertTrue(report.inputsWithNoOwners.isEmpty());

    assertEquals(inputs.size(), report.owners.size());
    assertTrue(report.owners.containsKey(targetNode));
    assertEquals(targetNode.getInputs(), report.owners.get(targetNode));
  }
Ejemplo n.º 7
0
 @Override
 public long getLastModifiedTime(Path path) throws IOException {
   Path normalizedPath = MorePaths.normalize(path);
   if (!exists(normalizedPath)) {
     throw new NoSuchFileException(path.toString());
   }
   return Preconditions.checkNotNull(fileLastModifiedTimes.get(normalizedPath)).toMillis();
 }
Ejemplo n.º 8
0
 @Override
 public Path setLastModifiedTime(Path path, FileTime time) throws IOException {
   Path normalizedPath = MorePaths.normalize(path);
   if (!exists(normalizedPath)) {
     throw new NoSuchFileException(path.toString());
   }
   fileLastModifiedTimes.put(normalizedPath, time);
   return normalizedPath;
 }
Ejemplo n.º 9
0
 @Override
 public void deleteRecursivelyIfExists(Path path) throws IOException {
   Path normalizedPath = MorePaths.normalize(path);
   for (Iterator<Path> iterator = fileContents.keySet().iterator(); iterator.hasNext(); ) {
     Path subPath = iterator.next();
     if (subPath.startsWith(normalizedPath)) {
       fileAttributes.remove(MorePaths.normalize(subPath));
       fileLastModifiedTimes.remove(MorePaths.normalize(subPath));
       iterator.remove();
     }
   }
   for (Iterator<Path> iterator = symLinks.keySet().iterator(); iterator.hasNext(); ) {
     Path subPath = iterator.next();
     if (subPath.startsWith(normalizedPath)) {
       iterator.remove();
     }
   }
   fileLastModifiedTimes.remove(path);
   directories.remove(path);
 }
Ejemplo n.º 10
0
  /**
   * Looks through filtered drawables for files not of the target density and replaces them with
   * scaled versions.
   *
   * <p>Any drawables found by this step didn't have equivalents in the target density. If they are
   * of a higher density, we can replicate what Android does and downscale them at compile-time.
   */
  private void scaleUnmatchedDrawables(ExecutionContext context)
      throws IOException, InterruptedException {
    ResourceFilters.Density targetDensity = ResourceFilters.Density.ORDERING.max(targetDensities);

    // Go over all the images that remain after filtering.
    Preconditions.checkNotNull(drawableFinder);
    Collection<Path> drawables =
        drawableFinder.findDrawables(inResDirToOutResDirMap.values(), filesystem);
    for (Path drawable : drawables) {
      if (drawable.toString().endsWith(".9.png")) {
        // Skip nine-patch for now.
        continue;
      }

      ResourceFilters.Qualifiers qualifiers = new ResourceFilters.Qualifiers(drawable);
      ResourceFilters.Density density = qualifiers.density;

      // If the image has a qualifier but it's not the right one.
      Preconditions.checkNotNull(targetDensities);
      if (!targetDensities.contains(density)) {

        // Replace density qualifier with target density using regular expression to match
        // the qualifier in the context of a path to a drawable.
        String fromDensity =
            (density == ResourceFilters.Density.NO_QUALIFIER ? "" : "-") + density.toString();
        Path destination =
            Paths.get(
                MorePaths.pathWithUnixSeparators(drawable)
                    .replaceFirst(
                        "((?:^|/)drawable[^/]*)" + Pattern.quote(fromDensity) + "(-|$|/)",
                        "$1-" + targetDensity + "$2"));

        double factor = targetDensity.value() / density.value();
        if (factor >= 1.0) {
          // There is no point in up-scaling, or converting between drawable and drawable-mdpi.
          continue;
        }

        // Make sure destination folder exists and perform downscaling.
        filesystem.createParentDirs(destination);
        Preconditions.checkNotNull(imageScaler);
        imageScaler.scale(factor, drawable, destination, context);

        // Delete source file.
        filesystem.deleteFileAtPath(drawable);

        // Delete newly-empty directories to prevent missing resources errors in apkbuilder.
        Path parent = drawable.getParent();
        if (filesystem.listFiles(parent).length == 0) {
          filesystem.deleteFileAtPath(parent);
        }
      }
    }
  }
Ejemplo n.º 11
0
  @Nullable
  public Path resolvePathThatMayBeOutsideTheProjectFilesystem(@Nullable Path path) {
    if (path == null) {
      return path;
    }

    if (path.isAbsolute()) {
      return path;
    }

    Path expandedPath = MorePaths.expandHomeDir(path);
    return projectFilesystem.getAbsolutifier().apply(expandedPath);
  }
Ejemplo n.º 12
0
 @Override
 public void move(Path source, Path target, CopyOption... options) throws IOException {
   fileContents.put(MorePaths.normalize(target), fileContents.remove(MorePaths.normalize(source)));
   fileAttributes.put(
       MorePaths.normalize(target), fileAttributes.remove(MorePaths.normalize(source)));
   fileLastModifiedTimes.put(
       MorePaths.normalize(target), fileLastModifiedTimes.remove(MorePaths.normalize(source)));
 }
Ejemplo n.º 13
0
  @Override
  public void writeBytesToPath(byte[] bytes, Path path, FileAttribute<?>... attrs)
      throws IOException {
    Path normalizedPath = MorePaths.normalize(path);
    fileContents.put(normalizedPath, Preconditions.checkNotNull(bytes));
    fileAttributes.put(normalizedPath, ImmutableSet.copyOf(attrs));

    Path directory = normalizedPath.getParent();
    while (directory != null) {
      directories.add(directory);
      directory = directory.getParent();
    }
    fileLastModifiedTimes.put(normalizedPath, FileTime.fromMillis(clock.currentTimeMillis()));
  }
Ejemplo n.º 14
0
 @Test
 public void testDirectoryAndFile() throws IOException {
   File notADirectory = tempDir.newFile("not_a_directory.txt");
   Files.write("not_a_directory.txt", notADirectory, Charsets.UTF_8);
   File yesADir = tempDir.newFolder("is_a_directory");
   Files.write("foo.txt", new File(yesADir, "foo.txt"), Charsets.UTF_8);
   Files.write("bar.txt", new File(yesADir, "bar.txt"), Charsets.UTF_8);
   File aSubDir = new File(yesADir, "fizzbuzz");
   assertTrue("Failed to create dir: " + aSubDir, aSubDir.mkdir());
   Files.write(
       MorePaths.pathWithPlatformSeparators("fizzbuzz/whatever.txt"),
       new File(aSubDir, "whatever.txt"),
       Charsets.UTF_8);
   verifyFileLike(4, notADirectory, yesADir);
 }
Ejemplo n.º 15
0
  private WriteFileStep getWriteRegistrationFileStep(Path registrationFile, Path packageDb) {
    Map<String, String> entries = new LinkedHashMap<>();

    entries.put("name", packageInfo.getName());
    entries.put("version", packageInfo.getVersion());
    entries.put("id", packageInfo.getIdentifier());

    entries.put("exposed", "True");
    entries.put("exposed-modules", Joiner.on(' ').join(modules));

    Path pkgRoot = getProjectFilesystem().getRootPath().getFileSystem().getPath("${pkgroot}");

    if (!modules.isEmpty()) {
      List<String> importDirs = new ArrayList<>();
      for (SourcePath interfaceDir : interfaces) {
        Path relInterfaceDir =
            pkgRoot.resolve(
                packageDb.getParent().relativize(getResolver().getRelativePath(interfaceDir)));
        importDirs.add('"' + relInterfaceDir.toString() + '"');
      }
      entries.put("import-dirs", Joiner.on(", ").join(importDirs));
    }

    List<String> libDirs = new ArrayList<>();
    List<String> libs = new ArrayList<>();
    for (SourcePath library : libraries) {
      Path relLibPath =
          pkgRoot.resolve(packageDb.getParent().relativize(getResolver().getRelativePath(library)));
      libDirs.add('"' + relLibPath.getParent().toString() + '"');
      libs.add(MorePaths.stripPathPrefixAndExtension(relLibPath.getFileName(), "lib"));
    }
    entries.put("library-dirs", Joiner.on(", ").join(libDirs));
    // Use extra libraries here, so GHC won't try to find libraries with any extra suffices
    // (e.g. lib<name>-ghc7.10.3.dylib).
    entries.put("extra-libraries", Joiner.on(", ").join(libs));

    entries.put("depends", Joiner.on(", ").join(depPackages.keySet()));

    return new WriteFileStep(
        getProjectFilesystem(),
        Joiner.on(System.lineSeparator())
            .join(
                FluentIterable.from(entries.entrySet())
                    .transform(input -> input.getKey() + ": " + input.getValue())),
        registrationFile,
        /* executable */ false);
  }
Ejemplo n.º 16
0
 /**
  * @param cellRoot root path to the cell the rule is defined in.
  * @param map the map of values that define the rule.
  * @param rulePathForDebug path to the build file the rule is defined in, only used for debugging.
  * @return the build target defined by the rule.
  */
 public static UnflavoredBuildTarget parseBuildTargetFromRawRule(
     Path cellRoot, Map<String, Object> map, Path rulePathForDebug) {
   String basePath = (String) map.get("buck.base_path");
   String name = (String) map.get("name");
   if (basePath == null || name == null) {
     throw new IllegalStateException(
         String.format(
             "Attempting to parse build target from malformed raw data in %s: %s.",
             rulePathForDebug, Joiner.on(",").withKeyValueSeparator("->").join(map)));
   }
   Path otherBasePath = cellRoot.relativize(MorePaths.getParentOrEmpty(rulePathForDebug));
   if (!otherBasePath.equals(otherBasePath.getFileSystem().getPath(basePath))) {
     throw new IllegalStateException(
         String.format(
             "Raw data claims to come from [%s], but we tried rooting it at [%s].",
             basePath, otherBasePath));
   }
   return UnflavoredBuildTarget.builder(UnflavoredBuildTarget.BUILD_TARGET_PREFIX + basePath, name)
       .setCellPath(cellRoot)
       .build();
 }
Ejemplo n.º 17
0
  /** Verify that owners are correctly detected: - one owner, multiple inputs, json output */
  @Test
  public void verifyInputsWithOneOwnerAreCorrectlyReportedInJson()
      throws CmdLineException, IOException, InterruptedException {
    FakeProjectFilesystem filesystem =
        new FakeProjectFilesystem() {
          @Override
          public File getFileForRelativePath(String pathRelativeToProjectRoot) {
            return new ExistingFile(getRootPath(), pathRelativeToProjectRoot);
          }
        };

    ImmutableSet<String> inputs =
        ImmutableSet.of(
            "java/somefolder/badfolder/somefile.java",
            "java/somefolder/perfect.java",
            "com/test/subtest/random.java");
    ImmutableSortedSet<Path> inputPaths = MorePaths.asPaths(inputs);

    BuildTarget target = BuildTargetFactory.newInstance("//base/name:name");
    TargetNode<?> targetNode = createTargetNode(target, inputPaths);

    AuditOwnerCommand command = new AuditOwnerCommand();
    CommandRunnerParams params = createAuditOwnerCommandRunnerParams(filesystem);
    AuditOwnerCommand.OwnersReport report =
        AuditOwnerCommand.generateOwnersReport(params, targetNode, inputs, false);
    command.printOwnersOnlyJsonReport(params, report);

    String expectedJson =
        Joiner.on("")
            .join(
                "{",
                "\"com/test/subtest/random.java\":[\"//base/name:name\"],",
                "\"java/somefolder/badfolder/somefile.java\":[\"//base/name:name\"],",
                "\"java/somefolder/perfect.java\":[\"//base/name:name\"]",
                "}");

    assertEquals(expectedJson, console.getTextWrittenToStdOut());
    assertEquals("", console.getTextWrittenToStdErr());
  }
  @SuppressWarnings({"rawtypes", "unchecked"})
  private TargetNode<?> createTargetNode(
      BuckEventBus eventBus,
      Cell cell,
      Path buildFile,
      BuildTarget target,
      Map<String, Object> rawNode,
      TargetNodeListener nodeListener) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);

    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);

    if (target.isFlavored()) {
      if (description instanceof Flavored) {
        if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
          throw new HumanReadableException(
              "Unrecognized flavor in target %s while parsing %s%s.",
              target,
              UnflavoredBuildTarget.BUILD_TARGET_PREFIX,
              MorePaths.pathWithUnixSeparators(
                  target.getBasePath().resolve(cell.getBuildFileName())));
        }
      } else {
        LOG.warn(
            "Target %s (type %s) must implement the Flavored interface "
                + "before we can check if it supports flavors: %s",
            target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
        throw new HumanReadableException(
            "Target %s (type %s) does not currently support flavors (tried %s)",
            target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
      }
    }

    Cell targetCell = cell.getCell(target);
    BuildRuleFactoryParams factoryParams =
        new BuildRuleFactoryParams(
            targetCell.getFilesystem(),
            target.withoutCell(),
            new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()),
            targetCell.isEnforcingBuckPackageBoundaries());
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
      ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
      ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder();
      try (SimplePerfEvent.Scope scope =
          SimplePerfEvent.scope(
              eventBus, PerfEventId.of("MarshalledConstructorArg"), "target", target)) {
        marshaller.populate(
            targetCell.getCellRoots(),
            targetCell.getFilesystem(),
            factoryParams,
            constructorArg,
            declaredDeps,
            visibilityPatterns,
            rawNode);
      }
      try (SimplePerfEvent.Scope scope =
          SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"), "target", target)) {
        Hasher hasher = Hashing.sha1().newHasher();
        hasher.putString(BuckVersion.getVersion(), UTF_8);
        JsonObjectHashing.hashJsonObject(hasher, rawNode);
        synchronized (this) {
          targetsCornucopia.put(target.getUnflavoredBuildTarget(), target);
        }
        TargetNode<?> node =
            new TargetNode(
                hasher.hash(),
                description,
                constructorArg,
                typeCoercerFactory,
                factoryParams,
                declaredDeps.build(),
                visibilityPatterns.build(),
                targetCell.getCellRoots());
        nodeListener.onCreate(buildFile, node);
        return node;
      }
    } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) {
      throw new HumanReadableException(e);
    } catch (ConstructorArgMarshalException e) {
      throw new HumanReadableException("%s: %s", target, e.getMessage());
    } catch (IOException e) {
      throw new HumanReadableException(e.getMessage(), e);
    }
  }
Ejemplo n.º 19
0
  public void assertFilesEqual(Path expected, Path actual) throws IOException {
    if (!expected.isAbsolute()) {
      expected = templatePath.resolve(expected);
    }
    if (!actual.isAbsolute()) {
      actual = destPath.resolve(actual);
    }
    if (!Files.isRegularFile(actual)) {
      fail("Expected file " + actual + " could not be found.");
    }

    String extension = MorePaths.getFileExtension(actual);
    String cleanPathToObservedFile =
        MoreStrings.withoutSuffix(templatePath.relativize(expected).toString(), EXPECTED_SUFFIX);

    switch (extension) {
        // For Apple .plist and .stringsdict files, we define equivalence if:
        // 1. The two files are the same type (XML or binary)
        // 2. If binary: unserialized objects are deeply-equivalent.
        //    Otherwise, fall back to exact string match.
      case "plist":
      case "stringsdict":
        NSObject expectedObject;
        try {
          expectedObject = BinaryPropertyListParser.parse(expected.toFile());
        } catch (Exception e) {
          // Not binary format.
          expectedObject = null;
        }

        NSObject observedObject;
        try {
          observedObject = BinaryPropertyListParser.parse(actual.toFile());
        } catch (Exception e) {
          // Not binary format.
          observedObject = null;
        }

        assertTrue(
            String.format(
                "In %s, expected plist to be of %s type.",
                cleanPathToObservedFile, (expectedObject != null) ? "binary" : "XML"),
            (expectedObject != null) == (observedObject != null));

        if (expectedObject != null) {
          // These keys depend on the locally installed version of Xcode, so ignore them
          // in comparisons.
          String[] ignoredKeys = {
            "DTSDKName",
            "DTPlatformName",
            "DTPlatformVersion",
            "MinimumOSVersion",
            "DTSDKBuild",
            "DTPlatformBuild",
            "DTXcode",
            "DTXcodeBuild"
          };
          if (observedObject instanceof NSDictionary && expectedObject instanceof NSDictionary) {
            for (String key : ignoredKeys) {
              ((NSDictionary) observedObject).remove(key);
              ((NSDictionary) expectedObject).remove(key);
            }
          }

          assertEquals(
              String.format(
                  "In %s, expected binary plist contents to match.", cleanPathToObservedFile),
              expectedObject,
              observedObject);
          break;
        } else {
          assertFileContentsEqual(expected, actual);
        }
        break;

      default:
        assertFileContentsEqual(expected, actual);
    }
  }
Ejemplo n.º 20
0
 @Override
 public boolean isFile(Path path) {
   return fileContents.containsKey(MorePaths.normalize(path));
 }
Ejemplo n.º 21
0
  /** Returns a set of source folders of the java files of a library. */
  @VisibleForTesting
  static ImmutableSet<String> getPathToSourceFolders(
      JavaLibrary rule,
      Optional<DefaultJavaPackageFinder> defaultJavaPackageFinderOptional,
      ProjectFilesystem filesystem) {
    ImmutableSet<Path> javaSrcs = rule.getJavaSrcs();

    // A Java library rule with just resource files has an empty javaSrcs.
    if (javaSrcs.isEmpty()) {
      return ImmutableSet.of();
    }

    // If defaultJavaPackageFinderOptional is not present, then it could mean that there was an
    // error reading from the buck configuration file.
    if (!defaultJavaPackageFinderOptional.isPresent()) {
      throw new HumanReadableException(
          "Please include a [java] section with src_root property in the .buckconfig file.");
    }

    DefaultJavaPackageFinder defaultJavaPackageFinder = defaultJavaPackageFinderOptional.get();

    // Iterate through all source paths to make sure we are generating a complete set of source
    // folders for the source paths.
    Set<String> srcFolders = Sets.newHashSet();
    loopThroughSourcePath:
    for (Path javaSrcPath : javaSrcs) {
      if (!MorePaths.isGeneratedFile(javaSrcPath)) {
        // If the source path is already under a known source folder, then we can skip this
        // source path.
        for (String srcFolder : srcFolders) {
          if (javaSrcPath.startsWith(srcFolder)) {
            continue loopThroughSourcePath;
          }
        }

        // If the source path is under one of the source roots, then we can just add the source
        // root.
        ImmutableSortedSet<String> pathsFromRoot = defaultJavaPackageFinder.getPathsFromRoot();
        for (String root : pathsFromRoot) {
          if (javaSrcPath.startsWith(root)) {
            srcFolders.add(root);
            continue loopThroughSourcePath;
          }
        }

        // Traverse the file system from the parent directory of the java file until we hit the
        // parent of the src root directory.
        ImmutableSet<String> pathElements = defaultJavaPackageFinder.getPathElements();
        File directory = filesystem.getFileForRelativePath(javaSrcPath.getParent());
        while (directory != null && !pathElements.contains(directory.getName())) {
          directory = directory.getParentFile();
        }

        if (directory != null) {
          String directoryPath = directory.getPath();
          if (!directoryPath.endsWith("/")) {
            directoryPath += "/";
          }
          srcFolders.add(directoryPath);
        }
      }
    }

    return ImmutableSet.copyOf(srcFolders);
  }
Ejemplo n.º 22
0
 @Override
 public boolean isDirectory(Path path, LinkOption... linkOptions) {
   return directories.contains(MorePaths.normalize(path));
 }
Ejemplo n.º 23
0
  static OwnersReport buildOwnersReport(
      CommandRunnerParams params,
      ParserConfig parserConfig,
      BuildFileTree buildFileTree,
      Iterable<String> arguments,
      boolean guessForDeletedEnabled)
      throws IOException, InterruptedException, BuildFileParseException, BuildTargetException {
    final Path rootPath = params.getCell().getFilesystem().getRootPath();
    Preconditions.checkState(rootPath.isAbsolute());
    Map<Path, List<TargetNode<?>>> targetNodes = Maps.newHashMap();
    OwnersReport report = OwnersReport.emptyReport();

    for (Path filePath : getArgumentsAsPaths(rootPath, arguments)) {
      Optional<Path> basePath = buildFileTree.getBasePathOfAncestorTarget(filePath);
      if (!basePath.isPresent()) {
        report =
            report.updatedWith(
                new OwnersReport(
                    ImmutableSetMultimap.<TargetNode<?>, Path>of(),
                    /* inputWithNoOwners */ ImmutableSet.of(filePath),
                    Sets.<String>newHashSet(),
                    Sets.<String>newHashSet()));
        continue;
      }

      Path buckFile = basePath.get().resolve(parserConfig.getBuildFileName());
      Preconditions.checkState(params.getCell().getFilesystem().exists(buckFile));

      // Parse buck files and load target nodes.
      if (!targetNodes.containsKey(buckFile)) {
        try {
          targetNodes.put(
              buckFile,
              params
                  .getParser()
                  .getOrLoadTargetNodes(
                      buckFile,
                      parserConfig,
                      params.getBuckEventBus(),
                      params.getConsole(),
                      params.getEnvironment()));
        } catch (BuildFileParseException | BuildTargetException e) {
          Path targetBasePath = MorePaths.relativize(rootPath, rootPath.resolve(basePath.get()));
          String targetBaseName = "//" + MorePaths.pathWithUnixSeparators(targetBasePath);

          params
              .getConsole()
              .getStdErr()
              .format("Could not parse build targets for %s", targetBaseName);
          throw e;
        }
      }

      for (TargetNode<?> targetNode : targetNodes.get(buckFile)) {
        report =
            report.updatedWith(
                generateOwnersReport(
                    params,
                    targetNode,
                    ImmutableList.of(filePath.toString()),
                    guessForDeletedEnabled));
      }
    }
    return report;
  }
Ejemplo n.º 24
0
 private void rmFile(Path path) {
   fileContents.remove(MorePaths.normalize(path));
   fileAttributes.remove(MorePaths.normalize(path));
   fileLastModifiedTimes.remove(MorePaths.normalize(path));
 }
Ejemplo n.º 25
0
  @VisibleForTesting
  static String createQuery(
      ObjectMapper objectMapper,
      String watchRoot,
      Optional<String> watchPrefix,
      String uuid,
      Iterable<Path> ignorePaths,
      Iterable<String> ignoreGlobs,
      Set<Capability> watchmanCapabilities) {
    List<Object> queryParams = new ArrayList<>();
    queryParams.add("query");
    queryParams.add(watchRoot);
    // Note that we use LinkedHashMap so insertion order is preserved. That
    // helps us write tests that don't depend on the undefined order of HashMap.
    Map<String, Object> sinceParams = new LinkedHashMap<>();
    sinceParams.put("since", new StringBuilder("n:buckd").append(uuid).toString());

    // Exclude any expressions added to this list.
    List<Object> excludeAnyOf = Lists.<Object>newArrayList("anyof");

    // Exclude all directories.
    excludeAnyOf.add(Lists.newArrayList("type", "d"));

    Path projectRoot = Paths.get(watchRoot);
    if (watchPrefix.isPresent()) {
      projectRoot = projectRoot.resolve(watchPrefix.get());
    }

    // Exclude all files under directories in project.ignorePaths.
    //
    // Note that it's OK to exclude .git in a query (event though it's
    // not currently OK to exclude .git in .watchmanconfig). This id
    // because watchman's .git cookie magic is done before the query
    // is applied.
    for (Path ignorePath : ignorePaths) {
      if (ignorePath.isAbsolute()) {
        ignorePath = MorePaths.relativize(projectRoot, ignorePath);
      }
      if (watchmanCapabilities.contains(Capability.DIRNAME)) {
        excludeAnyOf.add(Lists.newArrayList("dirname", ignorePath.toString()));
      } else {
        excludeAnyOf.add(Lists.newArrayList("match", ignorePath.toString() + "/*", "wholename"));
      }
    }

    // Exclude all filenames matching globs. We explicitly don't match
    // against the full path ("wholename"), just the filename.
    for (String ignoreGlob : ignoreGlobs) {
      excludeAnyOf.add(Lists.newArrayList("match", ignoreGlob));
    }

    sinceParams.put("expression", Lists.newArrayList("not", excludeAnyOf));
    sinceParams.put("empty_on_fresh_instance", true);
    sinceParams.put("fields", Lists.newArrayList("name", "exists", "new"));
    if (watchPrefix.isPresent()) {
      sinceParams.put("relative_root", watchPrefix.get());
    }
    queryParams.add(sinceParams);
    try {
      return objectMapper.writeValueAsString(queryParams);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Ejemplo n.º 26
0
 private byte[] getFileBytes(Path path) {
   return Preconditions.checkNotNull(fileContents.get(MorePaths.normalize(path)));
 }