sbt-assembly icon indicating copy to clipboard operation
sbt-assembly copied to clipboard

Support to convert cross-versioned ModuleID to ModuleCoordinate

Open cryeo opened this issue 2 years ago • 2 comments

Resolve #403

Support to convert cross-versioned ModuleID to ModuleCoordinate.

Background

  • ShadePattern.inLibrary extension method provides an easy way to add sbt-librarymanagement's ModuleID as jarjar-abrams's ModuleCoordinate to ShadeRule.
  • However, current conversion from ModuleID to ModuleCoordinate does not support for cross-versioned ModuleID.

Consideration

  • Should be completed on sbt-assembly plugin layer.
    • This support should essentially be provided in sbt-assembly plugin, so it is not good to modify jarjar-abrams.
  • Should not break current syntax.
    • Introduce wrapper class AssemblyShadeRule for ShadeRule without modifying jarjar-abrams.
    • Provide implicit conversion for compatibility.

cryeo avatar Jul 10 '23 11:07 cryeo

alternatively if you don't wan't for this PR to get published:

import com.eed3si9n.jarjarabrams
import sbt.librarymanagement.{CrossVersion, ScalaModuleInfo}

implicit class RichShadePattern(pattern: jarjarabrams.ShadePattern) {
    def inLibraryWithScalaModuleInfo(scalaModuleInfo: Option[ScalaModuleInfo])(moduleIds: ModuleID*): jarjarabrams.ShadeRule = {
      val moduleIdsWithCrossVersion = 
        moduleIds.map(module =>
          (for {
            sm <- scalaModuleInfo
            f <- CrossVersion(module, scalaModuleInfo)
          } yield module.withName(f(module.name))).getOrElse(module)
        )

      inLibraryBase(moduleIdsWithCrossVersion: _*)
    }

    private def inLibraryBase(moduleId: ModuleID*): jarjarabrams.ShadeRule =
      pattern.inModuleCoordinates(
        moduleId.toVector
          .map(m => jarjarabrams.ModuleCoordinate(m.organization, m.name, m.revision)): _*
      )
  }

usage:

ShadeRule.rename("cats.kernel.**" -> s"new_cats.kernel.@1").inLibraryWithScalaModuleInfo(scalaModuleInfo.value)(
    "org.typelevel" %% "spire"  % "0.17.0"
)

MasseGuillaume avatar Nov 18 '23 21:11 MasseGuillaume

Sorry about the delayed response. Given that I control Jar Jar Abrams as well, I feel like a better solution would be to capture whatever information we need to about ModuleID at Jar Jar Abrams level, if current ModuleCoordinate is insufficient. % vs %% for instance is expressed by crossVersion here - https://github.com/sbt/librarymanagement/blob/54011d5e74335bab229bc3f6532b6a1ca6d0d5cd/core/src/main/contraband-scala/sbt/librarymanagement/ModuleID.scala

eed3si9n avatar Nov 26 '23 00:11 eed3si9n