1717package com .facebook .buck .step .isolatedsteps .android ;
1818
1919import com .android .common .utils .ILogger ;
20- import com .android .manifmerger .ManifestMerger2 ;
21- import com .android .manifmerger .MergingReport ;
2220import com .facebook .buck .android .BuckEventAndroidLogger ;
23- import com .facebook .buck .android .apkmodule . APKModule ;
21+ import com .facebook .buck .android .manifest . GenerateManifest ;
2422import com .facebook .buck .core .build .execution .context .IsolatedExecutionContext ;
25- import com .facebook .buck .core .exceptions .HumanReadableException ;
2623import com .facebook .buck .core .filesystems .AbsPath ;
2724import com .facebook .buck .core .filesystems .RelPath ;
2825import com .facebook .buck .event .IsolatedEventBus ;
2926import com .facebook .buck .io .filesystem .impl .ProjectFilesystemUtils ;
3027import com .facebook .buck .step .StepExecutionResult ;
3128import com .facebook .buck .step .StepExecutionResults ;
3229import com .facebook .buck .step .isolatedsteps .IsolatedStep ;
33- import com .facebook .buck .util .environment .Platform ;
3430import com .google .common .collect .ImmutableSet ;
35- import com .google .common .collect .Iterables ;
36- import com .google .common .io .Files ;
37- import java .io .File ;
3831import java .io .IOException ;
39- import java .nio .file .Path ;
40- import java .util .ArrayList ;
41- import java .util .List ;
4232
4333public class GenerateManifestStep extends IsolatedStep {
4434
@@ -64,85 +54,24 @@ public GenerateManifestStep(
6454 @ Override
6555 public StepExecutionResult executeIsolatedStep (IsolatedExecutionContext context )
6656 throws IOException , InterruptedException {
67- if (skeletonManifestPath .getNameCount () == 0 ) {
68- throw new HumanReadableException ("Skeleton manifest filepath is missing" );
69- }
70-
71- if (outManifestPath .getNameCount () == 0 ) {
72- throw new HumanReadableException ("Output Manifest filepath is missing" );
73- }
74-
75- Path resolvedOutManifestPath =
76- ProjectFilesystemUtils .getPathForRelativePath (context .getRuleCellRoot (), outManifestPath );
77- Files .createParentDirs (resolvedOutManifestPath .toFile ());
78-
79- List <File > libraryManifestFiles = new ArrayList <>();
80- for (RelPath path : libraryManifestPaths ) {
81- Path manifestPath =
82- ProjectFilesystemUtils .getPathForRelativePath (context .getRuleCellRoot (), path );
83- libraryManifestFiles .add (manifestPath .toFile ());
84- }
85-
86- File skeletonManifestFile =
87- ProjectFilesystemUtils .getPathForRelativePath (
88- context .getRuleCellRoot (), skeletonManifestPath )
89- .toFile ();
90- BuckEventAndroidLogger logger = new ManifestMergerLogger (context .getIsolatedEventBus ());
57+ AbsPath root = context .getRuleCellRoot ();
58+ String xmlText =
59+ GenerateManifest .generateXml (
60+ ProjectFilesystemUtils .getPathForRelativePath (root , skeletonManifestPath ),
61+ moduleName ,
62+ libraryManifestPaths .stream ()
63+ .map (relPath -> ProjectFilesystemUtils .getPathForRelativePath (root , relPath ))
64+ .collect (ImmutableSet .toImmutableSet ()),
65+ ProjectFilesystemUtils .getPathForRelativePath (root , outManifestPath ),
66+ ProjectFilesystemUtils .getPathForRelativePath (root , mergeReportPath ),
67+ new ManifestMergerLogger (context .getIsolatedEventBus ()));
9168
92- MergingReport mergingReport =
93- mergeManifests (
94- context .getRuleCellRoot (), skeletonManifestFile , libraryManifestFiles , logger );
95-
96- String xmlText = mergingReport .getMergedDocument (MergingReport .MergedManifestKind .MERGED );
97- if (context .getPlatform () == Platform .WINDOWS ) {
98- // Convert line endings to Lf on Windows.
99- xmlText = xmlText .replace ("\r \n " , "\n " );
100- }
10169 ProjectFilesystemUtils .writeContentsToPath (
10270 context .getRuleCellRoot (), xmlText , outManifestPath .getPath ());
10371
10472 return StepExecutionResults .SUCCESS ;
10573 }
10674
107- private MergingReport mergeManifests (
108- AbsPath ruleCellRoot ,
109- File mainManifestFile ,
110- List <File > libraryManifestFiles ,
111- BuckEventAndroidLogger logger ) {
112- try {
113- ManifestMerger2 .Invoker <?> manifestInvoker =
114- ManifestMerger2 .newMerger (
115- mainManifestFile , logger , ManifestMerger2 .MergeType .APPLICATION );
116- if (!APKModule .isRootModule (moduleName )) {
117- manifestInvoker .setPlaceHolderValue ("split" , moduleName );
118- } else {
119- manifestInvoker .withFeatures (ManifestMerger2 .Invoker .Feature .NO_PLACEHOLDER_REPLACEMENT );
120- }
121-
122- MergingReport mergingReport =
123- manifestInvoker
124- .withFeatures (
125- ManifestMerger2 .Invoker .Feature .REMOVE_TOOLS_DECLARATIONS ,
126- ManifestMerger2 .Invoker .Feature .SKIP_BLAME )
127- .addLibraryManifests (Iterables .toArray (libraryManifestFiles , File .class ))
128- .setMergeReportFile (
129- ProjectFilesystemUtils .getPathForRelativePath (ruleCellRoot , mergeReportPath )
130- .toFile ())
131- .merge ();
132- if (mergingReport .getResult ().isError ()) {
133- for (MergingReport .Record record : mergingReport .getLoggingRecords ()) {
134- logger .error (null , record .toString ());
135- }
136- throw new HumanReadableException ("Error generating manifest file" );
137- }
138-
139- return mergingReport ;
140- } catch (ManifestMerger2 .MergeFailureException e ) {
141- throw new HumanReadableException (
142- e .getCause (), "Error generating manifest file: %s" , e .getMessage ());
143- }
144- }
145-
14675 @ Override
14776 public String getIsolatedStepDescription (IsolatedExecutionContext context ) {
14877 return String .format ("generate-manifest %s" , skeletonManifestPath );
0 commit comments