diff --git a/application/build.gradle.kts b/application/build.gradle.kts deleted file mode 100644 index 0732656..0000000 --- a/application/build.gradle.kts +++ /dev/null @@ -1,3 +0,0 @@ -plugins { - id("catalog.plugin") -} diff --git a/application/libs.versions.toml b/application/libs.versions.toml deleted file mode 100644 index e69de29..0000000 diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts deleted file mode 100644 index fe439a2..0000000 --- a/build-logic/build.gradle.kts +++ /dev/null @@ -1,4 +0,0 @@ -plugins { - `kotlin-dsl` - `version-catalog` -} diff --git a/build-logic/settings.gradle.kts b/build-logic/settings.gradle.kts deleted file mode 100644 index ae3fd76..0000000 --- a/build-logic/settings.gradle.kts +++ /dev/null @@ -1,9 +0,0 @@ -rootProject.name = "build-logic" - -dependencyResolutionManagement { - repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) - repositories { - gradlePluginPortal() - mavenCentral() - } -} diff --git a/build-logic/src/main/kotlin/catalog.plugin.gradle.kts b/build-logic/src/main/kotlin/catalog.plugin.gradle.kts deleted file mode 100644 index 4d9d4e2..0000000 --- a/build-logic/src/main/kotlin/catalog.plugin.gradle.kts +++ /dev/null @@ -1,27 +0,0 @@ -import plugin.CatalogJoinerPlugin - -apply() - -plugins { - `version-catalog` - `maven-publish` -} - -publishing { - publications { - create("maven") { - from(components["versionCatalog"]) - } - } - - repositories { - maven { - url = uri("https://maven.pkg.github.com/TriumphCraft/catalog/") - - credentials { - username = System.getenv("GITHUB_ACTOR") - password = System.getenv("GITHUB_TOKEN") - } - } - } -} diff --git a/build-logic/src/main/kotlin/plugin/CatalogJoinerPlugin.kt b/build-logic/src/main/kotlin/plugin/CatalogJoinerPlugin.kt deleted file mode 100644 index 757dc6f..0000000 --- a/build-logic/src/main/kotlin/plugin/CatalogJoinerPlugin.kt +++ /dev/null @@ -1,33 +0,0 @@ -package plugin - -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.internal.catalog.parser.TomlCatalogFileParser -import org.gradle.api.plugins.catalog.CatalogPluginExtension -import java.nio.file.Path - -/** Allows joining catalog files into a combination. */ -class CatalogJoinerPlugin : Plugin { - - private companion object { - private const val CATALOG_PATH = "libs.versions.toml" - } - - override fun apply(project: Project) = with(project) { - // Apply the catalog plugin so we can use it - plugins.apply("org.gradle.version-catalog") - - afterEvaluate { - extensions.getByType(CatalogPluginExtension::class.java).versionCatalog { - TomlCatalogFileParser.parse(rootProject.versionCatalog, this) - // We don't want to import root twice - if (project == rootProject) return@versionCatalog - TomlCatalogFileParser.parse(project.versionCatalog, this) - } - } - } - - /** Quick util for getting the catalog file. */ - private val Project.versionCatalog: Path - get() = file(CATALOG_PATH).toPath() -} diff --git a/build.gradle.kts b/build.gradle.kts index 0732656..6b90789 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,13 @@ plugins { - id("catalog.plugin") + kotlin("jvm") version "1.9.10" + `java-gradle-plugin` + `kotlin-dsl` +} + +dependencies { + implementation(gradleTestKit()) +} + +kotlin { + explicitApi() } diff --git a/gradle.properties b/gradle.properties index 8a2b887..cc3187a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ -group=com.triumphcraft +group=dev.triumphteam version=1.0-SNAPSHOT diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e583..7f93135 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fae0804..ac72c34 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 index 1b6c787..0adc8e1 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +80,11 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,22 +131,29 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,6 +198,10 @@ if "$cygwin" || "$msys" ; then done fi + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + # Collect all arguments for the java command; # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of # shell script including quotes and variable substitutions, so put them in @@ -205,6 +214,12 @@ set -- \ org.gradle.wrapper.GradleWrapperMain \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index 107acd3..93e3f59 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -14,7 +14,7 @@ @rem limitations under the License. @rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +25,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/libs.versions.toml b/libs.versions.toml deleted file mode 100644 index 092f107..0000000 --- a/libs.versions.toml +++ /dev/null @@ -1,25 +0,0 @@ -[versions] -# Nebula -nebula = "1.0.0-SNAPSHOT" - -# Kotlin -kotlin-coroutines = "1.7.1" -kotlin-serialization = "1.5.1" - -# Plugins -kotlin = "1.8.21" -detekt = "1.22.0" -spotless = "6.18.0" - -[libraries] -# Kotlin -kotlin-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines" } -kotlin-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlin-serialization" } -kotlin-serialization-hocon = { module = "org.jetbrains.kotlinx:kotlinx-serialization-hocon", version.ref = "kotlin-serialization" } -kotlin-refection = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } - -# Plugins -kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } -kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" } -detekt = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detekt" } -spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" } diff --git a/lily/build.gradle.kts b/lily/build.gradle.kts deleted file mode 100644 index 0732656..0000000 --- a/lily/build.gradle.kts +++ /dev/null @@ -1,3 +0,0 @@ -plugins { - id("catalog.plugin") -} diff --git a/lily/libs.versions.toml b/lily/libs.versions.toml deleted file mode 100644 index e69de29..0000000 diff --git a/root/build.gradle.kts b/root/build.gradle.kts deleted file mode 100644 index 0732656..0000000 --- a/root/build.gradle.kts +++ /dev/null @@ -1,3 +0,0 @@ -plugins { - id("catalog.plugin") -} diff --git a/root/libs.versions.toml b/root/libs.versions.toml deleted file mode 100644 index e69de29..0000000 diff --git a/service/build.gradle.kts b/service/build.gradle.kts deleted file mode 100644 index 0732656..0000000 --- a/service/build.gradle.kts +++ /dev/null @@ -1,3 +0,0 @@ -plugins { - id("catalog.plugin") -} diff --git a/service/libs.versions.toml b/service/libs.versions.toml deleted file mode 100644 index e69de29..0000000 diff --git a/settings.gradle.kts b/settings.gradle.kts index 6e1f1e8..91c5d30 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,25 +1 @@ -dependencyResolutionManagement { - includeBuild("build-logic") - repositories.gradlePluginPortal() -} - -val projectName = "catalog" -rootProject.name = "catalog-common" - -listOf( - "root", - "lily", - "service", - "application" -).forEach(::includeProject) - -fun includeProject(name: String) { - include(name) { - this.name = "$projectName-$name" - } -} - -fun include(name: String, block: ProjectDescriptor.() -> Unit) { - include(name) - project(":$name").apply(block) -} +rootProject.name = "cataloger" diff --git a/src/main/kotlin/CatalogerPlugin.kt b/src/main/kotlin/CatalogerPlugin.kt new file mode 100644 index 0000000..ea8ac54 --- /dev/null +++ b/src/main/kotlin/CatalogerPlugin.kt @@ -0,0 +1,48 @@ +package dev.triumphteam.cataloguer + +import dev.triumphteam.cataloguer.task.ValidateCatalogTask +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.internal.catalog.parser.TomlCatalogFileParser +import org.gradle.api.plugins.catalog.CatalogPluginExtension +import org.gradle.api.plugins.catalog.internal.DefaultVersionCatalogPluginExtension +import org.gradle.kotlin.dsl.register +import java.nio.file.Path + +public abstract class CatalogerPlugin : Plugin { + + private companion object { + private const val GRADLE_CATALOG_PLUGIN = "org.gradle.version-catalog" + private const val VALIDATE_CATALOG_TASK = "validateCatalog" + private const val CATALOG_PATH = "libs.versions.toml" + } + + override fun apply(project: Project): Unit = with(project) { + // Apply the catalog plugin + plugins.apply(GRADLE_CATALOG_PLUGIN) + + val catalogExtension = extensions.getByType(CatalogPluginExtension::class.java) + + afterEvaluate { + catalogExtension.versionCatalog { + // Importing root toml first + TomlCatalogFileParser.parse(rootProject.tomlCatalog, this) + // We don't want to import root twice + if (project == rootProject) return@versionCatalog + // Then combining it with the actual project's toml + TomlCatalogFileParser.parse(project.tomlCatalog, this) + } + } + + if (catalogExtension is DefaultVersionCatalogPluginExtension) { + // Register validator + tasks.register(VALIDATE_CATALOG_TASK) { + this.catalogExtension.set(catalogExtension) + } + } + } + + /** Quick util for getting the catalog file. */ + private val Project.tomlCatalog: Path + get() = file(CATALOG_PATH).toPath() +} diff --git a/src/main/kotlin/package-info.kt b/src/main/kotlin/package-info.kt new file mode 100644 index 0000000..865033f --- /dev/null +++ b/src/main/kotlin/package-info.kt @@ -0,0 +1,2 @@ + +package dev.triumphteam.cataloguer diff --git a/src/main/kotlin/task/ValidateCatalogTask.kt b/src/main/kotlin/task/ValidateCatalogTask.kt new file mode 100644 index 0000000..cabd5f8 --- /dev/null +++ b/src/main/kotlin/task/ValidateCatalogTask.kt @@ -0,0 +1,163 @@ +package dev.triumphteam.cataloguer.task + +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.artifacts.repositories.MavenArtifactRepository +import org.gradle.api.internal.catalog.DefaultVersionCatalog +import org.gradle.api.plugins.catalog.internal.DefaultVersionCatalogPluginExtension +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.TaskAction +import org.gradle.testkit.runner.GradleRunner +import java.io.File +import java.lang.StringBuilder +import kotlin.io.path.createTempDirectory + +public abstract class ValidateCatalogTask : DefaultTask() { + + private companion object { + private const val GRADLE_SETTINGS_FILE_NAME = "settings.gradle.kts" + private const val GRADLE_BUILD_FILE_NAME = "build.gradle.kts" + private const val FAILED_DEPENDENCY_TRIGGER = "FAILED" + } + + /** Property containing the imported already combined catalog. */ + @get:Input + public abstract val catalogExtension: Property + + /** Validates the catalog by creating and setting up the necessary gradle test files. */ + @TaskAction + public fun validate() { + // Temporary project directory + val directory = createTempDirectory().toFile() + + // Create and set up the gradle test files + File(directory, GRADLE_SETTINGS_FILE_NAME).writeText("rootProject.name = \"cataloger-${project.name}\"") + File(directory, GRADLE_BUILD_FILE_NAME).writeBuild() + + val result = GradleRunner.create() + .withProjectDir(directory) + // Run with the dependencies task + .withArguments("dependencies") + .build() + + val failedDependencies = result.output.lines().filter { FAILED_DEPENDENCY_TRIGGER in it } + + // Success we don't do anything + if (failedDependencies.isEmpty()) return + + throw InvalidDependencyException(failedDependencies) + } + + /** + * Writes the build script for a file. + * + * The function generates a basic Gradle build script. + * It uses the repositories and version catalog defined in the project to populate the script. + * The generated script sets the Java language version to 17. + */ + private fun File.writeBuild() { + val repositories = project.repositories.filterIsInstance() + val catalogue = catalogExtension.get().versionCatalog.get() + + // Generate basic build script + val gradleBuildScript = """ + plugins { + `java-library` + } + + ${repositories.toRepositoriesBlock(16)} + + ${catalogue.toDependenciesBlock(16)} + + java { + toolchain.languageVersion.set(JavaLanguageVersion.of(17)) + } + """.trimIndent() + + // Write it to the file + writeText(gradleBuildScript) + } + + /** + * Converts a list of Maven artifact repositories to a Gradle repositories block. + * + * @param startIndent The number of spaces to indent the repositories block. + * @return The Gradle repositories block as a string. + */ + private fun List.toRepositoriesBlock(startIndent: Int): String { + return buildString { + appendLine("repositories {") + this@toRepositoriesBlock.forEach { repo -> + append(startIndent + 4, "maven(\"${repo.url}\")") + + val credentials = repo.credentials + val username = credentials.username + val password = credentials.password + + if (username == null && password == null) { + appendLine() + return@forEach + } + + appendLine(" {") + appendLine(startIndent + 8, "credentials {") + if (username != null) appendLine(startIndent + 12, "username = \"$username\"") + if (username != null) appendLine(startIndent + 12, "password = \"$password\"") + appendLine(startIndent + 8, "}") + appendLine(startIndent + 4, "}") + } + appendLine(startIndent,"}") + } + } + + /** + * Generates a dependencies block for the given DefaultVersionCatalog. + * + * @param startIndent The number of spaces to indent the dependencies block. + * @return The dependencies block as a String. + */ + private fun DefaultVersionCatalog.toDependenciesBlock(startIndent: Int): String { + val dependencies = libraryAliases.map(::getDependencyData) + return buildString { + appendLine("dependencies {") + dependencies.forEach { model -> + append(startIndent + 4, "compileOnly(\"") + append(model.group).append(":") + append(model.name).append(":") + append(model.version) + appendLine("\")") + } + appendLine(startIndent,"}") + } + } + + /** + * Appends a new line with a specified indentation level to the StringBuilder. + * + * @param indent The number of spaces to indent the line. + * @param line The line to be appended. + */ + private fun StringBuilder.appendLine(indent: Int, line: String) { + appendLine(" ".repeat(indent) + line) + } + + /** + * Appends the given text to this StringBuilder with the specified indentation level. + * + * @param indent the number of spaces to indent the text + * @param text the text to be appended + */ + private fun StringBuilder.append(indent: Int, text: String) { + append(" ".repeat(indent) + text) + } +} + +/** This class represents an exception that is thrown when invalid dependencies are found. */ +public class InvalidDependencyException(errorLines: List) : RuntimeException( + """ + Found invalid dependencies, check groupId, artifactId, version, version.ref, or missing repository. + Dependencies: + ${errorLines.joinToString("\n ") { it.trimIndent().trim() }} + """.trimIndent() +)