Skip to content

Commit

Permalink
Rewrite runMigrations task to fork Ant
Browse files Browse the repository at this point in the history
* runMigrations can now execute with a different JVM.
* runMigrations now support MPS 2022.2 and above.
  • Loading branch information
sergej-koscejev committed Oct 26, 2023
1 parent 5be4639 commit 13af452
Showing 1 changed file with 107 additions and 38 deletions.
145 changes: 107 additions & 38 deletions src/main/kotlin/de/itemis/mps/gradle/runmigrations/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package de.itemis.mps.gradle.runmigrations

import de.itemis.mps.gradle.BasePluginExtensions
import de.itemis.mps.gradle.getMPSVersion
import de.itemis.mps.gradle.runAnt
import groovy.xml.MarkupBuilder
import net.swiftzer.semver.SemVer
import org.gradle.api.GradleException
import org.gradle.api.Plugin
Expand Down Expand Up @@ -74,60 +76,127 @@ open class RunMigrationsMpsProjectPlugin : Plugin<Project> {
} else {
tasks.create("resolveMpsForMigrations")
}

tasks.named("runMigrations") {
dependsOn(resolveMps)
doLast {
if (!mpsLocation.isDirectory) {
throw GradleException("Specified MPS location does not exist or is not a directory: $mpsLocation")
}

ant.withGroovyBuilder {
"path"("id" to "path.mps.ant.path",) {
// The different MPS versions need different jars. Let's just keep it simple and include all jars.
"fileset"("dir" to "$mpsLocation/lib", "includes" to "**/*.jar")
}
"taskdef"("resource" to "jetbrains/mps/build/ant/antlib.xml", "classpathref" to "path.mps.ant.path")

val argsToMigrate = mutableListOf<Pair<String, Any>>().run {
add("project" to projectLocation)
add("mpsHome" to mpsLocation)

extension.force?.let { add("force" to it) }
extension.haltOnPrecheckFailure?.let { add("haltOnPrecheckFailure" to it) }
extension.haltOnDependencyError?.let { add("haltOnDependencyError" to it) }
val buildFile = temporaryDir.resolve("build.xml")
buildFile.printWriter().use {
MarkupBuilder(it).withGroovyBuilder {
"project" {
"path"("id" to "path.mps.ant.path") {
// The different MPS versions need different jars. Let's just keep it simple and include all jars.
"fileset"("dir" to "$mpsLocation/lib", "includes" to "**/*.jar")
}
"taskdef"(
"resource" to "jetbrains/mps/build/ant/antlib.xml",
"classpathref" to "path.mps.ant.path"
)

toTypedArray()
}
val argsToMigrate = mutableListOf<Pair<String, Any>>().run {
add("project" to projectLocation)
add("mpsHome" to mpsLocation)

"migrate"(*argsToMigrate) {
"macro"("name" to "mps_home", "path" to mpsLocation)
extension.force?.let { add("force" to it) }
extension.haltOnPrecheckFailure?.let { add("haltOnPrecheckFailure" to it) }
extension.haltOnDependencyError?.let { add("haltOnDependencyError" to it) }

extension.macros.forEach {
"macro"("name" to it.name, "path" to it.value)
}

"jvmargs" {
"arg"("value" to "-Didea.log.config.file=log.xml")
"arg"("value" to "-ea")
if (extension.maxHeap != null) {
"arg"("value" to "-Xmx${extension.maxHeap}")
toTypedArray()
}
}

extension.pluginsProperty.get().forEach {
// Same handling as in mps-build-backends
if (File(it.path).isAbsolute) {
"plugin"("path" to it.path, "id" to it.id)
}
else if (extension.pluginLocation != null && File(extension.pluginLocation, it.path).exists()) {
"plugin"("path" to File(extension.pluginLocation, it.path), "id" to it.id)
} else {
"plugin"("path" to mpsLocation.resolve("plugins").resolve(it.path), "id" to it.id)
"migrate"(*argsToMigrate) {
"macro"("name" to "mps_home", "path" to mpsLocation)

extension.macros.forEach {
"macro"("name" to it.name, "path" to it.value)
}

"jvmargs" {
"arg"("value" to "-Didea.log.config.file=log.xml")
"arg"("value" to "-ea")

if (extension.maxHeap != null) {
"arg"("value" to "-Xmx${extension.maxHeap}")
}

"arg"("value" to "--add-opens=java.base/java.io=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.lang=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.net=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.nio=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.nio.charset=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.text=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.time=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.util=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/jdk.internal.vm=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/sun.security.ssl=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.base/sun.security.util=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/java.awt=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/java.awt.dnd.peer=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/java.awt.event=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/java.awt.image=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/java.awt.peer=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/javax.swing=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/javax.swing.text.html=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.awt.datatransfer=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.awt.image=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.awt=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.font=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.java2d=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/sun.swing=ALL-UNNAMED")
"arg"("value" to "--add-opens=jdk.attach/sun.tools.attach=ALL-UNNAMED")
"arg"("value" to "--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
"arg"("value" to "--add-opens=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED")
"arg"("value" to "--add-opens=jdk.jdi/com.sun.tools.jdi=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/com.apple.laf=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/com.apple.eawt=ALL-UNNAMED")
"arg"("value" to "--add-opens=java.desktop/com.apple.eawt.event=ALL-UNNAMED")
}

extension.pluginsProperty.get().forEach {
// Same handling as in mps-build-backends
if (File(it.path).isAbsolute) {
"plugin"("path" to it.path, "id" to it.id)
} else if (extension.pluginLocation != null && File(
extension.pluginLocation,
it.path
).exists()
) {
"plugin"(
"path" to File(extension.pluginLocation, it.path),
"id" to it.id
)
} else {
"plugin"(
"path" to mpsLocation.resolve("plugins").resolve(it.path),
"id" to it.id
)
}
}
}
}
}
}

val classpath = project.fileTree(mpsLocation.resolve("lib")) {
include("ant/lib/*.jar")
include("*.jar")
builtBy(resolveMps)
}

runAnt(
extension.javaExec, temporaryDir, args = listOf(),
includeDefaultClasspath = false,
scriptClasspath = classpath
)
}
}
}
Expand Down

0 comments on commit 13af452

Please sign in to comment.