Skip to content

Commit

Permalink
minor: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Stepin committed Aug 13, 2024
1 parent de14bff commit 4db2227
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/configDirFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 25 additions & 12 deletions src/nativeMain/kotlin/name/stepin/action/update/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ data class Extension(
private val reservedFiles =
setOf(
"gradle/libs.versions.toml",
"vars",
)

fun loadExtension(
Expand All @@ -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()
Expand All @@ -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<String, String>()) { map, (k, v) ->
map[k] =
if (map.containsKey(k)) {
map[k] + "\n" + v
} else {
v
}
map
}

return Extension(
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class FilesGenerator(
val variables: Map<String, String>,
) {
companion object {
private val varRegEx = Regex("%[a-zA-Z0-9\\-_]*%")
private val varRegEx = Regex("%[A-Z0-9_]+%")

fun substituteVariables(
template: String,
Expand All @@ -32,7 +32,7 @@ data class FilesGenerator(
}

// replace undefined variables
content.replace(varRegEx, "")
content = content.replace(varRegEx, "")

return content
}
Expand Down
20 changes: 16 additions & 4 deletions src/nativeMain/kotlin/name/stepin/action/update/UpdateAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,20 @@ class UpdateAction {
extensions: List<Extension>,
variables: Map<String, String>,
): Map<String, String> {
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<String, String> {
Expand Down Expand Up @@ -128,7 +139,8 @@ class UpdateAction {

private fun isBinary(): (Map.Entry<WriteTarget, Any>) -> Boolean =
{
it.key.relativeFilename.endsWith(".jar")
it.key.relativeFilename.endsWith(".jar") ||
it.key.relativeFilename.endsWith(".bat")
}

private fun prepareLibsVersionsTemplate(
Expand Down
1 change: 0 additions & 1 deletion src/nativeMain/kotlin/name/stepin/utils/MapUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package name.stepin.utils

object MapUtils {

fun Map<String, String>.uppercaseKeys(): Map<String, String> {
return entries.associate { (key, value) -> key.uppercase() to value }
}
Expand Down

0 comments on commit 4db2227

Please sign in to comment.