diff --git a/samples/voodoo-dev.bat b/samples/voodoo-dev.bat index 8e7b8bcf..cde8590e 100644 --- a/samples/voodoo-dev.bat +++ b/samples/voodoo-dev.bat @@ -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 \ No newline at end of file diff --git a/samples/voodoo.bat b/samples/voodoo.bat index eb7286c7..cb70acaf 100644 --- a/samples/voodoo.bat +++ b/samples/voodoo.bat @@ -1,2 +1,2 @@ -java -jar wrapper\wrapper.jar %* -IF EXIST wrapper\new.jar MOVE wrapper\new.jar wrapper\wrapper.jar \ No newline at end of file +java -jar "%~dp0wrapper\wrapper.jar" %* +IF EXIST %~dp0wrapper\new.jar MOVE %~dp0wrapper\new.jar %~dp0wrapper\wrapper.jar \ No newline at end of file diff --git a/util/maven/src/main/kotlin/voodoo.util/maven/MavenReleaseStability.kt b/util/maven/src/main/kotlin/voodoo.util/maven/MavenReleaseStability.kt new file mode 100644 index 00000000..638e29b2 --- /dev/null +++ b/util/maven/src/main/kotlin/voodoo.util/maven/MavenReleaseStability.kt @@ -0,0 +1,5 @@ +package voodoo.util.maven + +enum class MavenReleaseStability { + RELEASE, LATEST +} \ No newline at end of file diff --git a/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt b/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt index 8bd2da1f..1ae5857e 100644 --- a/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt +++ b/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt @@ -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 @@ -38,6 +30,7 @@ object MavenUtil { group: String, artifactId: String, version: String, + snapshotVersion: String = version, classifier: String? = null, extension: String = "jar" ): File { @@ -47,7 +40,7 @@ object MavenUtil { .resolve(group.replace('.','/')) .resolve(artifactId) .resolve(version) - .resolve("$artifactId-$version$classifierSuffix.$extension") + .resolve("$artifactId-$snapshotVersion$classifierSuffix.$extension") } suspend fun downloadArtifact( @@ -55,6 +48,7 @@ object MavenUtil { group: String, artifactId: String, version: String, + snapshotVersion: String = version, classifier: String? = null, extension: String = "jar", outputDir: File, @@ -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() @@ -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 -> @@ -196,7 +191,7 @@ object MavenUtil { return latest } - suspend fun getALlVersionFromMavenMetadata( + suspend fun getALlVersionsFromMavenMetadata( mavenUrl: String, group: String, artifactId: String @@ -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 { + 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 + } + } \ No newline at end of file diff --git a/voodoo/build.gradle.kts b/voodoo/build.gradle.kts index 5f4387a9..d3028216 100644 --- a/voodoo/build.gradle.kts +++ b/voodoo/build.gradle.kts @@ -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") diff --git a/voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt b/voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt index a5342a8d..8f442c53 100644 --- a/voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt +++ b/voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt @@ -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.* @@ -29,6 +29,12 @@ class UpdateCommand : CliktCommand( private val logger = KotlinLogging.logger {} val cliContext by requireObject() + val stability by option( + "--stability", + ) + .enum(ignoreCase = true) + .default(MavenReleaseStability.RELEASE) + override fun run(): Unit = withLoggingContext("command" to commandName) { val rootDir = cliContext.rootDir val directories = Directories.get(moduleName = "update") @@ -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("/")) @@ -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 @@ -100,7 +136,7 @@ class UpdateCommand : CliktCommand( complete.update(buffer, 0, numRead) } } while (numRead != -1) - complete.digest() + complete.digest() } } } \ No newline at end of file diff --git a/voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt b/voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt index b7046ecd..7cec6ff5 100644 --- a/voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt +++ b/voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt @@ -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" } }