<commit_before>/*
 * Copyright 2015-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.jvm.java;

import com.facebook.buck.rules.AbstractBuildRule;
import com.facebook.buck.rules.BuildContext;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildableContext;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.fs.MkdirStep;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Nullable;

/**
 * A {@link BuildRule} used to have the provided {@link JavaLibrary} published to a maven repository
 *
 * @see #create
 */
public class MavenUberJar extends AbstractBuildRule implements MavenPublishable {

  private final Optional<String> mavenCoords;
  private final Optional<SourcePath> mavenPomTemplate;
  private final TraversedDeps traversedDeps;

  private MavenUberJar(
      TraversedDeps traversedDeps,
      BuildRuleParams params,
      Optional<String> mavenCoords,
      Optional<SourcePath> mavenPomTemplate) {
    super(params);
    this.traversedDeps = traversedDeps;
    this.mavenCoords = mavenCoords;
    this.mavenPomTemplate = mavenPomTemplate;
  }

  private static BuildRuleParams adjustParams(BuildRuleParams params, TraversedDeps traversedDeps) {
    return params.copyWithDeps(
        Suppliers.ofInstance(
            ImmutableSortedSet.copyOf(Ordering.natural(), traversedDeps.packagedDeps)),
        Suppliers.ofInstance(ImmutableSortedSet.of()));
  }

  /**
   * Will traverse transitive dependencies of {@code rootRule}, separating those that do and don't
   * have maven coordinates. Those that do will be considered maven-external dependencies. They will
   * be returned by {@link #getMavenDeps} and will end up being specified as dependencies in
   * pom.xml. Others will be packaged in the same jar as if they are just a part of the one
   * published item.
   */
  public static MavenUberJar create(
      JavaLibrary rootRule,
      BuildRuleParams params,
      Optional<String> mavenCoords,
      Optional<SourcePath> mavenPomTemplate) {
    TraversedDeps traversedDeps = TraversedDeps.traverse(ImmutableSet.of(rootRule));
    return new MavenUberJar(
        traversedDeps,
        adjustParams(params, traversedDeps),
        mavenCoords,
        mavenPomTemplate);
  }

  @Override
  public ImmutableList<Step> getBuildSteps(
      BuildContext context, BuildableContext buildableContext) {
    Path pathToOutput = context.getSourcePathResolver().getRelativePath(getSourcePathToOutput());
    MkdirStep mkOutputDirStep = new MkdirStep(getProjectFilesystem(), pathToOutput.getParent());
    JarDirectoryStep mergeOutputsStep = new JarDirectoryStep(
        getProjectFilesystem(),
        pathToOutput,
        toOutputPaths(traversedDeps.packagedDeps),
        /* mainClass */ null,
        /* manifestFile */ null);
    return ImmutableList.of(mkOutputDirStep, mergeOutputsStep);
  }

  private static ImmutableSortedSet<Path> toOutputPaths(Iterable<? extends BuildRule> rules) {
    return FluentIterable
          .from(rules)
          .transform(
              new Function<BuildRule, Path>() {
                @Nullable
                @Override
                public Path apply(BuildRule input) {
                  Path pathToOutput = input.getPathToOutput();
                  if (pathToOutput == null) {
                    return null;
                  }
                  return input.getProjectFilesystem().resolve(pathToOutput);
                }
              })
          .filter(Objects::nonNull)
          .toSortedSet(Ordering.natural());
  }

  @Override
  public Path getPathToOutput() {
    return DefaultJavaLibrary.getOutputJarPath(getBuildTarget(), getProjectFilesystem());
  }

  @Override
  public Optional<String> getMavenCoords() {
    return mavenCoords;
  }

  @Override
  public Optional<SourcePath> getPomTemplate() {
    return mavenPomTemplate;
  }

