Skip to content

Commit

Permalink
make reading model XMLs more robust + release v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dslmeinte committed Jun 17, 2019
1 parent f5f8bf9 commit 8d68ebe
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions mps-inspector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Capabilities:
1. ... Export structure of languages, directly from XML to JSON.
1. ... Generate Kotlin code for deserializing models from XML.
1. ... Export models, directly from XML to JSON.
1. Track dependencies, and check for incorrect/unmatched language versions, and such.
1. ✓ Warn about language dependencies being specified with an invalid version of -1.
1. Warn about unused (i.e., not referenced from `.mps/modules.xml`) modules.
Or more generally: compare project/modules on disk with `.mps/modules.xml`, and generate fixes.
1. Normalize structure on disk to standard layout.
1. Track dependencies, and check for incorrect/unmatched language versions, and such.

The reason for this tool's existence is having had bad experiences with MPS in combination with merging/rebasing.
MPS regularly looses track of correct versions, and such.
Expand Down
2 changes: 1 addition & 1 deletion mps-inspector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>nl.dslconsultancy.mps</groupId>
<artifactId>mps-inspector</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<packaging>jar</packaging>

<name>MPS Inspector</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ fun Configuration.run() {
.filter { mpsFileType(it) == MpsFileType.Model }
.forEach {
val modelXml = modelXmlFromDisk(it)
if (modelXml.dependencies.importedLanguages.any { il -> il.version == -1 }) {
println("'$it' relies on at least one language with version -1")
if (modelXml.dependencies != null) {
if (modelXml.dependencies!!.importedLanguages.any { il -> il.version == -1 }) {
println("'$it' relies on at least one language with version -1")
}
}
}
println("checked for occurrences of language version '-1' in models")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fun usage(mpsProjectOnDisk: MpsProjectOnDisk): CountingMap =

private fun usage(modelPath: Path): CountingMap {
val modelXml = modelXmlFromDiskSafe(modelPath) ?: return emptyMap()
// (Using S(t)AX should be much more efficient.)
val metaConcepts = modelXml.metaConcepts()
val allNodes = modelXml.nodes.flatMap { it.allNodes() }
val result = hashMapOf<String, Int>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.lang.RuntimeException
import java.nio.file.Path

object JacksonXmlUtil {
Expand Down Expand Up @@ -38,11 +37,15 @@ object JacksonXmlUtil {
return xmlMapper_!!
}

inline fun <reified T> readXml(path: Path): T =
inline fun <reified T> readXml(path: Path): T = xmlMapper().readValue(path.toFile())

inline fun <reified T> readXml(path: Path, resultOnFail: (path: Path, e: Exception) -> T): T =
try {
xmlMapper().readValue(path.toFile())
readXml(path)
} catch (e: Exception) {
throw RuntimeException("could not read '$path' as XML file (of indicated type)")
System.err.println("could not read '$path' as XML file (of indicated type): ${e.message}")
e.printStackTrace(System.err)
resultOnFail(path, e)
}

fun <T> writeXml(content: T, path: Path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.nio.file.Path
data class ModelXml(

@set:JsonProperty("languages")
var dependencies: Dependencies,
var dependencies: Dependencies?,

val registry: RegistryXml?,

Expand All @@ -20,6 +20,8 @@ data class ModelXml(

)

val emptyModelXml = ModelXml(null, null)

data class Dependencies(
@set:JsonProperty("use")
var importedLanguages: List<ImportedLanguage> = arrayListOf()
Expand Down Expand Up @@ -137,7 +139,10 @@ data class ReferenceXml(
)


fun modelXmlFromDisk(path: Path): ModelXml = readXml(path)
fun modelXmlFromDisk(path: Path): ModelXml = readXml(path) { skippedPath, _ ->
System.out.println("skipped '$skippedPath'")
emptyModelXml
}

fun ModelXml.metaConcepts(): List<MetaConceptXml> = if (this.registry == null) emptyList() else this.registry.languages.flatMap { it.metaConcepts }

Expand Down

0 comments on commit 8d68ebe

Please sign in to comment.