Search in sources :

Example 1 with HasOutputName

use of com.facebook.buck.model.HasOutputName in project buck by facebook.

the class CopyResourcesStep method buildSteps.

@VisibleForTesting
ImmutableList<Step> buildSteps() {
    ImmutableList.Builder<Step> allSteps = ImmutableList.builder();
    if (resources.isEmpty()) {
        return allSteps.build();
    }
    String targetPackageDir = javaPackageFinder.findJavaPackage(target);
    for (SourcePath rawResource : resources) {
        // If the path to the file defining this rule were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/BUCK"
        //
        // And the value of resource were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Assuming that `src_roots = tests` were in the [java] section of the .buckconfig file,
        // then javaPackageAsPath would be:
        // "com/facebook/orca/protocol/base/"
        //
        // And the path that we would want to copy to the classes directory would be:
        // "com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Therefore, some path-wrangling is required to produce the correct string.
        Optional<BuildRule> underlyingRule = ruleFinder.getRule(rawResource);
        Path relativePathToResource = resolver.getRelativePath(rawResource);
        String resource;
        if (underlyingRule.isPresent()) {
            BuildTarget underlyingTarget = underlyingRule.get().getBuildTarget();
            if (underlyingRule.get() instanceof HasOutputName) {
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(((HasOutputName) underlyingRule.get()).getOutputName()));
            } else {
                Path genOutputParent = BuildTargets.getGenPath(filesystem, underlyingTarget, "%s").getParent();
                Path scratchOutputParent = BuildTargets.getScratchPath(filesystem, underlyingTarget, "%s").getParent();
                Optional<Path> outputPath = MorePaths.stripPrefix(relativePathToResource, genOutputParent).map(Optional::of).orElse(MorePaths.stripPrefix(relativePathToResource, scratchOutputParent));
                Preconditions.checkState(outputPath.isPresent(), "%s is used as a resource but does not output to a default output directory", underlyingTarget.getFullyQualifiedName());
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(outputPath.get()));
            }
        } else {
            resource = MorePaths.pathWithUnixSeparators(relativePathToResource);
        }
        Path javaPackageAsPath = javaPackageFinder.findJavaPackageFolder(outputDirectory.getFileSystem().getPath(resource));
        Path relativeSymlinkPath;
        if ("".equals(javaPackageAsPath.toString())) {
            // In this case, the project root is acting as the default package, so the resource path
            // works fine.
            relativeSymlinkPath = relativePathToResource.getFileName();
        } else {
            int lastIndex = resource.lastIndexOf(MorePaths.pathWithUnixSeparatorsAndTrailingSlash(javaPackageAsPath));
            if (lastIndex < 0) {
                Preconditions.checkState(rawResource instanceof BuildTargetSourcePath, "If resource path %s does not contain %s, then it must be a BuildTargetSourcePath.", relativePathToResource, javaPackageAsPath);
                // Handle the case where we depend on the output of another BuildRule. In that case, just
                // grab the output and put in the same package as this target would be in.
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(String.format("%s%s%s", targetPackageDir, targetPackageDir.isEmpty() ? "" : "/", resolver.getRelativePath(rawResource).getFileName()));
            } else {
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(resource.substring(lastIndex));
            }
        }
        Path target = outputDirectory.resolve(relativeSymlinkPath);
        MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(filesystem, resolver.getAbsolutePath(rawResource), target);
        allSteps.add(link);
    }
    return allSteps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) HasOutputName(com.facebook.buck.model.HasOutputName) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with HasOutputName

use of com.facebook.buck.model.HasOutputName in project buck by facebook.

the class SourcePathResolver method getSourcePathName.

public String getSourcePathName(BuildTarget target, SourcePath sourcePath) {
    Preconditions.checkArgument(!(sourcePath instanceof ArchiveMemberSourcePath));
    if (sourcePath instanceof BuildTargetSourcePath) {
        BuildRule rule = ruleFinder.getRuleOrThrow((BuildTargetSourcePath<?>) sourcePath);
        if (rule instanceof HasOutputName) {
            HasOutputName hasOutputName = (HasOutputName) rule;
            return hasOutputName.getOutputName();
        }
        if (sourcePath instanceof ForwardingBuildTargetSourcePath) {
            ForwardingBuildTargetSourcePath castPath = (ForwardingBuildTargetSourcePath) sourcePath;
            return getSourcePathName(target, castPath.getDelegate());
        } else if (sourcePath instanceof ExplicitBuildTargetSourcePath) {
            Path path = ((ExplicitBuildTargetSourcePath) sourcePath).getResolvedPath();
            if (path.startsWith(rule.getProjectFilesystem().getBuckPaths().getGenDir())) {
                path = rule.getProjectFilesystem().getBuckPaths().getGenDir().relativize(path);
            }
            if (path.startsWith(rule.getBuildTarget().getBasePath())) {
                return rule.getBuildTarget().getBasePath().relativize(path).toString();
            }
        }
        return rule.getBuildTarget().getShortName();
    }
    Preconditions.checkArgument(sourcePath instanceof PathSourcePath);
    Path path = ((PathSourcePath) sourcePath).getRelativePath();
    return MorePaths.relativize(target.getBasePath(), path).toString();
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) HasOutputName(com.facebook.buck.model.HasOutputName)

Aggregations

HasOutputName (com.facebook.buck.model.HasOutputName)2 Path (java.nio.file.Path)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 Step (com.facebook.buck.step.Step)1 MkdirAndSymlinkFileStep (com.facebook.buck.step.fs.MkdirAndSymlinkFileStep)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1