<commit_before>package coursier.cli.params

import cats.data.{NonEmptyList, Validated, ValidatedNel}
import cats.implicits._
import coursier.cli.options.DependencyOptions
import coursier.cli.util.DeprecatedModuleRequirements0
import coursier.core._
import coursier.install.ScalaPlatform
import coursier.parse.{DependencyParser, JavaOrScalaDependency, JavaOrScalaModule, ModuleParser}

import scala.io.Source

final case class DependencyParams(
  exclude: Set[JavaOrScalaModule],
  perModuleExclude: Map[JavaOrScalaModule, Set[JavaOrScalaModule]], // FIXME key should be Module
  intransitiveDependencies: Seq[(JavaOrScalaDependency, Map[String, String])],
  sbtPluginDependencies: Seq[(JavaOrScalaDependency, Map[String, String])],
  platformOpt: Option[ScalaPlatform]
) {
  def native: Boolean =
    platformOpt match {
      case Some(ScalaPlatform.Native) => true
      case _                          => false
    }
}

object DependencyParams {
  def apply(
    options: DependencyOptions,
    forcedScalaVersionOpt: Option[String]
  ): ValidatedNel[String, DependencyParams] = {

    val excludeV =
      ModuleParser.javaOrScalaModules(options.exclude).either match {
        case Left(errors) =>
          Validated.invalidNel(
            s"Cannot parse excluded modules:" + System.lineSeparator() +
              errors
                .map("  " + _)
                .mkString(System.lineSeparator())
          )

        case Right(excludes0) =>
          val (excludesNoAttr, excludesWithAttr) = excludes0.partition(_.attributes.isEmpty)

          if (excludesWithAttr.isEmpty)
            Validated.validNel(
              excludesNoAttr
                .toSet
            )
          else
            Validated.invalidNel(
              s"Excluded modules with attributes not supported:" + System.lineSeparator() +
                excludesWithAttr
                  .map("  " + _)
                  .mkString(System.lineSeparator())
            )
      }

    val perModuleExcludeV: ValidatedNel[String, Map[JavaOrScalaModule, Set[JavaOrScalaModule]]] =
      if (options.localExcludeFile.isEmpty)
        Validated.validNel(Map.empty[JavaOrScalaModule, Set[JavaOrScalaModule]])
      else {

        // meh, I/O

        val source = Source.fromFile(options.localExcludeFile) // default codec...
        val lines =
          try source.mkString.split("\n")
          finally source.close()

        lines
          .toList
          .traverse { str =>
            val parent_and_child = str.split("--")
            if (parent_and_child.length != 2)
              Validated.invalidNel(s"Failed to parse $str")
            else {
              val child_org_name = parent_and_child(1).split(":")
              if (child_org_name.length != 2)
                Validated.invalidNel(s"Failed to parse $child_org_name")
              else
                Validated.fromEither(
                  ModuleParser.javaOrScalaModule(parent_and_child(0)).left.map(NonEmptyList.one)
                ).map { from =>
                  // accept scala modules too?
                  val mod0 = Module(
                    Organization(child_org_name(0)),
                    ModuleName(child_org_name(1)),
                    Map()
                  )
                  val mod: JavaOrScalaModule = JavaOrScalaModule.JavaModule(mod0)
                  (from, mod)
                }
            }
          }<commit_msg>Fix excludes error for invalid child name (#2387)<commit_after>package coursier.cli.params

import cats.data.{NonEmptyList, Validated, ValidatedNel}
import cats.implicits._
import coursier.cli.options.DependencyOptions
import coursier.cli.util.DeprecatedModuleRequirements0
import coursier.core._
import coursier.install.ScalaPlatform
import coursier.parse.{DependencyParser, JavaOrScalaDependency, JavaOrScalaModule, ModuleParser}

import scala.io.Source

final case class DependencyParams(
  exclude: Set[JavaOrScalaModule],
  perModuleExclude: Map[JavaOrScalaModule, Set[JavaOrScalaModule]], // FIXME key should be Module
  intransitiveDependencies: Seq[(JavaOrScalaDependency, Map[String, String])],
  sbtPluginDependencies: Seq[(JavaOrScalaDependency, Map[String, String])],
  platformOpt: Option[ScalaPlatform]
) {
  def native: Boolean =
    platformOpt match {
      case Some(ScalaPlatform.Native) => true
      case _                          => false
    }
}

object DependencyParams {
  def apply(
    options: DependencyOptions,
    forcedScalaVersionOpt: Option[String]
  ): ValidatedNel[String, DependencyParams] = {

    val excludeV =
      ModuleParser.javaOrScalaModules(options.exclude).either match {
        case Left(errors) =>
          Validated.invalidNel(
            s"Cannot parse excluded modules:" + System.lineSeparator() +
              errors
                .map("  " + _)
                .mkString(System.lineSeparator())
          )

        case Right(excludes0) =>
          val (excludesNoAttr, excludesWithAttr) = excludes0.partition(_.attributes.isEmpty)

          if (excludesWithAttr.isEmpty)
            Validated.validNel(
              excludesNoAttr
                .toSet
            )
          else
            Validated.invalidNel(
              s"Excluded modules with attributes not supported:" + System.lineSeparator() +
                excludesWithAttr
                  .map("  " + _)
                  .mkString(System.lineSeparator())
            )
      }

    val perModuleExcludeV: ValidatedNel[String, Map[JavaOrScalaModule, Set[JavaOrScalaModule]]] =
      if (options.localExcludeFile.isEmpty)
        Validated.validNel(Map.empty[JavaOrScalaModule, Set[JavaOrScalaModule]])
      else {

        // meh, I/O

        val source = Source.fromFile(options.localExcludeFile) // default codec...
        val lines =
          try source.mkString.split("\n")
          finally source.close()

        lines
          .toList
          .traverse { str =>
            val parent_and_child = str.split("--")
            if (parent_and_child.length != 2)
              Validated.invalidNel(s"Failed to parse $str")
            else {
              val child          = parent_and_child(1)
              val child_org_name = child.split(":")
              if (child_org_name.length != 2)
                Validated.invalidNel(s"Failed to parse $child")
              else
                Validated.fromEither(
                  ModuleParser.javaOrScalaModule(parent_and_child(0)).left.map(NonEmptyList.one)
                ).map { from =>
                  // accept scala modules too?
                  val mod0 = Module(
                    Organization(child_org_name(0)),
                    ModuleName(child_org_name(1)),
                    Map()
                  )
                  val mod: JavaOrScalaModule = JavaOrScalaModule.JavaModule(mod0)
                  (from, mod)
                }
            }
          }
{'fraction_non_alphanumeric': 0.0918840579710145, 'fraction_numerical': 0.0037681159420289855, 'mean_word_length': 2.0467991169977924, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793630', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1886, 'n_words': 358}