<commit_before>/*
 * 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.shell;

import com.facebook.buck.core.build.buildable.context.BuildableContext;
import com.facebook.buck.core.build.context.BuildContext;
import com.facebook.buck.core.build.execution.context.ExecutionContext;
import com.facebook.buck.core.cell.CellPathResolver;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.impl.BuildTargetPaths;
import com.facebook.buck.core.rulekey.AddToRuleKey;
import com.facebook.buck.core.rules.BuildRule;
import com.facebook.buck.core.rules.BuildRuleParams;
import com.facebook.buck.core.rules.SourcePathRuleFinder;
import com.facebook.buck.core.rules.attr.HasRuntimeDeps;
import com.facebook.buck.core.rules.impl.AbstractBuildRuleWithDeclaredAndExtraDeps;
import com.facebook.buck.core.rules.tool.BinaryBuildRule;
import com.facebook.buck.core.sourcepath.DefaultBuildTargetSourcePath;
import com.facebook.buck.core.sourcepath.ExplicitBuildTargetSourcePath;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.core.sourcepath.resolver.SourcePathResolver;
import com.facebook.buck.core.toolchain.tool.Tool;
import com.facebook.buck.core.toolchain.tool.impl.CommandTool;
import com.facebook.buck.event.ConsoleEvent;
import com.facebook.buck.io.BuildCellRelativePath;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.rules.args.SourcePathArg;
import com.facebook.buck.step.AbstractExecutionStep;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.StepExecutionResult;
import com.facebook.buck.step.StepExecutionResults;
import com.facebook.buck.step.fs.MakeCleanDirectoryStep;
import com.facebook.buck.step.fs.MakeExecutableStep;
import com.facebook.buck.step.fs.StringTemplateStep;
import com.facebook.buck.step.fs.SymlinkTreeStep;
import com.facebook.buck.util.Escaper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Stream;

public class ShBinary extends AbstractBuildRuleWithDeclaredAndExtraDeps
    implements BinaryBuildRule, HasRuntimeDeps {

  private static final Path TEMPLATE =
      Paths.get(
          System.getProperty(
              "buck.path_to_sh_binary_template", "src/com/facebook/buck/shell/sh_binary_template"));

  private static final String RUNTIME_RESOURCES_DIR = "runtime_resources";

  private static final String ROOT_CELL_LINK_NAME = "__default__";
  private static final String EXTERNAL_CELL_LINK_NAME = "__external__";

  private static final String STEP_CATEGORY = "sh_binary";

  @AddToRuleKey private final SourcePath main;
  @AddToRuleKey private final ImmutableSet<SourcePath> resources;
  private final CellPathResolver cellRoots;

  /** The path where the output will be written. */
  private final Path output;

  private final Path runtimeResourcesDir;

  protected ShBinary(
      BuildTarget buildTarget,
      CellPathResolver cellRoots,
      ProjectFilesystem projectFilesystem,
      BuildRuleParams params,
      SourcePath main,
      ImmutableSet<SourcePath> resources) {
    super(buildTarget, projectFilesystem, params);
    this.main = main;
    this.resources = resources;
    this.cellRoots = cellRoots;

    this.output =
        BuildTargetPaths.getGenPath(
            getProjectFilesystem(),
            buildTarget,
            String.format("__%%s__/%s.sh", buildTarget.getShortNameAndFlavorPostfix()));

    this.runtimeResourcesDir =
        BuildTargetPaths.getGenPath(
            getProjectFilesystem(),
            this.getBuildTarget(),
            String.format(
                "__%%s__/" + RUNTIME_RESOURCES_DIR,
                this.getBuildTarget().getShortNameAndFlavorPostfix()));
  }

  @Override
  public ImmutableList<Step> getBuildSteps(
      BuildContext context, BuildableContext buildableContext) {
    buildableContext.recordArtifact(output);

    ImmutableList.Builder<String> cellsPathsStringsBuilder = new ImmutableList.Builder<String>();
    ImmutableList.Builder<String> cellsNamesBuilder = new ImmutableList.Builder<String>();

    ProjectFilesystem projectFilesystem = getProjectFilesystem();

    // Create symlink to the root cell.
    Path rootPath = cellRoots.getCellPathOrThrow(Optional.empty());
    Path relativePath = projectFilesystem.getRootPath().relativize(rootPath);
    cellsPathsStringsBuilder.add(Escaper.BASH_ESCAPER.apply(relativePath.toString()));
    cellsNamesBuilder.add(Escaper.BASH_ESCAPER.apply(ROOT_CELL_LINK_NAME));

    // Create symlink to the cells.
    for (ImmutableMap.Entry<String, Path> ent : cellRoots.getCellPaths().entrySet()) {<commit_msg>Remove spurious format arg (#2168)<commit_after>/*
 * 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.shell;

import com.facebook.buck.core.build.buildable.context.BuildableContext;
import com.facebook.buck.core.build.context.BuildContext;
import com.facebook.buck.core.build.execution.context.ExecutionContext;
import com.facebook.buck.core.cell.CellPathResolver;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.impl.BuildTargetPaths;
import com.facebook.buck.core.rulekey.AddToRuleKey;
import com.facebook.buck.core.rules.BuildRule;
import com.facebook.buck.core.rules.BuildRuleParams;
import com.facebook.buck.core.rules.SourcePathRuleFinder;
import com.facebook.buck.core.rules.attr.HasRuntimeDeps;
import com.facebook.buck.core.rules.impl.AbstractBuildRuleWithDeclaredAndExtraDeps;
import com.facebook.buck.core.rules.tool.BinaryBuildRule;
import com.facebook.buck.core.sourcepath.DefaultBuildTargetSourcePath;
import com.facebook.buck.core.sourcepath.ExplicitBuildTargetSourcePath;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.core.sourcepath.resolver.SourcePathResolver;
import com.facebook.buck.core.toolchain.tool.Tool;
import com.facebook.buck.core.toolchain.tool.impl.CommandTool;
import com.facebook.buck.event.ConsoleEvent;
import com.facebook.buck.io.BuildCellRelativePath;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.rules.args.SourcePathArg;
import com.facebook.buck.step.AbstractExecutionStep;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.StepExecutionResult;
import com.facebook.buck.step.StepExecutionResults;
import com.facebook.buck.step.fs.MakeCleanDirectoryStep;
import com.facebook.buck.step.fs.MakeExecutableStep;
import com.facebook.buck.step.fs.StringTemplateStep;
import com.facebook.buck.step.fs.SymlinkTreeStep;
import com.facebook.buck.util.Escaper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Stream;

public class ShBinary extends AbstractBuildRuleWithDeclaredAndExtraDeps
    implements BinaryBuildRule, HasRuntimeDeps {

  private static final Path TEMPLATE =
      Paths.get(
          System.getProperty(
              "buck.path_to_sh_binary_template", "src/com/facebook/buck/shell/sh_binary_template"));

  private static final String RUNTIME_RESOURCES_DIR = "runtime_resources";

  private static final String ROOT_CELL_LINK_NAME = "__default__";
  private static final String EXTERNAL_CELL_LINK_NAME = "__external__";

  private static final String STEP_CATEGORY = "sh_binary";

  @AddToRuleKey private final SourcePath main;
  @AddToRuleKey private final ImmutableSet<SourcePath> resources;
  private final CellPathResolver cellRoots;

  /** The path where the output will be written. */
  private final Path output;

  private final Path runtimeResourcesDir;

  protected ShBinary(
      BuildTarget buildTarget,
      CellPathResolver cellRoots,
      ProjectFilesystem projectFilesystem,
      BuildRuleParams params,
      SourcePath main,
      ImmutableSet<SourcePath> resources) {
    super(buildTarget, projectFilesystem, params);
    this.main = main;
    this.resources = resources;
    this.cellRoots = cellRoots;

    this.output =
        BuildTargetPaths.getGenPath(
            getProjectFilesystem(),
            buildTarget,
            String.format("__%%s__/%s.sh", buildTarget.getShortNameAndFlavorPostfix()));

    this.runtimeResourcesDir =
        BuildTargetPaths.getGenPath(
            getProjectFilesystem(), this.getBuildTarget(), "__%s__/" + RUNTIME_RESOURCES_DIR);
  }

  @Override
  public ImmutableList<Step> getBuildSteps(
      BuildContext context, BuildableContext buildableContext) {
    buildableContext.recordArtifact(output);

    ImmutableList.Builder<String> cellsPathsStringsBuilder = new ImmutableList.Builder<String>();
    ImmutableList.Builder<String> cellsNamesBuilder = new ImmutableList.Builder<String>();

    ProjectFilesystem projectFilesystem = getProjectFilesystem();

    // Create symlink to the root cell.
    Path rootPath = cellRoots.getCellPathOrThrow(Optional.empty());
    Path relativePath = projectFilesystem.getRootPath().relativize(rootPath);
    cellsPathsStringsBuilder.add(Escaper.BASH_ESCAPER.apply(relativePath.toString()));
    cellsNamesBuilder.add(Escaper.BASH_ESCAPER.apply(ROOT_CELL_LINK_NAME));

    // Create symlink to the cells.
    for (ImmutableMap.Entry<String, Path> ent : cellRoots.getCellPaths().entrySet()) {
{'fraction_non_alphanumeric': 0.09394381612550164, 'fraction_numerical': 0.0018241517694272164, 'mean_word_length': 4.82624867162593, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1356746', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3097, 'n_words': 652}