<commit_before>package coursier.cli

import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.Scanner

import caseapp.core.RemainingArgs
import caseapp.core.help.Help
import caseapp.core.parser.Parser
import coursier.cli.bootstrap.Bootstrap
import coursier.cli.complete.Complete
import coursier.cli.fetch.Fetch
import coursier.cli.get.Get
import coursier.cli.install.{Install, List, Uninstall, Update}
import coursier.cli.jvm.{Java, JavaHome}
import coursier.cli.launch.Launch
import coursier.cli.publish.Publish
import coursier.cli.resolve.Resolve
import coursier.cli.setup.{Setup, SetupOptions}
import coursier.core.Version
import coursier.install.InstallDir
import coursier.launcher.internal.{FileUtil, Windows}
import io.github.alexarchambault.windowsansi.WindowsAnsi
import shapeless._

import scala.util.control.NonFatal

object Coursier extends CommandAppPreA(Parser[LauncherOptions], Help[LauncherOptions], CoursierCommand.parser, CoursierCommand.help) {

  if (System.console() != null && Windows.isWindows)
    try WindowsAnsi.setup()
    catch {
      case NonFatal(e) =>
        val doThrow = java.lang.Boolean.getBoolean("coursier.windows-ansi.throw-exception")
        if (doThrow || java.lang.Boolean.getBoolean("coursier.windows-ansi.verbose"))
          System.err.println(s"Error setting up Windows terminal for ANSI escape codes: $e")
        if (doThrow)
           throw e
    }

  override val appName = "Coursier"
  override val progName =
    if (sys.props.contains("org.graalvm.nativeimage.imagecode")) "cs"
    else "coursier"
  override val appVersion = coursier.util.Properties.version

  private def zshCompletions(): String = {
    var is: InputStream = null
    val b = try {
      is = Thread.currentThread()
        .getContextClassLoader
        .getResource("completions/zsh")
        .openStream()
      FileUtil.readFully(is)
    } finally {
      if (is != null)
        is.close()
    }
    new String(b, StandardCharsets.UTF_8)
  }

  private def runSetup(): Unit = {
    Setup.run(SetupOptions(banner = Some(true)), RemainingArgs(Nil, Nil))

    // https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on/26184535#26184535
    println("Press \"ENTER\" to continue...")
    val scanner = new Scanner(System.in)
    scanner.nextLine()
  }

  private def isInstalledLauncher: Boolean =
    System.getenv(InstallDir.isInstalledLauncherEnvVar) == "true"

  override def main(args: Array[String]): Unit = {

    if (args.nonEmpty)
      super.main(args)<commit_msg>Handle -J-D* options ourselves from native launcher<commit_after>package coursier.cli

import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.Scanner

import caseapp.core.RemainingArgs
import caseapp.core.help.Help
import caseapp.core.parser.Parser
import coursier.cli.bootstrap.Bootstrap
import coursier.cli.complete.Complete
import coursier.cli.fetch.Fetch
import coursier.cli.get.Get
import coursier.cli.install.{Install, List, Uninstall, Update}
import coursier.cli.jvm.{Java, JavaHome}
import coursier.cli.launch.Launch
import coursier.cli.publish.Publish
import coursier.cli.resolve.Resolve
import coursier.cli.setup.{Setup, SetupOptions}
import coursier.core.Version
import coursier.install.InstallDir
import coursier.launcher.internal.{FileUtil, Windows}
import io.github.alexarchambault.windowsansi.WindowsAnsi
import shapeless._

import scala.util.control.NonFatal

object Coursier extends CommandAppPreA(Parser[LauncherOptions], Help[LauncherOptions], CoursierCommand.parser, CoursierCommand.help) {

  val isGraalvmNativeImage = sys.props.contains("org.graalvm.nativeimage.imagecode")

  if (System.console() != null && Windows.isWindows)
    try WindowsAnsi.setup()
    catch {
      case NonFatal(e) =>
        val doThrow = java.lang.Boolean.getBoolean("coursier.windows-ansi.throw-exception")
        if (doThrow || java.lang.Boolean.getBoolean("coursier.windows-ansi.verbose"))
          System.err.println(s"Error setting up Windows terminal for ANSI escape codes: $e")
        if (doThrow)
           throw e
    }

  override val appName = "Coursier"
  override val progName =
    if (isGraalvmNativeImage) "cs"
    else "coursier"
  override val appVersion = coursier.util.Properties.version

  private def zshCompletions(): String = {
    var is: InputStream = null
    val b = try {
      is = Thread.currentThread()
        .getContextClassLoader
        .getResource("completions/zsh")
        .openStream()
      FileUtil.readFully(is)
    } finally {
      if (is != null)
        is.close()
    }
    new String(b, StandardCharsets.UTF_8)
  }

  private def runSetup(): Unit = {
    Setup.run(SetupOptions(banner = Some(true)), RemainingArgs(Nil, Nil))

    // https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on/26184535#26184535
    println("Press \"ENTER\" to continue...")
    val scanner = new Scanner(System.in)
    scanner.nextLine()
  }

  private def isInstalledLauncher: Boolean =
    System.getenv(InstallDir.isInstalledLauncherEnvVar) == "true"

  override def main(args: Array[String]): Unit = {

    val csArgs =
      if (isGraalvmNativeImage) {
        // process -J* args ourselves

        val (jvmArgs, csArgs0) = args.partition(_.startsWith("-J"))

        for (jvmArg <- jvmArgs) {
          val arg = jvmArg.stripPrefix("-J")
          if (arg.startsWith("-D"))
            arg.stripPrefix("-D").split("=", 2) match {
              case Array(k) => System.setProperty(k, "")
              case Array(k, v) => System.setProperty(k, v)
            }
          else
            System.err.println(s"Warning: ignoring unhandled -J argument: $jvmArg")
        }

        csArgs0
      } else
        args

    if (csArgs.nonEmpty)
      super.main(csArgs)
{'fraction_non_alphanumeric': 0.11409510776599384, 'fraction_numerical': 0.009066028053369826, 'mean_word_length': 3.4907834101382487, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '932065', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1837, 'n_words': 371}