-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
359 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
gradle-plugin/src/main/java/org/dmfs/gradle/gver/tasks/ApplicableReleaseTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.dmfs.gradle.gver.tasks; | ||
|
||
import org.dmfs.gver.dsl.GitVersionConfig; | ||
import org.dmfs.gver.dsl.ReleaseType; | ||
import org.dmfs.jems2.iterable.DelegatingIterable; | ||
import org.dmfs.jems2.iterable.Mapped; | ||
import org.dmfs.jems2.iterable.Sieved; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.revwalk.RevCommit; | ||
|
||
import java.util.Map; | ||
|
||
public final class ApplicableReleaseTypes extends DelegatingIterable<ReleaseType> | ||
{ | ||
public ApplicableReleaseTypes(GitVersionConfig config, Repository repository, RevCommit revCommit, String name) | ||
{ | ||
super(new Mapped<>(Map.Entry::getValue, | ||
new Sieved<>(entry -> entry.getKey().matches(repository, revCommit, name), | ||
config.mTagConfig.mTagMappings.entrySet()))); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
gradle-plugin/src/main/java/org/dmfs/gradle/gver/tasks/TagPreReleaseTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.dmfs.gradle.gver.tasks; | ||
|
||
import org.dmfs.gradle.gver.tasks.procedure.CreateTag; | ||
import org.dmfs.gradle.gver.utils.ProjectRepositoryFunction; | ||
import org.dmfs.gver.dsl.GitVersionConfig; | ||
import org.dmfs.semver.StrictParser; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.Task; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.dmfs.gver.dsl.ReleaseType.PRERELEASE; | ||
|
||
|
||
/** | ||
* A Gradle {@link Task} that tags the current HEAD with a new pre-release version. | ||
* Note, that this task will throw an {@link IllegalStateException} when the current HEAD is not on a pre-release branch. | ||
*/ | ||
public class TagPreReleaseTask extends DefaultTask | ||
{ | ||
public TagPreReleaseTask() | ||
{ | ||
setGroup("gver"); | ||
setDescription("Tags the HEAD commit with a new pre-release version."); | ||
} | ||
|
||
@TaskAction | ||
public void perform() throws IOException, GitAPIException | ||
{ | ||
try (Repository repository = ProjectRepositoryFunction.INSTANCE.value(getProject())) | ||
{ | ||
new CreateTag( | ||
getLogger(), | ||
repository, | ||
(GitVersionConfig) getProject().getExtensions().getByName("gver"), | ||
new StrictParser().parse(getProject().getVersion().toString())) | ||
.process(PRERELEASE::equals); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
gradle-plugin/src/main/java/org/dmfs/gradle/gver/tasks/procedure/CreateTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.dmfs.gradle.gver.tasks.procedure; | ||
|
||
import org.dmfs.gradle.gver.tasks.ApplicableReleaseTypes; | ||
import org.dmfs.gver.dsl.GitVersionConfig; | ||
import org.dmfs.gver.dsl.ReleaseType; | ||
import org.dmfs.jems2.FragileProcedure; | ||
import org.dmfs.jems2.Optional; | ||
import org.dmfs.jems2.Predicate; | ||
import org.dmfs.jems2.iterable.Mapped; | ||
import org.dmfs.jems2.optional.First; | ||
import org.dmfs.semver.Version; | ||
import org.dmfs.semver.VersionSequence; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.lib.ObjectId; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.revwalk.RevCommit; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.logging.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.eclipse.jgit.lib.Constants.R_TAGS; | ||
|
||
public final class CreateTag implements FragileProcedure<Predicate<ReleaseType>, IOException> | ||
{ | ||
private final Logger mLogger; | ||
private final Repository mRepository; | ||
private final GitVersionConfig mGitVersionConfig; | ||
private final Version mVersion; | ||
|
||
public CreateTag(Logger logger, Repository repository, GitVersionConfig gitVersionConfig, Version version) | ||
{ | ||
mLogger = logger; | ||
mRepository = repository; | ||
mGitVersionConfig = gitVersionConfig; | ||
mVersion = version; | ||
} | ||
|
||
@Override | ||
public void process(Predicate<ReleaseType> releaseTypePredicate) throws IOException | ||
{ | ||
try | ||
{ | ||
Git git = new Git(mRepository); | ||
|
||
if (git.status().call().hasUncommittedChanges()) | ||
{ | ||
throw new GradleException( | ||
"The Git working tree must not be dirty. Please commit, stash or revert any uncommitted changes." | ||
); | ||
} | ||
|
||
ObjectId headId = mRepository.resolve("HEAD"); | ||
RevCommit head = mRepository.parseCommit(headId); | ||
|
||
Optional<String> versionString = | ||
new org.dmfs.jems2.optional.Mapped<>( | ||
Object::toString, | ||
new org.dmfs.jems2.optional.Mapped<>( | ||
VersionSequence::new, | ||
new org.dmfs.jems2.optional.Mapped<>( | ||
releaseType -> releaseType.value(mVersion), | ||
new First<>( | ||
releaseTypePredicate, | ||
new ApplicableReleaseTypes(mGitVersionConfig, mRepository, head, mRepository.getBranch()))))); | ||
|
||
if (!versionString.isPresent()) | ||
{ | ||
throw new RuntimeException("No release type found that's applicable to the current head"); | ||
} | ||
|
||
if (!new First<>(versionString.value()::equals, | ||
new Mapped<>(tag -> tag.getName().startsWith(R_TAGS) ? tag.getName().substring(R_TAGS.length()) : tag.getName(), | ||
git.tagList().call())).isPresent()) | ||
{ | ||
git.tag() | ||
.setObjectId(head) | ||
.setName(versionString.value()) | ||
.call(); | ||
} | ||
else | ||
{ | ||
mLogger.lifecycle("Tag {} already exists. Not adding tag.", versionString.value()); | ||
} | ||
} | ||
catch (GitAPIException apiException) | ||
{ | ||
throw new IOException("Error while executing Git Command", apiException); | ||
} | ||
} | ||
} |
Oops, something went wrong.