<commit_before>/*
 * Zinc - The incremental compiler for Scala.
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * This software is released under the terms written in LICENSE.
 */

package sbt
package internal
package inc

import java.io.File

import sbt.io.{ Hash, IO }
import sbt.internal.librarymanagement.{
  InlineConfiguration,
  IvyActions,
  IvyConfiguration,
  IvySbt,
  JsonUtil,
  LogicalClock,
  RetrieveConfiguration,
  UnresolvedWarning,
  UnresolvedWarningConfiguration
}
import sbt.librarymanagement.{
  ArtifactTypeFilter,
  Configurations,
  ModuleID,
  ModuleInfo,
  Resolver,
  UpdateConfiguration,
  UpdateLogging,
  UpdateOptions
}
import sbt.librarymanagement.syntax._
import sbt.util.{ InterfaceUtil, Logger }
import xsbti.compile.CompilerBridgeProvider

private[sbt] object ZincComponentCompiler {
  final val binSeparator = "-bin_"
  final val javaClassVersion = System.getProperty("java.class.version")

  private[inc] final val sbtOrgTemp = JsonUtil.sbtOrgTemp
  private[inc] final val modulePrefixTemp = "temp-module-"

  private final val ZincVersionPropertyFile = "/incrementalcompiler.version.properties"
  private final val ZincVersionProperty = "version"
  final lazy val incrementalVersion: String = {
    val cl = this.getClass.getClassLoader
    ResourceLoader.getPropertiesFor(ZincVersionPropertyFile, cl).getProperty(ZincVersionProperty)
  }

  private class ZincCompilerBridgeProvider(manager: ZincComponentManager,
                                           ivyConfiguration: IvyConfiguration,
                                           bridgeModule: ModuleID)
      extends CompilerBridgeProvider {

    override def getCompiledBridge(scalaInstance: xsbti.compile.ScalaInstance,
                                   logger: xsbti.Logger): File = {
      val autoClasspath = ClasspathOptionsUtil.auto
      val bridgeCompiler = new RawCompiler(scalaInstance, autoClasspath, logger)
      val ivyComponent =
        new ZincComponentCompiler(bridgeCompiler, manager, ivyConfiguration, bridgeModule, logger)
      logger.debug(InterfaceUtil.f0(s"Getting $bridgeModule for Scala ${scalaInstance.version}"))
      ivyComponent.getCompiledBridgeJar
    }
  }

  def interfaceProvider(manager: ZincComponentManager,
                        ivyConfiguration: IvyConfiguration,
                        sourcesModule: ModuleID): CompilerBridgeProvider =
    new ZincCompilerBridgeProvider(manager, ivyConfiguration, sourcesModule)

}

/**
 * Component compiler which is able to to retrieve the compiler bridge sources
 * `sourceModule` using Ivy.
 * The compiled classes are cached using the provided component manager according
 * to the actualVersion field of the RawCompiler.
 */
