Skip to content

Commit 9e16144

Browse files
authored
Merge pull request #148 from KyoriPowered/feat/loud-tag-requirement
feat(common,git): Replace versioning tag check with task-based tag check
2 parents 18874b5 + 3910c6f commit 9e16144

File tree

9 files changed

+155
-117
lines changed

9 files changed

+155
-117
lines changed

indra-common/src/main/java/net/kyori/indra/internal/AbstractIndraPublishingPlugin.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -31,7 +31,6 @@
3131
import net.kyori.indra.api.model.License;
3232
import net.kyori.indra.api.model.SourceCodeManagement;
3333
import net.kyori.indra.git.GitPlugin;
34-
import net.kyori.indra.git.task.RequireClean;
3534
import net.kyori.indra.util.Versioning;
3635
import net.kyori.mammoth.ProjectPlugin;
3736
import org.gradle.api.Action;
@@ -117,16 +116,22 @@ public void apply(final @NotNull Project project, final @NotNull PluginContainer
117116
}
118117
});
119118

119+
final Provider<Boolean> forceSign = project.getProviders().gradleProperty(FORCE_SIGN_PROPERTY).map(x -> true).orElse(false);
120+
final Provider<Boolean> isRelease = project.getProviders().provider(() -> Versioning.isRelease(project));
120121
tasks.withType(Sign.class).configureEach(task -> {
121-
final Provider<Boolean> forceSign = project.getProviders().gradleProperty(FORCE_SIGN_PROPERTY).map(x -> true).orElse(false);
122-
final Provider<Boolean> isRelease = project.getProviders().provider(() -> Versioning.isRelease(project));
123122
task.onlyIf(spec -> forceSign.zip(isRelease, (a, b) -> a || b).get());
124123
});
125124

126-
final TaskProvider<RequireClean> requireClean = tasks.named(GitPlugin.REQUIRE_CLEAN_TASK, RequireClean.class);
125+
final TaskProvider<?> requireClean = tasks.named(GitPlugin.REQUIRE_CLEAN_TASK);
126+
final TaskProvider<?> requireTagged = tasks.named(GitPlugin.REQUIRE_TAGGED_TASK);
127+
requireTagged.configure(task -> {
128+
task.getInputs().property("isRelease", isRelease);
129+
task.onlyIf(t -> isRelease.get());
130+
});
131+
127132
tasks.withType(AbstractPublishToMaven.class).configureEach(task -> {
128133
if (!(task instanceof PublishToMavenLocal)) {
129-
task.dependsOn(requireClean);
134+
task.dependsOn(requireClean, requireTagged);
130135
}
131136
});
132137

indra-common/src/main/java/net/kyori/indra/util/Versioning.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -23,12 +23,9 @@
2323
*/
2424
package net.kyori.indra.util;
2525

26-
import net.kyori.indra.git.IndraGitExtension;
27-
import org.eclipse.jgit.lib.Ref;
2826
import org.gradle.api.JavaVersion;
2927
import org.gradle.api.Project;
3028
import org.jetbrains.annotations.NotNull;
31-
import org.jetbrains.annotations.Nullable;
3229