  @Override<commit_msg>MavenUberJar: Use getSourcePathToOutput not getPathToOutput<commit_after>/*
 * Copyright 2015-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.jvm.java;

import com.facebook.buck.rules.AbstractBuildRule;
import com.facebook.buck.rules.BuildContext;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildableContext;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.step.Step;
import com.facebook.buck.step.fs.MkdirStep;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.RichStream;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

/**
 * A {@link BuildRule} used to have the provided {@link JavaLibrary} published to a maven repository
 *
 * @see #create
 */
public class MavenUberJar extends AbstractBuildRule implements MavenPublishable {

  private final Optional<String> mavenCoords;
  private final Optional<SourcePath> mavenPomTemplate;
  private final TraversedDeps traversedDeps;

  private MavenUberJar(
      TraversedDeps traversedDeps,
      BuildRuleParams params,
      Optional<String> mavenCoords,
      Optional<SourcePath> mavenPomTemplate) {
    super(params);
    this.traversedDeps = traversedDeps;
    this.mavenCoords = mavenCoords;
    this.mavenPomTemplate = mavenPomTemplate;
  }

  private static BuildRuleParams adjustParams(BuildRuleParams params, TraversedDeps traversedDeps) {
    return params.copyWithDeps(
        Suppliers.ofInstance(
            ImmutableSortedSet.copyOf(Ordering.natural(), traversedDeps.packagedDeps)),
        Suppliers.ofInstance(ImmutableSortedSet.of()));
  }

  /**
   * Will traverse transitive dependencies of {@code rootRule}, separating those that do and don't
   * have maven coordinates. Those that do will be considered maven-external dependencies. They will
   * be returned by {@link #getMavenDeps} and will end up being specified as dependencies in
   * pom.xml. Others will be packaged in the same jar as if they are just a part of the one
   * published item.
   */
  public static MavenUberJar create(
      JavaLibrary rootRule,
      BuildRuleParams params,
      Optional<String> mavenCoords,
      Optional<SourcePath> mavenPomTemplate) {
    TraversedDeps traversedDeps = TraversedDeps.traverse(ImmutableSet.of(rootRule));
    return new MavenUberJar(
        traversedDeps,
        adjustParams(params, traversedDeps),
        mavenCoords,
        mavenPomTemplate);
  }

  @Override
  public ImmutableList<Step> getBuildSteps(
      BuildContext context, BuildableContext buildableContext) {
    Path pathToOutput = context.getSourcePathResolver().getRelativePath(getSourcePathToOutput());
    MkdirStep mkOutputDirStep = new MkdirStep(getProjectFilesystem(), pathToOutput.getParent());
    JarDirectoryStep mergeOutputsStep = new JarDirectoryStep(
        getProjectFilesystem(),
        pathToOutput,
        toOutputPaths(context.getSourcePathResolver(), traversedDeps.packagedDeps),
        /* mainClass */ null,
        /* manifestFile */ null);
    return ImmutableList.of(mkOutputDirStep, mergeOutputsStep);
  }

  private static ImmutableSortedSet<Path> toOutputPaths(
      SourcePathResolver pathResolver,
      Iterable<? extends BuildRule> rules) {
    return RichStream.from(rules)
        .map(BuildRule::getSourcePathToOutput)
        .filter(Objects::nonNull)
        .map(pathResolver::getAbsolutePath)
        .collect(MoreCollectors.toImmutableSortedSet());
  }

  @Override
  public Path getPathToOutput() {
    return DefaultJavaLibrary.getOutputJarPath(getBuildTarget(), getProjectFilesystem());
  }

  @Override
  public Optional<String> getMavenCoords() {
    return mavenCoords;
  }

  @Override
  public Optional<SourcePath> getPomTemplate() {
    return mavenPomTemplate;
  }

  @Override
{'fraction_non_alphanumeric': 0.08489704838218887, 'fraction_numerical': 0.0016228826452987118, 'mean_word_length': 3.890873015873016, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2133939', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2761, 'n_words': 710}