private[inc] class ZincComponentCompiler(
    compiler: RawCompiler,
    manager: ZincComponentManager,
    ivyConfiguration: IvyConfiguration,
    bridgeSources: ModuleID,
    log: Logger
) {
  import sbt.internal.util.{ BufferedLogger, FullLogger }
  private final val ivySbt: IvySbt = new IvySbt(ivyConfiguration)
  private final val buffered = new BufferedLogger(FullLogger(log))

  def getCompiledBridgeJar: File = {
    val jarBinaryName = createBridgeSourcesID(bridgeSources)
    manager.file(jarBinaryName)(IfMissing.define(true, compileAndInstall(jarBinaryName)))
  }

  /**
   * Returns the id for the compiler interface component.
   *
   * The ID contains the following parts:
   *   - The organization, name and revision.
   *   - The bin separator to make clear the jar represents binaries.
   *   - The Scala version for which the compiler interface is meant to.
   *   - The JVM class version.
   *
   * Example: "org.scala-sbt-compiler-bridge-1.0.0-bin_2.11.7__50.0".
   *
   * @param sources The moduleID representing the compiler bridge sources.
   * @return The complete jar identifier for the bridge sources.
   */
  private def createBridgeSourcesID(sources: ModuleID): String = {
    import ZincComponentCompiler.{ binSeparator, javaClassVersion }
    val id = s"${sources.organization}-${sources.name}-${sources.revision}"
    val scalaVersion = compiler.scalaInstance.actualVersion()
    s"$id$binSeparator${scalaVersion}__$javaClassVersion"
  }

  /**
   * Resolves the compiler bridge sources, compiles them and installs the sbt component
   * in the local filesystem to make sure that it's reused the next time is required.
   *
   * @param compilerBridgeId The identifier for the compiler bridge sources.
   */
  private def compileAndInstall(compilerBridgeId: String): Unit = {
    import UnresolvedWarning.unresolvedWarningLines
    val ivyModuleForBridge = wrapDependencyInModule(bridgeSources)
    IO.withTemporaryDirectory { binaryDirectory =>
      val target = new File(binaryDirectory, s"$compilerBridgeId.jar")
      buffered bufferQuietly {
        IO.withTemporaryDirectory { retrieveDirectory =>
          update(ivyModuleForBridge, retrieveDirectory) match {
            case Left(uw) =>
              val mod = bridgeSources.toString
              val unresolvedLines = unresolvedWarningLines.showLines(uw)
              val unretrievedMessage = s"The compiler bridge sources $mod could not be retrieved."
              throw new InvalidComponent(s"$unresolvedLines\n$unresolvedLines")

            case Right(allArtifacts) =>
              val (srcs, xsbtiJars) = allArtifacts.partition(_.getName.endsWith("-sources.jar"))
              val toCompileID = bridgeSources.name
              AnalyzingCompiler.compileSources(srcs, target, xsbtiJars, toCompileID, compiler, log)
              manager.define(compilerBridgeId, Seq(target))
          }
        }

      }
    }
  }

  /**
   * Returns an ivy module that will wrap and download a given `moduleID`.
   *
   * @param moduleID The `moduleID` that needs to be wrapped in a dummy module to be downloaded.
   */
  private def wrapDependencyInModule(moduleID: ModuleID): ivySbt.Module = {
    def getModule(moduleID: ModuleID, deps: Vector[ModuleID], uo: UpdateOptions): ivySbt.Module = {
      val moduleInfo = ModuleInfo(moduleID.name)
      val componentIvySettings = InlineConfiguration(
        validate = false,
        ivyScala = None,<commit_msg>Fix oversight in InvalidComponent message<commit_after>/*
 * Zinc - The incremental compiler for Scala.
 * Copyright 2011 - 2017, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * This software is released under the terms written in LICENSE.
 */

package sbt
package internal
package inc

import java.io.File

import sbt.io.{ Hash, IO }
import sbt.internal.librarymanagement.{
  InlineConfiguration,
  IvyActions,
  IvyConfiguration,
  IvySbt,
  JsonUtil,
  LogicalClock,
  RetrieveConfiguration,
  UnresolvedWarning,
  UnresolvedWarningConfiguration
}
import sbt.librarymanagement.{
  ArtifactTypeFilter,
  Configurations,
  ModuleID,
  ModuleInfo,
  Resolver,
  UpdateConfiguration,
  UpdateLogging,
  UpdateOptions
}
import sbt.librarymanagement.syntax._
import sbt.util.{ InterfaceUtil, Logger }
import xsbti.compile.CompilerBridgeProvider

private[sbt] object ZincComponentCompiler {
  final val binSeparator = "-bin_"
  final val javaClassVersion = System.getProperty("java.class.version")

  private[inc] final val sbtOrgTemp = JsonUtil.sbtOrgTemp
  private[inc] final val modulePrefixTemp = "temp-module-"

  private final val ZincVersionPropertyFile = "/incrementalcompiler.version.properties"
  private final val ZincVersionProperty = "version"
  final lazy val incrementalVersion: String = {
    val cl = this.getClass.getClassLoader
    ResourceLoader.getPropertiesFor(ZincVersionPropertyFile, cl).getProperty(ZincVersionProperty)
  }

  private class ZincCompilerBridgeProvider(manager: ZincComponentManager,
                                           ivyConfiguration: IvyConfiguration,
                                           bridgeModule: ModuleID)
      extends CompilerBridgeProvider {

    override def getCompiledBridge(scalaInstance: xsbti.compile.ScalaInstance,
                                   logger: xsbti.Logger): File = {
      val autoClasspath = ClasspathOptionsUtil.auto
      val bridgeCompiler = new RawCompiler(scalaInstance, autoClasspath, logger)
      val ivyComponent =
        new ZincComponentCompiler(bridgeCompiler, manager, ivyConfiguration, bridgeModule, logger)
      logger.debug(InterfaceUtil.f0(s"Getting $bridgeModule for Scala ${scalaInstance.version}"))
      ivyComponent.getCompiledBridgeJar
    }
  }

  def interfaceProvider(manager: ZincComponentManager,
                        ivyConfiguration: IvyConfiguration,
                        sourcesModule: ModuleID): CompilerBridgeProvider =
    new ZincCompilerBridgeProvider(manager, ivyConfiguration, sourcesModule)

}

/**
 * Component compiler which is able to to retrieve the compiler bridge sources
 * `sourceModule` using Ivy.
 * The compiled classes are cached using the provided component manager according
 * to the actualVersion field of the RawCompiler.
 */
private[inc] class ZincComponentCompiler(
    compiler: RawCompiler,
    manager: ZincComponentManager,
    ivyConfiguration: IvyConfiguration,
    bridgeSources: ModuleID,
    log: Logger
) {
  import sbt.internal.util.{ BufferedLogger, FullLogger }
  private final val ivySbt: IvySbt = new IvySbt(ivyConfiguration)
  private final val buffered = new BufferedLogger(FullLogger(log))

  def getCompiledBridgeJar: File = {
    val jarBinaryName = createBridgeSourcesID(bridgeSources)
    manager.file(jarBinaryName)(IfMissing.define(true, compileAndInstall(jarBinaryName)))
  }

  /**
   * Returns the id for the compiler interface component.
   *
   * The ID contains the following parts:
   *   - The organization, name and revision.
   *   - The bin separator to make clear the jar represents binaries.
   *   - The Scala version for which the compiler interface is meant to.
   *   - The JVM class version.
   *
   * Example: "org.scala-sbt-compiler-bridge-1.0.0-bin_2.11.7__50.0".
   *
   * @param sources The moduleID representing the compiler bridge sources.
   * @return The complete jar identifier for the bridge sources.
   */
  private def createBridgeSourcesID(sources: ModuleID): String = {
    import ZincComponentCompiler.{ binSeparator, javaClassVersion }
    val id = s"${sources.organization}-${sources.name}-${sources.revision}"
    val scalaVersion = compiler.scalaInstance.actualVersion()
    s"$id$binSeparator${scalaVersion}__$javaClassVersion"
  }

  /**
   * Resolves the compiler bridge sources, compiles them and installs the sbt component
   * in the local filesystem to make sure that it's reused the next time is required.
   *
   * @param compilerBridgeId The identifier for the compiler bridge sources.
   */
  private def compileAndInstall(compilerBridgeId: String): Unit = {
    import UnresolvedWarning.unresolvedWarningLines
    val ivyModuleForBridge = wrapDependencyInModule(bridgeSources)
    IO.withTemporaryDirectory { binaryDirectory =>
      val target = new File(binaryDirectory, s"$compilerBridgeId.jar")
      buffered bufferQuietly {
        IO.withTemporaryDirectory { retrieveDirectory =>
          update(ivyModuleForBridge, retrieveDirectory) match {
            case Left(uw) =>
              val mod = bridgeSources.toString
              val unresolvedLines = unresolvedWarningLines.showLines(uw)
              val unretrievedMessage = s"The compiler bridge sources $mod could not be retrieved."
              throw new InvalidComponent(s"$unretrievedMessage\n$unresolvedLines")

            case Right(allArtifacts) =>
              val (srcs, xsbtiJars) = allArtifacts.partition(_.getName.endsWith("-sources.jar"))
              val toCompileID = bridgeSources.name
              AnalyzingCompiler.compileSources(srcs, target, xsbtiJars, toCompileID, compiler, log)
              manager.define(compilerBridgeId, Seq(target))
          }
        }

      }
    }
  }

  /**
   * Returns an ivy module that will wrap and download a given `moduleID`.
   *
   * @param moduleID The `moduleID` that needs to be wrapped in a dummy module to be downloaded.
   */
  private def wrapDependencyInModule(moduleID: ModuleID): ivySbt.Module = {
    def getModule(moduleID: ModuleID, deps: Vector[ModuleID], uo: UpdateOptions): ivySbt.Module = {
      val moduleInfo = ModuleInfo(moduleID.name)
      val componentIvySettings = InlineConfiguration(
        validate = false,
        ivyScala = None,
{'fraction_non_alphanumeric': 0.07653266736485942, 'fraction_numerical': 0.0043502779344235885, 'mean_word_length': 3.4702916816708678, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5329944', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3272, 'n_words': 925}