Cleaned up methods. Added min/max version requirements for build files
parent
781633f4bd
commit
a165b5529d
|
@ -16,6 +16,7 @@
|
|||
package dorkbox.gradle
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.api.tasks.wrapper.Wrapper
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.json.JSONObject
|
||||
|
@ -23,47 +24,42 @@ import java.net.URL
|
|||
|
||||
open class
|
||||
GradleUpdateTask : DefaultTask() {
|
||||
@Volatile var foundGradleVersion : String? = "0.0"
|
||||
private val wrapper = project.tasks.create("wrapperUpdate", Wrapper::class.java)
|
||||
|
||||
init {
|
||||
outputs.upToDateWhen { false }
|
||||
outputs.cacheIf { false }
|
||||
description = "Automatically update GRADLE to the latest version"
|
||||
|
||||
project.afterEvaluate {
|
||||
// always make sure this task when specified. ALWAYS skip for other tasks, Never skip for us.
|
||||
// This is a little bit of a PITA, because of how gradle configures, then runs tasks...
|
||||
wrapper.apply {
|
||||
group = "gradle"
|
||||
outputs.upToDateWhen { false }
|
||||
outputs.cacheIf { false }
|
||||
|
||||
if (project.gradle.startParameter.taskNames.contains(this.name)) {
|
||||
val releaseText = URL("https://services.gradle.org/versions/current").readText()
|
||||
val foundGradleVersion = JSONObject(releaseText)["version"] as String?
|
||||
gradleVersion = foundGradleVersion
|
||||
distributionUrl = distributionUrl.replace("bin", "all")
|
||||
}
|
||||
|
||||
if (foundGradleVersion.isNullOrEmpty()) {
|
||||
println("\tUnable to detect New Gradle Version. Output json: $releaseText")
|
||||
}
|
||||
else {
|
||||
val current = GradleVersion.current().version
|
||||
finalizedBy(wrapper)
|
||||
}
|
||||
|
||||
if (current == foundGradleVersion) {
|
||||
println("\tGradle is already latest version '$foundGradleVersion'")
|
||||
} else {
|
||||
println("\tDetected new Gradle Version: '$foundGradleVersion', updating from $current")
|
||||
@TaskAction
|
||||
fun run() {
|
||||
val releaseText = URL("https://services.gradle.org/versions/current").readText()
|
||||
foundGradleVersion = JSONObject(releaseText)["version"] as String?
|
||||
|
||||
val wrapper = project.tasks.create("wrapperUpdate", Wrapper::class.java)
|
||||
wrapper.apply {
|
||||
group = "gradle"
|
||||
outputs.upToDateWhen { false }
|
||||
outputs.cacheIf { false }
|
||||
if (foundGradleVersion.isNullOrEmpty()) {
|
||||
println("\tUnable to detect New Gradle Version. Output json: $releaseText")
|
||||
}
|
||||
else {
|
||||
val current = GradleVersion.current().version
|
||||
|
||||
gradleVersion = foundGradleVersion
|
||||
distributionUrl = distributionUrl.replace("bin", "all")
|
||||
}
|
||||
|
||||
finalizedBy(wrapper)
|
||||
}
|
||||
}
|
||||
if (current == foundGradleVersion) {
|
||||
println("\tGradle is already latest version '$foundGradleVersion'")
|
||||
} else {
|
||||
println("\tDetected new Gradle Version: '$foundGradleVersion', updating from $current")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDescription(): String {
|
||||
return "Automatically updates GRADLE to the latest version"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,13 +21,16 @@ import org.gradle.api.Project
|
|||
|
||||
|
||||
/**
|
||||
* For managing (what should be common sense) gradle tasks, such as updating gradle and dependencies
|
||||
* For managing (what should be common sense) gradle tasks, such as:
|
||||
* - updating gradle
|
||||
* - updating dependencies
|
||||
* - checking version requirements
|
||||
*/
|
||||
class GradleUtils : Plugin<Project> {
|
||||
private lateinit var propertyMappingExtension: LoadPropertyFile
|
||||
private lateinit var propertyMappingExtension: StaticMethodsAndTools
|
||||
|
||||
override fun apply(project: Project) {
|
||||
propertyMappingExtension = project.extensions.create("GradleUtils", LoadPropertyFile::class.java, project)
|
||||
propertyMappingExtension = project.extensions.create("GradleUtils", StaticMethodsAndTools::class.java, project)
|
||||
|
||||
project.tasks.create("autoUpdateGradle", GradleUpdateTask::class.java).apply {
|
||||
group = "gradle"
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package dorkbox.gradle
|
||||
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.Project
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.reflect.KMutableProperty
|
||||
import kotlin.reflect.full.declaredMemberProperties
|
||||
|
||||
open class LoadPropertyFile(private val project: Project) {
|
||||
open class StaticMethodsAndTools(private val project: Project) {
|
||||
/**
|
||||
* Maps the property (key/value) pairs of a property file onto the specified target object.
|
||||
*/
|
||||
|
@ -37,4 +38,24 @@ open class LoadPropertyFile(private val project: Project) {
|
|||
}}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the minimum version of gradle supported
|
||||
*/
|
||||
fun minVersion(version: String) {
|
||||
val compared = org.gradle.util.GradleVersion.current().compareTo(org.gradle.util.GradleVersion.version(version))
|
||||
if (compared == -1) {
|
||||
throw GradleException("This project requires Gradle $version or higher.")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the maximum version of gradle supported
|
||||
*/
|
||||
fun maxVersion(version: String) {
|
||||
val compared = org.gradle.util.GradleVersion.current().compareTo(org.gradle.util.GradleVersion.version(version))
|
||||
if (compared == 1) {
|
||||
throw GradleException("This project requires Gradle $version or lower.")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue