Search in sources :

Example 1 with MkdirAndSymlinkFileStep

use of com.facebook.buck.step.fs.MkdirAndSymlinkFileStep 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 MkdirAndSymlinkFileStep

use of com.facebook.buck.step.fs.MkdirAndSymlinkFileStep 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)

Example 3 with MkdirAndSymlinkFileStep

use of com.facebook.buck.step.fs.MkdirAndSymlinkFileStep in project buck by facebook.

the class CopyResourcesStepTest method testAddResourceCommandsWithBuildFileInJavaPackage.

@Test
public void testAddResourceCommandsWithBuildFileInJavaPackage() {
    // Files:
    // android/java/src/com/facebook/BUCK
    // android/java/src/com/facebook/base/data.json
    // android/java/src/com/facebook/common/util/data.json
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//android/java/src/com/facebook:resources");
    JavaPackageFinder javaPackageFinder = createJavaPackageFinder();
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    CopyResourcesStep step = new CopyResourcesStep(filesystem, new SourcePathResolver(ruleFinder), ruleFinder, buildTarget, ImmutableSet.of(new FakeSourcePath(filesystem, "android/java/src/com/facebook/base/data.json"), new FakeSourcePath(filesystem, "android/java/src/com/facebook/common/util/data.json")), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes"), javaPackageFinder);
    List<? extends Step> expected = ImmutableList.of(new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/base/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes/" + "com/facebook/base/data.json")), new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/common/util/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes/" + "com/facebook/common/util/data.json")));
    assertEquals(expected, step.buildSteps());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) Test(org.junit.Test)

Example 4 with MkdirAndSymlinkFileStep

use of com.facebook.buck.step.fs.MkdirAndSymlinkFileStep in project buck by facebook.

the class JavaBinary method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    Path outputDirectory = getOutputDirectory();
    Step mkdir = new MkdirStep(getProjectFilesystem(), outputDirectory);
    commands.add(mkdir);
    ImmutableSortedSet<Path> includePaths;
    if (metaInfDirectory != null) {
        Path stagingRoot = outputDirectory.resolve("meta_inf_staging");
        Path stagingTarget = stagingRoot.resolve("META-INF");
        MakeCleanDirectoryStep createStagingRoot = new MakeCleanDirectoryStep(getProjectFilesystem(), stagingRoot);
        commands.add(createStagingRoot);
        MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(metaInfDirectory), stagingTarget);
        commands.add(link);
        includePaths = ImmutableSortedSet.<Path>naturalOrder().add(stagingRoot).addAll(context.getSourcePathResolver().getAllAbsolutePaths(getTransitiveClasspaths())).build();
    } else {
        includePaths = context.getSourcePathResolver().getAllAbsolutePaths(getTransitiveClasspaths());
    }
    Path outputFile = context.getSourcePathResolver().getRelativePath(getSourcePathToOutput());
    Path manifestPath = manifestFile == null ? null : context.getSourcePathResolver().getAbsolutePath(manifestFile);
    Step jar = new JarDirectoryStep(getProjectFilesystem(), outputFile, includePaths, mainClass, manifestPath, mergeManifests, blacklist);
    commands.add(jar);
    buildableContext.recordArtifact(outputFile);
    return commands.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep)

Example 5 with MkdirAndSymlinkFileStep

use of com.facebook.buck.step.fs.MkdirAndSymlinkFileStep in project buck by facebook.

the class CopyResourcesStepTest method testAddResourceCommandsWithBuildFileParentOfJavaPackage.

@Test
public void testAddResourceCommandsWithBuildFileParentOfJavaPackage() {
    // Files:
    // android/java/src/BUCK
    // android/java/src/com/facebook/base/data.json
    // android/java/src/com/facebook/common/util/data.json
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//android/java/src:resources");
    JavaPackageFinder javaPackageFinder = createJavaPackageFinder();
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    CopyResourcesStep step = new CopyResourcesStep(filesystem, new SourcePathResolver(ruleFinder), ruleFinder, buildTarget, ImmutableSet.<SourcePath>of(new FakeSourcePath(filesystem, "android/java/src/com/facebook/base/data.json"), new FakeSourcePath(filesystem, "android/java/src/com/facebook/common/util/data.json")), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/lib__resources__classes"), javaPackageFinder);
    List<? extends Step> expected = ImmutableList.of(new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/base/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/lib__resources__classes/com/facebook/base/data.json")), new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/common/util/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/lib__resources__classes/com/facebook/common/util/data.json")));
    assertEquals(expected, step.buildSteps());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) Test(org.junit.Test)

Aggregations

MkdirAndSymlinkFileStep (com.facebook.buck.step.fs.MkdirAndSymlinkFileStep)9 BuildTarget (com.facebook.buck.model.BuildTarget)7 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)6 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)6 Test (org.junit.Test)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)5 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)5 Step (com.facebook.buck.step.Step)5 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)5 Path (java.nio.file.Path)5 PathSourcePath (com.facebook.buck.rules.PathSourcePath)4 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)4 JavaPackageFinder (com.facebook.buck.jvm.core.JavaPackageFinder)3 BuildRule (com.facebook.buck.rules.BuildRule)3 SourcePath (com.facebook.buck.rules.SourcePath)3 ImmutableList (com.google.common.collect.ImmutableList)3 BuildContext (com.facebook.buck.rules.BuildContext)2 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)2