Search in sources :

Example 1 with VersionStringComparator

use of com.facebook.buck.util.VersionStringComparator in project buck by facebook.

the class DefaultAndroidDirectoryResolver method findBuildTools.

private Optional<Path> findBuildTools() {
    if (!sdk.isPresent()) {
        buildToolsErrorMessage = Optional.of(TOOLS_NEED_SDK_MESSAGE);
        return Optional.empty();
    }
    final Path sdkDir = sdk.get();
    final Path toolsDir = sdkDir.resolve("build-tools");
    if (toolsDir.toFile().isDirectory()) {
        // In older versions of the ADT that have been upgraded via the SDK manager, the build-tools
        // directory appears to contain subfolders of the form "17.0.0". However, newer versions of
        // the ADT that are downloaded directly from http://developer.android.com/ appear to have
        // subfolders of the form android-4.2.2. There also appear to be cases where subfolders
        // are named build-tools-18.0.0. We need to support all of these scenarios.
        File[] directories;
        try {
            directories = toolsDir.toFile().listFiles(pathname -> {
                if (!pathname.isDirectory()) {
                    return false;
                }
                String version = stripBuildToolsPrefix(pathname.getName());
                if (!VersionStringComparator.isValidVersionString(version)) {
                    throw new HumanReadableException("%s in %s is not a valid build tools directory.%n" + "Build tools directories should be follow the naming scheme: " + "android-<VERSION>, build-tools-<VERSION>, or <VERSION>. Please remove " + "directory %s.", pathname.getName(), buildTools, pathname.getName());
                }
                if (targetBuildToolsVersion.isPresent()) {
                    return targetBuildToolsVersion.get().equals(pathname.getName());
                }
                return true;
            });
        } catch (HumanReadableException e) {
            buildToolsErrorMessage = Optional.of(e.getHumanReadableErrorMessage());
            return Optional.empty();
        }
        if (targetBuildToolsVersion.isPresent()) {
            if (directories.length == 0) {
                buildToolsErrorMessage = unableToFindTargetBuildTools();
                return Optional.empty();
            } else {
                return Optional.of(directories[0].toPath());
            }
        }
        // We aren't looking for a specific version, so we pick the newest version
        final VersionStringComparator comparator = new VersionStringComparator();
        File newestBuildDir = null;
        String newestBuildDirVersion = null;
        for (File directory : directories) {
            String currentDirVersion = stripBuildToolsPrefix(directory.getName());
            if (newestBuildDir == null || newestBuildDirVersion == null || comparator.compare(newestBuildDirVersion, currentDirVersion) < 0) {
                newestBuildDir = directory;
                newestBuildDirVersion = currentDirVersion;
            }
        }
        if (newestBuildDir == null) {
            buildToolsErrorMessage = Optional.of(buildTools + " was empty, but should have " + "contained a subdirectory with build tools. Install them using the Android " + "SDK Manager (" + toolsDir.getParent().resolve("tools").resolve("android") + ").");
            return Optional.empty();
        }
        return Optional.of(newestBuildDir.toPath());
    }
    if (targetBuildToolsVersion.isPresent()) {
        // We were looking for a specific version, but we aren't going to find it at this point since
        // nothing under platform-tools was versioned.
        buildToolsErrorMessage = unableToFindTargetBuildTools();
        return Optional.empty();
    }
    // Build tools used to exist inside of platform-tools, so fallback to that.
    return Optional.of(sdkDir.resolve("platform-tools"));
}
Also used : Path(java.nio.file.Path) Charsets(com.google.common.base.Charsets) ImmutableSet(com.google.common.collect.ImmutableSet) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HumanReadableException(com.facebook.buck.util.HumanReadableException) FileSystem(java.nio.file.FileSystem) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Strings(com.google.common.base.Strings) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Escaper(com.facebook.buck.util.Escaper) Optional(java.util.Optional) Pair(com.facebook.buck.model.Pair) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BufferedReader(java.io.BufferedReader) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) Path(java.nio.file.Path) HumanReadableException(com.facebook.buck.util.HumanReadableException) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) File(java.io.File)

Example 2 with VersionStringComparator

use of com.facebook.buck.util.VersionStringComparator in project buck by facebook.

the class PythonTestIntegrationTest method assumePythonVersionIsAtLeast.

private void assumePythonVersionIsAtLeast(String expectedVersion, String message) throws InterruptedException {
    PythonVersion actualVersion = new PythonBuckConfig(FakeBuckConfig.builder().build(), new ExecutableFinder()).getPythonEnvironment(new DefaultProcessExecutor(new TestConsole())).getPythonVersion();
    assumeTrue(String.format("Needs at least Python-%s, but found Python-%s: %s", expectedVersion, actualVersion, message), new VersionStringComparator().compare(actualVersion.getVersionString(), expectedVersion) >= 0);
}
Also used : ExecutableFinder(com.facebook.buck.io.ExecutableFinder) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 3 with VersionStringComparator

