Skip to content

Commit e472371

Browse files
committed
refactor(bulk-model-sync-gradle): cleanup
1 parent 305f9f8 commit e472371

File tree

5 files changed

+71
-63
lines changed

5 files changed

+71
-63
lines changed

bulk-model-sync-gradle/src/main/kotlin/org/modelix/model/sync/bulk/gradle/ModelSyncGradlePlugin.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import org.modelix.model.sync.bulk.gradle.tasks.ImportIntoModelServer
3333
import org.modelix.model.sync.bulk.gradle.tasks.ImportIntoMps
3434
import org.modelix.model.sync.bulk.gradle.tasks.ValidateSyncSettings
3535
import java.io.File
36-
import java.net.URL
37-
import java.util.Enumeration
3836
import java.util.Properties
3937

4038
class ModelSyncGradlePlugin : Plugin<Project> {
@@ -83,7 +81,7 @@ class ModelSyncGradlePlugin : Plugin<Project> {
8381
val sourceTask = when (syncDirection.source) {
8482
is LocalSource -> registerTasksForLocalSource(syncDirection, project, previousTask, jsonDir)
8583
is ServerSource -> registerTasksForServerSource(syncDirection, project, previousTask, jsonDir)
86-
else -> previousTask
84+
else -> error("Unknown sync direction source")
8785
}
8886

8987
when (syncDirection.target) {
@@ -226,8 +224,8 @@ class ModelSyncGradlePlugin : Plugin<Project> {
226224
}
227225

228226
private fun readModelixCoreVersion(): String? {
229-
val resources: Enumeration<URL>? = javaClass.classLoader.getResources("modelix.core.version.properties")
230-
while (resources != null && resources.hasMoreElements()) {
227+
val resources = javaClass.classLoader.getResources("modelix.core.version.properties") ?: return null
228+
if (resources.hasMoreElements()) {
231229
val properties = resources.nextElement().openStream().use { Properties().apply { load(it) } }
232230
return properties.getProperty("modelix.core.version")
233231
}

bulk-model-sync-gradle/src/main/kotlin/org/modelix/model/sync/bulk/gradle/config/ModelSyncGradleSettings.kt

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ data class SyncDirection(
7777
}
7878

7979
interface SyncEndPoint {
80-
fun getValidationErrors(): String
80+
fun getValidationErrors(): List<String>
8181
}
8282

8383
sealed interface LocalEndpoint : SyncEndPoint {
8484
var mpsHome: File?
8585
var mpsHeapSize: String
8686
var repositoryDir: File?
8787

88-
override fun getValidationErrors(): String {
89-
return buildString {
90-
if (mpsHome == null) {
91-
appendUndefinedLocalFieldError("mpsHome")
92-
}
93-
if (repositoryDir == null) {
94-
appendUndefinedLocalFieldError("repositoryDir")
95-
}
88+
override fun getValidationErrors(): List<String> {
89+
val errors = mutableListOf<String>()
90+
if (mpsHome == null) {
91+
errors.addUndefinedLocalFieldError("mpsHome")
9692
}
93+
if (repositoryDir == null) {
94+
errors.addUndefinedLocalFieldError("repositoryDir")
95+
}
96+
return errors
9797
}
9898
}
9999

@@ -114,12 +114,12 @@ sealed interface ServerEndpoint : SyncEndPoint {
114114
var repositoryId: String?
115115
var branchName: String?
116116

117-
override fun getValidationErrors(): String {
118-
return buildString {
119-
if (url == null) {
120-
appendUndefinedServerFieldError("url")
121-
}
117+
override fun getValidationErrors(): List<String> {
118+
val errors = mutableListOf<String>()
119+
if (url == null) {
120+
errors.addUndefinedServerFieldError("url")
122121
}
122+
return errors
123123
}
124124
}
125125

@@ -129,19 +129,28 @@ data class ServerSource(
129129
override var branchName: String? = null,
130130
var revision: String? = null,
131131
) : ServerEndpoint {
132-
override fun getValidationErrors(): String {
133-
return buildString {
134-
append(super.getValidationErrors())
135-
if (revision == null) {
136-
if (repositoryId == null && branchName == null) {
137-
appendLine("Invalid server source. Please either specify a revision or repositoryId and branchName.")
138-
} else if (repositoryId == null) {
139-
appendUndefinedServerFieldError("repositoryId")
140-
} else if (branchName == null) {
141-
appendUndefinedServerFieldError("branchName")
142-
}
143-
}
132+
override fun getValidationErrors(): List<String> {
133+
val errors = mutableListOf<String>()
134+
errors.addAll(super.getValidationErrors())
135+
if (revision != null) {
136+
// If a revision is specified, repo and branch are not required
137+
return errors
138+
}
139+
140+
if (repositoryId == null && branchName == null) {
141+
// Give hint is configuration is completely off
142+
errors.add("Invalid server source. Please either specify a revision or repositoryId and branchName.")
143+
return errors
144144
}
145+
146+
// Configuration is incomplete
147+
if (repositoryId == null) {
148+
errors.addUndefinedServerFieldError("repositoryId")
149+
} else if (branchName == null) {
150+
errors.addUndefinedServerFieldError("branchName")
151+
}
152+
153+
return errors
145154
}
146155
}
147156

@@ -150,29 +159,29 @@ data class ServerTarget(
150159
override var repositoryId: String? = null,
151160
override var branchName: String? = null,
152161
) : ServerEndpoint {
153-
override fun getValidationErrors(): String {
154-
return buildString {
155-
append(super.getValidationErrors())
162+
override fun getValidationErrors(): List<String> {
163+
val errors = mutableListOf<String>()
164+
errors.addAll(super.getValidationErrors())
156165

157-
if (repositoryId == null) {
158-
appendUndefinedServerFieldError("repositoryId")
159-
}
166+
if (repositoryId == null) {
167+
errors.addUndefinedServerFieldError("repositoryId")
168+
}
160169

161-
if (branchName == null) {
162-
appendUndefinedServerFieldError("branchName")
163-
}
170+
if (branchName == null) {
171+
errors.addUndefinedServerFieldError("branchName")
164172
}
173+
return errors
165174
}
166175
}
167176

168-
private fun StringBuilder.appendUndefinedLocalFieldError(fieldName: String) {
169-
appendUndefinedFieldError(fieldName, "LocalEndpoint")
177+
private fun MutableList<String>.addUndefinedLocalFieldError(fieldName: String) {
178+
addUndefinedFieldError(fieldName, "LocalEndpoint")
170179
}
171180

172-
private fun StringBuilder.appendUndefinedServerFieldError(fieldName: String) {
173-
appendUndefinedFieldError(fieldName, "ServerEndpoint")
181+
private fun MutableList<String>.addUndefinedServerFieldError(fieldName: String) {
182+
addUndefinedFieldError(fieldName, "ServerEndpoint")
174183
}
175184

176-
private fun StringBuilder.appendUndefinedFieldError(fieldName: String, block: String) {
177-
appendLine("Undefined '$fieldName' in '$block'.")
185+
private fun MutableList<String>.addUndefinedFieldError(fieldName: String, block: String) {
186+
add("Undefined '$fieldName' in '$block'.")
178187
}

bulk-model-sync-gradle/src/main/kotlin/org/modelix/model/sync/bulk/gradle/tasks/ExportFromModelServer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ abstract class ExportFromModelServer @Inject constructor(of: ObjectFactory) : De
7070

7171
branch.runRead {
7272
val root = branch.getRootNode()
73-
println("Got root node: $root")
73+
logger.info("Got root node: {}", root)
7474
val outputDir = outputDir.get().asFile
7575
root.allChildren.forEach {
76-
val outputFile = outputDir.resolve("${it.getPropertyValue(IProperty.fromName(RepositoryLanguage.NamePropertyUID))}.json")
76+
val nameRole = IProperty.fromName(RepositoryLanguage.NamePropertyUID)
77+
val outputFile = outputDir.resolve("${it.getPropertyValue(nameRole)}.json")
7778
ModelExporter(it).export(outputFile)
7879
}
7980
}

bulk-model-sync-gradle/src/main/kotlin/org/modelix/model/sync/bulk/gradle/tasks/ImportIntoModelServer.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ abstract class ImportIntoModelServer @Inject constructor(of: ObjectFactory) : De
7272
client.getReplicatedModel(branchRef).start()
7373
}
7474

75-
val files = inputDir.listFiles()?.filter { it.extension == "json" } ?: error("no json files found")
75+
val files = inputDir.listFiles()?.filter { it.extension == "json" }
76+
if (files.isNullOrEmpty()) error("no json files found")
7677

7778
branch.runWrite {
7879
val rootNode = branch.getRootNode()
79-
println("Got root node: $rootNode")
80-
println("Importing...")
80+
logger.info("Got root node: {}", rootNode)
81+
logger.info("Importing...")
8182
ModelImporter(branch.getRootNode()).importFilesAsRootChildren(*files.toTypedArray())
82-
println("Import finished")
83+
logger.info("Import finished")
8384
}
8485
}
8586
}

bulk-model-sync-gradle/src/main/kotlin/org/modelix/model/sync/bulk/gradle/tasks/ValidateSyncSettings.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import org.modelix.model.sync.bulk.gradle.config.ServerTarget
3030
import org.modelix.model.sync.bulk.gradle.config.SyncDirection
3131
import javax.inject.Inject
3232

33+
/**
34+
* Instead of throwing exceptions for single configuration errors,
35+
* this task collects all configuration errors and puts them into a single exception,
36+
* so the user can see all steps that must be taken at a glance.
37+
*/
3338
@CacheableTask
3439
abstract class ValidateSyncSettings @Inject constructor(of: ObjectFactory) : DefaultTask() {
3540

@@ -67,14 +72,8 @@ abstract class ValidateSyncSettings @Inject constructor(of: ObjectFactory) : Def
6772
appendLine("Undefined name.")
6873
}
6974

70-
if (direction.source == null) {
71-
appendLine("Undefined source.")
72-
}
73-
if (direction.target == null) {
74-
appendLine("Undefined target.")
75-
}
76-
7775
if (direction.source == null || direction.target == null) {
76+
appendLine("Both source and target have to be defined.")
7877
return@buildString
7978
}
8079

@@ -88,15 +87,15 @@ abstract class ValidateSyncSettings @Inject constructor(of: ObjectFactory) : Def
8887
return@buildString
8988
}
9089

91-
val sourceErrors = direction.source?.getValidationErrors() ?: ""
90+
val sourceErrors = direction.source?.getValidationErrors() ?: emptyList()
9291
if (sourceErrors.isNotEmpty()) {
9392
appendLine()
94-
appendLine(sourceErrors)
93+
appendLine(sourceErrors.joinToString(separator = "\n") { it })
9594
}
9695

97-
val targetErrors = direction.target?.getValidationErrors() ?: ""
96+
val targetErrors = direction.target?.getValidationErrors() ?: emptyList()
9897
if (targetErrors.isNotEmpty()) {
99-
appendLine(targetErrors)
98+
appendLine(targetErrors.joinToString(separator = "\n") { it })
10099
}
101100
}
102101

0 commit comments

Comments
 (0)