Search in sources :

Example 26 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class TrimUberRDotJavaTest method doTrimingTest.

private void doTrimingTest(Optional<String> keepResourcePattern, String rDotJavaContentsAfterFiltering) throws IOException, InterruptedException {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmpFolder.getRoot());
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    AaptPackageResources aaptPackageResources = new AaptPackageResources(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:aapt")).setProjectFilesystem(filesystem).build(), ruleFinder, resolver, null, new IdentityResourcesProvider(ImmutableList.of()), ImmutableList.of(), ImmutableSortedSet.of(), ImmutableSet.of(), Optional.empty(), false, false, /* includesVectorDrawables */
    false, EnumSet.noneOf(RType.class), null);
    resolver.addToIndex(aaptPackageResources);
    String rDotJavaContents = "package com.test;\n" + "\n" + "public class R {\n" + "  public static class string {\n" + "    public static final int my_first_resource=0x7f08005c;\n" + "    public static final int my_second_resource=0x7f083bc1;\n" + "    public static final int keep_resource=0x7f083bc2;\n" + "  }\n" + "}\n";
    Path rDotJavaPath = aaptPackageResources.getPathToGeneratedRDotJavaSrcFiles().resolve("com/test/R.java");
    filesystem.createParentDirs(rDotJavaPath);
    filesystem.writeContentsToPath(rDotJavaContents, rDotJavaPath);
    DexProducedFromJavaLibrary dexProducedFromJavaLibrary = new DexProducedFromJavaLibrary(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:dex")).setProjectFilesystem(filesystem).build(), new FakeJavaLibrary(BuildTargetFactory.newInstance("//:lib"), null));
    dexProducedFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(dexProducedFromJavaLibrary.initializeFromDisk(new FakeOnDiskBuildInfo().putMetadata(DexProducedFromJavaLibrary.WEIGHT_ESTIMATE, "1").putMetadata(DexProducedFromJavaLibrary.CLASSNAMES_TO_HASHES, "{}").putMetadata(DexProducedFromJavaLibrary.REFERENCED_RESOURCES, ImmutableList.of("com.test.my_first_resource"))));
    resolver.addToIndex(dexProducedFromJavaLibrary);
    TrimUberRDotJava trimUberRDotJava = new TrimUberRDotJava(new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//:trim")).setProjectFilesystem(filesystem).build(), aaptPackageResources, ImmutableList.of(dexProducedFromJavaLibrary), keepResourcePattern);
    resolver.addToIndex(trimUberRDotJava);
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
    BuildableContext buildableContext = new FakeBuildableContext();
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    ImmutableList<Step> steps = trimUberRDotJava.getBuildSteps(buildContext, buildableContext);
    for (Step step : steps) {
        step.execute(executionContext);
    }
    ZipInspector inspector = new ZipInspector(pathResolver.getAbsolutePath(trimUberRDotJava.getSourcePathToOutput()));
    inspector.assertFileContents("com/test/R.java", rDotJavaContentsAfterFiltering);
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) Step(com.facebook.buck.step.Step) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) BuildableContext(com.facebook.buck.rules.BuildableContext) FakeOnDiskBuildInfo(com.facebook.buck.rules.FakeOnDiskBuildInfo) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)

Example 27 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class GenruleTest method testCreateAndRunGenrule.