use of com.facebook.buck.util.VersionStringComparator in project buck by facebook.

the class DefaultAndroidDirectoryResolver method findNdkFromRepository.

private Optional<Path> findNdkFromRepository(Path repository) {
    ImmutableSet<Path> repositoryContents;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(repository)) {
        repositoryContents = ImmutableSet.copyOf(stream);
    } catch (IOException e) {
        ndkErrorMessage = Optional.of("Unable to read contents of Android ndk.repository or " + "ANDROID_NDK_REPOSITORY at " + repository);
        return Optional.empty();
    }
    VersionStringComparator versionComparator = new VersionStringComparator();
    List<Pair<Path, Optional<String>>> availableNdks = repositoryContents.stream().filter(Files::isDirectory).map(p -> new Pair<>(p, findNdkVersion(p))).filter(pair -> pair.getSecond().isPresent()).sorted((o1, o2) -> versionComparator.compare(o2.getSecond().get(), o1.getSecond().get())).collect(Collectors.toList());
    if (availableNdks.isEmpty()) {
        ndkErrorMessage = Optional.of(repository + " does not contain any valid Android NDK. Make" + " sure to specify ANDROID_NDK_REPOSITORY or ndk.repository.");
        return Optional.empty();
    }
    if (targetNdkVersion.isPresent()) {
        if (targetNdkVersion.get().isEmpty()) {
            ndkErrorMessage = Optional.of(NDK_TARGET_VERSION_IS_EMPTY_MESSAGE);
            return Optional.empty();
        }
        Optional<Path> targetNdkPath = availableNdks.stream().filter(p -> versionsMatch(targetNdkVersion.get(), p.getSecond().get())).map(Pair::getFirst).findFirst();
        if (targetNdkPath.isPresent()) {
            return targetNdkPath;
        }
        ndkErrorMessage = Optional.of("Target NDK version " + targetNdkVersion.get() + " is not " + "available. The following versions are available: " + availableNdks.stream().map(Pair::getSecond).map(Optional::get).collect(Collectors.joining(", ")));
        return Optional.empty();
    }
    return Optional.of(availableNdks.get(0).getFirst());
}
Also used : Path(java.nio.file.Path) Charsets(com.google.common.base.Charsets) ImmutableSet(com.google.common.collect.ImmutableSet) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HumanReadableException(com.facebook.buck.util.HumanReadableException) FileSystem(java.nio.file.FileSystem) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Strings(com.google.common.base.Strings) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Escaper(com.facebook.buck.util.Escaper) Optional(java.util.Optional) Pair(com.facebook.buck.model.Pair) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BufferedReader(java.io.BufferedReader) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) Path(java.nio.file.Path) Optional(java.util.Optional) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) IOException(java.io.IOException) Files(java.nio.file.Files) Pair(com.facebook.buck.model.Pair)

Example 4 with VersionStringComparator

use of com.facebook.buck.util.VersionStringComparator in project buck by facebook.

the class AndroidResourceFilterIntegrationTest method findBuildToolsVersion.

@BeforeClass
public static void findBuildToolsVersion() {
    AssumeAndroidPlatform.assumeSdkIsAvailable();
    ProjectFilesystem filesystem = new ProjectFilesystem(Paths.get(".").toAbsolutePath());
    AndroidDirectoryResolver resolver = new DefaultAndroidDirectoryResolver(filesystem.getRootPath().getFileSystem(), ImmutableMap.copyOf(System.getenv()), Optional.empty(), Optional.empty());
    pathToAapt = AndroidPlatformTarget.getDefaultPlatformTarget(resolver, Optional.empty()).getAaptExecutable();
    String buildToolsVersion = pathToAapt.getParent().getFileName().toString();
    isBuildToolsNew = new VersionStringComparator().compare(buildToolsVersion, "21") >= 0;
}
Also used : VersionStringComparator(com.facebook.buck.util.VersionStringComparator) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BeforeClass(org.junit.BeforeClass)

Aggregations

VersionStringComparator (com.facebook.buck.util.VersionStringComparator)4 Pair (com.facebook.buck.model.Pair)2 Escaper (com.facebook.buck.util.Escaper)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Charsets (com.google.common.base.Charsets)2 Strings (com.google.common.base.Strings)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 DirectoryStream (java.nio.file.DirectoryStream)2 FileSystem (java.nio.file.FileSystem)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2