diff --git a/build.gradle b/build.gradle index 66366e63..f471a4ab 100644 --- a/build.gradle +++ b/build.gradle @@ -58,15 +58,10 @@ dependencies { testImplementation gradleTestKit() testImplementation 'org.junit.jupiter:junit-jupiter' - testImplementation 'junit:junit' - testRuntimeOnly("org.junit.vintage:junit-vintage-engine") { - because 'allows JUnit 3 and JUnit 4 tests to run' - } testImplementation 'org.assertj:assertj-core' testImplementation('org.spockframework:spock-core') { exclude module: 'groovy-all' } - testImplementation 'com.fasterxml.jackson.core:jackson-databind' } java { diff --git a/src/groovy/groovy/com/palantir/gradle/gitversion/BuildScanPluginInterop.groovy b/src/groovy/groovy/com/palantir/gradle/gitversion/BuildScanPluginInterop.groovy deleted file mode 100644 index e83f07ea..00000000 --- a/src/groovy/groovy/com/palantir/gradle/gitversion/BuildScanPluginInterop.groovy +++ /dev/null @@ -1,56 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion - -import java.util.function.Supplier -import org.gradle.api.Project - -class BuildScanPluginInterop { - static void addBuildScanCustomValues(Project rootProject, Supplier> customValues) { - // Fix #353: Detect when the root project is already evaluated, thus anything is afterEvaluate - // and we can just execute immediately. This is due to afterEvaluate changes in Gradle 7. - // - // Note: We cannot use hasCompleted() in order to continue supporting gradle 5, a minor inconvenience - if (rootProject.getState().isUnconfigured() || rootProject.getState().isConfiguring()) { - // After evaluate because while we can detect the <5.x com.gradle.build-scan project on the root project, - // there is no way to detect the >6.x com.gradle.enterprise settings plugins using withPlugin - rootProject.afterEvaluate { - applyBuildScanCustomValues(rootProject, customValues) - } - } else { - applyBuildScanCustomValues(rootProject, customValues) - } - } - - private static void applyBuildScanCustomValues(Project rootProject, Supplier> customValues) { - def gradleEnterpriseExtension = rootProject.extensions.findByName('gradleEnterprise') - - // In Gradle Enterprise Plugin 3.2, the root project's buildScan extension was deprecated and you now need - // to call gradleEnterprise.buildScan - def buildScan = Optional.ofNullable(gradleEnterpriseExtension) - .map({ gradleEnterprise -> gradleEnterprise.buildScan }) - .orElseGet({ rootProject.extensions.findByName('buildScan') }) - - if (buildScan == null) { - return - } - - buildScan.buildFinished { - customValues.get().forEach({ name, value -> rootProject.buildScan.value(name, value) }) - } - } -} diff --git a/src/main/java/com/palantir/gradle/gitversion/GitVersionCacheService.java b/src/main/java/com/palantir/gradle/gitversion/GitVersionCacheService.java index 2072fa02..90b6fcc8 100644 --- a/src/main/java/com/palantir/gradle/gitversion/GitVersionCacheService.java +++ b/src/main/java/com/palantir/gradle/gitversion/GitVersionCacheService.java @@ -22,14 +22,9 @@ import org.gradle.api.provider.Provider; import org.gradle.api.services.BuildService; import org.gradle.api.services.BuildServiceParameters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class GitVersionCacheService implements BuildService { - private static final Logger log = LoggerFactory.getLogger(GitVersionCacheService.class); - - private final Timer timer = new Timer(); private final ConcurrentMap versionDetailsMap = new ConcurrentHashMap<>(); public final String getGitVersion(File project, Object args) { @@ -37,7 +32,7 @@ public final String getGitVersion(File project, Object args) { GitVersionArgs gitVersionArgs = GitVersionArgs.fromGroovyClosure(args); String key = gitDir.toPath() + "|" + gitVersionArgs.getPrefix(); String gitVersion = versionDetailsMap - .computeIfAbsent(key, _k -> createVersionDetails(gitDir, gitVersionArgs)) + .computeIfAbsent(key, _k -> new VersionDetailsImpl(gitDir, gitVersionArgs)) .getVersion(); return gitVersion; } @@ -47,18 +42,10 @@ public final VersionDetails getVersionDetails(File project, Object args) { GitVersionArgs gitVersionArgs = GitVersionArgs.fromGroovyClosure(args); String key = gitDir.toPath() + "|" + gitVersionArgs.getPrefix(); VersionDetails versionDetails = - versionDetailsMap.computeIfAbsent(key, _k -> createVersionDetails(gitDir, gitVersionArgs)); + versionDetailsMap.computeIfAbsent(key, _k -> new VersionDetailsImpl(gitDir, gitVersionArgs)); return versionDetails; } - private VersionDetails createVersionDetails(File gitDir, GitVersionArgs args) { - return TimingVersionDetails.wrap(timer, new VersionDetailsImpl(gitDir, args)); - } - - public final Timer timer() { - return timer; - } - private static File getRootGitDir(File currentRoot) { File gitDir = scanForRootGitDir(currentRoot); if (!gitDir.exists()) { diff --git a/src/main/java/com/palantir/gradle/gitversion/GitVersionRootPlugin.java b/src/main/java/com/palantir/gradle/gitversion/GitVersionRootPlugin.java index fbff709f..fc70548f 100644 --- a/src/main/java/com/palantir/gradle/gitversion/GitVersionRootPlugin.java +++ b/src/main/java/com/palantir/gradle/gitversion/GitVersionRootPlugin.java @@ -16,10 +16,8 @@ package com.palantir.gradle.gitversion; -import com.google.common.collect.ImmutableMap; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.provider.Provider; final class GitVersionRootPlugin implements Plugin { @Override @@ -28,22 +26,5 @@ public void apply(Project project) { throw new IllegalStateException(String.format( "The %s plugin must be applied to the root project", GitVersionRootPlugin.class.getSimpleName())); } - - Provider serviceProvider = - GitVersionCacheService.getSharedGitVersionCacheService(project); - - BuildScanPluginInterop.addBuildScanCustomValues(project, () -> { - Timer timer = serviceProvider.get().timer(); - - String timerJson = timer.toJson(); - - long totalTime = timer.totalMillis(); - - return ImmutableMap.of( - "com.palantir.git-version.timings", - timerJson, - "com.palantir.git-version.timings.total", - Long.toString(totalTime)); - }); } } diff --git a/src/main/java/com/palantir/gradle/gitversion/JsonUtils.java b/src/main/java/com/palantir/gradle/gitversion/JsonUtils.java deleted file mode 100644 index 4df533bb..00000000 --- a/src/main/java/com/palantir/gradle/gitversion/JsonUtils.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion; - -import java.util.Map; -import java.util.stream.Collectors; - -final class JsonUtils { - private JsonUtils() {} - - static String mapToJson(Map map) { - // Manually writing the json string here rather than using a library to avoid dependencies in this incredibly - // widely used plugin. - String middleJson = map.entrySet().stream() - .map(entry -> String.format( - "\"%s\":%s", entry.getKey(), entry.getValue().toString())) - .collect(Collectors.joining(",")); - - return "{" + middleJson + "}"; - } -} diff --git a/src/main/java/com/palantir/gradle/gitversion/Timer.java b/src/main/java/com/palantir/gradle/gitversion/Timer.java deleted file mode 100644 index 0606d24a..00000000 --- a/src/main/java/com/palantir/gradle/gitversion/Timer.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion; - -import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.TimeUnit; -import java.util.function.Supplier; - -final class Timer { - private final ConcurrentMap totalTimesTakenMillis = new ConcurrentHashMap<>(); - - public T record(String name, Supplier codeToTime) { - Stopwatch stopwatch = Stopwatch.createStarted(); - try { - return codeToTime.get(); - } finally { - stopwatch.stop(); - long timeTakenMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS); - - totalTimesTakenMillis.compute( - name, (_ignored, previousValue) -> timeTakenMillis + (previousValue == null ? 0 : previousValue)); - } - } - - public String toJson() { - Map withTotal = ImmutableMap.builder() - .putAll(totalTimesTakenMillis) - .put("total", totalMillis()) - .build(); - - return JsonUtils.mapToJson(withTotal); - } - - public long totalMillis() { - return totalTimesTakenMillis.values().stream().mapToLong(time -> time).sum(); - } -} diff --git a/src/main/java/com/palantir/gradle/gitversion/TimingVersionDetails.java b/src/main/java/com/palantir/gradle/gitversion/TimingVersionDetails.java deleted file mode 100644 index ee7afd22..00000000 --- a/src/main/java/com/palantir/gradle/gitversion/TimingVersionDetails.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Proxy; - -final class TimingVersionDetails { - private TimingVersionDetails() {} - - public static VersionDetails wrap(Timer timer, VersionDetails versionDetails) { - return (VersionDetails) Proxy.newProxyInstance( - VersionDetails.class.getClassLoader(), new Class[] {VersionDetails.class}, (_proxy, method, args) -> { - return timer.record(method.getName(), () -> { - try { - return method.invoke(versionDetails, args); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException("Failed in invoke method", e); - } - }); - }); - } -} diff --git a/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy b/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy index 194e717c..6dde80e9 100644 --- a/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy +++ b/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy @@ -700,82 +700,6 @@ class GitVersionPluginTests extends Specification { buildResult.output.contains(":printVersion\n1.0.0-${depth}-g${latestCommit.substring(0, 7)}\n") } - def 'does not crash when setting build scan custom values when Gradle 6 enterprise plugin 3.2 is applied'() { - when: - settingsFile.text = ''' - plugins { - id "com.gradle.enterprise" version "3.2" - } - '''.stripIndent() + settingsFile.text - - buildFile << ''' - plugins { - id 'com.palantir.git-version' - } - version gitVersion() - '''.stripIndent() - gitIgnoreFile << 'build' - Git git = new Git(projectDir, true) - git.runGitCommand("init", projectDir.toString()) - git.runGitCommand("add", ".") - git.runGitCommand("commit", "-m", "'initial commit'") - git.runGitCommand("tag", "-a", "1.0.0", "-m", "1.0.0") - - then: - with('printVersion').build() - } - - - def 'does not crash when setting build scan custom values when Gradle 7 build scan plugin is applied'() { - when: - settingsFile.text = ''' - plugins { - id "com.gradle.enterprise" version "3.2" - } - '''.stripIndent() + settingsFile.text - - buildFile << ''' - plugins { - id 'com.palantir.git-version' - } - version gitVersion() - '''.stripIndent() - gitIgnoreFile << 'build' - Git git = new Git(projectDir, true) - git.runGitCommand("init", projectDir.toString()) - git.runGitCommand("add", ".") - git.runGitCommand("commit", "-m", "'initial commit'") - git.runGitCommand("tag", "-a", "1.0.0", "-m", "1.0.0") - - then: - with(Optional.of('7.4.2'), 'printVersion').build() - } - - def 'does not crash when setting build scan custom values when Gradle 6 enterprise plugin 3.1 is applied'() { - when: - settingsFile.text = ''' - plugins { - id "com.gradle.enterprise" version "3.1" - } - '''.stripIndent() + settingsFile.text - - buildFile << ''' - plugins { - id 'com.palantir.git-version' - } - version gitVersion() - '''.stripIndent() - gitIgnoreFile << 'build' - Git git = new Git(projectDir, true) - git.runGitCommand("init", projectDir.toString()) - git.runGitCommand("add", ".") - git.runGitCommand("commit", "-m", "'initial commit'") - git.runGitCommand("tag", "-a", "1.0.0", "-m", "1.0.0") - - then: - with('printVersion').build() - } - private GradleRunner with(String... tasks) { return with(Optional.empty(), tasks) } diff --git a/src/test/java/com/palantir/gradle/gitversion/JsonUtilsTest.java b/src/test/java/com/palantir/gradle/gitversion/JsonUtilsTest.java deleted file mode 100644 index ae6a603d..00000000 --- a/src/test/java/com/palantir/gradle/gitversion/JsonUtilsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.common.collect.ImmutableMap; -import java.util.Collections; -import org.junit.jupiter.api.Test; - -class JsonUtilsTest { - @Test - void converts_empty_map_to_empty_json_object() { - String json = JsonUtils.mapToJson(Collections.emptyMap()); - - assertThat(json).isEqualTo("{}"); - } - - @Test - void converts_map_of_string_to_long_correctly() { - String json = JsonUtils.mapToJson(ImmutableMap.of("foo", 20, "bar", 40)); - - assertThat(json).isEqualTo("{\"foo\":20,\"bar\":40}"); - } -} diff --git a/src/test/java/com/palantir/gradle/gitversion/TimerTest.java b/src/test/java/com/palantir/gradle/gitversion/TimerTest.java deleted file mode 100644 index 970cf415..00000000 --- a/src/test/java/com/palantir/gradle/gitversion/TimerTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. - * - * 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 com.palantir.gradle.gitversion; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import org.junit.jupiter.api.Test; - -class TimerTest { - private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - - @Test - void generate_correct_json_with_total() throws JsonProcessingException { - Timer timer = new Timer(); - assertThat(timer.record("something", () -> 4)).isEqualTo(4); - assertThat(timer.record("another", () -> "foo")).isEqualTo("foo"); - - ObjectNode objectNode = OBJECT_MAPPER.readValue(timer.toJson(), ObjectNode.class); - long something = objectNode.get("something").asLong(); - long another = objectNode.get("another").asLong(); - long total = objectNode.get("total").asLong(); - - assertThat(something).isGreaterThanOrEqualTo(0); - assertThat(another).isGreaterThanOrEqualTo(0); - assertThat(total).isEqualTo(something + another); - assertThat(timer.totalMillis()).isEqualTo(total); - } -} diff --git a/versions.lock b/versions.lock index 670e393b..2bc3449b 100644 --- a/versions.lock +++ b/versions.lock @@ -9,22 +9,16 @@ org.checkerframework:checker-qual:3.42.0 (1 constraints: 4b0a47bf) org.immutables:value:2.10.1 (1 constraints: 3605303b) [Test dependencies] -com.fasterxml.jackson.core:jackson-annotations:2.16.1 (1 constraints: 8a123f21) -com.fasterxml.jackson.core:jackson-core:2.16.1 (1 constraints: 8a123f21) -com.fasterxml.jackson.core:jackson-databind:2.16.1 (1 constraints: 3c05423b) -junit:junit:4.13.2 (2 constraints: eb154ef7) net.bytebuddy:byte-buddy:1.14.16 (1 constraints: 830bcaea) -org.apiguardian:apiguardian-api:1.1.2 (6 constraints: 896455cc) +org.apiguardian:apiguardian-api:1.1.2 (5 constraints: 105480ac) org.assertj:assertj-core:3.26.0 (1 constraints: 3d054b3b) org.codehaus.groovy:groovy:3.0.9 (1 constraints: 380d141f) org.hamcrest:hamcrest:2.2 (1 constraints: d20cdc04) -org.hamcrest:hamcrest-core:1.3 (1 constraints: cc05fe3f) org.junit.jupiter:junit-jupiter:5.10.2 (1 constraints: 3a05433b) org.junit.jupiter:junit-jupiter-api:5.10.2 (3 constraints: f12f0e51) org.junit.jupiter:junit-jupiter-engine:5.10.2 (1 constraints: 350eff49) org.junit.jupiter:junit-jupiter-params:5.10.2 (1 constraints: 350eff49) org.junit.platform:junit-platform-commons:1.10.2 (2 constraints: 2f218183) -org.junit.platform:junit-platform-engine:1.10.2 (3 constraints: b22e4dc5) -org.junit.vintage:junit-vintage-engine:5.10.2 (1 constraints: 3a05433b) +org.junit.platform:junit-platform-engine:1.10.2 (2 constraints: 091e7f63) org.opentest4j:opentest4j:1.3.0 (2 constraints: cf209249) org.spockframework:spock-core:2.1-groovy-3.0 (1 constraints: 3808bd76) diff --git a/versions.props b/versions.props index 0459f064..82919105 100644 --- a/versions.props +++ b/versions.props @@ -1,10 +1,6 @@ +com.fasterxml.jackson.core:jackson-databind = 2.16.1 com.google.guava:guava = 33.2.1-jre -org.immutables:* = 2.10.1 - -# test dependencies -junit:junit = 4.13.2 org.assertj:assertj-core = 3.26.0 -org.spockframework:spock-core = 2.1-groovy-3.0 +org.immutables:* = 2.10.1 org.junit.jupiter:* = 5.10.2 -org.junit.vintage:* = 5.10.2 -com.fasterxml.jackson.core:jackson-databind = 2.16.1 +org.spockframework:spock-core = 2.1-groovy-3.0