/*
 * Copyright 2013-present Facebook, Inc.
 *
 * 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.rules;

import com.facebook.buck.step.AbstractExecutionStep;
import com.facebook.buck.step.ExecutionContext;
import com.facebook.buck.util.BuckConstant;
import com.facebook.buck.util.DirectoryTraversal;
import com.facebook.buck.util.ProjectFilesystem;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

/**
 * Not all build rules know the paths of the output files that their steps will generate. Such rules
 * often write their output to a directory under {@link BuckConstant#BIN_DIR}. This step traverses
 * the output directory, copies the appropriate files to {@link BuckConstant#GEN_DIR}, and records
 * these artifacts for upload via the {@link BuildableContext}.
 */
public class RecordArtifactsInDirectoryStep extends AbstractExecutionStep {

  private final BuildableContext buildableContext;
  private final Path binDirectory;
  private final Path genDirectory;

  /**
   * @param buildableContext Interface through which the outputs to include in the artifact should
   *     be recorded.
   * @param binDirectory Scratch directory where output files were generated.
   * @param genDirectory Output directory where files should be written.
   */
  public RecordArtifactsInDirectoryStep(BuildableContext buildableContext,
      Path binDirectory,
      Path genDirectory) {
    super("recording artifacts in " + binDirectory);
    this.buildableContext = Preconditions.checkNotNull(buildableContext);
    this.binDirectory = Preconditions.checkNotNull(binDirectory);
    this.genDirectory = Preconditions.checkNotNull(genDirectory);
  }

  @Override
  public int execute(final ExecutionContext context) {
    final ProjectFilesystem projectFilesystem = context.getProjectFilesystem();
    ImmutableSet<Path> ignorePaths = ImmutableSet.of();

    DirectoryTraversal traversal = new DirectoryTraversal(
        projectFilesystem.getFileForRelativePath(binDirectory),
        ignorePaths) {
      @Override
      public void visit(File file, String relativePath) throws IOException {
        Path source = binDirectory.resolve(relativePath);
        Path target = genDirectory.resolve(relativePath);
        projectFilesystem.createParentDirs(target);
        projectFilesystem.copyFile(source, target);
        buildableContext.recordArtifact(target);
      }
    };

    try {
      traversal.traverse();
    } catch (IOException e) {
      e.printStackTrace(context.getStdErr());
      return 1;
    }

    return 0;
  }

}

@@ -664,24 +664,16 @@
 ck.step.
-Abstract
 Executio
@@ -673,20 +673,23 @@
 xecution
-Step
+Context
 ;%0Aimport
@@ -712,32 +712,20 @@
 ck.step.
-ExecutionContext
+Step
 ;%0Aimport
@@ -1450,33 +1450,19 @@
 tep 
-extend
+implement
 s 
-AbstractExecution
 Step
@@ -2002,61 +2002,8 @@
 ) %7B%0A
-    super(%22recording artifacts in %22 + binDirectory);%0A
     
@@ -3074,16 +3074,16 @@
 %0A    %7D%0A%0A
-
     retu
@@ -3093,10 +3093,239 @@
 0;%0A  %7D%0A%0A
+  @Override%0A  public String getShortName() %7B%0A    return %22record_artifacts_in_dir%22;%0A  %7D%0A%0A  @Override%0A  public String getDescription(ExecutionContext context) %7B%0A    return String.format(%22%25s %25s%22, getShortName(), binDirectory);%0A  %7D%0A%0A
 %7D%0A

Rename `RecordArtifactsInDirectoryStep` name.