Skip to content

Commit

Permalink
feat(common,git): Replace versioning tag check with task-based tag check
Browse files Browse the repository at this point in the history
Closes GH-139
  • Loading branch information
zml2008 committed Aug 29, 2023
1 parent 8183dab commit 8a806e4
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,7 +31,6 @@
import net.kyori.indra.api.model.License;
import net.kyori.indra.api.model.SourceCodeManagement;
import net.kyori.indra.git.GitPlugin;
import net.kyori.indra.git.task.RequireClean;
import net.kyori.indra.util.Versioning;
import net.kyori.mammoth.ProjectPlugin;
import org.gradle.api.Action;
Expand Down Expand Up @@ -117,16 +116,22 @@ public void apply(final @NotNull Project project, final @NotNull PluginContainer
}
});

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

final TaskProvider<RequireClean> requireClean = tasks.named(GitPlugin.REQUIRE_CLEAN_TASK, RequireClean.class);
final TaskProvider<?> requireClean = tasks.named(GitPlugin.REQUIRE_CLEAN_TASK);
final TaskProvider<?> requireTagged = tasks.named(GitPlugin.REQUIRE_TAGGED_TASK);
requireTagged.configure(task -> {
task.getInputs().property("isRelease", isRelease);
task.onlyIf(t -> isRelease.get());
});

