Skip to content

Commit 0dc8589

Browse files
committed
refactor(bulk-model-sync): migrate MPSBulkSynchronizer to kotlin
1 parent 277497f commit 0dc8589

File tree

2 files changed

+111
-137
lines changed

2 files changed

+111
-137
lines changed

bulk-model-sync-mps/src/main/java/org/modelix/mps/model/sync/bulk/MPSBulkSynchronizer.java

Lines changed: 0 additions & 137 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.mps.model.sync.bulk
18+
19+
import com.intellij.openapi.application.ApplicationManager
20+
import com.intellij.openapi.project.ProjectManager
21+
import jetbrains.mps.ide.project.ProjectHelper
22+
import org.jetbrains.mps.openapi.module.SModule
23+
import org.jetbrains.mps.openapi.module.SRepository
24+
import org.modelix.model.mpsadapters.MPSModuleAsNode
25+
import org.modelix.model.sync.bulk.ModelExporter
26+
import org.modelix.model.sync.bulk.ModelImporter
27+
import org.modelix.model.sync.bulk.import
28+
import org.modelix.model.sync.bulk.isModuleIncluded
29+
import java.io.File
30+
31+
object MPSBulkSynchronizer {
32+
33+
@JvmStatic
34+
fun exportRepository() {
35+
val repository = getRepository()
36+
val includedModuleNames = parseRawPropertySet(System.getProperty("modelix.mps.model.sync.bulk.output.modules"))
37+
val includedModulePrefixes = parseRawPropertySet(System.getProperty("modelix.mps.model.sync.bulk.output.modules.prefixes"))
38+
39+
repository.modelAccess.runReadAction {
40+
val allModules = repository.modules
41+
val includedModules = allModules.filter {
42+
isModuleIncluded(it.moduleName!!, includedModuleNames, includedModulePrefixes)
43+
}
44+
val numIncludedModules = includedModules.count()
45+
val outputPath = System.getProperty("modelix.mps.model.sync.bulk.output.path")
46+
47+
for ((index, module) in includedModules.withIndex()) {
48+
println("Exporting module ${index + 1} of $numIncludedModules: '${module.moduleName}'")
49+
val exporter = ModelExporter(MPSModuleAsNode(module))
50+
val outputFile = File(outputPath + File.separator + module.moduleName + ".json")
51+
exporter.export(outputFile)
52+
}
53+
}
54+
}
55+
56+
@JvmStatic
57+
fun importRepository() {
58+
val repository = getRepository()
59+
val includedModuleNames = parseRawPropertySet(System.getProperty("modelix.mps.model.sync.bulk.input.modules"))
60+
val includedModulePrefixes = parseRawPropertySet(System.getProperty("modelix.mps.model.sync.bulk.input.modules.prefixes"))
61+
val inputPath = System.getProperty("modelix.mps.model.sync.bulk.input.path")
62+
val access = repository.modelAccess
63+
64+
access.runWriteInEDT {
65+
val allModules = repository.modules
66+
val includedModules: Iterable<SModule> = allModules.filter {
67+
isModuleIncluded(it.moduleName!!, includedModuleNames, includedModulePrefixes)
68+
}
69+
val numIncludedModules = includedModules.count()
70+
71+
access.executeCommand {
72+
for ((index, module) in includedModules.withIndex()) {
73+
println("Importing module ${index + 1} of $numIncludedModules: '${module.moduleName}'")
74+
val moduleFile = File(inputPath + File.separator + module.moduleName + ".json")
75+
if (moduleFile.exists()) {
76+
val importer = ModelImporter(
77+
MPSModuleAsNode(module),
78+
)
79+
importer.import(moduleFile)
80+
}
81+
}
82+
}
83+
}
84+
85+
ApplicationManager.getApplication().invokeAndWait {
86+
repository.modelAccess.runWriteAction {
87+
repository.saveAll()
88+
}
89+
}
90+
}
91+
92+
@JvmStatic
93+
private fun parseRawPropertySet(rawProperty: String): Set<String> {
94+
return if (rawProperty.isEmpty()) {
95+
emptySet()
96+
} else {
97+
rawProperty.split(",")
98+
.dropLastWhile { it.isEmpty() }
99+
.toSet()
100+
}
101+
}
102+
103+
@JvmStatic
104+
private fun getRepository(): SRepository {
105+
val repoPath = System.getProperty("modelix.mps.model.sync.bulk.repo.path")
106+
val project = ProjectManager.getInstance().loadAndOpenProject(repoPath)
107+
val repo = ProjectHelper.getProjectRepository(project)
108+
109+
return checkNotNull(repo) { "project repository not found" }
110+
}
111+
}

0 commit comments

Comments
 (0)