Skip to content

Commit

Permalink
feat: add a way to use BOM in dependencyOverrides
Browse files Browse the repository at this point in the history
See the context of this feature at heremaps#9

Signed-off-by: Gaël Jourdan-Weil <[email protected]>
  • Loading branch information
gaeljw committed Feb 4, 2024
1 parent 8ed583b commit 1f31460
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions plugin/src/main/scala/com/here/bom/Bom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ object Bom {
}
}

def dependencies(bomArtifact: ModuleID): Def.Setting[Seq[ModuleID]] = {
read(bomArtifact)(bom => bom.bomDependencies)
}

implicit def addBomSyntax(dep: OrganizationArtifactName): BomSyntax = new BomSyntax(dep)

class BomSyntax(dep: OrganizationArtifactName) {
Expand Down Expand Up @@ -85,4 +89,9 @@ object Bom {

trait Bom {
def version(dependency: OrganizationArtifactName): String

/**
* List of all dependencies declared in the BOM
*/
def bomDependencies: Seq[ModuleID]
}
7 changes: 7 additions & 0 deletions plugin/src/main/scala/com/here/bom/internal/BomReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
sys.error(s"Version for ${normalized.group}.${normalized.name} not found in BOM")
)
}

override def bomDependencies: Seq[ModuleID] = {
effectiveVersions.map { case ((group, name), version) =>
ModuleID(group, name, version)
}.toSeq
}

}
}

Expand Down
23 changes: 16 additions & 7 deletions plugin/src/test/scala/com/here/bom/internal/BomReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {

val dependency = "com.test" %% "test_module" % bom
dependency.revision should be("1.0")

bom.bomDependencies shouldBe Seq("com.test" % "test_module_2.12" % "1.0")
}

"BomReader" should "assemble the BOM correctly with parent" in {
Expand All @@ -55,13 +57,15 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {

(pomLocatorMock.getPomFile _)
.expects(where((moduleId: NormalizedArtifact) => {
moduleId.group.equals("com.test") && moduleId.name.equals("test_bom_parent") && moduleId.version.equals("1.0")
moduleId.group.equals("com.test") && moduleId.name
.equals("test_bom_parent") && moduleId.version.equals("1.0")
}))
.returns(Some(createMockPomFile("com.test", "test_bom_parent", "1.0", false)))

(pomLocatorMock.getPomFile _)
.expects(where((moduleId: NormalizedArtifact) => {
moduleId.group.equals("com.test") && moduleId.name.equals("test_bom") && moduleId.version.equals("1.0")
moduleId.group.equals("com.test") && moduleId.name.equals("test_bom") && moduleId.version
.equals("1.0")
}))
.returns(Some(createMockPomFile("com.test", "test_bom", "1.0", true)))

Expand All @@ -70,9 +74,16 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {

val dependency = "com.test" %% "test_module" % bom
dependency.revision should be("1.0")

bom.bomDependencies shouldBe Seq("com.test" % "test_module_2.12" % "1.0")
}

private def createMockPomFile(groupId: String, artifactId: String, version: String, addParent: Boolean): File = {
private def createMockPomFile(
groupId: String,
artifactId: String,
version: String,
addParent: Boolean
): File = {
val tempFile = File.createTempFile("mock", ".xml")

val pomContent =
Expand All @@ -81,8 +92,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {
| <groupId>$groupId</groupId>
| <artifactId>$artifactId</artifactId>
| <version>$version</version>
| ${
if (addParent)
| ${if (addParent)
s"""
| <parent>
| <groupId>$groupId</groupId>
Expand All @@ -101,8 +111,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {
| | </dependency>
| | </dependencies>
|</dependencyManagement>
|""".stripMargin
}
|""".stripMargin}
|</project >
| """.stripMargin

Expand Down

0 comments on commit 1f31460

Please sign in to comment.