From 0101fe0e1dee67623ae706cb5d7e17ff67eda8c4 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Thu, 11 Apr 2024 13:00:56 +0200 Subject: [PATCH] update ArchUnit-Examples dependencies on release We automatically keep the code of the separate ArchUnit-Examples repository in sync with the `archunit-example` module in this repository. However, sometimes an example makes an update of the dependencies necessary (as seen in 1d1a990eea6f7babb29847ad3b42d37c56f81f47). Up to now this made it necessary to manually update the dependencies in ArchUnit-Examples, or the release will fail when testing the updated ArchUnit-Examples. We now keep the (production) dependencies of ArchUnit-Examples automatically in sync with the `archunit-example` module. Since ArchUnit-Examples should be purely driven from this repository, I think it's an okay compromise to simply keep a static template here and fully overwrite the build file. Changes to the root build file should always be controlled from this repository. Signed-off-by: Peter Gafert --- .../release/archunit-examples-utils.gradle | 37 ++++++++++++++++++- .../archunit.java-examples-conventions.gradle | 20 ++++++---- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/build-steps/release/archunit-examples-utils.gradle b/build-steps/release/archunit-examples-utils.gradle index 0f4ce46bc8..6c8edc1d82 100644 --- a/build-steps/release/archunit-examples-utils.gradle +++ b/build-steps/release/archunit-examples-utils.gradle @@ -1,12 +1,47 @@ +import groovy.transform.Field + ext.archunitExamplesGitRepo = 'TNG/ArchUnit-Examples.git' ext.updateArchUnitExampleVersion = { File archUnitExampleDir -> fileTree(archUnitExampleDir) { include '**/build.gradle' - }.each {File buildFile -> + }.each { File buildFile -> buildFile.text = buildFile.text.replaceAll(/(com\.tngtech\.archunit:archunit[^:]*:)[\w.-]*/, "\$1${version}") } } ext.updateArchUnitExampleSources = { File targetArchUnitExampleDir -> + updateArchUnitExampleDependencies(targetArchUnitExampleDir) + updateArchUnitExampleJavaSources(targetArchUnitExampleDir) +} + +@Field +String archUnitExamplesRootBuildFileContent = """ +subprojects { + apply plugin: 'java-library' + + repositories { + mavenCentral() + } + + dependencies { + // These are the 'production' dependencies of the Demo src/main/java files -> just for Demo purposes, otherwise irrelevant +#{dependencies} + } +} +""".trim() + +private void updateArchUnitExampleDependencies(File targetArchUnitExampleDir) { + def buildFile = new File(targetArchUnitExampleDir, 'build.gradle') + + List> sortedDependencies = archUnitExamplesMainDependencies.collect() + .sort { first, second -> first.group <=> second.group ?: first.name <=> second.name } + def dependencyIndent = ' ' * 8 + List dependencyLines = sortedDependencies + .collect { "${dependencyIndent}implementation '${it.group}:${it.name}:${it.version}'".toString() } + + buildFile.text = archUnitExamplesRootBuildFileContent.replace('#{dependencies}', dependencyLines.join('\n')) // always Unix line separator +} + +private List updateArchUnitExampleJavaSources(File targetArchUnitExampleDir) { ['example-plain', 'example-junit4', 'example-junit5'].each { exampleFolder -> def targetSource = new File(new File(targetArchUnitExampleDir, exampleFolder), 'src') targetSource.deleteDir() diff --git a/buildSrc/src/main/groovy/archunit.java-examples-conventions.gradle b/buildSrc/src/main/groovy/archunit.java-examples-conventions.gradle index 0ae6b384a7..1d00e5ea80 100644 --- a/buildSrc/src/main/groovy/archunit.java-examples-conventions.gradle +++ b/buildSrc/src/main/groovy/archunit.java-examples-conventions.gradle @@ -10,16 +10,20 @@ archUnitTest { providesTestJar = true } +rootProject.ext.archUnitExamplesMainDependencies = [ + dependency.jodaTime, + dependency.javaxAnnotationApi, + dependency.jakartaInject, + dependency.jakartaAnnotations, + dependency.springBeans, + dependency.guice, + dependency.geronimoEjb, + dependency.geronimoJpa +] + dependencies { // `api` dependencies so we can access them within `archunit-integration-test` - api dependency.jodaTime - api dependency.javaxAnnotationApi - api dependency.jakartaInject - api dependency.jakartaAnnotations - api dependency.springBeans - api dependency.guice - api dependency.geronimoEjb - api dependency.geronimoJpa + archUnitExamplesMainDependencies.each { api it } testImplementation project(path: ':archunit') } \ No newline at end of file