Skip to content

Commit

Permalink
add stability option to update command, add snapshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Aug 8, 2021
1 parent 636177e commit 23614ea
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 32 deletions.
3 changes: 1 addition & 2 deletions samples/voodoo-dev.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@echo off
setlocal
set VOODOO_COMMAND=voodoo-dev
java -jar ..\voodoo\build\libs\voodoo-0.6.0-local-all.jar %*
if exist wrapper\new.jar move wrapper\new.jar wrapper\wrapper.jar
java -jar "%~dp0..\voodoo\build\libs\voodoo-0.6.0-local-all.jar" %*
endlocal
4 changes: 2 additions & 2 deletions samples/voodoo.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
java -jar wrapper\wrapper.jar %*
IF EXIST wrapper\new.jar MOVE wrapper\new.jar wrapper\wrapper.jar
java -jar "%~dp0wrapper\wrapper.jar" %*
IF EXIST %~dp0wrapper\new.jar MOVE %~dp0wrapper\new.jar %~dp0wrapper\wrapper.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package voodoo.util.maven

enum class MavenReleaseStability {
RELEASE, LATEST
}
59 changes: 45 additions & 14 deletions util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package voodoo.util.maven

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.features.cookies.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readText
import io.ktor.http.*
import io.ktor.util.cio.writeChannel
import io.ktor.utils.io.copyAndClose
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import org.intellij.lang.annotations.Language
import org.w3c.dom.NodeList
import org.xml.sax.InputSource
import voodoo.util.Directories
Expand All @@ -38,6 +30,7 @@ object MavenUtil {
group: String,
artifactId: String,
version: String,
snapshotVersion: String = version,
classifier: String? = null,
extension: String = "jar"
): File {
Expand All @@ -47,14 +40,15 @@ object MavenUtil {
.resolve(group.replace('.','/'))
.resolve(artifactId)
.resolve(version)
.resolve("$artifactId-$version$classifierSuffix.$extension")
.resolve("$artifactId-$snapshotVersion$classifierSuffix.$extension")
}

suspend fun downloadArtifact(
mavenUrl: String,
group: String,
artifactId: String,
version: String,
snapshotVersion: String = version,
classifier: String? = null,
extension: String = "jar",
outputDir: File,
Expand Down Expand Up @@ -90,7 +84,7 @@ object MavenUtil {

val groupPath = group.replace('.','/')

val artifactUrl = "$mavenUrl/$groupPath/$artifactId/$version/$artifactId-$version$classifierSuffix.$extension"
val artifactUrl = "$mavenUrl/$groupPath/$artifactId/$version/$artifactId-$snapshotVersion$classifierSuffix.$extension"
logger.trace { "downloading: $artifactUrl" }
val targetFile = outputFile ?: File(outputDir, "$artifactId-$version$classifierSuffix.$extension")
targetFile.absoluteFile.parentFile.mkdirs()
Expand Down Expand Up @@ -129,10 +123,11 @@ object MavenUtil {
suspend fun getMavenMetadata(
mavenUrl: String,
group: String,
artifactId: String
artifactId: String,
version: String? = null
): String {
val groupPath = group.split("\\.".toRegex()).joinToString("/")
val metadataUrl = "$mavenUrl/$groupPath/$artifactId/maven-metadata.xml"
val metadataUrl = listOfNotNull(mavenUrl, groupPath, artifactId, version, "maven-metadata.xml").joinToString("/")

val response = try {
useClient { client ->
Expand Down Expand Up @@ -196,7 +191,7 @@ object MavenUtil {
return latest
}

suspend fun getALlVersionFromMavenMetadata(
suspend fun getALlVersionsFromMavenMetadata(
mavenUrl: String,
group: String,
artifactId: String
Expand All @@ -221,4 +216,40 @@ object MavenUtil {
return list
}

suspend fun getSnapshotVersionFromMavenMetadata(
mavenUrl: String,
group: String,
artifactId: String,
version: String,
extension: String = "jar",
classifier: String? = "all"
): List<String> {
val metadataXml = getMavenMetadata(mavenUrl, group, artifactId, version)

val dbFactory = DocumentBuilderFactory.newInstance()
val dBuilder = dbFactory.newDocumentBuilder()
val xmlInput = metadataXml
val doc = dBuilder.parse(InputSource(ByteArrayInputStream(xmlInput.toByteArray(Charsets.UTF_8))))

val xpFactory = XPathFactory.newInstance()
val xPath = xpFactory.newXPath()

@Language("XPath") val classifierFilter = if(classifier != null)
""" and classifier="$classifier" """.trim()
else
""

@Language("XPath") val xPathExpression =
"""metadata/versioning/snapshotVersions/snapshotVersion
|[ extension="$extension" $classifierFilter ]
|/value/text()""".trimMargin()
val nodeList: NodeList = xPath.evaluate(xPathExpression, doc, XPathConstants.NODESET) as NodeList
logger.info { "nodeList: $nodeList" }
val list = (0 until nodeList.length).mapNotNull { i ->
// logger.info { "item($i): ${nodeList.item(i)}" }
nodeList.item(i)?.textContent
}
return list
}

}
2 changes: 1 addition & 1 deletion voodoo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {

implementation("blue.endless:jankson:_")

// implementation(KotlinX.coroutines.debug)
implementation(KotlinX.coroutines.debug)

implementation(group = "ch.qos.logback", name = "logback-classic", version = "_") {
exclude(module = "javax.mail")
Expand Down
58 changes: 47 additions & 11 deletions voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package voodoo.cli

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.enum
import io.ktor.client.request.*
import io.ktor.util.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.slf4j.MDCContext
import mu.KotlinLogging
import mu.withLoggingContext
import voodoo.util.Directories
import voodoo.util.download
import voodoo.util.maven.MavenReleaseStability
import voodoo.util.maven.MavenUtil
import voodoo.util.toHexString
import voodoo.util.useClient
import voodoo.voodoo.GeneratedConstants
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.StringWriter
import java.lang.Exception
import java.security.MessageDigest
import java.util.*

Expand All @@ -29,6 +29,12 @@ class UpdateCommand : CliktCommand(
private val logger = KotlinLogging.logger {}
val cliContext by requireObject<CLIContext>()

val stability by option(
"--stability",
)
.enum<MavenReleaseStability>(ignoreCase = true)
.default(MavenReleaseStability.RELEASE)

override fun run(): Unit = withLoggingContext("command" to commandName) {
val rootDir = cliContext.rootDir
val directories = Directories.get(moduleName = "update")
Expand All @@ -44,14 +50,43 @@ class UpdateCommand : CliktCommand(
}
}

val version = MavenUtil.getReleaseVersionFromMavenMetadata(
GeneratedConstants.MAVEN_URL,
GeneratedConstants.MAVEN_GROUP,
"voodoo"
)
val (version, snapshotVersion) = when (stability) {
MavenReleaseStability.RELEASE -> {
MavenUtil.getReleaseVersionFromMavenMetadata(
GeneratedConstants.MAVEN_URL,
GeneratedConstants.MAVEN_GROUP,
"voodoo"
)
}
MavenReleaseStability.LATEST -> {
MavenUtil.getLatestVersionFromMavenMetadata(
GeneratedConstants.MAVEN_URL,
GeneratedConstants.MAVEN_GROUP,
"voodoo"
)
}
}.let { version ->
if (version.endsWith("-SNAPSHOT")) {
MavenUtil.getSnapshotVersionFromMavenMetadata(
mavenUrl = GeneratedConstants.MAVEN_URL,
group = GeneratedConstants.MAVEN_GROUP,
artifactId = "voodoo",
version = version,
extension = "jar",
classifier = GeneratedConstants.MAVEN_SHADOW_CLASSIFIER
).first().let {
logger.info { "resolved to snapshot version $it" }
version to it
}
} else {
version to version
}
}

logger.info { "updating to $version" }
val groupPath = GeneratedConstants.MAVEN_GROUP.replace('.', '/')
val distributionUrl = "${GeneratedConstants.MAVEN_URL}/$groupPath/voodoo/$version/voodoo-$version-${GeneratedConstants.MAVEN_SHADOW_CLASSIFIER}.jar"
val distributionUrl =
"${GeneratedConstants.MAVEN_URL}/$groupPath/voodoo/$version/voodoo-$snapshotVersion-${GeneratedConstants.MAVEN_SHADOW_CLASSIFIER}.jar"

// validate the file exists and matches checksum
val tmpFile = cacheHome.resolve(distributionUrl.substringAfterLast("/"))
Expand Down Expand Up @@ -82,6 +117,7 @@ class UpdateCommand : CliktCommand(
GeneratedConstants.MAVEN_GROUP,
"wrapper",
version,
snapshotVersion = snapshotVersion,
outputFile = wrapperFile,
outputDir = wrapperFile.absoluteFile.parentFile,
classifier = GeneratedConstants.MAVEN_SHADOW_CLASSIFIER
Expand All @@ -100,7 +136,7 @@ class UpdateCommand : CliktCommand(
complete.update(buffer, 0, numRead)
}
} while (numRead != -1)
complete.digest()
complete.digest()
}
}
}
5 changes: 3 additions & 2 deletions voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ class InitProjectCommand : CliktCommand(
return@let
}

val PWD = "%~dp0"
batFile.writeText("""
java -jar wrapper\wrapper.jar %*
IF EXIST wrapper\new.jar MOVE /Y wrapper\new.jar wrapper\wrapper.jar
java -jar "${PWD}wrapper\wrapper.jar" %*
IF EXIST ${PWD}wrapper\new.jar MOVE ${PWD}wrapper\new.jar ${PWD}wrapper\wrapper.jar
""".trimIndent())
logger.info { "generated $batFile" }
}
Expand Down

0 comments on commit 23614ea

Please sign in to comment.