Search in sources :

Example 31 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class Resolver method getNewerVersionFile.

/**
   * @return {@link Path} to the file in {@code project} with filename consistent with the given
   * {@link Artifact}, but with a newer version. If no such file exists, {@link Optional#empty} is
   * returned. If multiple such files are present one with the newest version will be returned.
   */
@VisibleForTesting
Optional<Path> getNewerVersionFile(final Artifact artifactToDownload, Path project) throws IOException {
    final Version artifactToDownloadVersion;
    try {
        artifactToDownloadVersion = versionScheme.parseVersion(artifactToDownload.getVersion());
    } catch (InvalidVersionSpecificationException e) {
        throw new RuntimeException(e);
    }
    final Pattern versionExtractor = Pattern.compile(String.format(ARTIFACT_FILE_NAME_REGEX_FORMAT, artifactToDownload.getArtifactId(), VERSION_REGEX_GROUP, artifactToDownload.getExtension()));
    Iterable<Version> versionsPresent = FluentIterable.from(Files.newDirectoryStream(project)).transform(new Function<Path, Version>() {

        @Nullable
        @Override
        public Version apply(Path input) {
            Matcher matcher = versionExtractor.matcher(input.getFileName().toString());
            if (matcher.matches()) {
                try {
                    return versionScheme.parseVersion(matcher.group(1));
                } catch (InvalidVersionSpecificationException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return null;
            }
        }
    }).filter(Objects::nonNull);
    List<Version> newestPresent = Ordering.natural().greatestOf(versionsPresent, 1);
    if (newestPresent.isEmpty() || newestPresent.get(0).compareTo(artifactToDownloadVersion) <= 0) {
        return Optional.empty();
    } else {
        return Optional.of(project.resolve(String.format(ARTIFACT_FILE_NAME_FORMAT, artifactToDownload.getArtifactId(), newestPresent.get(0).toString(), artifactToDownload.getExtension())));
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Function(com.google.common.base.Function) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) Version(org.eclipse.aether.version.Version) Matcher(java.util.regex.Matcher) Objects(java.util.Objects) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 32 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class WorkerProcessPoolFactory method getEnvironmentForProcess.

@VisibleForTesting
ImmutableMap<String, String> getEnvironmentForProcess(ExecutionContext context, WorkerJobParams workerJobParams) {
    Path tmpDir = workerJobParams.getTempDir();
    Map<String, String> envVars = Maps.newHashMap(context.getEnvironment());
    envVars.put("TMP", filesystem.resolve(tmpDir).toString());
    envVars.putAll(workerJobParams.getStartupEnvironment());
    return ImmutableMap.copyOf(envVars);
}
Also used : Path(java.nio.file.Path) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class Genrule method isWorkerGenrule.

@VisibleForTesting
public boolean isWorkerGenrule() {
    Arg cmdArg = cmd.orElse(null);
    Arg bashArg = bash.orElse(null);
    Arg cmdExeArg = cmdExe.orElse(null);
    if ((cmdArg instanceof WorkerMacroArg) || (bashArg instanceof WorkerMacroArg) || (cmdExeArg instanceof WorkerMacroArg)) {
        if ((cmdArg != null && !(cmdArg instanceof WorkerMacroArg)) || (bashArg != null && !(bashArg instanceof WorkerMacroArg)) || (cmdExeArg != null && !(cmdExeArg instanceof WorkerMacroArg))) {
            throw new HumanReadableException("You cannot use a worker macro in one of the cmd, bash, " + "or cmd_exe properties and not in the others for genrule %s.", getBuildTarget().getFullyQualifiedName());
        }
        return true;
    }
    return false;
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) WorkerMacroArg(com.facebook.buck.rules.args.WorkerMacroArg) Arg(com.facebook.buck.rules.args.Arg) WorkerMacroArg(com.facebook.buck.rules.args.WorkerMacroArg) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 34 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class Genrule method getBuildSteps.

@Override
@VisibleForTesting
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    // Make sure that the directory to contain the output file exists, deleting any pre-existing
    // ones. Rules get output to a directory named after the base path, so we don't want to nuke
    // the entire directory.
    commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToOutDirectory));
    // Delete the old temp directory
    commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTmpDirectory));
    // Create a directory to hold all the source files.
    commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToSrcDirectory));
    addSymlinkCommands(context, commands);
    // Create a shell command that corresponds to this.cmd.
    if (this.isWorkerGenrule) {
        commands.add(createWorkerShellStep(context));
    } else {
        commands.add(createGenruleStep(context));
    }
    if (MorePaths.getFileExtension(pathToOutFile).equals("zip")) {
        commands.add(new ZipScrubberStep(getProjectFilesystem(), pathToOutFile));
    }
    buildableContext.recordArtifact(pathToOutFile);
    return commands.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ZipScrubberStep(com.facebook.buck.zip.ZipScrubberStep) ZipScrubberStep(com.facebook.buck.zip.ZipScrubberStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 35 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class Genrule method addSymlinkCommands.

@VisibleForTesting
void addSymlinkCommands(BuildContext context, ImmutableList.Builder<Step> commands) {
    Path basePath = getBuildTarget().getBasePath();
    // Symlink all sources into the temp directory so that they can be used in the genrule.
    for (SourcePath src : srcs) {
        Path relativePath = context.getSourcePathResolver().getRelativePath(src);
        Path absolutePath = context.getSourcePathResolver().getAbsolutePath(src);
        Path canonicalPath = absolutePath.normalize();
        // By the time we get this far, all source paths (the keys in the map) have been converted
        // to paths relative to the project root. We want the path relative to the build target, so
        // strip the base path.
        Path localPath;
        if (absolutePath.equals(canonicalPath)) {
            if (relativePath.startsWith(basePath) || getBuildTarget().isInCellRoot()) {
                localPath = MorePaths.relativize(basePath, relativePath);
            } else {
                localPath = canonicalPath.getFileName();
            }
        } else {
            localPath = relativePath;
        }
        Path destination = pathToSrcDirectory.resolve(localPath);
        commands.add(new MkdirAndSymlinkFileStep(getProjectFilesystem(), relativePath, destination));
    }
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1954 IOException (java.io.IOException)284 ArrayList (java.util.ArrayList)214 Map (java.util.Map)156 HashMap (java.util.HashMap)147 List (java.util.List)113 File (java.io.File)94 ImmutableMap (com.google.common.collect.ImmutableMap)72 HashSet (java.util.HashSet)67 Path (org.apache.hadoop.fs.Path)63 ImmutableList (com.google.common.collect.ImmutableList)60 Path (java.nio.file.Path)60 Set (java.util.Set)52 Matcher (java.util.regex.Matcher)46 Collectors (java.util.stream.Collectors)46 Collection (java.util.Collection)39 Optional (java.util.Optional)38 NotNull (org.jetbrains.annotations.NotNull)37 ImmutableSet (com.google.common.collect.ImmutableSet)34 TreeMap (java.util.TreeMap)34