|
| 1 | +/* |
| 2 | + * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +import ktorbuild.* |
| 6 | + |
| 7 | +plugins { |
| 8 | + id("ktorbuild.base") |
| 9 | + id("version-catalog") |
| 10 | + id("ktorbuild.publish") |
| 11 | +} |
| 12 | + |
| 13 | +val publishedProjectsProvider = projectsWithTag(ProjectTag.Published, Project::getName) |
| 14 | + |
| 15 | +// A hack to prevent all projects evaluation if version catalog generation wasn't requested |
| 16 | +// Issue: https://github.com/gradle/gradle/issues/33568 |
| 17 | +gradle.taskGraph.whenReady { |
| 18 | + if (allTasks.any { it.name == VersionCatalogPlugin.GENERATE_CATALOG_FILE_TASKNAME }) { |
| 19 | + catalog.versionCatalog { |
| 20 | + configureVersionCatalog( |
| 21 | + group = project.group.toString(), |
| 22 | + version = project.version.toString(), |
| 23 | + publishedProjects = publishedProjectsProvider.get().toSet(), |
| 24 | + ) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +fun VersionCatalogBuilder.configureVersionCatalog( |
| 30 | + group: String, |
| 31 | + version: String, |
| 32 | + publishedProjects: Set<String>, |
| 33 | +) { |
| 34 | + // Versions |
| 35 | + val versionAlias = version("ktor", version) |
| 36 | + |
| 37 | + // Libraries |
| 38 | + for (projectName in publishedProjects - excludedProjects) { |
| 39 | + if (projectName == "ktor-version-catalog") continue |
| 40 | + library( |
| 41 | + /* alias = */ libraryAlias(projectName, publishedProjects + hierarchyRoots), |
| 42 | + /* group = */ group, |
| 43 | + /* artifact = */ projectName, |
| 44 | + ).versionRef(versionAlias) |
| 45 | + } |
| 46 | + library("gradlePlugin", "io.ktor.plugin", "plugin").versionRef(versionAlias) |
| 47 | + |
| 48 | + // Plugins |
| 49 | + plugin("ktor", "io.ktor.plugin").versionRef(versionAlias) |
| 50 | +} |
| 51 | + |
| 52 | +// Projects that are published but should be excluded from the version catalog. |
| 53 | +// This means we don't want our users to use these dependencies. |
| 54 | +val excludedProjects = setOf( |
| 55 | + "ktor-client", |
| 56 | + "ktor-server", |
| 57 | + "ktor-server-host-common", |
| 58 | +) |
| 59 | + |
| 60 | +val hierarchyRoots = setOf( |
| 61 | + "ktor", |
| 62 | + "ktor-client", |
| 63 | + "ktor-server", |
| 64 | +) |
| 65 | + |
| 66 | +// Handle special cases when automatic mapping doesn't work well |
| 67 | +val manualOverrides = mapOf( |
| 68 | + // We want to preserve the hierarchy in case we add more config formats |
| 69 | + "ktor-server-config-yaml" to "server-config-yaml", |
| 70 | + // Fix a typo in the project name |
| 71 | + "ktor-websocket-serialization" to "websockets-serialization", |
| 72 | + // Prefer '*-jakarta' artifacts over the deprecated ones |
| 73 | + "ktor-client-jetty-jakarta" to "client-jetty", |
| 74 | + "ktor-client-jetty" to "client-jetty-legacy", |
| 75 | + "ktor-server-jetty-jakarta" to "server-jetty", |
| 76 | + "ktor-server-jetty" to "server-jetty-legacy", |
| 77 | + "ktor-server-servlet-jakarta" to "server-servlet", |
| 78 | + "ktor-server-servlet" to "server-servlet-legacy", |
| 79 | + "ktor-server-tomcat-jakarta" to "server-tomcat", |
| 80 | + "ktor-server-tomcat" to "server-tomcat-legacy", |
| 81 | +) |
| 82 | + |
| 83 | +/** |
| 84 | + * Generates a library alias for a given [projectName] to use in the version catalog. |
| 85 | + * |
| 86 | + * This function converts hierarchical project names into appropriately structured aliases. |
| 87 | + * For example, `ktor-client-content-negotiation` becomes `client-contentNegotiation`. |
| 88 | + */ |
| 89 | +fun libraryAlias(projectName: String, possiblePrefixes: Set<String>): String { |
| 90 | + if (projectName in manualOverrides) return manualOverrides.getValue(projectName) |
| 91 | + |
| 92 | + var prefix = projectName.substringBeforeLast("-") |
| 93 | + var suffix = projectName.substringAfterLast("-") |
| 94 | + |
| 95 | + while (prefix !in possiblePrefixes) { |
| 96 | + val lastWord = prefix.substringAfterLast("-") |
| 97 | + prefix = prefix.substringBeforeLast("-") |
| 98 | + suffix = lastWord + suffix.replaceFirstChar { it.uppercase() } |
| 99 | + } |
| 100 | + |
| 101 | + return if (prefix != "ktor") { |
| 102 | + "${libraryAlias(prefix, possiblePrefixes)}-$suffix" |
| 103 | + } else { |
| 104 | + suffix |
| 105 | + } |
| 106 | +} |
0 commit comments