tasks.withType(AbstractPublishToMaven.class).configureEach(task -> {
if (!(task instanceof PublishToMavenLocal)) {
task.dependsOn(requireClean);
task.dependsOn(requireClean, requireTagged);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,12 +23,9 @@
*/
package net.kyori.indra.util;

import net.kyori.indra.git.IndraGitExtension;
import org.eclipse.jgit.lib.Ref;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

private Versioning() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,21 +23,13 @@
*/
package net.kyori.indra.util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import net.kyori.indra.git.GitPlugin;
import net.kyori.indra.test.IndraTesting;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -104,7 +96,7 @@ void testIsReleaseDoesNotMatch(final String version) {
}

@TestFactory
Stream<DynamicNode> testIsReleaseMatches(@TempDir final Path tempDir) {
Stream<DynamicNode> testIsReleaseMatches() {
return Stream.of(
"1.0.0",
"1.0.0-KITTENS",
Expand All @@ -113,72 +105,9 @@ Stream<DynamicNode> testIsReleaseMatches(@TempDir final Path tempDir) {
).flatMap(version -> Stream.of(
dynamicTest(version + " - matches, no repo", () -> {
final Project project = IndraTesting.project();
project.getPlugins().apply(GitPlugin.class);
project.setVersion(version);
assertTrue(Versioning.isRelease(project));
}),
dynamicTest(version + " - matches, on tag (non-annotated)", () -> {
final Path testPath = tempDir.resolve(version + "-ontag");
Files.createDirectories(testPath);
final Git repo = initRepoWithCommit(testPath);

repo.tag()
.setName("v" + version)
.call();

final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
project.getPlugins().apply(GitPlugin.class);
project.setVersion(version);
assertTrue(Versioning.isRelease(project));
}),
dynamicTest(version + " - matches, on tag (annotated)", () -> {
final Path testPath = tempDir.resolve(version + "-onannotatedtag");
Files.createDirectories(testPath);
final Git repo = initRepoWithCommit(testPath);

repo.tag()
.setName("v" + version)
.setAnnotated(true)
.setMessage("Release " + version)
.call();

final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
project.getPlugins().apply(GitPlugin.class);
project.setVersion(version);
assertTrue(Versioning.isRelease(project));
}),
dynamicTest(version + " - does not match, with repo no tag", () -> {
final Path testPath = tempDir.resolve(version + "-untagged");
Files.createDirectories(testPath);
initRepoWithCommit(testPath);

final Project project = IndraTesting.project(b -> b.withProjectDir(testPath.toFile()));
project.getPlugins().apply(GitPlugin.class);
project.setVersion(version);
assertFalse(Versioning.isRelease(project));
})
));
}

private static Git initRepoWithCommit(final Path repoDir) throws IOException, GitAPIException {
Files.createDirectories(repoDir);
final Git repo = Git.init()
.setDirectory(repoDir.toFile())
.setInitialBranch("trunk")
.call();

Files.write(repoDir.resolve("gradle.properties"), "filler=test".getBytes(StandardCharsets.UTF_8));
repo.commit()
.setAuthor("CI", "[email protected]")
.setCommitter("CI", "[email protected]")
.setAll(true)
.setMessage("Initial commit")
.call();

return repo;
}

// release matches non-git checkout content


}
15 changes: 9 additions & 6 deletions indra-git/src/main/java/net/kyori/indra/git/GitPlugin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,7 +26,9 @@
import java.io.File;
import net.kyori.indra.git.internal.IndraGitExtensionImpl;
import net.kyori.indra.git.internal.IndraGitService;
import net.kyori.indra.git.task.RepositoryTask;
import net.kyori.indra.git.task.RequireClean;
import net.kyori.indra.git.task.RequireTagged;
import net.kyori.mammoth.ProjectOrSettingsPlugin;
import org.gradle.api.Project;
import org.gradle.api.initialization.Settings;
Expand All @@ -44,9 +46,8 @@
*/
public class GitPlugin implements ProjectOrSettingsPlugin {
private static final String EXTENSION_NAME = "indraGit";
private static final String SERVICE_NAME = "indraGitService";

public static final String REQUIRE_CLEAN_TASK = "requireClean";
public static final String REQUIRE_TAGGED_TASK = "requireTagged";

@Override
public void applyToProject(
Expand All @@ -63,8 +64,10 @@ public void applyToProject(
target.getDisplayName()
);

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

private Provider<IndraGitService> applyCommon(final @NotNull Gradle gradle, final ExtensionContainer extensions, final File rootDir, final File projectDir, final String displayName) {
// Register the service, then create an extension
final Provider<IndraGitService> service = gradle.getSharedServices().registerIfAbsent(SERVICE_NAME, IndraGitService.class, params -> {
final Provider<IndraGitService> service = gradle.getSharedServices().registerIfAbsent(IndraGitService.SERVICE_NAME, IndraGitService.class, params -> {
params.getParameters().getBaseDirectory().set(rootDir);
});
extensions.create(IndraGitExtension.class, EXTENSION_NAME, IndraGitExtensionImpl.class, projectDir, displayName, service);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -71,11 +71,7 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
}
}

@Override
public @Nullable Ref headTag() {
final @Nullable Git git = this.git();
if(git == null) return null;

public static @Nullable Ref headTag(final Git git) {
try {
final @Nullable Ref head = git.getRepository().findRef(Constants.HEAD);
if(head == null) return null;
Expand All @@ -98,6 +94,13 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
return null;
}

@Override
public @Nullable Ref headTag() {
final @Nullable Git git = this.git();
if (git == null) return null;
return headTag(git);
}

@Override
public @Nullable String describe() {
final @Nullable Git git = this.git();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -51,6 +51,7 @@
* @since 2.0.0
*/
public abstract class IndraGitService implements BuildService<IndraGitService.Parameters>, AutoCloseable {
public static final String SERVICE_NAME = "indraGitService";
private static final Logger LOGGER = Logging.getLogger(IndraGitService.class);

private volatile boolean open = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of indra, licensed under the MIT License.
*
* Copyright (c) 2023 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.indra.git.task;

import net.kyori.indra.git.internal.IndraGitService;
import org.eclipse.jgit.api.Git;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.services.ServiceReference;
import org.gradle.api.tasks.Internal;
import org.jetbrains.annotations.Nullable;

/**
* Base class for tasks that work with a {@link Git} repository.
*
* @since 4.0.0
*/
public abstract class RepositoryTask extends DefaultTask {
@Internal
@ServiceReference(IndraGitService.SERVICE_NAME)
public abstract Property<IndraGitService> getGit();

@Internal
protected abstract DirectoryProperty getProjectDirectory();

@Internal
protected abstract Property<String> getProjectDisplayName();

public RepositoryTask() {
this.getProjectDirectory().fileValue(this.getProject().getProjectDir());
this.getProjectDisplayName().convention(this.getProject().getDisplayName());
}

/**
* Get the actual repo.
*
* @return the repo
*/
protected @Nullable Git repo() {
return this.getGit().get().git(this.getProjectDirectory().get().getAsFile(), this.getProjectDisplayName().get());
}
}
Loading

0 comments on commit 8a806e4

Please sign in to comment.