Skip to content

Commit 41c30f0

Browse files
committed
also resolve environment variables in password of maven repo
1 parent 525ee23 commit 41c30f0

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/main/kotlin/kscript/app/Script.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ data class Script(val lines: List<String>, val extension: String = "kts") : Iter
7070
}
7171

7272

73-
private val KSCRIPT_DIRECTIVE_ANNO: List<Regex> = listOf("DependsOn", "KotlinOpts", "Include", "EntryPoint", "MavenRepository", "DependsOnMaven", "CompilerOpts")
74-
.map { "^@file:$it[(]".toRegex() }
73+
private val KSCRIPT_DIRECTIVE_ANNO: List<Regex> =
74+
listOf("DependsOn", "KotlinOpts", "Include", "EntryPoint", "MavenRepository", "DependsOnMaven", "CompilerOpts")
75+
.map { "^@file:$it[(]".toRegex() }
7576

7677
private fun isKscriptAnnotation(line: String) =
7778
KSCRIPT_DIRECTIVE_ANNO.any { line.contains(it) }
@@ -140,8 +141,8 @@ private fun String.extractAnnotParams(): List<String> {
140141
// https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks
141142
val annotationArgs = """(["'])(\\?.*?)\1""".toRegex()
142143
.findAll(this).toList().map {
143-
it.groupValues[2]
144-
}
144+
it.groupValues[2]
145+
}
145146

146147
// fail if any argument is a comma separated list of artifacts (see #101)
147148
annotationArgs.filter { it.contains(",[^)]".toRegex()) }.let {
@@ -201,18 +202,20 @@ fun Script.collectRepos(): List<MavenRepo> {
201202
val keyValSep = "[ ]*=[ ]*\"".toRegex()
202203

203204
val namedArgs = annotationParams
204-
.filter { it.contains(keyValSep) }
205-
.map { keyVal -> keyVal.split(keyValSep).map { it.trim(' ', '\"') }.let{ it.first() to it.last()}}
206-
.toMap()
205+
.filter { it.contains(keyValSep) }
206+
.map { keyVal ->
207+
keyVal.split(keyValSep).map { it.trim(' ', '\"') }.let { it.first() to it.last() }
208+
}
209+
.toMap()
207210

208211
if (annotationParams.size < 2) {
209212
throw IllegalArgumentException("Missing ${2 - annotationParams.size} of the required arguments for @file:MavenRepository(id, url)")
210213
}
211214
MavenRepo(
212-
namedArgs.getOrDefault("id", annotationParams[0]),
213-
decodeEnv(namedArgs.getOrDefault("url", annotationParams[1])),
214-
decodeEnv(namedArgs.getOrDefault("user", annotationParams.getOrNull(2) ?: "")),
215-
namedArgs.getOrDefault("password", annotationParams.getOrNull(3) ?: "")
215+
namedArgs.getOrDefault("id", annotationParams[0]),
216+
decodeEnv(namedArgs.getOrDefault("url", annotationParams[1])),
217+
decodeEnv(namedArgs.getOrDefault("user", annotationParams.getOrNull(2) ?: "")),
218+
decodeEnv(namedArgs.getOrDefault("password", annotationParams.getOrNull(3) ?: ""))
216219
)
217220
}
218221
}
@@ -235,9 +238,9 @@ fun Script.collectRuntimeOptions(): String {
235238
//support for @file:KotlinOpts see #47
236239
val annotatonPrefix = "^@file:KotlinOpts[(]".toRegex()
237240
kotlinOpts = kotlinOpts + lines
238-
.filter { it.contains(annotatonPrefix) }
239-
.map { it.replaceFirst(annotatonPrefix, "").split(")")[0] }
240-
.map { it.trim(' ', '"') }
241+
.filter { it.contains(annotatonPrefix) }
242+
.map { it.replaceFirst(annotatonPrefix, "").split(")")[0] }
243+
.map { it.trim(' ', '"') }
241244

242245

243246
// Append $KSCRIPT_KOTLIN_OPTS if defined in the parent environment
@@ -255,7 +258,8 @@ fun Script.collectRuntimeOptions(): String {
255258
fun Script.collectCompilerOptions(): String {
256259
val koptsPrefix = "//COMPILER_OPTS "
257260

258-
val compilerOpts = lines.filter { it.startsWith(koptsPrefix) }.map { it.replaceFirst(koptsPrefix, "").trim() }.toMutableList()
261+
val compilerOpts =
262+
lines.filter { it.startsWith(koptsPrefix) }.map { it.replaceFirst(koptsPrefix, "").trim() }.toMutableList()
259263

260264
val annotationPrefix = "^@file:CompilerOpts[(]".toRegex()
261265
compilerOpts += lines

0 commit comments

Comments
 (0)