gradle: добавлена автоматизация версионирования
This commit is contained in:
62
buildSrc/src/main/groovy/LogicPlugin.groovy
Normal file
62
buildSrc/src/main/groovy/LogicPlugin.groovy
Normal file
@@ -0,0 +1,62 @@
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.nio.file.Path
|
||||
|
||||
class LogicPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
void apply(Project project) {
|
||||
project.tasks.register('incrementVersion', IncrementVersion)
|
||||
}
|
||||
}
|
||||
|
||||
class IncrementVersion extends DefaultTask {
|
||||
|
||||
@TaskAction
|
||||
def incrementVersion() {
|
||||
String nextVersion = nextVersion()
|
||||
|
||||
incrementReadmeVersion(nextVersion)
|
||||
incrementGradlePropsVersion(nextVersion)
|
||||
}
|
||||
|
||||
private void incrementReadmeVersion(String nextVersion) {
|
||||
String nextVersion2 = nextVersion.replaceAll(/-/, '--')
|
||||
|
||||
Reader reader = this.getClass().getResource('README.SRC.MD').toURI().toURL().newReader()
|
||||
File readmeFile = project.file(project.getRootDir().toPath().resolve('README.MD'))
|
||||
|
||||
readmeFile.withWriter { wr ->
|
||||
reader.eachLine { line ->
|
||||
wr << line.replaceAll('@VERSION@', nextVersion)
|
||||
.replaceAll('@VERSION2@', nextVersion2)
|
||||
wr << "\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void incrementGradlePropsVersion(String nextVersion) {
|
||||
Path gradlePropertiesPath = project.projectDir.toPath().resolve('gradle.properties');
|
||||
|
||||
Properties gradleProperties = new Properties()
|
||||
gradleProperties.load(gradlePropertiesPath.newReader())
|
||||
gradleProperties.setProperty('moduleVersion', nextVersion)
|
||||
|
||||
gradlePropertiesPath.withWriter(StandardCharsets.UTF_8.name(), { wr ->
|
||||
gradleProperties.each {
|
||||
wr << "${it.key}=${it.value}"
|
||||
wr << "\n"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private String nextVersion() {
|
||||
def matcher = project.property('moduleVersion') =~ /^(.*\d+\.)(\d+)(-.+)?$/
|
||||
matcher.find()
|
||||
return matcher.group(1) + (matcher.group(2).toInteger() + 1) + (matcher.group(3) == null ? '' : matcher.group(3))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user