diff --git a/docs/configDirFormat.md b/docs/configDirFormat.md index b9ade8b..ccdb432 100644 --- a/docs/configDirFormat.md +++ b/docs/configDirFormat.md @@ -11,7 +11,7 @@ Top-level folders are `presets`. It's like template when you create new project Next, `type` should be selected. It's main template files. Default is `root`. It's for root project. There is idea that in some cases variations of main template files will be needed (with the same extensions). -Files with extension `.jar` is considered binary files, and they will be copied without variables substitution. +Files with extensions `.jar` and `.bat` are considered binary files, and they will be copied without variables substitution. All other files are considered template files and variable substitution will be done. ## Extension @@ -25,7 +25,7 @@ Case is ignored, it will be uppercase as variable. Values of variable is content Please, note that `project` extension name is reserved for data from `kbre.yaml` file. -Files with extension `.jar` is considered binary files, and they will be copied without variables substitution. +Files with extensions `.jar` and `.bat` are considered binary files, and they will be copied without variables substitution. All other files are considered template files and variable substitution will be done. ## Variable diff --git a/src/nativeMain/kotlin/name/stepin/action/update/Extension.kt b/src/nativeMain/kotlin/name/stepin/action/update/Extension.kt index 679b2ad..3be2b75 100644 --- a/src/nativeMain/kotlin/name/stepin/action/update/Extension.kt +++ b/src/nativeMain/kotlin/name/stepin/action/update/Extension.kt @@ -14,7 +14,6 @@ data class Extension( private val reservedFiles = setOf( "gradle/libs.versions.toml", - "vars", ) fun loadExtension( @@ -25,13 +24,17 @@ data class Extension( val path = extensionsPath / name val extra = listRecursively(path) - .filterKeys { !reservedFiles.contains(it.relativeFilename) } + .filterKeys { + !reservedFiles.contains(it.relativeFilename) && !it.relativeFilename.startsWith("vars/") + } .entries.associate { it.key to it.value } val newExtra = if (new) { listRecursively(extensionsPath / "$name-new") - .filterKeys { !reservedFiles.contains(it.relativeFilename) } + .filterKeys { + !reservedFiles.contains(it.relativeFilename) && !it.relativeFilename.startsWith("vars/") + } .entries.associate { it.key to it.value } } else { emptyMap() @@ -40,18 +43,28 @@ data class Extension( val libs = loadIfExists(path / "gradle/libs.versions.toml") val libsMap = if (libs.isNullOrEmpty()) emptyMap() else mapOf("LIBS" to libs) - val vars = listRecursively(extensionsPath / "vars") - .entries.mapNotNull { - val filename = it.key.relativeFilename + val vars = + listRecursively(path / "vars") + .entries.mapNotNull { + val filename = it.key.relativeFilename - val content = loadIfExists(path / filename) - ?: return@mapNotNull null + val content = + loadIfExists(path / "vars" / filename) + ?: return@mapNotNull null - val varName = filename.substringBeforeLast(".").uppercase() + val varName = filename.substringBeforeLast(".").uppercase() - varName to content - } - .toMap() + varName to content + } + .fold(mutableMapOf()) { map, (k, v) -> + map[k] = + if (map.containsKey(k)) { + map[k] + "\n" + v + } else { + v + } + map + } return Extension( name = name, diff --git a/src/nativeMain/kotlin/name/stepin/action/update/FilesGenerator.kt b/src/nativeMain/kotlin/name/stepin/action/update/FilesGenerator.kt index 1a19f4b..8877d4c 100644 --- a/src/nativeMain/kotlin/name/stepin/action/update/FilesGenerator.kt +++ b/src/nativeMain/kotlin/name/stepin/action/update/FilesGenerator.kt @@ -18,7 +18,7 @@ data class FilesGenerator( val variables: Map, ) { companion object { - private val varRegEx = Regex("%[a-zA-Z0-9\\-_]*%") + private val varRegEx = Regex("%[A-Z0-9_]+%") fun substituteVariables( template: String, @@ -32,7 +32,7 @@ data class FilesGenerator( } // replace undefined variables - content.replace(varRegEx, "") + content = content.replace(varRegEx, "") return content } diff --git a/src/nativeMain/kotlin/name/stepin/action/update/UpdateAction.kt b/src/nativeMain/kotlin/name/stepin/action/update/UpdateAction.kt index 3df056b..ca9840a 100644 --- a/src/nativeMain/kotlin/name/stepin/action/update/UpdateAction.kt +++ b/src/nativeMain/kotlin/name/stepin/action/update/UpdateAction.kt @@ -81,9 +81,20 @@ class UpdateAction { extensions: List, variables: Map, ): Map { - return extensions.flatMap { it.variables.entries }.associate { - it.key to substituteVariables(it.value, variables) - } + return extensions.map { it.variables } + .fold(mutableMapOf()) { map, varsMap -> + varsMap.forEach { + val k = it.key + val v = substituteVariables(it.value, variables) + map[k] = + if (map.containsKey(k)) { + map[k] + "\n" + v + } else { + v + } + } + map + } } private fun prepareVariables(configuration: UpdateConfiguration): Map { @@ -128,7 +139,8 @@ class UpdateAction { private fun isBinary(): (Map.Entry) -> Boolean = { - it.key.relativeFilename.endsWith(".jar") + it.key.relativeFilename.endsWith(".jar") || + it.key.relativeFilename.endsWith(".bat") } private fun prepareLibsVersionsTemplate( diff --git a/src/nativeMain/kotlin/name/stepin/utils/MapUtils.kt b/src/nativeMain/kotlin/name/stepin/utils/MapUtils.kt index 3c1acc8..f890ea8 100644 --- a/src/nativeMain/kotlin/name/stepin/utils/MapUtils.kt +++ b/src/nativeMain/kotlin/name/stepin/utils/MapUtils.kt @@ -1,7 +1,6 @@ package name.stepin.utils object MapUtils { - fun Map.uppercaseKeys(): Map { return entries.associate { (key, value) -> key.uppercase() to value } }