<commit_before>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  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.twosigma.beakerx.kernel.magic.command;

import static com.google.common.base.Preconditions.checkNotNull;

import com.twosigma.beakerx.kernel.commands.MavenInvocationSilentOutputHandler;
import com.twosigma.beakerx.kernel.commands.MavenJarResolverSilentLogger;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.twosigma.beakerx.kernel.magic.command.functionality.MvnLoggerWidget;
import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;

public class MavenJarResolver {

  public static final String MVN_DIR = File.separator + "mvnJars";
  public static final String POM_XML = "PomTemplateMagicCommand.xml";
  public static final List<String> GOALS = Collections.singletonList("validate");

  private final String pathToMavenRepo;
  private ResolverParams commandParams;
  private String mavenLocation;
  private PomFactory pomFactory;

  public MavenJarResolver(final ResolverParams commandParams,
                          PomFactory pomFactory) {
    this.commandParams = checkNotNull(commandParams);
    this.pathToMavenRepo = getOrCreateFile(commandParams.getPathToNotebookJars()).getAbsolutePath();
    this.pomFactory = pomFactory;
  }

  public AddMvnCommandResult retrieve(String groupId, String artifactId, String version, MvnLoggerWidget progress) {
    File finalPom = null;
    try {
      Dependency dependency = new Dependency(groupId, artifactId, version);
      String pomAsString = pomFactory.createPom(pathToMavenRepo, dependency, commandParams.getRepos());
      finalPom = saveToFile(commandParams.getPathToNotebookJars(), dependency, pomAsString);
      InvocationRequest request = createInvocationRequest();
      request.setOffline(commandParams.getOffline());
      request.setPomFile(finalPom);
      Invoker invoker = getInvoker(progress);
      progress.display();
      InvocationResult invocationResult = invoker.execute(request);
      progress.close();
      return getResult(invocationResult, groupId, artifactId, version);
    } catch (Exception e) {
      return AddMvnCommandResult.error(e.getMessage());
    } finally {
      deletePomFolder(finalPom);
    }
  }

  private File saveToFile(String pathToNotebookJars, Dependency dependency, String pomAsString)
          throws IOException {
    File finalPom = new File(pathToNotebookJars + "/poms/pom-" + UUID.randomUUID() + "-" +
            dependency.getGroupId() + dependency.getArtifactId() + dependency.getVersion() + "xml");

    FileUtils.writeStringToFile(finalPom, pomAsString, StandardCharsets.UTF_8);
    return finalPom;
  }

  private Invoker getInvoker(MvnLoggerWidget progress) {
    Invoker invoker = new DefaultInvoker();
    String mvn = findMvn();<commit_msg>#6571 mvn check for updates by default (#6592)<commit_after>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  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.twosigma.beakerx.kernel.magic.command;

import static com.google.common.base.Preconditions.checkNotNull;

import com.twosigma.beakerx.kernel.commands.MavenInvocationSilentOutputHandler;
import com.twosigma.beakerx.kernel.commands.MavenJarResolverSilentLogger;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.twosigma.beakerx.kernel.magic.command.functionality.MvnLoggerWidget;
import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;

public class MavenJarResolver {

  public static final String MVN_DIR = File.separator + "mvnJars";
  public static final String POM_XML = "PomTemplateMagicCommand.xml";
  public static final List<String> GOALS = Collections.singletonList("validate");

  private final String pathToMavenRepo;
  private ResolverParams commandParams;
  private String mavenLocation;
  private PomFactory pomFactory;

  public MavenJarResolver(final ResolverParams commandParams,
                          PomFactory pomFactory) {
    this.commandParams = checkNotNull(commandParams);
    this.pathToMavenRepo = getOrCreateFile(commandParams.getPathToNotebookJars()).getAbsolutePath();
    this.pomFactory = pomFactory;
  }

  public AddMvnCommandResult retrieve(String groupId, String artifactId, String version, MvnLoggerWidget progress) {
    File finalPom = null;
    try {
      Dependency dependency = new Dependency(groupId, artifactId, version);
      String pomAsString = pomFactory.createPom(pathToMavenRepo, dependency, commandParams.getRepos());
      finalPom = saveToFile(commandParams.getPathToNotebookJars(), dependency, pomAsString);
      InvocationRequest request = createInvocationRequest();
      request.setOffline(commandParams.getOffline());
      request.setPomFile(finalPom);
      request.setUpdateSnapshots(true);
      Invoker invoker = getInvoker(progress);
      progress.display();
      InvocationResult invocationResult = invoker.execute(request);
      progress.close();
      return getResult(invocationResult, groupId, artifactId, version);
    } catch (Exception e) {
      return AddMvnCommandResult.error(e.getMessage());
    } finally {
      deletePomFolder(finalPom);
    }
  }

  private File saveToFile(String pathToNotebookJars, Dependency dependency, String pomAsString)
          throws IOException {
    File finalPom = new File(pathToNotebookJars + "/poms/pom-" + UUID.randomUUID() + "-" +
            dependency.getGroupId() + dependency.getArtifactId() + dependency.getVersion() + "xml");

    FileUtils.writeStringToFile(finalPom, pomAsString, StandardCharsets.UTF_8);
    return finalPom;
  }

  private Invoker getInvoker(MvnLoggerWidget progress) {
    Invoker invoker = new DefaultInvoker();
    String mvn = findMvn();
{'fraction_non_alphanumeric': 0.0888476329399284, 'fraction_numerical': 0.0034478185916987135, 'mean_word_length': 4.521229868228404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5221716', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2138, 'n_words': 527}