Search in sources :

Example 66 with MakeCleanDirectoryStep

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

the class GenAidl method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> commands = ImmutableList.builder();
    commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), genPath));
    BuildTarget target = getBuildTarget();
    Path outputDirectory = BuildTargets.getScratchPath(getProjectFilesystem(), target, "__%s.aidl");
    commands.add(new MakeCleanDirectoryStep(getProjectFilesystem(), outputDirectory));
    AidlStep command = new AidlStep(getProjectFilesystem(), target, context.getSourcePathResolver().getAbsolutePath(aidlFilePath), ImmutableSet.of(importPath), outputDirectory);
    commands.add(command);
    // Files must ultimately be written to GEN_DIR to be used as source paths.
    Path genDirectory = getProjectFilesystem().getBuckPaths().getGenDir().resolve(importPath);
    // Warn the user if the genDirectory is not under the output directory.
    if (!importPath.startsWith(target.getBasePath().toString())) {
        // TODO(shs96c): Make this fatal. Give people some time to clean up their rules.
        context.getEventBus().post(ConsoleEvent.warning("%s, gen_aidl import path (%s) should be a child of %s", target, importPath, target.getBasePath()));
    }
    commands.add(new MkdirStep(getProjectFilesystem(), genDirectory));
    commands.add(new JarDirectoryStep(getProjectFilesystem(), output, ImmutableSortedSet.of(outputDirectory), /* main class */
    null, /* manifest */
    null));
    buildableContext.recordArtifact(output);
    return commands.build();
}
Also used : ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) BuildTarget(com.facebook.buck.model.BuildTarget) MkdirStep(com.facebook.buck.step.fs.MkdirStep) JarDirectoryStep(com.facebook.buck.jvm.java.JarDirectoryStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) JarDirectoryStep(com.facebook.buck.jvm.java.JarDirectoryStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep)

Example 67 with MakeCleanDirectoryStep

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

the class GenerateCodeForMergedLibraryMap method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    Path output = context.getSourcePathResolver().getRelativePath(getSourcePathToOutput());
    buildableContext.recordArtifact(output);
    buildableContext.recordArtifact(getMappingPath());
    return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), output.getParent()), new WriteMapDataStep(), new RunCodeGenStep(context.getSourcePathResolver()));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep)

Example 68 with MakeCleanDirectoryStep

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

the class IntraDexReorderStep method reorderEntry.

private int reorderEntry(Path inputPath, boolean isPrimaryDex, ImmutableList.Builder<Step> steps) {
    if (!isPrimaryDex) {
        String tmpname = "dex-tmp-" + inputPath.getFileName().toString() + "-%s";
        Path temp = BuildTargets.getScratchPath(filesystem, buildTarget, tmpname);
        // Create tmp directory if necessary
        steps.add(new MakeCleanDirectoryStep(filesystem, temp));
        // un-zip
        steps.add(new UnzipStep(filesystem, inputPath, temp));
        // run reorder tool
        steps.add(new DefaultShellStep(filesystem.getRootPath(), ImmutableList.of(reorderTool.toString(), reorderDataFile.toString(), temp.resolve("classes.dex").toString())));
        Path outputPath = Paths.get(inputPath.toString().replace(inputSubDir, outputSubDir));
        // re-zip
        steps.add(new ZipStep(filesystem, outputPath, /* paths */
        ImmutableSet.of(), /* junkPaths */
        false, ZipCompressionLevel.MAX_COMPRESSION_LEVEL, temp));
    } else {
        // copy dex
        // apply reorder directly on dex
        steps.add(CopyStep.forFile(filesystem, inputPrimaryDexPath, outputPrimaryDexPath));
        steps.add(new DefaultShellStep(filesystem.getRootPath(), ImmutableList.of(reorderTool.toString(), reorderDataFile.toString(), outputPrimaryDexPath.toString())));
    }
    return 0;
}
Also used : Path(java.nio.file.Path) DefaultShellStep(com.facebook.buck.shell.DefaultShellStep) ZipStep(com.facebook.buck.zip.ZipStep) UnzipStep(com.facebook.buck.zip.UnzipStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep)

Example 69 with MakeCleanDirectoryStep

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

Aggregations

MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)69 Path (java.nio.file.Path)57 SourcePath (com.facebook.buck.rules.SourcePath)53 Step (com.facebook.buck.step.Step)51 ImmutableList (com.google.common.collect.ImmutableList)47 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)42 MkdirStep (com.facebook.buck.step.fs.MkdirStep)25 ExecutionContext (com.facebook.buck.step.ExecutionContext)18 CopyStep (com.facebook.buck.step.fs.CopyStep)13 RmStep (com.facebook.buck.step.fs.RmStep)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 BuildTarget (com.facebook.buck.model.BuildTarget)10 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)9 BuildContext (com.facebook.buck.rules.BuildContext)9 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)9 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)9 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)9 AbstractBuildRule (com.facebook.buck.rules.AbstractBuildRule)8 ShellStep (com.facebook.buck.shell.ShellStep)8 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)8