3330
public final class Versioning {
3431
public static int versionNumber(final @NotNull JavaVersion version) {
@@ -71,9 +68,7 @@ public static boolean isSnapshot(final @NotNull Project project) {
7168
* @return if the project is recognized as a release
7269
*/
7370
public static boolean isRelease(final @NotNull Project project) {
74-
final @Nullable IndraGitExtension git = project.getExtensions().findByType(IndraGitExtension.class);
75-
final @Nullable Ref tag = git == null ? null : git.headTag();
76-
return (tag != null || git == null || !git.isPresent()) && !isSnapshot(project);
71+
return !isSnapshot(project);
7772
}
7873

7974
private Versioning() {

indra-common/src/test/java/net/kyori/indra/util/VersioningTest.java

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -23,21 +23,13 @@
2323
*/
2424
package net.kyori.indra.util;
2525

26-
import java.io.IOException;
27-
import java.nio.charset.StandardCharsets;
28-
import java.nio.file.Files;
29-
import java.nio.file.Path;
3026
import java.util.stream.Stream;
31-
import net.kyori.indra.git.GitPlugin;
3227
import net.kyori.indra.test.IndraTesting;
33-
import org.eclipse.jgit.api.Git;
34-
import org.eclipse.jgit.api.errors.GitAPIException;
3528
import org.gradle.api.JavaVersion;
3629
import org.gradle.api.Project;
3730
import org.junit.jupiter.api.DynamicNode;
3831
import org.junit.jupiter.api.Test;
3932
import org.junit.jupiter.api.TestFactory;
40-
import org.junit.jupiter.api.io.TempDir;
4133
import org.junit.jupiter.params.ParameterizedTest;
4234
import org.junit.jupiter.params.provider.ValueSource;
4335

@@ -104,7 +96,7 @@ void testIsReleaseDoesNotMatch(final String version) {
10496
}
10597

10698
@TestFactory
107-
Stream<DynamicNode> testIsReleaseMatches(@TempDir final Path tempDir) {
99+
Stream<DynamicNode> testIsReleaseMatches() {
108100
return Stream.of(
109101
"1.0.0",
110102
"1.0.0-KITTENS",
@@ -113,72 +105,9 @@ Stream<DynamicNode> testIsReleaseMatches(@TempDir final Path tempDir) {
113105
).flatMap(version -> Stream.of(
114106
dynamicTest(version + " - matches, no repo", () -> {
115107
final Project project = IndraTesting.project();
116-
project.getPlugins().apply(GitPlugin.class);
117108
project.setVersion(version);
118109
assertTrue(Versioning.isRelease(project));
119-
}),
120-
dynamicTest(version + " - matches, on tag (non-annotated)", () -> {
121-
final Path testPath = tempDir.resolve(version + "-ontag");
122-
Files.createDirectories(testPath);
123-
final Git repo = initRepoWithCommit(testPath);
124-
125-
repo.tag()
126-
.setName("v" + version)
127-
.call();
128-
129-
final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
130-
project.getPlugins().apply(GitPlugin.class);
131-
project.setVersion(version);
132-
assertTrue(Versioning.isRelease(project));
133-
}),
134-
dynamicTest(version + " - matches, on tag (annotated)", () -> {
135-
final Path testPath = tempDir.resolve(version + "-onannotatedtag");
136-
Files.createDirectories(testPath);
137-
final Git repo = initRepoWithCommit(testPath);
138-
139-
repo.tag()
140-
.setName("v" + version)
141-
.setAnnotated(true)
142-
.setMessage("Release " + version)
143-
.call();
144-
145-
final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
146-
project.getPlugins().apply(GitPlugin.class);
147-
project.setVersion(version);
148-
assertTrue(Versioning.isRelease(project));
149-
}),
150-
dynamicTest(version + " - does not match, with repo no tag", () -> {
151-
final Path testPath = tempDir.resolve(version + "-untagged");
152-
Files.createDirectories(testPath);
153-
initRepoWithCommit(testPath);
154-
155-
final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
156-
project.getPlugins().apply(GitPlugin.class);
157-
project.setVersion(version);
158-
assertFalse(Versioning.isRelease(project));
159110
})
160111
));
161112
}
162-
163-
private static Git initRepoWithCommit(final Path repoDir) throws IOException, GitAPIException {
164-
Files.createDirectories(repoDir);
165-
final Git repo = Git.init()
166-
.setDirectory(repoDir.toFile())
167-
.setInitialBranch("trunk")
168-
.call();
169-
170-
Files.write(repoDir.resolve("gradle.properties"), "filler=test".getBytes(StandardCharsets.UTF_8));
171-
repo.commit()
172-
.setAuthor("CI", "[email protected]")
173-
.setCommitter("CI", "[email protected]")
174-
.setAll(true)
175-
.setMessage("Initial commit")
176-
.call();
177-
178-
return repo;
179-
}
180-
181-
// release matches non-git checkout content
182-
183-
184113
}

indra-git/src/main/java/net/kyori/indra/git/GitPlugin.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,9 @@
2626
import java.io.File;
2727
import net.kyori.indra.git.internal.IndraGitExtensionImpl;
2828
import net.kyori.indra.git.internal.IndraGitService;
29+
import net.kyori.indra.git.task.RepositoryTask;
2930
import net.kyori.indra.git.task.RequireClean;
31+
import net.kyori.indra.git.task.RequireTagged;
3032
import net.kyori.mammoth.ProjectOrSettingsPlugin;
3133
import org.gradle.api.Project;
3234
import org.gradle.api.initialization.Settings;
@@ -44,9 +46,8 @@
4446
*/
4547
public class GitPlugin implements ProjectOrSettingsPlugin {
4648
private static final String EXTENSION_NAME = "indraGit";
47-
private static final String SERVICE_NAME = "indraGitService";
48-
4949
public static final String REQUIRE_CLEAN_TASK = "requireClean";
50+
public static final String REQUIRE_TAGGED_TASK = "requireTagged";
5051

5152
@Override
5253
public void applyToProject(
@@ -63,8 +64,10 @@ public void applyToProject(
6364
target.getDisplayName()
6465
);
6566

66-
// And create a task, but don't ever make it run
67-
tasks.register(REQUIRE_CLEAN_TASK, RequireClean.class, task -> {
67+
// And create some validation tasks, but don't ever make them run
68+
tasks.register(REQUIRE_CLEAN_TASK, RequireClean.class);
69+
tasks.register(REQUIRE_TAGGED_TASK, RequireTagged.class);
70+
tasks.withType(RepositoryTask.class).configureEach(task -> {
6871
task.getGit().set(service);
6972
});
7073
}
@@ -86,7 +89,7 @@ public void applyToSettings(
8689

8790
private Provider<IndraGitService> applyCommon(final @NotNull Gradle gradle, final ExtensionContainer extensions, final File rootDir, final File projectDir, final String displayName) {
8891
// Register the service, then create an extension
89-
final Provider<IndraGitService> service = gradle.getSharedServices().registerIfAbsent(SERVICE_NAME, IndraGitService.class, params -> {
92+
final Provider<IndraGitService> service = gradle.getSharedServices().registerIfAbsent(IndraGitService.SERVICE_NAME, IndraGitService.class, params -> {
9093
params.getParameters().getBaseDirectory().set(rootDir);
9194
});
9295
extensions.create(IndraGitExtension.class, EXTENSION_NAME, IndraGitExtensionImpl.class, projectDir, displayName, service);

indra-git/src/main/java/net/kyori/indra/git/internal/IndraGitExtensionImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -71,11 +71,7 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
7171
}
7272
}
7373

74-
@Override
75-
public @Nullable Ref headTag() {
76-
final @Nullable Git git = this.git();
77-
if(git == null) return null;
78-
74+
public static @Nullable Ref headTag(final Git git) {
7975
try {
8076
final @Nullable Ref head = git.getRepository().findRef(Constants.HEAD);
8177
if(head == null) return null;
@@ -98,6 +94,13 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
9894
return null;
9995
}
10096

97+
@Override
98+
public @Nullable Ref headTag() {
99+
final @Nullable Git git = this.git();
100+
if (git == null) return null;
101+
return headTag(git);
102+
}
103+
101104
@Override
102105
public @Nullable String describe() {
103106
final @Nullable Git git = this.git();

indra-git/src/main/java/net/kyori/indra/git/internal/IndraGitService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -51,6 +51,7 @@
5151
* @since 2.0.0
5252
*/
5353
public abstract class IndraGitService implements BuildService<IndraGitService.Parameters>, AutoCloseable {
54+
public static final String SERVICE_NAME = "indraGitService";
5455
private static final Logger LOGGER = Logging.getLogger(IndraGitService.class);
5556

5657
private volatile boolean open = true;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of indra, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2023 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.indra.git.task;
25+
26+
import net.kyori.indra.git.internal.IndraGitService;
27+
import org.eclipse.jgit.api.Git;
28+
import org.gradle.api.DefaultTask;
29+
import org.gradle.api.file.DirectoryProperty;
30+
import org.gradle.api.provider.Property;
31+
import org.gradle.api.services.ServiceReference;
32+
import org.gradle.api.tasks.Internal;
33+
import org.jetbrains.annotations.Nullable;
34+
35+
/**
36+
* Base class for tasks that work with a {@link Git} repository.
37+
*
38+
* @since 4.0.0
39+
*/
40+
public abstract class RepositoryTask extends DefaultTask {
41+
@ServiceReference(IndraGitService.SERVICE_NAME)
42+
public abstract Property<IndraGitService> getGit();
43+
44+
@Internal
45+
protected abstract DirectoryProperty getProjectDirectory();
46+
47+
@Internal
48+
protected abstract Property<String> getProjectDisplayName();
49+
50+
public RepositoryTask() {
51+
this.getProjectDirectory().fileValue(this.getProject().getProjectDir());
52+
this.getProjectDisplayName().convention(this.getProject().getDisplayName());
53+
}
54+
55+
/**
56+
* Get the actual repo.
57+
*
58+
* @return the repo
59+
*/
60+
protected @Nullable Git repo() {
61+
return this.getGit().get().git(this.getProjectDirectory().get().getAsFile(), this.getProjectDisplayName().get());
62+
}
63+
}

0 commit comments

Comments
 (0)