Skip to content

Commit

Permalink
Update Gradle wrapper to 8.9 (elastic#110109)
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby authored Jul 19, 2024
1 parent 0f58417 commit cb0ecb8
Show file tree
Hide file tree
Showing 12 changed files with 4,165 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build-tools-internal/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=f8b4f4772d302c8ff580bc40d0f56e715de69b163546944f787c87abf209c961
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
distributionSha256Sum=258e722ec21e955201e31447b0aed14201765a3bfbae296a46cf60b70e66db70
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.8
8.9
24 changes: 24 additions & 0 deletions convert-deps.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import groovy.io.FileType
import java.nio.file.*
import java.nio.charset.StandardCharsets
import java.util.regex.Pattern

// Define the base directory to start the search
def baseDir = new File('/Users/rene/dev/elastic/elasticsearch/plugins')
def pattern = Pattern.compile(/(\w+)\s+['"](\w[^'"]+):([^'"]+):([^'"]+)['"]/)

// Define the pattern to match dependencies
def dependencyPattern = ~/(\w+)\s+['"](\w[^'"]+):([^'"]+):([^'"]+)['"]/

baseDir.eachFileRecurse(FileType.FILES) { file ->
if (file.name.endsWith('.gradle')) {
def content = file.text
def newContent = content.replaceAll(dependencyPattern) { match, config, group, name, version ->
def libName = "${name.replaceAll('-', '.')}".toLowerCase()
"$config libs.${libName}"
}
file.text = newContent
}
}

println "Dependency patterns replaced successfully."
319 changes: 319 additions & 0 deletions generate-version-catalog.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
import java.nio.file.*
import java.nio.charset.StandardCharsets
import java.util.regex.Pattern

REPO_ROOT = "/Users/rene/dev/elastic/elasticsearch/plugins"
VERSION_PROPS = REPO_ROOT + "/../build-tools-internal/version.properties"

def parseGradleFiles(Path directory) {
def pattern = Pattern.compile(/(\w+)\s+['"](\w[^'"]+):([^'"]+):([^'"]+)['"]/)
def dependencies = []

Files.walk(directory).each { path ->
if (Files.isRegularFile(path) && path.toString().endsWith('.gradle') && path.toString().contains("plugins/examples") == false){
def lines = Files.readAllLines(path, StandardCharsets.UTF_8)
lines.each { line ->
def matcher = pattern.matcher(line)
if (matcher.find()) {
def configuration = matcher.group(1)
def group = matcher.group(2)
def name = matcher.group(3)
def version = matcher.group(4)
dependencies << [file: path.toString(), configuration: configuration, group: group, name: name, version: version]
}
}
}
}
return dependencies
}

String convertToVersionCatalogEntry(def dependencies) {
Set<String> versions = new TreeSet<>()
Set<String> entries = new TreeSet<>()

}

def resolveVersion(Properties props, String versionString) {
println "Resolving version: ${versionString}"
if(versionString.startsWith("\${versions.")) {
def versionId = versionString.substring(versionString.indexOf('.') + 1, versionString.indexOf('}'))
if(props.containsKey(versionId)) {
return props.getProperty(versionId)
} else {
println "unknown version ${versionString} found in build.gradle file. Please add it to the version.properties file."
return versionId
}
}

return versionString
}


Properties loadVersionProperties() {
def properties = new Properties()
def file = new File(VERSION_PROPS)
if (!file.exists()) {
println "The properties file '${VERSION_PROPS}' does not exist."
return null
}
file.withInputStream { stream ->
properties.load(stream)
}
properties.each { key, value ->
println "Loaded version property: ${key} = ${value}"
}
return properties
}

def convertToCamelCase(String input) {
def parts = input.split('-')
def camelCaseString = parts[0]
parts.tail().each { part ->
// for now skip camel casing
//camelCaseString += part.capitalize()
camelCaseString += part
}
return camelCaseString
}

String calculateVersionRef(String libraryName, Map<String, String> versionCatalog, Properties properties, String version) {
// String versionRefName = convertToCamelCase(libraryName)
String versionRefName = libraryName

if(versionCatalog.containsKey(versionRefName)) {
def existingMajor = versionCatalog[libraryName].split("\\.")[0] as int
def newMajor = version.split("\\.")[0] as int
println "existingMajor: ${existingMajor}, newMajor: ${newMajor}"

if(newMajor > existingMajor) {
return versionRefName + newMajor
}
}
return versionRefName
}

def checkOptimizations(Map<String, String> versionCatalog, Properties versionProperties) {
def simplifications = [:]
versionCatalog.each { givenKey, givenVersion ->
def simpleKey = givenKey.contains("-") ? givenKey.split('-')[0] : givenKey
def candidates = versionCatalog.findAll {k, v -> givenKey != k && k.startsWith("${simpleKey}-")}
if(candidates.size() == 0 && versionProperties[simpleKey] != null) {
assert versionProperties[simpleKey] == givenVersion
simplifications[givenKey] = simpleKey
} else {
candidates.each {candidateKey , candidateVersion ->
if(candidateVersion == givenVersion) {
simplifications[candidateKey] = simpleKey
}
}
}

if(simplifications[givenKey] == null){
def converted = convertToCamelCase(givenKey)

if(givenKey != converted) {
simplifications[givenKey] = converted
}
}
}

return simplifications
}


def parseValue(value) {
if (value.startsWith('"') && value.endsWith('"')) {
return value[1..-2] // String value
} else if (value ==~ /\d+/) {
return value.toInteger() // Integer value
} else if (value ==~ /\d+\.\d+/) {
return value.toDouble() // Double value
} else if (value == 'true' || value == 'false') {
return value.toBoolean() // Boolean value
} else if (value.startsWith('[') && value.endsWith(']')) {
return value[1..-2].split(',').collect { parseValue(it.trim()) } // Array value
} else {
return value // Default to string if not matched
}
}

def parseTomlFile(filePath) {
def tomlMap = [:]
def currentSection = null
def file = new File(filePath)

file.eachLine { line ->
line = line.trim()

if (line.startsWith('#') || line.isEmpty()) {
// Skip comments and empty lines
return
}

if (line.startsWith('[') && line.endsWith(']')) {
// New section
currentSection = line[1..-2]
tomlMap[currentSection] = [:]
} else if (line.contains('=')) {
// Key-value pair
def (key, value) = line.split('=', 2).collect { it.trim() }
value = parseValue(value)
if (currentSection) {
tomlMap[currentSection][key] = value
} else {
tomlMap[key] = value
}
}
}

return tomlMap
}

def main() {
// def directoryPath = System.console().readLine('Enter the directory path to search for *.gradle files: ').trim()
// def directory = Paths.get("directoryPath")
def directory = Paths.get(REPO_ROOT)

if (!Files.exists(directory) || !Files.isDirectory(directory)) {
println "The directory '${directoryPath}' does not exist or is not a directory."
return
}

def dependencies = parseGradleFiles(directory)

def librariesCatalog = [:]
def versionsCatalog = [:]

Properties versionProperties = loadVersionProperties()
println "Version Properties: ${versionProperties.contains('junit')}"
if (dependencies) {
def depsByFile = dependencies.groupBy {it.file}
depsByFile.each { file, deps ->
println "File: ${file}"
deps.each { dep ->
def effectiveVersion = resolveVersion(versionProperties, dep.version)
def versionRefName = calculateVersionRef(dep.name, versionsCatalog, versionProperties, effectiveVersion)
versionsCatalog.put(versionRefName, effectiveVersion)
depLibraryEntry = [group: dep.group, name: dep.name, version:versionRefName]
println "\"${dep.group}:${dep.name}:${dep.version}\" -> \"${depLibraryEntry}\""
if(librariesCatalog.containsKey(versionRefName)) {
assert librariesCatalog[versionRefName] == depLibraryEntry
} else {
librariesCatalog.put(versionRefName, depLibraryEntry)
}
}

println ""
}

println "libraries Catalog versions"

librariesCatalog.each { key, value ->
println "${key} = ${value}"
}

println "Version Catalog libraries"
versionsCatalog.each { key, value ->
println "${key} = ${value}"
}
println "Found ${dependencies.size()} dependencies in ${depsByFile.size()} files."

} else {
println "No dependencies found."
}

def versionOptimizations = checkOptimizations(versionsCatalog, versionProperties)

versionOptimizations.each { given, simplified ->
println "$given -> $simplified"
println "${versionsCatalog[simplified]}"
if(versionsCatalog[simplified] == null) {
versionsCatalog[simplified] = versionsCatalog[given]
}
versionsCatalog.remove(given)
}

librariesCatalog.each { key, value ->
def simplified = versionOptimizations[key]
if(simplified != null) {
librariesCatalog[key].version = simplified
}
}

println "\n\nversions: "
versionsCatalog.sort().each { key, value ->
println "${key} = \"${value}\""
}

librariesCatalog.sort()
println "\n\nlibraries: "
librariesCatalog.sort().each { k, v ->
println "${k} = { group = \"${v['group']}\", name = \"${v['name']}\", version.ref = \"${v['version']}\" } "
}

// Example usage
def tomlFilePath = '/Users/rene/dev/elastic/elasticsearch/gradle/versions.toml'
def parsedToml = parseTomlFile(tomlFilePath)

// Access parsed data
existingVersions = parsedToml['versions']

// println "\n\nExisting versions:"
// existingVersions.forEach { key, value ->
// println "${key} = ${value}"
// }

// existingLibs = parsedToml['libraries']

// existingLibs.forEach { key, value ->
// println "${key} = ${value}"
// }

def finalVersions = [:]
def finalLibraries = [:]

existingVersions.each { key, value ->
finalVersions[key] = value
if(versionsCatalog.containsKey(key)) {
assert value == versionsCatalog[key]
versionsCatalog.remove(key)
}
}
finalVersions.putAll(versionsCatalog)


println "\n\n[versions]"
finalVersions.sort().each { key, value ->
println "${key} = \"${value}\""
}

def existingLibs = parsedToml['libraries']
existingLibs.each { key, value ->
finalLibraries[key] = value
if(librariesCatalog[key] != null) {
def newValue = librariesCatalog[key]
assert value == "{ group = \"${newValue['group']}\", name = \"${newValue['name']}\", version.ref = \"${newValue['version']}\" }"
librariesCatalog.remove(key)
}
}
finalLibraries.putAll(librariesCatalog)

println "\n\n[libraries]"
finalLibraries.sort().each { key, value ->
if(value instanceof Map) {
println "${key} = { group = \"${value['group']}\", name = \"${value['name']}\", version.ref = \"${value['version']}\" }"
} else if (value.startsWith("{")) {
println "${key} = $value"
} else {
println "${key} = \"$value\""
}
}

// println "Title: ${parsedToml['title']}"
// println "Owner Name: ${parsedToml['versions']['name']}"
// println "Database Server: ${parsedToml['database']['server']}"
// println "Database Ports: ${parsedToml['database']['ports']}"

}

main()
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=f8b4f4772d302c8ff580bc40d0f56e715de69b163546944f787c87abf209c961
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
distributionSha256Sum=258e722ec21e955201e31447b0aed14201765a3bfbae296a46cf60b70e66db70
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
5 changes: 4 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
Loading

0 comments on commit cb0ecb8

Please sign in to comment.