|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:Suppress("DuplicatedCode") |
| 6 | + |
| 7 | +pluginManagement { |
| 8 | + fun logAbsentProperty(name: String): Nothing? { |
| 9 | + logger.info("Property '$name' is not present for repository credentials.") |
| 10 | + |
| 11 | + return null |
| 12 | + } |
| 13 | + |
| 14 | + fun getEnv(propertyName: String): String? = System.getenv( |
| 15 | + propertyName.replace(".", "_").uppercase() |
| 16 | + )?.ifEmpty { null } |
| 17 | + |
| 18 | + fun getLocalProperties(): java.util.Properties { |
| 19 | + return java.util.Properties().apply { |
| 20 | + val propertiesDir = File( |
| 21 | + rootDir.path |
| 22 | + .removeSuffix("/gradle-conventions") |
| 23 | + .removeSuffix("/gradle-conventions-settings") |
| 24 | + .removeSuffix("/compiler-plugin") |
| 25 | + .removeSuffix("/gradle-plugin") |
| 26 | + ) |
| 27 | + val localFile = File(propertiesDir, "local.properties") |
| 28 | + if (localFile.exists()) { |
| 29 | + localFile.inputStream().use { load(it) } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + fun getSpacePassword(): String? { |
| 35 | + val password = "kotlinx.rpc.team.space.password" |
| 36 | + return getLocalProperties()[password] as String? |
| 37 | + ?: settings.providers.gradleProperty(password).orNull |
| 38 | + ?: getEnv(password) |
| 39 | + ?: logAbsentProperty(password) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a publishing repository targeting Space Packages on jetbrains.team. |
| 44 | + * |
| 45 | + * @param repoName the name of the Space Packages repository |
| 46 | + */ |
| 47 | + fun RepositoryHandler.jbTeamPackages(repoName: String) { |
| 48 | + maven { |
| 49 | + name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } } |
| 50 | + url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName") |
| 51 | + |
| 52 | + val spacePassword = getSpacePassword() |
| 53 | + |
| 54 | + if (spacePassword != null) { |
| 55 | + credentials(HttpHeaderCredentials::class.java) { |
| 56 | + name = "Authorization" |
| 57 | + value = "Bearer $spacePassword" |
| 58 | + } |
| 59 | + |
| 60 | + authentication { |
| 61 | + create<HttpHeaderAuthentication>("http_auth_header") |
| 62 | + } |
| 63 | + } else { |
| 64 | + logger.info("Skipping adding credentials for Space repository '$repoName'") |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + fun RepositoryHandler.buildDeps() = jbTeamPackages(repoName = "build-deps") |
| 70 | + fun RepositoryHandler.buildDepsEap() = jbTeamPackages(repoName = "build-deps-eap") |
| 71 | + |
| 72 | + repositories { |
| 73 | + val useProxyProperty = getLocalProperties()["kotlinx.rpc.useProxyRepositories"] as String? |
| 74 | + val useProxy = useProxyProperty == null || useProxyProperty == "true" |
| 75 | + |
| 76 | + if (useProxy) { |
| 77 | + buildDeps() |
| 78 | + buildDepsEap() |
| 79 | + } else { |
| 80 | + mavenCentral() |
| 81 | + gradlePluginPortal() |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +gradle.rootProject { |
| 87 | + fun logAbsentProperty(name: String): Nothing? { |
| 88 | + logger.info("Property '$name' is not present for repository credentials.") |
| 89 | + |
| 90 | + return null |
| 91 | + } |
| 92 | + |
| 93 | + fun getEnv(propertyName: String): String? = System.getenv( |
| 94 | + propertyName.replace(".", "_").uppercase() |
| 95 | + )?.ifEmpty { null } |
| 96 | + |
| 97 | + fun getLocalProperties(): java.util.Properties { |
| 98 | + return java.util.Properties().apply { |
| 99 | + val propertiesDir = File( |
| 100 | + rootDir.path |
| 101 | + .removeSuffix("/gradle-conventions") |
| 102 | + .removeSuffix("/gradle-conventions-settings") |
| 103 | + .removeSuffix("/compiler-plugin") |
| 104 | + .removeSuffix("/gradle-plugin") |
| 105 | + ) |
| 106 | + val localFile = File(propertiesDir, "local.properties") |
| 107 | + if (localFile.exists()) { |
| 108 | + localFile.inputStream().use { load(it) } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fun java.util.Properties.isUsingProxyRepositories(): Boolean { |
| 114 | + val useProxyProperty = this["kotlinx.rpc.useProxyRepositories"] as String? |
| 115 | + return useProxyProperty == null || useProxyProperty == "true" |
| 116 | + } |
| 117 | + |
| 118 | + fun getSpacePassword(): String? { |
| 119 | + val password = "kotlinx.rpc.team.space.password" |
| 120 | + return getLocalProperties()[password] as String? |
| 121 | + ?: settings.providers.gradleProperty(password).orNull |
| 122 | + ?: getEnv(password) |
| 123 | + ?: logAbsentProperty(password) |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Creates a publishing repository targeting Space Packages on jetbrains.team. |
| 128 | + * |
| 129 | + * @param repoName the name of the Space Packages repository |
| 130 | + */ |
| 131 | + fun RepositoryHandler.jbTeamPackages(repoName: String) { |
| 132 | + maven { |
| 133 | + name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } } |
| 134 | + url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName") |
| 135 | + |
| 136 | + val spacePassword = getSpacePassword() |
| 137 | + |
| 138 | + if (spacePassword != null) { |
| 139 | + credentials(HttpHeaderCredentials::class.java) { |
| 140 | + name = "Authorization" |
| 141 | + value = "Bearer $spacePassword" |
| 142 | + } |
| 143 | + |
| 144 | + authentication { |
| 145 | + create<HttpHeaderAuthentication>("http_auth_header") |
| 146 | + } |
| 147 | + } else { |
| 148 | + logger.info("Skipping adding credentials for Space repository '$repoName'") |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + fun RepositoryHandler.buildDeps() = jbTeamPackages(repoName = "build-deps") |
| 154 | + fun RepositoryHandler.buildDepsEap() = jbTeamPackages(repoName = "build-deps-eap") |
| 155 | + |
| 156 | + allprojects { |
| 157 | + val localProps = getLocalProperties() |
| 158 | + |
| 159 | + this.extra["spacePassword"] = getSpacePassword() |
| 160 | + this.extra["localProperties"] = localProps |
| 161 | + this.extra["useProxyRepositories"] = localProps.isUsingProxyRepositories() |
| 162 | + |
| 163 | + val useProxy = localProps.isUsingProxyRepositories() |
| 164 | + |
| 165 | + buildscript { |
| 166 | + repositories { |
| 167 | + if (useProxy) { |
| 168 | + buildDeps() |
| 169 | + buildDepsEap() |
| 170 | + } else { |
| 171 | + mavenCentral() |
| 172 | + gradlePluginPortal() |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + repositories { |
| 177 | + if (useProxy) { |
| 178 | + buildDeps() |
| 179 | + buildDepsEap() |
| 180 | + } else { |
| 181 | + mavenCentral() |
| 182 | + gradlePluginPortal() |
| 183 | + |
| 184 | + maven("https://www.jetbrains.com/intellij-repository/releases") |
| 185 | + |
| 186 | + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies") |
| 187 | + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") |
| 188 | + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") |
| 189 | + |
| 190 | + maven("https://maven.pkg.jetbrains.space/public/p/ktor/eap") |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments