-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support version catalog references in BundleHandler and DependenciesH…
…andler. (#1006)
- Loading branch information
1 parent
7e710df
commit 017445e
Showing
14 changed files
with
276 additions
and
95 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/functionalTest/groovy/com/autonomousapps/jvm/VersionCatalogSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.autonomousapps.jvm | ||
|
||
import com.autonomousapps.jvm.projects.VersionCatalogProject | ||
import com.autonomousapps.utils.Colors | ||
|
||
import static com.autonomousapps.utils.Runner.build | ||
import static com.google.common.truth.Truth.assertThat | ||
|
||
final class VersionCatalogSpec extends AbstractJvmSpec { | ||
|
||
def "version catalogs work (#gradleVersion)"() { | ||
given: | ||
def project = new VersionCatalogProject() | ||
gradleProject = project.gradleProject | ||
when: | ||
build(gradleVersion, gradleProject.rootDir, 'buildHealth', '-Pdependency.analysis.print.build.health=true') | ||
then: | ||
assertThat(project.actualProjectAdvice()).containsExactlyElementsIn(project.expectedProjectAdvice) | ||
when: 'We ask about the reason using the version catalog alias' | ||
def result = build(gradleVersion, gradleProject.rootDir, 'lib:reason', '--id', 'libs.commonCollections') | ||
then: 'It works' | ||
assertThat(Colors.decolorize(result.output)).contains( | ||
'''\ | ||
You asked about the dependency 'org.apache.commons:commons-collections4:4.4 (libs.commonCollections)'. | ||
You have been advised to remove this dependency from 'implementation'.'''.stripIndent() | ||
) | ||
where: | ||
gradleVersion << gradleVersions() | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/functionalTest/groovy/com/autonomousapps/jvm/projects/VersionCatalogProject.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.autonomousapps.jvm.projects | ||
|
||
import com.autonomousapps.AbstractProject | ||
import com.autonomousapps.kit.GradleProject | ||
import com.autonomousapps.kit.Source | ||
import com.autonomousapps.kit.SourceType | ||
import com.autonomousapps.kit.gradle.Plugin | ||
import com.autonomousapps.model.Advice | ||
import com.autonomousapps.model.ProjectAdvice | ||
|
||
import static com.autonomousapps.AdviceHelper.* | ||
import static com.autonomousapps.kit.gradle.Dependency.versionCatalog | ||
|
||
final class VersionCatalogProject extends AbstractProject { | ||
|
||
final GradleProject gradleProject | ||
|
||
VersionCatalogProject() { | ||
this.gradleProject = build() | ||
} | ||
|
||
private GradleProject build() { | ||
def builder = newGradleProjectBuilder() | ||
builder.withRootProject { root -> | ||
root.withFile('gradle/libs.versions.toml', '''\ | ||
[versions] | ||
commonCollections = "4.4" | ||
[libraries] | ||
commonCollections = { module = "org.apache.commons:commons-collections4", version.ref = "commonCollections"} | ||
'''.stripIndent()) | ||
} | ||
|
||
builder.withSubproject('lib') { c -> | ||
c.sources = sources | ||
c.withBuildScript { bs -> | ||
bs.plugins = [Plugin.javaLibraryPlugin] | ||
bs.dependencies = [ | ||
versionCatalog('implementation', 'libs.commonCollections') | ||
] | ||
} | ||
} | ||
|
||
def project = builder.build() | ||
project.writer().write() | ||
return project | ||
} | ||
|
||
private sources = [ | ||
new Source( | ||
SourceType.JAVA, 'Library', 'com/example/library', | ||
"""\ | ||
package com.example.library; | ||
public class Library { | ||
} | ||
""".stripIndent() | ||
) | ||
] | ||
|
||
Set<ProjectAdvice> actualProjectAdvice() { | ||
return actualProjectAdvice(gradleProject) | ||
} | ||
|
||
private Set<Advice> libAdvice = [ | ||
Advice.ofRemove( | ||
moduleCoordinates('org.apache.commons:commons-collections4', '4.4'), | ||
'implementation' | ||
) | ||
] | ||
|
||
final Set<ProjectAdvice> expectedProjectAdvice = [ | ||
projectAdviceForDependencies(':lib', libAdvice), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/com/autonomousapps/extension/BundleHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.autonomousapps.extension | ||
|
||
import org.gradle.api.Named | ||
import org.gradle.api.artifacts.MinimalExternalModuleDependency | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.provider.SetProperty | ||
import org.gradle.kotlin.dsl.setProperty | ||
import org.intellij.lang.annotations.Language | ||
import javax.inject.Inject | ||
|
||
/** | ||
* ``` | ||
* bundle("kotlin-stdlib") { | ||
* // 0 (Optional): Specify the primary entry point that the user is "supposed" to declare. | ||
* primary("org.something:primary-entry-point") | ||
* | ||
* // 1: include all in group as a single logical dependency | ||
* includeGroup("org.jetbrains.kotlin") | ||
* | ||
* // 2: include all supplied dependencies as a single logical dependency | ||
* includeDependency("org.jetbrains.kotlin:kotlin-stdlib") | ||
* includeDependency("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
* | ||
* // 3: include all dependencies that match the regex as a single logical dependency | ||
* include(".*kotlin-stdlib.*") | ||
* } | ||
* ``` | ||
*/ | ||
abstract class BundleHandler @Inject constructor( | ||
private val name: String, | ||
objects: ObjectFactory, | ||
) : Named { | ||
|
||
override fun getName(): String = name | ||
|
||
val primary: Property<String> = objects.property(String::class.java).convention("") | ||
val includes: SetProperty<Regex> = objects.setProperty<Regex>().convention(emptySet()) | ||
|
||
fun primary(identifier: String) { | ||
primary.set(identifier) | ||
primary.disallowChanges() | ||
} | ||
|
||
fun primary(module: Provider<MinimalExternalModuleDependency>) { | ||
primary(module.identifier()) | ||
} | ||
|
||
fun includeGroup(group: String) { | ||
include("^$group:.*") | ||
} | ||
|
||
fun includeGroup(module: Provider<MinimalExternalModuleDependency>) { | ||
includeGroup(module.group()) | ||
} | ||
|
||
fun includeDependency(identifier: String) { | ||
include("^$identifier\$") | ||
} | ||
|
||
fun includeDependency(module: Provider<MinimalExternalModuleDependency>) { | ||
includeDependency(module.identifier()) | ||
} | ||
|
||
fun include(@Language("RegExp") regex: String) { | ||
include(regex.toRegex()) | ||
} | ||
|
||
fun include(regex: Regex) { | ||
includes.add(regex) | ||
} | ||
|
||
private fun Provider<MinimalExternalModuleDependency>.identifier(): String { | ||
return map { "${it.group}:${it.name}" }.get() | ||
} | ||
|
||
private fun Provider<MinimalExternalModuleDependency>.group(): String { | ||
return map { | ||
// group is in fact @Nullable | ||
@Suppress("USELESS_ELVIS") | ||
it.group ?: error("No group for $it") | ||
}.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.