<commit_before>package dotty.tools.scripting

import java.io.File
import java.nio.file.{Path, Paths}
import dotty.tools.dotc.config.Properties.isWin 

/** Main entry point to the Scripting execution engine */
object Main:
  /** All arguments before -script <target_script> are compiler arguments.
      All arguments afterwards are script arguments.*/
  private def distinguishArgs(args: Array[String]): (Array[String], File, Array[String], Boolean, Boolean) =
    val (leftArgs, rest) = args.splitAt(args.indexOf("-script"))
    assert(rest.size >= 2,s"internal error: rest == Array(${rest.mkString(",")})")

    val file = File(rest(1))
    val scriptArgs = rest.drop(2)
    var saveJar = false
    var invokeFlag = true // by default, script main method is invoked
    val compilerArgs = leftArgs.filter {
      case "-save" | "-savecompiled" =>
        saveJar = true
        false
      case "-compile-only" =>
        invokeFlag = false // no call to script main method
        false
      case _ =>
        true
    }
    (compilerArgs, file, scriptArgs, saveJar, invokeFlag)
  end distinguishArgs

  def main(args: Array[String]): Unit =
    val (compilerArgs, scriptFile, scriptArgs, saveJar, invokeFlag) = distinguishArgs(args)
    val driver = ScriptingDriver(compilerArgs, scriptFile, scriptArgs)
    try driver.compileAndRun { (outDir:Path, classpathEntries:Seq[Path], mainClass: String) =>
      if saveJar then
        // write a standalone jar to the script parent directory
        writeJarfile(outDir, scriptFile, scriptArgs, classpathEntries, mainClass)
      invokeFlag
    }
    catch
      case ScriptingException(msg) =>
        println(s"Error: $msg")
        sys.exit(1)

      case e: java.lang.reflect.InvocationTargetException =>
        throw e.getCause

  private def writeJarfile(outDir: Path, scriptFile: File, scriptArgs:Array[String],
      classpathEntries:Seq[Path], mainClassName: String): Unit =

    //val javaClasspath = sys.props("java.class.path")
    //val runtimeClasspath = s"${classpath}$pathsep$javaClasspath"

    val jarTargetDir: Path = Option(scriptFile.toPath.toAbsolutePath.getParent) match {
      case None => sys.error(s"no parent directory for script file [$scriptFile]")
      case Some(parent) => parent
    }

    def scriptBasename = scriptFile.getName.takeWhile(_!='.')
    val jarPath = s"$jarTargetDir/$scriptBasename.jar"

    //val cpPaths = runtimeClasspath.split(pathsep).map(_.toUrl)
    val cpPaths = classpathEntries.map { _.toString.toUrl }

    import java.util.jar.Attributes.Name
    val cpString:String = cpPaths.distinct.mkString(" ")
    val manifestAttributes:Seq[(Name, String)] = Seq(
      (Name.MANIFEST_VERSION, "1.0"),
      (Name.MAIN_CLASS, mainClassName),
      (Name.CLASS_PATH, cpString),
    )
    import dotty.tools.io.{Jar, Directory}
    val jar = new Jar(jarPath)
    val writer = jar.jarWriter(manifestAttributes:_*)
    try
      writer.writeAllFrom(Directory(outDir))
    finally
      writer.close()
  end writeJarfile

  def pathsep = sys.props("path.separator")

  extension(path: String) {
    // Normalize path separator, convert relative path to absolute
    def norm: String =
      path.replace('\\', '/') match {
        case s if s.secondChar == ":" => s
        case s if s.startsWith("./") => s.drop(2)<commit_msg>removed obsolete comments<commit_after>package dotty.tools.scripting

import java.io.File
import java.nio.file.{Path, Paths}
import dotty.tools.dotc.config.Properties.isWin 

/** Main entry point to the Scripting execution engine */
object Main:
  /** All arguments before -script <target_script> are compiler arguments.
      All arguments afterwards are script arguments.*/
  private def distinguishArgs(args: Array[String]): (Array[String], File, Array[String], Boolean, Boolean) =
    val (leftArgs, rest) = args.splitAt(args.indexOf("-script"))
    assert(rest.size >= 2,s"internal error: rest == Array(${rest.mkString(",")})")

    val file = File(rest(1))
    val scriptArgs = rest.drop(2)
    var saveJar = false
    var invokeFlag = true // by default, script main method is invoked
    val compilerArgs = leftArgs.filter {
      case "-save" | "-savecompiled" =>
        saveJar = true
        false
      case "-compile-only" =>
        invokeFlag = false // no call to script main method
        false
      case _ =>
        true
    }
    (compilerArgs, file, scriptArgs, saveJar, invokeFlag)
  end distinguishArgs

  def main(args: Array[String]): Unit =
    val (compilerArgs, scriptFile, scriptArgs, saveJar, invokeFlag) = distinguishArgs(args)
    val driver = ScriptingDriver(compilerArgs, scriptFile, scriptArgs)
    try driver.compileAndRun { (outDir:Path, classpathEntries:Seq[Path], mainClass: String) =>
      if saveJar then
        // write a standalone jar to the script parent directory
        writeJarfile(outDir, scriptFile, scriptArgs, classpathEntries, mainClass)
      invokeFlag
    }
    catch
      case ScriptingException(msg) =>
        println(s"Error: $msg")
        sys.exit(1)

      case e: java.lang.reflect.InvocationTargetException =>
        throw e.getCause

  private def writeJarfile(outDir: Path, scriptFile: File, scriptArgs:Array[String],
      classpathEntries:Seq[Path], mainClassName: String): Unit =

    val jarTargetDir: Path = Option(scriptFile.toPath.toAbsolutePath.getParent) match {
      case None => sys.error(s"no parent directory for script file [$scriptFile]")
      case Some(parent) => parent
    }

    def scriptBasename = scriptFile.getName.takeWhile(_!='.')
    val jarPath = s"$jarTargetDir/$scriptBasename.jar"

    val cpPaths = classpathEntries.map { _.toString.toUrl }

    import java.util.jar.Attributes.Name
    val cpString:String = cpPaths.distinct.mkString(" ")
    val manifestAttributes:Seq[(Name, String)] = Seq(
      (Name.MANIFEST_VERSION, "1.0"),
      (Name.MAIN_CLASS, mainClassName),
      (Name.CLASS_PATH, cpString),
    )
    import dotty.tools.io.{Jar, Directory}
    val jar = new Jar(jarPath)
    val writer = jar.jarWriter(manifestAttributes:_*)
    try
      writer.writeAllFrom(Directory(outDir))
    finally
      writer.close()
  end writeJarfile

  def pathsep = sys.props("path.separator")

  extension(path: String) {
    // Normalize path separator, convert relative path to absolute
    def norm: String =
      path.replace('\\', '/') match {
        case s if s.secondChar == ":" => s
        case s if s.startsWith("./") => s.drop(2)
{'fraction_non_alphanumeric': 0.1180469715698393, 'fraction_numerical': 0.0021631644004944375, 'mean_word_length': 3.2867549668874174, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 27, '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': '2549458', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1897, 'n_words': 504}