<commit_before>package coursier.launcher

import java.io.InputStream
import java.nio.charset.{Charset, StandardCharsets}

import dataclass._

import scala.io.{Codec, Source}

@data class Preamble(
  kind: Preamble.Kind = Preamble.Kind.Sh,
  javaOpts: Seq[String] = Nil,
  jarPath: Option[String] = None,
  command: Option[String] = None,
  extraEnv: Map[String, String] = Map.empty,
  @since
  jvmOptionFile: Option[String] = None
) {

  def withOsKind(isWindows: Boolean): Preamble =
    withKind(if (isWindows) Preamble.Kind.Bat else Preamble.Kind.Sh)

  def callsItself(isWindows: Boolean): Preamble = {
    val updatedJarPath =
      if (isWindows) "%~dpnx0"
      else "$0"
    withJarPath(updatedJarPath)
  }

  def withJarPath(path: String): Preamble =
    withJarPath(Some(path))
  def withCommand(command: String): Preamble =
    withCommand(Some(command))

  def addExtraEnvVar(key: String, value: String): Preamble =
    withExtraEnv(extraEnv + (key -> value))

  def sh: Array[Byte] = {
    val setVars = extraEnv
      .toVector
      .sorted
      // escaping possibly a bit loose :-|
      .map { case (k, v) => s"""export $k="$v"""" }
    val lines = command match {
      case None =>
        val javaCmd = Seq("java") ++
          // escaping possibly a bit loose :-|
          javaOpts.map(s => "'" + s.replace("'", "\\'") + "'") ++
          jvmOptionFile.toSeq.map(_ => "${extra_jvm_opts[@]}") ++
          Seq("$JAVA_OPTS", "\"$@\"")

        val sh = jvmOptionFile.fold("sh")(_ => "bash")

        Seq(s"#!/usr/bin/env $sh") ++
          setVars ++
          Seq(Preamble.shArgsPartitioner(jarPath.getOrElse("$0"))) ++
          jvmOptionFile.toSeq.map(f => Preamble.bashJvmOptFile(f)) ++
          Seq("exec " + javaCmd.mkString(" "))

      case Some(c) =>
        Seq("#!/usr/bin/env sh") ++
          setVars ++
          Seq(
            "exec \"" + c + "\" \"$@\""
          )
    }

    lines.mkString("", "\n", "\n").getBytes(StandardCharsets.UTF_8)
  }<commit_msg>fix: make sure JAVA_HOME is used in bootstrapped file (#2441)<commit_after>package coursier.launcher

import java.io.InputStream
import java.nio.charset.{Charset, StandardCharsets}

import dataclass._

import scala.io.{Codec, Source}

@data class Preamble(
  kind: Preamble.Kind = Preamble.Kind.Sh,
  javaOpts: Seq[String] = Nil,
  jarPath: Option[String] = None,
  command: Option[String] = None,
  extraEnv: Map[String, String] = Map.empty,
  @since
  jvmOptionFile: Option[String] = None
) {

  def withOsKind(isWindows: Boolean): Preamble =
    withKind(if (isWindows) Preamble.Kind.Bat else Preamble.Kind.Sh)

  def callsItself(isWindows: Boolean): Preamble = {
    val updatedJarPath =
      if (isWindows) "%~dpnx0"
      else "$0"
    withJarPath(updatedJarPath)
  }

  def withJarPath(path: String): Preamble =
    withJarPath(Some(path))
  def withCommand(command: String): Preamble =
    withCommand(Some(command))

  def addExtraEnvVar(key: String, value: String): Preamble =
    withExtraEnv(extraEnv + (key -> value))

  def sh: Array[Byte] = {
    val setVars = extraEnv
      .toVector
      .sorted
      // escaping possibly a bit loose :-|
      .map { case (k, v) => s"""export $k="$v"""" }

    val lines = command match {
      case None =>
        val setJavaCmd =
          """[ -x "$JAVA_HOME/bin/java" ] && JAVA_CMD="$JAVA_HOME/bin/java" || JAVA_CMD=java"""

        val javaCmd = Seq("$JAVA_CMD") ++
          // escaping possibly a bit loose :-|
          javaOpts.map(s => "'" + s.replace("'", "\\'") + "'") ++
          jvmOptionFile.toSeq.map(_ => "${extra_jvm_opts[@]}") ++
          Seq("$JAVA_OPTS", "\"$@\"")

        val sh = jvmOptionFile.fold("sh")(_ => "bash")

        Seq(s"#!/usr/bin/env $sh") ++
          setVars ++
          Seq(Preamble.shArgsPartitioner(jarPath.getOrElse("$0"))) ++
          jvmOptionFile.toSeq.map(f => Preamble.bashJvmOptFile(f)) ++
          Seq(setJavaCmd) ++
          Seq("exec " + javaCmd.mkString(" "))

      case Some(c) =>
        Seq("#!/usr/bin/env sh") ++
          setVars ++
          Seq(
            "exec \"" + c + "\" \"$@\""
          )
    }

    lines.mkString("", "\n", "\n").getBytes(StandardCharsets.UTF_8)
  }
{'fraction_non_alphanumeric': 0.16940671908506075, 'fraction_numerical': 0.0028591851322373124, 'mean_word_length': 2.6824561403508773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '2369388', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1476, 'n_words': 288}