@Test
public void testCreateAndRunGenrule() throws IOException, NoSuchBuildTargetException {
    /*
     * Programmatically build up a Genrule that corresponds to:
     *
     * genrule(
     *   name = 'katana_manifest',
     *   srcs = [
     *     'convert_to_katana.py',
     *     'AndroidManifest.xml',
     *   ],
     *   cmd = 'python $SRCDIR/* > $OUT',
     *   out = 'AndroidManifest.xml',
     * )
     */
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
    createSampleJavaBinaryRule(ruleResolver);
    // From the Python object, create a GenruleBuildRuleFactory to create a Genrule.Builder
    // that builds a Genrule from the Python object.
    //    BuildTargetParser parser = BuildTargetParser.INSTANCE;
    //    EasyMock.expect(parser.parse(EasyMock.eq("//java/com/facebook/util:util"),
    //        EasyMock.anyObject(BuildTargetPatternParser.class)))
    //        .andStubReturn(BuildTargetFactory.newInstance("//java/com/facebook/util:util"));
    //    EasyMock.replay(parser);
    BuildTarget buildTarget = BuildTargetFactory.newInstance(filesystem.getRootPath(), "//src/com/facebook/katana:katana_manifest");
    BuildRule genrule = GenruleBuilder.newGenruleBuilder(buildTarget).setBash("python convert_to_katana.py AndroidManifest.xml > $OUT").setCmdExe("python convert_to_katana.py AndroidManifest.xml > %OUT%").setOut("AndroidManifest.xml").setSrcs(ImmutableList.of(new PathSourcePath(filesystem, filesystem.getPath("src/com/facebook/katana/convert_to_katana.py")), new PathSourcePath(filesystem, filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml")))).build(ruleResolver, filesystem);
    // Verify all of the observers of the Genrule.
    assertEquals(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml"), pathResolver.getRelativePath(genrule.getSourcePathToOutput()));
    assertEquals(filesystem.resolve(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml")).toString(), ((Genrule) genrule).getAbsoluteOutputFilePath(pathResolver));
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(pathResolver);
    ImmutableList<Path> inputsToCompareToOutputs = ImmutableList.of(filesystem.getPath("src/com/facebook/katana/convert_to_katana.py"), filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml"));
    assertEquals(inputsToCompareToOutputs, pathResolver.filterInputsToCompareToOutput(((Genrule) genrule).getSrcs()));
    // Verify that the shell commands that the genrule produces are correct.
    List<Step> steps = genrule.getBuildSteps(buildContext, new FakeBuildableContext());
    assertEquals(6, steps.size());
    ExecutionContext executionContext = newEmptyExecutionContext();
    Step firstStep = steps.get(0);
    assertTrue(firstStep instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep mkdirCommand = (MakeCleanDirectoryStep) firstStep;
    Path pathToOutDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest");
    assertEquals("First command should make sure the output directory exists and is empty.", pathToOutDir, mkdirCommand.getPath());
    Step mkTmpDir = steps.get(1);
    assertTrue(mkTmpDir instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep secondMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir;
    Path pathToTmpDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest__tmp");
    assertEquals("Second command should create the temp directory to be written by the genrule.", pathToTmpDir, secondMkdirCommand.getPath());
    Step mkSrcDir = steps.get(2);
    assertTrue(mkSrcDir instanceof MakeCleanDirectoryStep);
    MakeCleanDirectoryStep thirdMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir;
    Path pathToSrcDir = filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest__srcs");
    assertEquals("Third command should create the temp source directory to be written by the genrule.", pathToTmpDir, thirdMkdirCommand.getPath());
    MkdirAndSymlinkFileStep linkSource1 = (MkdirAndSymlinkFileStep) steps.get(3);
    assertEquals(filesystem.getPath("src/com/facebook/katana/convert_to_katana.py"), linkSource1.getSource());
    assertEquals(filesystem.getPath(pathToSrcDir + "/convert_to_katana.py"), linkSource1.getTarget());
    MkdirAndSymlinkFileStep linkSource2 = (MkdirAndSymlinkFileStep) steps.get(4);
    assertEquals(filesystem.getPath("src/com/facebook/katana/AndroidManifest.xml"), linkSource2.getSource());
    assertEquals(filesystem.getPath(pathToSrcDir + "/AndroidManifest.xml"), linkSource2.getTarget());
    Step sixthStep = steps.get(5);
    assertTrue(sixthStep instanceof AbstractGenruleStep);
    AbstractGenruleStep genruleCommand = (AbstractGenruleStep) sixthStep;
    assertEquals("genrule", genruleCommand.getShortName());
    assertEquals(ImmutableMap.<String, String>builder().put("OUT", filesystem.resolve(filesystem.getBuckPaths().getGenDir().resolve("src/com/facebook/katana/katana_manifest/AndroidManifest.xml")).toString()).build(), genruleCommand.getEnvironmentVariables(executionContext));
    Path scriptFilePath = genruleCommand.getScriptFilePath(executionContext);
    String scriptFileContents = genruleCommand.getScriptFileContents(executionContext);
    if (Platform.detect() == Platform.WINDOWS) {
        assertEquals(ImmutableList.of(scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
        assertEquals("python convert_to_katana.py AndroidManifest.xml > %OUT%", scriptFileContents);
    } else {
        assertEquals(ImmutableList.of("/bin/bash", "-e", scriptFilePath.toString()), genruleCommand.getShellCommand(executionContext));
        assertEquals("python convert_to_katana.py AndroidManifest.xml > $OUT", scriptFileContents);
    }
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) PathSourcePath(com.facebook.buck.rules.PathSourcePath) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildTarget(com.facebook.buck.model.BuildTarget) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) Test(org.junit.Test)

Example 28 with BuildContext

use of com.facebook.buck.rules.BuildContext in project buck by facebook.

the class GenruleTest method testGetShellCommand.

@Test
public void testGetShellCommand() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildContext buildContext = FakeBuildContext.withSourcePathResolver(new SourcePathResolver(new SourcePathRuleFinder(resolver)));
    String bash = "rm -rf /usr";
    String cmdExe = "rmdir /s /q C:\\Windows";
    String cmd = "echo \"Hello\"";
    ExecutionContext linuxExecutionContext = newEmptyExecutionContext(Platform.LINUX);
    ExecutionContext windowsExecutionContext = newEmptyExecutionContext(Platform.WINDOWS);
    // Test platform-specific
    Genrule genrule = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//example:genrule1")).setBash(bash).setCmdExe(cmdExe).setOut("out.txt").build(resolver);
    assertGenruleCommandAndScript(genrule.createGenruleStep(buildContext), linuxExecutionContext, ImmutableList.of("/bin/bash", "-e"), bash);
    assertGenruleCommandAndScript(genrule.createGenruleStep(buildContext), windowsExecutionContext, ImmutableList.of(), cmdExe);
    // Test fallback
    genrule = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//example:genrule2")).setCmd(cmd).setOut("out.txt").build(resolver);
    assertGenruleCommandAndScript(genrule.createGenruleStep(buildContext), linuxExecutionContext, ImmutableList.of("/bin/bash", "-e"), cmd);
    assertGenruleCommandAndScript(genrule.createGenruleStep(buildContext), windowsExecutionContext, ImmutableList.of(), cmd);
    // Test command absent
    genrule = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//example:genrule3")).setOut("out.txt").build(resolver);
    try {
        genrule.createGenruleStep(buildContext).getShellCommand(linuxExecutionContext);
    } catch (HumanReadableException e) {
        assertEquals(String.format("You must specify either bash or cmd for genrule %s.", genrule.getBuildTarget()), e.getHumanReadableErrorMessage());
    }
    try {
        genrule.createGenruleStep(buildContext).getShellCommand(windowsExecutionContext);
    } catch (HumanReadableException e) {
        assertEquals(String.format("You must specify either cmd_exe or cmd for genrule %s.", genrule.getBuildTarget()), e.getHumanReadableErrorMessage());
    }
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) HumanReadableException(com.facebook.buck.util.HumanReadableException) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Aggregations

BuildContext (com.facebook.buck.rules.BuildContext)28 Step (com.facebook.buck.step.Step)22 FakeBuildContext (com.facebook.buck.rules.FakeBuildContext)19 FakeBuildableContext (com.facebook.buck.rules.FakeBuildableContext)18 Test (org.junit.Test)18 BuildTarget (com.facebook.buck.model.BuildTarget)17 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)15 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)13 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)13 Path (java.nio.file.Path)13 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)12 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)12 BuildRule (com.facebook.buck.rules.BuildRule)9 SourcePath (com.facebook.buck.rules.SourcePath)9 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)8 BuildableContext (com.facebook.buck.rules.BuildableContext)8 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)8 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)8 ExecutionContext (com.facebook.buck.step.ExecutionContext)8 ImmutableList (com.google.common.collect.ImmutableList)8