/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.buck.android;

import com.facebook.buck.core.build.context.BuildContext;
import com.facebook.buck.core.filesystems.RelPath;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.rulekey.AddToRuleKey;
import com.facebook.buck.core.rules.SourcePathRuleFinder;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.rules.modern.BuildCellRelativePathFactory;
import com.facebook.buck.rules.modern.Buildable;
import com.facebook.buck.rules.modern.ModernBuildRule;
import com.facebook.buck.rules.modern.OutputPath;
import com.facebook.buck.rules.modern.OutputPathResolver;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.isolatedsteps.android.MergeJarResourcesStep;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;

/** Merges resources from third party jars for exo-for-resources. */
public class MergeThirdPartyJarResources extends ModernBuildRule<MergeThirdPartyJarResources>
    implements Buildable {
  @AddToRuleKey private final ImmutableSortedSet<SourcePath> pathsToThirdPartyJars;
  @AddToRuleKey private final OutputPath mergedPath;

  protected MergeThirdPartyJarResources(
      BuildTarget buildTarget,
      ProjectFilesystem projectFilesystem,
      SourcePathRuleFinder ruleFinder,
      ImmutableCollection<SourcePath> pathsToThirdPartyJars) {
    super(buildTarget, projectFilesystem, ruleFinder, MergeThirdPartyJarResources.class);
    this.pathsToThirdPartyJars = ImmutableSortedSet.copyOf(pathsToThirdPartyJars);
    this.mergedPath = new OutputPath("java.resources");
  }

  @Override
  public SourcePath getSourcePathToOutput() {
    return getSourcePath(mergedPath);
  }

  @Override
  public ImmutableList<Step> getBuildSteps(
      BuildContext buildContext,
      ProjectFilesystem filesystem,
      OutputPathResolver outputPathResolver,
      BuildCellRelativePathFactory buildCellPathFactory) {
    ImmutableSortedSet<RelPath> thirdPartyJars =
        buildContext.getSourcePathResolver().getAllRelativePaths(filesystem, pathsToThirdPartyJars);
    return ImmutableList.of(
        new MergeJarResourcesStep(
            thirdPartyJars,
            filesystem.resolve(outputPathResolver.resolvePath(mergedPath).getPath())));
  }
}

@@ -1738,173 +1738,16 @@
 rces
-%3E%0A    implements Buildable %7B%0A  @AddToRuleKey private final ImmutableSortedSet%3CSourcePath%3E pathsToThirdPartyJars;%0A  @AddToRuleKey private final OutputPath mergedPath;
+.Impl%3E %7B
 %0A%0A  
@@ -1971,16 +1971,25 @@
   super(
+%0A        
 buildTar
@@ -1992,16 +1992,24 @@
 dTarget,
+%0A       
  project
@@ -2019,16 +2019,24 @@
 esystem,
+%0A       
  ruleFin
@@ -2043,77 +2043,38 @@
 der,
- MergeThirdPartyJarResources.class);%0A    this.pathsToThirdPartyJars =
+%0A        new Impl(%0A           
  Imm
@@ -2122,31 +2122,9 @@
 ars)
-;%0A    this.mergedPath =
+,
  new
@@ -2152,16 +2152,18 @@
 ources%22)
+))
 ;%0A  %7D%0A%0A 
@@ -2240,24 +2240,39 @@
 tSourcePath(
+getBuildable().
 mergedPath);
@@ -2269,32 +2269,408 @@
 rgedPath);%0A  %7D%0A%0A
+  static class Impl implements Buildable %7B%0A    @AddToRuleKey private final ImmutableSortedSet%3CSourcePath%3E pathsToThirdPartyJars;%0A    @AddToRuleKey private final OutputPath mergedPath;%0A%0A    Impl(ImmutableSortedSet%3CSourcePath%3E pathsToThirdPartyJars, OutputPath mergedPath) %7B%0A      this.pathsToThirdPartyJars = pathsToThirdPartyJars;%0A      this.mergedPath = mergedPath;%0A    %7D%0A%0A  
   @Override%0A  pu
@@ -2657,32 +2657,34 @@
 %0A%0A    @Override%0A
+  
   public Immutab
@@ -2713,24 +2713,26 @@
 teps(%0A      
+  
 BuildContext
@@ -2744,32 +2744,34 @@
 dContext,%0A      
+ 
+ 
 ProjectFilesyste
@@ -2780,24 +2780,26 @@
 filesystem,%0A
+  
       Output
@@ -2827,24 +2827,26 @@
 thResolver,%0A
+  
       BuildC
@@ -2892,24 +2892,26 @@
 tory) %7B%0A    
+  
 ImmutableSor
@@ -2947,24 +2947,26 @@
 s =%0A        
+  
 buildContext
@@ -2965,16 +2965,31 @@
 dContext
+%0A              
 .getSour
@@ -3004,16 +3004,31 @@
 solver()
+%0A              
 .getAllR
@@ -3076,16 +3076,105 @@
 yJars);%0A
+      buildContext.getSourcePathResolver().getAllAbsolutePaths(pathsToThirdPartyJars);%0A  
     retu
@@ -3194,16 +3194,18 @@
 ist.of(%0A
+  
         
@@ -3243,16 +3243,18 @@
         
+  
 thirdPar
@@ -3261,16 +3261,18 @@
 tyJars,%0A
+  
         
@@ -3347,18 +3347,24 @@
 tPath())));%0A
+    %7D%0A
   %7D%0A%7D%0A

Convert MergeThirdPartyJarResources to use an internal nested static class that implements the Buildable interface