Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some IP problems in the build #28929

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build-logic-commons/basics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java {
}

dependencies {
api("gradlebuild:build-environment")
api(platform(project(":build-platform")))

implementation("com.google.guava:guava") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,13 @@ import gradlebuild.basics.BuildParams.CI_ENVIRONMENT_VARIABLE
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.internal.os.OperatingSystem
import org.gradle.kotlin.dsl.*


abstract class BuildEnvironmentExtension {
abstract val gitCommitId: Property<String>
abstract val gitBranch: Property<String>
abstract val repoRoot: DirectoryProperty
}


// `generatePrecompiledScriptPluginAccessors` task invokes this method without `gradle.build-environment` applied
fun Project.getBuildEnvironmentExtension(): BuildEnvironmentExtension? = rootProject.extensions.findByType(BuildEnvironmentExtension::class.java)
fun Project.getBuildEnvironmentExtension(): BuildEnvironmentExtension? = extensions.findByType(BuildEnvironmentExtension::class.java)


fun Project.repoRoot(): Directory = getBuildEnvironmentExtension()?.repoRoot?.get() ?: layout.projectDirectory.parentOrRoot()
Expand Down Expand Up @@ -65,24 +56,6 @@ fun Project.currentGitBranchViaFileSystemQuery(): Provider<String> = getBuildEnv
fun Project.currentGitCommitViaFileSystemQuery(): Provider<String> = getBuildEnvironmentExtension()?.gitCommitId ?: objects.property(String::class.java)


@Suppress("UnstableApiUsage")
fun Project.git(vararg args: String): Provider<String> {
val projectDir = layout.projectDirectory.asFile
val execOutput = providers.exec {
workingDir = projectDir
isIgnoreExitValue = true
commandLine = listOf("git", *args)
if (OperatingSystem.current().isWindows) {
commandLine = listOf("cmd.exe", "/d", "/c") + commandLine
}
}
return execOutput.result.zip(execOutput.standardOutput.asText) { result, outputText ->
if (result.exitValue == 0) outputText.trim()
else "<unknown>" // It's a source distribution, we don't know.
}
}


// pre-test/master/queue/alice/feature -> master
// pre-test/release/current/bob/bugfix -> release
// gh-readonly-queue/master/pr-1234-5678abcdef -> master
Expand Down
2 changes: 2 additions & 0 deletions build-logic-commons/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
}

includeBuild("../build-logic-settings")

// Shared basics for all
include("basics")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import gradlebuild.basics.BuildEnvironmentExtension
import gradlebuild.basics.git
import gradlebuild.basics.parentOrRoot

/*
* Copyright 2022 the original author or authors.
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +14,17 @@ import gradlebuild.basics.parentOrRoot
* limitations under the License.
*/

plugins {
`kotlin-dsl`
}

description = "Provides plugins for configuring build environment"

group = "gradlebuild"

val buildEnvironmentExtension = extensions.create("buildEnvironment", BuildEnvironmentExtension::class)
buildEnvironmentExtension.gitCommitId = git("rev-parse", "HEAD")
buildEnvironmentExtension.gitBranch = git("rev-parse", "--abbrev-ref", "HEAD")
buildEnvironmentExtension.repoRoot = layout.projectDirectory.parentOrRoot()
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
vendor = JvmVendorSpec.ADOPTIUM
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import gradlebuild.basics.BuildEnvironmentExtension
import gradlebuild.basics.BuildEnvironmentService

