Added increment major/minor/patch and git-tag
parent
a6fe2d4fdc
commit
97b91753a7
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
|||
- Version -
|
||||
- VersionUpdater -
|
||||
https://git.dorkbox.com/dorkbox/Version
|
||||
Copyright 2012 - The Apache Software License, Version 2.0
|
||||
dorkbox, llc
|
||||
|
|
218
LICENSE.Apachev2
218
LICENSE.Apachev2
|
@ -0,0 +1,218 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
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.
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
163
build.gradle
163
build.gradle
|
@ -13,8 +13,6 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import dorkbox.license.License
|
||||
|
||||
import java.nio.file.Paths
|
||||
import java.time.Instant
|
||||
|
||||
|
@ -29,14 +27,18 @@ buildscript {
|
|||
project.ext.set(key, val)
|
||||
}
|
||||
|
||||
// for plugin publishing
|
||||
repositories {
|
||||
maven {
|
||||
url "https://plugins.gradle.org/m2/"
|
||||
url 'https://plugins.gradle.org/m2/'
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
classpath "com.gradle.publish:plugin-publish-plugin:0.9.10"
|
||||
// for plugin publishing
|
||||
classpath 'com.gradle.publish:plugin-publish-plugin:0.9.10'
|
||||
|
||||
// for license sources
|
||||
classpath 'gradle.plugin.com.dorkbox:Licensing:1.2.2'
|
||||
classpath 'gradle.plugin.com.dorkbox:Licensing:1.2.2:sources'
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,20 +48,20 @@ plugins {
|
|||
id 'maven-publish'
|
||||
id 'signing'
|
||||
|
||||
id "org.jetbrains.kotlin.jvm" version "1.2.51"
|
||||
id "com.dorkbox.Licensing" version "1.1.0"
|
||||
id 'com.dorkbox.Licensing' version '1.1.0'
|
||||
id 'org.jetbrains.kotlin.jvm' version '1.2.60'
|
||||
}
|
||||
|
||||
// for publishing the plugin to gradle
|
||||
apply plugin: "com.gradle.plugin-publish"
|
||||
apply plugin: 'com.gradle.plugin-publish'
|
||||
|
||||
|
||||
project.description = 'Plugin to set version information in maven POM, java or kotlin files, and git tags'
|
||||
project.group = 'com.dorkbox'
|
||||
project.version = '1.0.0'
|
||||
|
||||
project.ext.name = 'Gradle Licensing Plugin'
|
||||
project.ext.id = 'Version'
|
||||
project.ext.name = 'Gradle Version Update Plugin'
|
||||
project.ext.id = 'VersionUpdater'
|
||||
project.ext.website = 'https://dorkbox.com/'
|
||||
project.ext.url = 'https://git.dorkbox.com/dorkbox/Version'
|
||||
project.ext.tags = ['version', 'versioning']
|
||||
|
@ -88,36 +90,14 @@ repositories {
|
|||
|
||||
|
||||
dependencies {
|
||||
implementation(project('Utilities')) {
|
||||
// don't include any of the project dependencies for anything
|
||||
transitive = false
|
||||
}
|
||||
compileOnly 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.60'
|
||||
|
||||
implementation 'org.eclipse.jgit:org.eclipse.jgit:4.5.4+'
|
||||
|
||||
compileOnly 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51'
|
||||
implementation 'com.dorkbox:Version:1.0'
|
||||
|
||||
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
|
||||
|
||||
runtime group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.6'
|
||||
}
|
||||
|
||||
|
||||
project('Utilities') {
|
||||
tasks.withType(Test) {
|
||||
// want to remove utilities project from unit tests. It's unnecessary to run unit tests for the entire Utilities project
|
||||
exclude('**/*')
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
options.encoding = 'UTF-8'
|
||||
options.incremental = true
|
||||
options.fork = true
|
||||
options.forkOptions.executable = 'javac'
|
||||
|
||||
// setup compile options. we specifically want to suppress usage of "Unsafe"
|
||||
options.compilerArgs = ['-XDignore.symbol.file', '-Xlint:deprecation']
|
||||
}
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
|
@ -134,40 +114,13 @@ tasks.withType(Jar) {
|
|||
}
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
////// UTILITIES COMPILE (for inclusion into jars)
|
||||
///////////////////////////////
|
||||
task compileUtils(type: JavaCompile) {
|
||||
// we don't want the default include of **/*.java
|
||||
getIncludes().clear()
|
||||
|
||||
source = Collections.singletonList('../Utilities/src/dorkbox/util')
|
||||
include('Version.java')
|
||||
|
||||
classpath = sourceSets.main.compileClasspath
|
||||
destinationDir = file("$rootDir/build/classes_utilities")
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
////// Jar Tasks
|
||||
///////////////////////////////
|
||||
jar {
|
||||
dependsOn compileUtils
|
||||
|
||||
// include applicable class files from subset of Utilities project
|
||||
from compileUtils.destinationDir
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
///////////////////////////////
|
||||
////// Plugin Publishing + Release
|
||||
///////////////////////////////
|
||||
gradlePlugin {
|
||||
plugins {
|
||||
Version {
|
||||
id = 'com.dorkbox.Version'
|
||||
id = 'com.dorkbox.VersionUpdater'
|
||||
implementationClass = 'dorkbox.version.VersionPlugin'
|
||||
}
|
||||
}
|
||||
|
@ -187,87 +140,3 @@ pluginBundle {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
///////////////////////////////
|
||||
////// Maven Publishing + Release
|
||||
///////////////////////////////
|
||||
task sourceJar(type: Jar) {
|
||||
description = "Creates a JAR that contains the source code."
|
||||
|
||||
from sourceSets.main.allSource
|
||||
|
||||
classifier = "sources"
|
||||
}
|
||||
|
||||
task javaDocJar(type: Jar) {
|
||||
description = "Creates a JAR that contains the javadocs."
|
||||
|
||||
classifier = "javadoc"
|
||||
}
|
||||
|
||||
// for testing, we don't publish to maven central, but only to local maven
|
||||
publishing {
|
||||
publications {
|
||||
version(MavenPublication) {
|
||||
from components.java
|
||||
|
||||
artifact(javaDocJar)
|
||||
artifact(sourceJar)
|
||||
|
||||
groupId project.group
|
||||
artifactId project.ext.id
|
||||
version project.version
|
||||
|
||||
pom {
|
||||
name = project.ext.id
|
||||
url = project.ext.url
|
||||
description = project.description
|
||||
|
||||
issueManagement {
|
||||
url = "${project.ext.url}/issues".toString()
|
||||
system = 'Gitea Issues'
|
||||
}
|
||||
|
||||
organization {
|
||||
name = 'dorkbox, llc'
|
||||
url = 'https://dorkbox.com'
|
||||
}
|
||||
|
||||
developers {
|
||||
developer {
|
||||
name = 'dorkbox, llc'
|
||||
email = 'email@dorkbox.com'
|
||||
}
|
||||
}
|
||||
|
||||
scm {
|
||||
url = project.ext.url
|
||||
connection = "scm:${project.ext.url}.git".toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// we don't use maven with the plugin (it's uploaded separately to gradle plugins)
|
||||
tasks.withType(PublishToMavenRepository) {
|
||||
onlyIf {
|
||||
repository == publishing.repositories.maven &&
|
||||
publication == publishing.publications.version
|
||||
}
|
||||
}
|
||||
tasks.withType(PublishToMavenLocal) {
|
||||
onlyIf {
|
||||
publication == publishing.publications.version
|
||||
}
|
||||
}
|
||||
|
||||
signing {
|
||||
required { hasProperty('sonatypeUsername') }
|
||||
sign publishing.publications.version
|
||||
}
|
||||
|
|
|
@ -13,14 +13,4 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
include("Utilities")
|
||||
|
||||
for (project in rootProject.children) {
|
||||
project.projectDir = file("../$project.name")
|
||||
project.buildFileName = "build.gradle"
|
||||
|
||||
assert (project.projectDir.directory)
|
||||
assert (project.buildFile.file)
|
||||
}
|
||||
|
||||
enableFeaturePreview('STABLE_PUBLISHING')
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package dorkbox.version
|
||||
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.internal.storage.file.FileRepository
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
|
||||
class
|
||||
GitTasks {
|
||||
companion object {
|
||||
internal fun getGit(project: Project): Git {
|
||||
try {
|
||||
val gitDir = getRootGitDir(project.projectDir)
|
||||
return Git.wrap(FileRepository(gitDir))
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getRootGitDir(currentRoot: File): File {
|
||||
val gitDir = scanForRootGitDir(currentRoot)
|
||||
if (!gitDir.exists()) {
|
||||
throw IllegalArgumentException("Cannot find '.git' directory")
|
||||
}
|
||||
return gitDir
|
||||
}
|
||||
|
||||
private fun scanForRootGitDir(currentRoot: File): File {
|
||||
val gitDir = File(currentRoot, ".git")
|
||||
|
||||
if (gitDir.exists()) {
|
||||
return gitDir
|
||||
}
|
||||
|
||||
// always stop at the root directory
|
||||
return if (currentRoot.parentFile == null) {
|
||||
gitDir
|
||||
}
|
||||
else scanForRootGitDir(currentRoot.parentFile)
|
||||
}
|
||||
}
|
||||
|
||||
open class CreateTag : DefaultTask() {
|
||||
@TaskAction
|
||||
fun run() {
|
||||
// make sure there are no uncommitted files, only exception is build files + files with version info
|
||||
// because it is natural to expect that creating a git tag would happen IMMEDIATELY after incrementing the version
|
||||
|
||||
val version = VersionPlugin.getVersion(project)
|
||||
|
||||
|
||||
// make sure all code is committed (no un-committed files and no staged files). Throw error and exit if there is
|
||||
val git = getGit(project)
|
||||
val status = git.status().call()
|
||||
|
||||
if (status.hasUncommittedChanges()) {
|
||||
// have to get the git "root" project location (for which all of the uncommitted files are based on)
|
||||
val subStringLength = git.repository.directory.parentFile.absolutePath.length + 1
|
||||
|
||||
// combo of added/changed/removed/missing/modified/conflicting.
|
||||
val uncommittedFiles = status.uncommittedChanges
|
||||
|
||||
// have to change the full path, to it's relative path to the .git root dir
|
||||
val filesWithVersionInfo = VersionPlugin.verifyVersion(project, version).map { data -> data.file.absolutePath.substring(subStringLength) }
|
||||
|
||||
uncommittedFiles.removeAll(filesWithVersionInfo)
|
||||
|
||||
if (uncommittedFiles.isNotEmpty()) {
|
||||
println("Please commit or stash the following files: $uncommittedFiles")
|
||||
throw GradleException("Cannot create and save a GIT tag when files have not been committed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// the ony files now allowed to not be committed are our build + other version info files.
|
||||
val tagName = "Version_$version"
|
||||
|
||||
// do we already have this tag?
|
||||
val call = git.tagList().call()
|
||||
for (ref in call) {
|
||||
if (ref.name == tagName) {
|
||||
throw GradleException("Tag $tagName already exists. Please delete the old tag in order to continue.")
|
||||
}
|
||||
}
|
||||
|
||||
// create a git tag on the current HEAD
|
||||
val tag = git.tag().setName(tagName).call()
|
||||
println("Successfully created tag $tagName")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package dorkbox.version
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
class
|
||||
IncrementTasks {
|
||||
open class Major : DefaultTask() {
|
||||
@TaskAction
|
||||
fun run() {
|
||||
val version = VersionPlugin.getVersion(project)
|
||||
|
||||
// make an update to the version number based on major/minor/patch
|
||||
val incrementMajorVersion = version.incrementMajorVersion()
|
||||
|
||||
println("Incrementing Major version from $version -> $incrementMajorVersion")
|
||||
|
||||
// update the all java files and build file with the new version number
|
||||
VersionPlugin.saveVersionInFiles(project, version, incrementMajorVersion)
|
||||
}
|
||||
}
|
||||
|
||||
open class Minor : DefaultTask() {
|
||||
@TaskAction
|
||||
fun run() {
|
||||
val version = VersionPlugin.getVersion(project)
|
||||
|
||||
// make an update to the version number based on major/minor/patch
|
||||
val incrementMinorVersion = version.incrementMinorVersion()
|
||||
|
||||
println("Incrementing Minor version from $version -> $incrementMinorVersion")
|
||||
|
||||
// update the all java files and build file with the new version number
|
||||
VersionPlugin.saveVersionInFiles(project, version, incrementMinorVersion)
|
||||
}
|
||||
}
|
||||
|
||||
open class Patch : DefaultTask() {
|
||||
@TaskAction
|
||||
fun run() {
|
||||
val version = VersionPlugin.getVersion(project)
|
||||
|
||||
// make an update to the version number based on major/minor/patch
|
||||
val incrementPatchVersion = version.incrementPatchVersion()
|
||||
|
||||
println("Incrementing Patch version from $version -> $incrementPatchVersion")
|
||||
|
||||
// update the all java files and build file with the new version number
|
||||
VersionPlugin.saveVersionInFiles(project, version, incrementPatchVersion)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +1,158 @@
|
|||
package dorkbox.version
|
||||
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.internal.storage.file.FileRepository
|
||||
import com.dorkbox.version.Version
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.plugins.JavaPluginConvention
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* For automatically updating version information based on a MAJOR, MINOR, or PATCH update based on a build definition file
|
||||
* For automatically setting version information based on a MAJOR, MINOR, or PATCH update based on a build definition file
|
||||
*/
|
||||
class VersionPlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) {
|
||||
|
||||
project.tasks.create("incrementMajor", IncrementTasks.Major::class.java).apply {
|
||||
group = "version"
|
||||
}
|
||||
project.tasks.create("incrementMinor", IncrementTasks.Minor::class.java).apply {
|
||||
group = "version"
|
||||
}
|
||||
project.tasks.create("incrementPatch", IncrementTasks.Patch::class.java).apply {
|
||||
group = "version"
|
||||
}
|
||||
project.tasks.create("createGitTag", GitTasks.CreateTag::class.java).apply {
|
||||
group = "version"
|
||||
}
|
||||
|
||||
project.afterEvaluate {
|
||||
// get the version info from project.version
|
||||
val version = project.version
|
||||
// just make sure that we have a version defined.
|
||||
val version = project.version.toString()
|
||||
project.version.toString()
|
||||
|
||||
val javaText = """String getVersion\(\) \{""".toRegex()
|
||||
val kotlinText = """getVersion\(\) : String \{""".toRegex()
|
||||
if (version.isBlank() || version == Project.DEFAULT_VERSION) {
|
||||
// no version info specified, but version task was called
|
||||
throw GradleException("Project version information is unset. Please set via `project.version = '1.0.0'`")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val versionText = """(return ")(.*)(")""".toRegex()
|
||||
|
||||
companion object {
|
||||
private val buildText = """version""".toRegex()
|
||||
private val javaText = """String getVersion\(\) \{""".toRegex()
|
||||
private val kotlinText = """getVersion\(\) : String \{""".toRegex()
|
||||
|
||||
private val versionText = """(return ")(.*)(")""".toRegex()
|
||||
// version = '1.0.0'
|
||||
// project.version = '1.0.0'
|
||||
private val buildFileVersionText = """(.*version\s*=\s*'|"\s*)(.*)('|")""".toRegex()
|
||||
|
||||
// val readmeOrigText = "return \"" + original.toString() + "\";"
|
||||
// val readmeNewText = "return \"" + toString() + "\";"
|
||||
|
||||
data class VerData(val file: File, val line: Int, val version: String, val isBuildFile: Boolean)
|
||||
|
||||
|
||||
/**
|
||||
* Get's the version info from the project
|
||||
*/
|
||||
fun getVersion(project: Project): Version {
|
||||
// get the version info from project.version
|
||||
val version = project.version.toString()
|
||||
|
||||
if (version.isBlank() || version == Project.DEFAULT_VERSION) {
|
||||
// no version info specified, but version task was called
|
||||
throw GradleException("Project version information is unset.")
|
||||
}
|
||||
|
||||
return Version.from(version)
|
||||
}
|
||||
|
||||
|
||||
fun saveVersionInFiles(project: Project, oldVersion: Version, newVersion: Version) {
|
||||
// Verifies that all of the project files are set to the specified version
|
||||
val filesWithVersionInfo = verifyVersion(project, oldVersion)
|
||||
|
||||
// now save the NEW version to all of the files (this also has our build file)
|
||||
filesWithVersionInfo.forEach { data ->
|
||||
run {
|
||||
var lineNumber = 1 // visual editors start at 1, so we should too
|
||||
val tempFile = createTempFile()
|
||||
|
||||
tempFile.printWriter().use { writer ->
|
||||
when {
|
||||
data.file.extension == "java" -> {
|
||||
data.file.useLines { lines ->
|
||||
lines.forEach { line ->
|
||||
if (lineNumber == data.line) {
|
||||
writer.println(line.replace(oldVersion.toString(), newVersion.toString()))
|
||||
println("Updating file '${data.file}' to version $newVersion at line $lineNumber")
|
||||
}
|
||||
else {
|
||||
writer.println(line)
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
data.file.extension == "kt" -> {
|
||||
data.file.useLines { lines ->
|
||||
lines.forEach { line ->
|
||||
if (lineNumber == data.line) {
|
||||
writer.println(line.replace(oldVersion.toString(), newVersion.toString()))
|
||||
println("Updating file '${data.file}' to version $newVersion at line $lineNumber")
|
||||
}
|
||||
else {
|
||||
writer.println(line)
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
data.isBuildFile -> {
|
||||
data.file.useLines { lines ->
|
||||
lines.forEach { line ->
|
||||
if (lineNumber == data.line) {
|
||||
writer.println(line.replace(oldVersion.toString(), newVersion.toString()))
|
||||
println("Updating file '${data.file}' to version $newVersion at line $lineNumber")
|
||||
}
|
||||
else {
|
||||
writer.println(line)
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// shouldn't get here, but just in case...
|
||||
throw GradleException("Cannot process unknown file type ${data.file}. Aborting")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check(data.file.delete() && tempFile.renameTo(data.file)) { "Failed to replace file" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that all of the project files are set to the specified version
|
||||
*/
|
||||
internal fun verifyVersion(project: Project, version: Version): List<VerData> {
|
||||
val alreadyParsedFiles = getSourceFiles(project)
|
||||
val filesWithVersionInfo = HashMap<File, String>()
|
||||
val filesWithVersionInfo = HashMap<File, VerData>()
|
||||
|
||||
|
||||
// collect all of the class files that have a version defined (look in source sets. must match our pre-defined pattern)
|
||||
alreadyParsedFiles.forEach { file ->
|
||||
run fileCheck@{
|
||||
var matchesText = false;
|
||||
var matchesText = false
|
||||
var lineNumber = 1 // visual editors start at 1, so we should too
|
||||
|
||||
if (file.extension == "java") {
|
||||
file.useLines { lines ->
|
||||
lines.forEach { line ->
|
||||
|
@ -46,10 +163,11 @@ class VersionPlugin : Plugin<Project> {
|
|||
val matchResult = versionText.find(line)
|
||||
if (matchResult != null) {
|
||||
val (_, ver, _) = matchResult.destructured
|
||||
filesWithVersionInfo[file] = ver
|
||||
filesWithVersionInfo[file] = VerData(file, lineNumber, ver, false)
|
||||
return@fileCheck
|
||||
}
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,84 +181,61 @@ class VersionPlugin : Plugin<Project> {
|
|||
val matchResult = versionText.find(line)
|
||||
if (matchResult != null) {
|
||||
val (_, ver, _) = matchResult.destructured
|
||||
filesWithVersionInfo[file] = ver
|
||||
filesWithVersionInfo[file] = VerData(file, lineNumber, ver, false)
|
||||
return@fileCheck
|
||||
}
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get version info by file parsing from gradle.build file
|
||||
project.buildFile.useLines { lines ->
|
||||
var lineNumber = 1 // visual editors start at 1, so we should too
|
||||
|
||||
lines.forEach { line ->
|
||||
if (line.contains(buildText)) {
|
||||
val matchResult = buildFileVersionText.find(line)
|
||||
if (matchResult != null) {
|
||||
val (_, ver, _) = matchResult.destructured
|
||||
// verify it's what we think it is
|
||||
if (ver == version.toString()) {
|
||||
filesWithVersionInfo[project.buildFile] = VerData(project.buildFile, lineNumber, ver, true)
|
||||
return@useLines
|
||||
}
|
||||
}
|
||||
}
|
||||
lineNumber++
|
||||
}
|
||||
}
|
||||
|
||||
// make sure version info all match (throw error and exit if they do not)
|
||||
filesWithVersionInfo.forEach { f, v ->
|
||||
if (v != version) {
|
||||
throw GradleException("Version information mismatch, expected $version, got $v in file: $f")
|
||||
// println("Verifying file '$f' for version '${v.version} at line ${v.line}'")
|
||||
if (Version.from(v.version) != version) {
|
||||
throw GradleException("Version information mismatch, expected $version, got ${v.version} in file: $f at line ${v.line}")
|
||||
}
|
||||
}
|
||||
|
||||
// make sure all code is committed (no un-committed files and no staged files). Throw error and exit if there is
|
||||
|
||||
// make an update to the version number based on major/minor/patch
|
||||
|
||||
// update the all java files and build file with the new version number
|
||||
|
||||
// make a git tag with the NEW version number, commit the tag
|
||||
|
||||
|
||||
// Version will increment/update only if the project needed to build. This is current, not the new one that will be used to build
|
||||
// public static BuildVersion version = BuildVersion . get ("3.6").readme(readme).sourceFile(srcName, src, dorkbox.console.Console.class);
|
||||
|
||||
return filesWithVersionInfo.values.toList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSourceFiles(project: Project): HashSet<File> {
|
||||
val alreadyParsedFiles = HashSet<File>()
|
||||
private fun getSourceFiles(project: Project): HashSet<File> {
|
||||
val alreadyParsedFiles = HashSet<File>()
|
||||
|
||||
val convention = project.convention.getPlugin(JavaPluginConvention::class.java)
|
||||
convention.sourceSets.all {
|
||||
it.java {
|
||||
it.forEach { file ->
|
||||
alreadyParsedFiles.add(file)
|
||||
val convention = project.convention.getPlugin(JavaPluginConvention::class.java)
|
||||
convention.sourceSets.all {sourceSet ->
|
||||
sourceSet.java {directorySet ->
|
||||
directorySet.forEach { file ->
|
||||
alreadyParsedFiles.add(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return alreadyParsedFiles
|
||||
}
|
||||
|
||||
private fun getGit(project: Project): Git {
|
||||
try {
|
||||
val gitDir = getRootGitDir(project.projectDir)
|
||||
return Git.wrap(FileRepository(gitDir))
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
return alreadyParsedFiles
|
||||
}
|
||||
}
|
||||
|
||||
private fun getRootGitDir(currentRoot: File): File {
|
||||
val gitDir = scanForRootGitDir(currentRoot)
|
||||
if (!gitDir.exists()) {
|
||||
throw IllegalArgumentException("Cannot find '.git' directory")
|
||||
}
|
||||
return gitDir
|
||||
}
|
||||
|
||||
private fun scanForRootGitDir(currentRoot: File): File {
|
||||
val gitDir = File(currentRoot, ".git")
|
||||
|
||||
if (gitDir.exists()) {
|
||||
return gitDir
|
||||
}
|
||||
|
||||
// stop at the root directory, return non-existing File object;
|
||||
return if (currentRoot.parentFile == null) {
|
||||
gitDir
|
||||
}
|
||||
else scanForRootGitDir(currentRoot.parentFile)
|
||||
|
||||
// look in parent directory;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue