Skip to content

Commit

Permalink
Merge pull request #59 from modelix/workspaces-fixes
Browse files Browse the repository at this point in the history
Workspaces fixes
  • Loading branch information
slisson authored Mar 4, 2024
2 parents bb70966 + a1f3dd8 commit d9bcc94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion mps-generator-execution-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ publishing {
publications {
create<MavenPublication>("maven") {
groupId = "org.modelix.mps"
artifactId = "genertor-execution-plugin"
artifactId = "generator-execution-plugin"
artifact(tasks.buildPlugin) {
extension = "zip"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ class AsyncGenerator {

val projects = ProjectManager.getInstance().openedProjects
val project: Project = projects.first()
val textGenName =
resolveFacetName("jetbrains.mps.make.facets.TextGen", "jetbrains.mps.lang.core.TextGen")
val scr: IScript =
ScriptBuilder(project.getComponent<FacetRegistry>(FacetRegistry::class.java))
.withFacetNames(
resolveFacetName("Generate"),
resolveFacetName("TextGen"),
resolveFacetName("Make"),
).withFinalTarget(ITarget.Name(resolveFacetName("TextGen").toString() + ".textGenToMemory"))
resolveFacetName("jetbrains.mps.make.facets.Generate", "jetbrains.mps.lang.core.Generate"),
textGenName,
resolveFacetName("jetbrains.mps.make.facets.Make", "jetbrains.mps.lang.core.Make"),
).withFinalTarget(ITarget.Name("$textGenName.textGenToMemory"))
.toScript()
val messageHandler: IMessageHandler = object : IMessageHandler {
override fun handle(message: IMessage) {
Expand Down Expand Up @@ -118,19 +120,15 @@ class AsyncGenerator {
return generationResult
}

private fun resolveFacetName(shortName: String): IFacet.Name {
private fun resolveFacetName(vararg alternativeNames: String): IFacet.Name {
// Some facets where moved to a different package in MPS 2022.3
// See https://github.com/JetBrains/MPS/commit/e15a11d4b5e84ff3372365cdab832c56b68b7050
// To make the plugin compatible with version before and after the renaming we use the short name only and
// look up their fully qualified name.
// To make the plugin compatible with version before and after the renaming we have to look up the correct name.
val facetRegistry = ProjectManager.getInstance().openedProjects.mapNotNull { it.getComponent(FacetRegistry::class.java) }.first()
val registeredNames = facetRegistry.allFacets().keys
val matchingNames = registeredNames.filter { it.name == shortName }
return when (matchingNames.size) {
0 -> throw IllegalArgumentException("Facet '$shortName' not found in $registeredNames")
1 -> matchingNames[0]
else -> throw IllegalArgumentException("Multiple facets found for '$shortName': $matchingNames")
}
val resolvedFacet = alternativeNames.asSequence().mapNotNull { facetRegistry.lookup(IFacet.Name(it)) }.firstOrNull()
return requireNotNull(resolvedFacet) {
"None of the facet names found: $alternativeNames"
}.name
}

private fun computeModelHash(model: SModel): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.html.respondHtml
import io.ktor.server.response.header
import io.ktor.server.response.respondOutputStream
import io.ktor.server.response.respondRedirect
Expand All @@ -18,16 +17,19 @@ import io.ktor.util.pipeline.PipelineContext
import jetbrains.mps.smodel.MPSModuleRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.html.FlowContent
import kotlinx.html.HTML
import kotlinx.html.a
import kotlinx.html.body
import kotlinx.html.br
import kotlinx.html.div
import kotlinx.html.h1
import kotlinx.html.h2
import kotlinx.html.head
import kotlinx.html.html
import kotlinx.html.id
import kotlinx.html.meta
import kotlinx.html.pre
import kotlinx.html.stream.createHTML
import kotlinx.html.style
import kotlinx.html.table
import kotlinx.html.td
Expand Down Expand Up @@ -55,7 +57,7 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {
val modules: List<Pair<String, String>> = repository.modelAccess.computeRead {
repository.modules.map { (it.moduleName ?: "") to it.moduleId.toString() }.toList()
}
call.respondHtml {
call.respondHtmlSafe {
body {
div {
+"Choose a module for generation:"
Expand Down Expand Up @@ -83,7 +85,7 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {
call.respondText("Module not found: $moduleIdStr", status = HttpStatusCode.NotFound)
return@get
}
call.respondHtml {
call.respondHtmlSafe {
body {
div {
+"Choose a model for generation:"
Expand Down Expand Up @@ -161,7 +163,7 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {
get {
val generatorOutput: AsyncGenerator.GeneratorOutput = generator.getTextGenOutput(getModel())
val files = getFilesIfCompleted(generatorOutput) ?: return@get
call.respondHtml {
call.respondHtmlSafe {
body {
generateFullOutput(generatorOutput)
}
Expand All @@ -177,7 +179,7 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {

private suspend fun PipelineContext<Unit, ApplicationCall>.getFilesIfCompleted(output: AsyncGenerator.GeneratorOutput): List<AsyncGenerator.GeneratedFile>? {
return if (output.outputFiles.isActive) {
call.respondHtml {
call.respondHtmlSafe {
head {
meta {
httpEquiv = "refresh"
Expand All @@ -198,7 +200,7 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {
} else if (output.outputFiles.isCompleted) {
output.outputFiles.getCompleted()
} else {
call.respondHtml {
call.respondHtmlSafe {
body {
div {
+"Generation failed"
Expand Down Expand Up @@ -287,3 +289,14 @@ class GeneratorOutputHandlerImpl(val generator: AsyncGenerator) {
}
}
}

/**
* respondHtml fails to respond anything if an exception is thrown in the body and an error handler is installed
* that tries to respond an error page.
*/
suspend fun ApplicationCall.respondHtmlSafe(status: HttpStatusCode = HttpStatusCode.OK, block: HTML.() -> Unit) {
val htmlText = createHTML().html {
block()
}
respondText(htmlText, ContentType.Text.Html, status)
}

0 comments on commit d9bcc94

Please sign in to comment.