with(layout.rootDirectory) {
gradle.lifecycle.beforeProject {
val service = gradle.sharedServices.registerIfAbsent("buildEnvironmentService", BuildEnvironmentService::class) {
parameters.rootProjectDir = this@with
}
val buildEnvironmentExtension = extensions.create("buildEnvironment", BuildEnvironmentExtension::class)
buildEnvironmentExtension.gitCommitId = service.flatMap { it.gitCommitId }
buildEnvironmentExtension.gitBranch = service.flatMap { it.gitBranch }
buildEnvironmentExtension.repoRoot = this@with
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gradlebuild.basics

import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property


interface BuildEnvironmentExtension {
val gitCommitId: Property<String>
val gitBranch: Property<String>
val repoRoot: DirectoryProperty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gradlebuild.basics

import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.internal.os.OperatingSystem
import javax.inject.Inject


abstract class BuildEnvironmentService : BuildService<BuildEnvironmentService.Parameters> {

interface Parameters : BuildServiceParameters {
val rootProjectDir: DirectoryProperty
}

@get:Inject
abstract val providers: ProviderFactory

val gitCommitId = git("rev-parse", "HEAD")
val gitBranch = git("rev-parse", "--abbrev-ref", "HEAD")

@Suppress("UnstableApiUsage")
private
fun git(vararg args: String): Provider<String> {
val projectDir = parameters.rootProjectDir.asFile.get()
val execOutput = providers.exec {
workingDir = projectDir
isIgnoreExitValue = true
commandLine = listOf("git", *args)
if (OperatingSystem.current().isWindows) {
commandLine = listOf("cmd.exe", "/d", "/c") + commandLine
}
}
return execOutput.result.zip(execOutput.standardOutput.asText) { result, outputText ->
if (result.exitValue == 0) outputText.trim()
else "<unknown>" // It's a source distribution, we don't know.
}
}
}
17 changes: 17 additions & 0 deletions build-logic-settings/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

description = "Provides settings plugins for configuring global build configuration"
30 changes: 30 additions & 0 deletions build-logic-settings/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencyResolutionManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
}

include("build-environment")

rootProject.name = "build-logic-settings"
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
id("gradlebuild.build-environment")
id("gradlebuild.root-build")

id("gradlebuild.teamcity-import-test-data") // CI: Import Test tasks' JUnit XML if they're UP-TO-DATE or FROM-CACHE
Expand Down
92 changes: 49 additions & 43 deletions gradle/shared-with-buildSrc/mirrors.settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,73 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.gradle.api.internal.artifacts.BaseRepositoryFactory.PLUGIN_PORTAL_OVERRIDE_URL_PROPERTY
import org.gradle.api.internal.GradleInternal
import org.gradle.build.event.BuildEventsListenerRegistry
import org.gradle.internal.nativeintegration.network.HostnameLookup
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener

val originalUrls: Map<String, String> = mapOf(
"jcenter" to "https://jcenter.bintray.com/",
"mavencentral" to "https://repo.maven.apache.org/maven2/",
"google" to "https://dl.google.com/dl/android/maven2/",
"gradle" to "https://repo.gradle.org/gradle/repo",
"gradle-prod-plugins" to "https://plugins.gradle.org/m2",
"gradlejavascript" to "https://repo.gradle.org/gradle/javascript-public",
"gradle-public" to "https://repo.gradle.org/gradle/public",
"gradle-enterprise-rc" to "https://repo.gradle.org/gradle/enterprise-libs-release-candidates"
)

val mirrorUrls: Map<String, String> =
providers.environmentVariable("REPO_MIRROR_URLS").orNull
?.ifBlank { null }
?.split(',')
?.associate { nameToUrl ->
val (name, url) = nameToUrl.split(':', limit = 2)
name to url
}
?: emptyMap()
class Helper(private val providers: ProviderFactory) {
val originalUrls: Map<String, String> = mapOf(
"jcenter" to "https://jcenter.bintray.com/",
"mavencentral" to "https://repo.maven.apache.org/maven2/",
"google" to "https://dl.google.com/dl/android/maven2/",
"gradle" to "https://repo.gradle.org/gradle/repo",
"gradle-prod-plugins" to "https://plugins.gradle.org/m2",
"gradlejavascript" to "https://repo.gradle.org/gradle/javascript-public",
"gradle-public" to "https://repo.gradle.org/gradle/public",
"gradle-enterprise-rc" to "https://repo.gradle.org/gradle/enterprise-libs-release-candidates"
)

fun ignoreMirrors() = providers.environmentVariable("IGNORE_MIRROR").orNull?.toBoolean() == true
val mirrorUrls: Map<String, String> =
providers.environmentVariable("REPO_MIRROR_URLS").orNull
?.ifBlank { null }
?.split(',')
?.associate { nameToUrl ->
val (name, url) = nameToUrl.split(':', limit = 2)
name to url
}
?: emptyMap()

fun isCI() = providers.environmentVariable("CI").isPresent()
fun ignoreMirrors() = providers.environmentVariable("IGNORE_MIRROR").orNull?.toBoolean() == true

fun withMirrors(handler: RepositoryHandler) {
if (!isCI()) {
return
}
handler.all {
if (this is MavenArtifactRepository) {
originalUrls.forEach { name, originalUrl ->
if (normalizeUrl(originalUrl) == normalizeUrl(this.url.toString()) && mirrorUrls.containsKey(name)) {
mirrorUrls.get(name)?.let { this.setUrl(it) }
fun isCI() = providers.environmentVariable("CI").isPresent()

fun withMirrors(handler: RepositoryHandler) {
if (!isCI()) {
return
}
handler.all {
if (this is MavenArtifactRepository) {
originalUrls.forEach { name, originalUrl ->
if (normalizeUrl(originalUrl) == normalizeUrl(this.url.toString()) && mirrorUrls.containsKey(name)) {
mirrorUrls.get(name)?.let { this.setUrl(it) }
}
}
}
}
}
}

fun normalizeUrl(url: String): String {
val result = url.replace("https://", "http://")
return if (result.endsWith("/")) result else "$result/"
fun normalizeUrl(url: String): String {
val result = url.replace("https://", "http://")
return if (result.endsWith("/")) result else "$result/"
}
}

gradle.allprojects {
buildscript.configurations["classpath"].incoming.beforeResolve {
withMirrors(buildscript.repositories)
}
afterEvaluate {
withMirrors(repositories)
with(Helper(providers)) {
gradle.lifecycle.beforeProject {
buildscript.configurations["classpath"].incoming.beforeResolve {
withMirrors(buildscript.repositories)
}
afterEvaluate {
withMirrors(repositories)
}
}
}

gradle.settingsEvaluated {
withMirrors(settings.pluginManagement.repositories)
gradle.settingsEvaluated {
withMirrors(settings.pluginManagement.repositories)
}
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pluginManagement {
}
gradlePluginPortal()
}
includeBuild("build-logic-settings")
}

plugins {
id("gradlebuild.build-environment")
id("com.gradle.develocity").version("3.17.3") // Sync with `build-logic-commons/build-platform/build.gradle.kts`
id("io.github.gradle.gradle-enterprise-conventions-plugin").version("0.10.0")
id("org.gradle.toolchains.foojay-resolver-convention") version ("0.8.0")
Expand Down