Skip to content

Commit

Permalink
Merge pull request #316 from modelix/fix/bulk-sync-included-modules
Browse files Browse the repository at this point in the history
MODELIX-588 includedModules are not taken into account for the direction model-server -> MPS
  • Loading branch information
mhuster23 authored Nov 10, 2023
2 parents a22bb44 + 4b60d35 commit 4cbd113
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 237 deletions.
1 change: 1 addition & 0 deletions bulk-model-sync-gradle-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ modelSync {
}
}
direction("testPull") {
includeModule("GraphSolution")
fromModelServer {
url = "http://0.0.0.0:${Main.DEFAULT_PORT}/v2"
repositoryId = "ci-test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class ModelSyncGradlePlugin : Plugin<Project> {
it.repositoryId.set(serverSource.repositoryId)
it.branchName.set(serverSource.branchName)
it.revision.set(serverSource.revision)
it.includedModules.set(syncDirection.includedModules)
it.includedModulePrefixes.set(syncDirection.includedModulePrefixes)
}
return exportFromModelServer
}
Expand Down Expand Up @@ -173,6 +175,8 @@ class ModelSyncGradlePlugin : Plugin<Project> {
it.url.set(serverTarget.url)
it.repositoryId.set(serverTarget.repositoryId)
it.branchName.set(serverTarget.branchName)
it.includedModules.set(syncDirection.includedModules)
it.includedModulePrefixes.set(syncDirection.includedModulePrefixes)
}

project.tasks.register("runSync${syncDirection.name.replaceFirstChar { it.uppercaseChar() }}") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.modelix.model.ModelFacade
import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.IBranch
import org.modelix.model.api.INode
import org.modelix.model.api.PBranch
import org.modelix.model.api.getRootNode
import org.modelix.model.client2.IModelClientV2
Expand All @@ -36,6 +38,7 @@ import org.modelix.model.client2.ModelClientV2PlatformSpecificBuilder
import org.modelix.model.client2.getReplicatedModel
import org.modelix.model.lazy.RepositoryId
import org.modelix.model.sync.bulk.ModelExporter
import org.modelix.model.sync.bulk.isModuleIncluded
import javax.inject.Inject

abstract class ExportFromModelServer @Inject constructor(of: ObjectFactory) : DefaultTask() {
Expand All @@ -58,33 +61,52 @@ abstract class ExportFromModelServer @Inject constructor(of: ObjectFactory) : De
@OutputDirectory
val outputDir: DirectoryProperty = of.directoryProperty()

@Input
val includedModules: SetProperty<String> = of.setProperty(String::class.java)

@Input
val includedModulePrefixes: SetProperty<String> = of.setProperty(String::class.java)

@TaskAction
fun export() {
val client = ModelClientV2PlatformSpecificBuilder()
val modelClient = ModelClientV2PlatformSpecificBuilder()
.url(url.get())
.build()
modelClient.use { client ->
runBlocking { client.init() }

runBlocking { client.init() }
val branch = if (revision.isPresent) {
getBranchByRevision(client)
} else {
getBranchByRepoIdAndBranch(client)
}

val branch = if (revision.isPresent) {
getBranchByRevision(client)
} else {
getBranchByRepoIdAndBranch(client)
}
branch.runRead {
val root = branch.getRootNode()
logger.info("Got root node: {}", root)
val outputDir = outputDir.get().asFile

branch.runRead {
val root = branch.getRootNode()
logger.info("Got root node: {}", root)
val outputDir = outputDir.get().asFile
root.allChildren.forEach {
val nameRole = BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name
val fileName = it.getPropertyValue(nameRole)
val outputFile = outputDir.resolve("$fileName.json")
ModelExporter(it).export(outputFile)
getIncludedModules(root).forEach {
val fileName = it.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name)
val outputFile = outputDir.resolve("$fileName.json")
ModelExporter(it).export(outputFile)
}
}
}
}

private fun getIncludedModules(root: INode): Iterable<INode> {
val nameRole = BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name

return root.allChildren.filter {
val isModule = it.concept?.getUID() == BuiltinLanguages.MPSRepositoryConcepts.Module.getUID()
val moduleName = it.getPropertyValue(nameRole) ?: return@filter false
val isIncluded = isModuleIncluded(moduleName, includedModules.get(), includedModulePrefixes.get())

isModule && isIncluded
}
}

private fun getBranchByRepoIdAndBranch(client: ModelClientV2): IBranch {
val repoId = RepositoryId(repositoryId.get())
val branchRef = ModelFacade.createBranchReference(repoId, branchName.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package org.modelix.model.sync.bulk.gradle.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
Expand Down Expand Up @@ -55,10 +55,10 @@ abstract class GenerateAntScriptForMps @Inject constructor(of: ObjectFactory) :
val antScriptFile: RegularFileProperty = of.fileProperty()

@Input
val includedModules: ListProperty<String> = of.listProperty(String::class.java)
val includedModules: SetProperty<String> = of.setProperty(String::class.java)

@Input
val includedModulePrefixes: ListProperty<String> = of.listProperty(String::class.java)
val includedModulePrefixes: SetProperty<String> = of.setProperty(String::class.java)

@Optional
@Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.modelix.model.client2.runWrite
import org.modelix.model.lazy.RepositoryId
import org.modelix.model.sync.bulk.ModelImporter
import org.modelix.model.sync.bulk.importFilesAsRootChildren
import org.modelix.model.sync.bulk.isModuleIncluded
import javax.inject.Inject

abstract class ImportIntoModelServer @Inject constructor(of: ObjectFactory) : DefaultTask() {
Expand All @@ -55,6 +56,12 @@ abstract class ImportIntoModelServer @Inject constructor(of: ObjectFactory) : De
@Input
val registeredLanguages: SetProperty<ILanguage> = of.setProperty(ILanguage::class.java)

@Input
val includedModules: SetProperty<String> = of.setProperty(String::class.java)

@Input
val includedModulePrefixes: SetProperty<String> = of.setProperty(String::class.java)

@TaskAction
fun import() {
registeredLanguages.get().forEach {
Expand All @@ -66,8 +73,10 @@ abstract class ImportIntoModelServer @Inject constructor(of: ObjectFactory) : De

val branchRef = ModelFacade.createBranchReference(repoId, branchName.get())
val client = ModelClientV2PlatformSpecificBuilder().url(url.get()).build()
val files = inputDir.listFiles()?.filter { it.extension == "json" }
if (files.isNullOrEmpty()) error("no json files found")
val files = inputDir.listFiles()?.filter {
it.extension == "json" && isModuleIncluded(it.nameWithoutExtension, includedModules.get(), includedModulePrefixes.get())
}
if (files.isNullOrEmpty()) error("no json files found for included modules")

runBlocking {
client.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.data.ModelData
import org.modelix.model.data.NodeData

/**
* Checks if a module is included in the sync.
*
* @param moduleName name of the module to be checked
* @param includedModules collection of included module names
* @param includedPrefixes collection of included module name prefixes
*/
fun isModuleIncluded(moduleName: String, includedModules: Collection<String>, includedPrefixes: Collection<String>): Boolean {
val includedDirectly = includedModules.contains(moduleName)
val includedByPrefix = includedPrefixes.any { prefix -> moduleName.startsWith(prefix) }

return includedDirectly || includedByPrefix
}

fun mergeModelData(models: Collection<ModelData>): ModelData {
return ModelData(root = NodeData(children = models.map { it.root }))
}
Expand Down
Loading

0 comments on commit 4cbd113

Please sign in to comment.