Repository navigation
Expand file tree
/
Copy pathThriveVersioningPlugin.groovy
More file actions
174 lines (153 loc) · 7.01 KB
/
Copy pathThriveVersioningPlugin.groovy
File metadata and controls
174 lines (153 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.github.thriveframework.plugin
import com.github.thriveframework.plugin.extension.ThriveVersioningExtension
import groovy.util.logging.Slf4j
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.jvm.tasks.Jar
import org.unbrokendome.gradle.plugins.gitversion.GitVersionPlugin
import org.unbrokendome.gradle.plugins.gitversion.version.ImmutableSemVersionImpl
import org.unbrokendome.gradle.plugins.gitversion.version.MutableSemVersion
import org.unbrokendome.gradle.plugins.gitversion.version.SemVersion
import static com.github.thriveframework.utils.plugin.Projects.applyPlugin
import static com.github.thriveframework.utils.plugin.Projects.fullName
import static com.github.thriveframework.utils.plugin.task.VersionTasks.createPrintVersion
import static com.github.thriveframework.utils.plugin.task.VersionTasks.createWriteVersion
@Slf4j
class ThriveVersioningPlugin implements Plugin<Project> {
private ThriveVersioningExtension extension
@Override
void apply(Project project) {
configureExtension(project)
configureVersioning(project)
addVersionTasks(project)
project.afterEvaluate {
configurePublishing(project)
}
}
private void configureExtension(Project project){
extension = project.extensions.create("thriveVersioning", ThriveVersioningExtension)
}
private void configureVersioning(Project project){
applyPlugin(project, GitVersionPlugin)
def semverRegex = /(\d+[.]\d+[.]\d+)/
def versioningContext = [fullyDetermined: false]
//todo what happens when there is not tag at all?
project.gitVersion {
rules {
before {
def tag = findLatestTag(~semverRegex, true)
def taggedVersion
if (tag)
taggedVersion = tag.matches[1]
else
taggedVersion = "0.0.0"
versioningContext.taggedVersion = taggedVersion
def commitsSinceTag = tag ? countCommitsSince(tag) : 0
versioningContext.commitsSinceTag = commitsSinceTag
version = SemVersion.parse(taggedVersion)
versioningContext.lastVersion = SemVersion.parse(taggedVersion)
if (commitsSinceTag > 0 || branchName != "master")
version.incrementMinor()
}
onBranch("master"){
if (versioningContext.commitsSinceTag > 0) {
version.prereleaseTag = "RC"
project.ext {
projectState = "RC"
}
} else {
project.ext {
projectState = "RELEASE"
}
}
versioningContext.fullyDetermined = true
}
onBranch(~semverRegex) {
def candidate = matches[1]
def successors = getSuccessors(versioningContext.lastVersion)
log.debug("Acceptable version successors: $successors")
assert successors.contains("$candidate"), "Current development branch should be one of ${successors}, but is ${candidate}! (context: $versioningContext)"
version = MutableSemVersion.parse(candidate)
version.prereleaseTag = "SNAPSHOT"
project.ext {
projectState = "SNAPSHOT"
}
versioningContext.fullyDetermined = true
}
after {
if (!versioningContext.fullyDetermined){
version.prereleaseTag = "FEATURE"
version.buildMetadata = "${branchName.hashCode()}_${versioningContext.commitsSinceTag}"
versioningContext.fullyDetermined = true
project.ext {
projectState = "FEATURE"
}
}
}
}
}
project.version = project.gitVersion.determineVersion()
}
private List<String> getSuccessors(SemVersion version){
[
"${new ImmutableSemVersionImpl(version.major+1, 0, 0, "", "")}",
"${new ImmutableSemVersionImpl(version.major, version.minor+1, 0, "", "")}",
"${new ImmutableSemVersionImpl(version.major, version.minor, version.patch+1, "", "")}",
]
}
private void addVersionTasks(Project project){
createWriteVersion(project)
def versionPrefix = "Project ${fullName(project)} version: "
def statePrefix = "Project ${fullName(project)} state: "
createPrintVersion(project, versionPrefix).doLast {
println "$statePrefix ${project.projectState}"
}
}
private void configurePublishing(Project project){
if (extension.configurePublishing.get()){
log.info("Configuring publishing")
applyPlugin(project, 'maven-publish')
project.sourceCompatibility = 1.8
project.targetCompatibility = 1.8
project.tasks.create("sourcesJar", Jar) {
archiveClassifier = 'sources'
from project.sourceSets.main.allSource
}
project.tasks.create("javadocJar", Jar) {
archiveClassifier = 'javadoc'
from project.javadoc.destinationDir
}
project.publishing {
publications {
main(MavenPublication) {
from project.components.java
artifact project.sourcesJar
artifact project.javadocJar
}
}
}
configureJitpack(project)
} else {
log.info("Publishing nor JitPack not configured")
}
}
private void configureJitpack(Project project){
if (extension.configureForJitpack.get()){
log.info("Configuring JitPack")
project.publishMainPublicationToMavenLocal.dependsOn project.build
//todo this feature is broken https://github.com/thrive-framework/thrive-versioning-plugin/issues/7
// if (project.parent) {// this is not a root project
//change group so that it matches JitPack convention for multi-project
// builds
// see: https://jitpack.io/docs/BUILDING/#multi-module-projects
//todo does assumption rootProject.name == repository name hold?
// project.group = "${project.rootProject.group}.${project.rootProject.name}"
//todo this solution won't take more than two levels into account
// can gradle even support multi-level project structure?
// }
} else {
log.info("JitPack not configured")
}
}
}