Skip to content

Commit 923e67d

Browse files
committed
Support whitespace around maven repo credentials (fixes #228)
1 parent d5e4349 commit 923e67d

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,23 @@ data class MavenRepo(val id: String, val url: String, val user: String = "", val
188188
* Collect custom artifact repos declared with @file:MavenRepository
189189
*/
190190
fun Script.collectRepos(): List<MavenRepo> {
191-
val dependsOnMavenPrefix = "^@file:MavenRepository[(]".toRegex()
191+
val mvnRepoAnnotPrefix = "^@file:MavenRepository[(]".toRegex()
192192
// only supported annotation format for now
193193

194194
// @file:MavenRepository("imagej", "http://maven.imagej.net/content/repositories/releases/")
195195
// @file:MavenRepository("imagej", "http://maven.imagej.net/content/repositories/releases/", user="user", password="pass")
196196
return lines
197-
.filter { it.contains(dependsOnMavenPrefix) }
198-
.map { it.replaceFirst(dependsOnMavenPrefix, "").substringBeforeLast(")") }
199-
.map {
200-
it.split(",").map { it.trim(' ', '"', '(') }.let { annotationParams ->
201-
val namedArgs = annotationParams.filter { it.contains("=") }.map { item: String -> item.let { item.substringBefore("=") to item.substringAfter("=\"") } }.toMap()
197+
.filter { it.contains(mvnRepoAnnotPrefix) }
198+
.map { it.replaceFirst(mvnRepoAnnotPrefix, "").substringBeforeLast(")") }
199+
.map { repoLine ->
200+
repoLine.split(",").map { it.trim(' ', '"', '(') }.let { annotationParams ->
201+
val keyValSep = "[ ]*=[ ]*\"".toRegex()
202+
203+
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()
207+
202208
MavenRepo(annotationParams[0], annotationParams[1], namedArgs.getOrDefault("user", ""), namedArgs.getOrDefault("password", ""))
203209
}
204210
}

src/test/kotlin/Tests.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ class Tests {
9393
fun customRepoWithCreds() {
9494
val lines = listOf(
9595
"""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="user", password="pass") """,
96+
// Same but name arg comes last
9697
"""@file:MavenRepository("imagej-snapshots", "http://maven.imagej.net/content/repositories/snapshots", password="pass", user="user") """,
98+
// Whitespaces around credentials see #228
99+
"""@file:MavenRepository("spaceAroundCredentials", "http://maven.imagej.net/content/repositories/snapshots", password= "pass" , user= "user" ) """,
100+
// Different whitespaces around credentials see #228
101+
"""@file:MavenRepository("spaceAroundCredentials2", "http://maven.imagej.net/content/repositories/snapshots", password= "pass", user="user" ) """,
102+
103+
// some other script bits unrelated to the repo definition
97104
"""@file:DependsOnMaven("net.clearvolume:cleargl:2.0.1")""",
98105
"""@file:DependsOn("log4j:log4j:1.2.14")""",
99106
"""println("foo")"""
@@ -103,7 +110,9 @@ class Tests {
103110

104111
collectRepos() shouldBe listOf(
105112
MavenRepo("imagej-releases", "http://maven.imagej.net/content/repositories/releases", "user", "pass"),
106-
MavenRepo("imagej-snapshots", "http://maven.imagej.net/content/repositories/snapshots", "user", "pass") //Provided with name in non-typical order
113+
MavenRepo("imagej-snapshots", "http://maven.imagej.net/content/repositories/snapshots", "user", "pass"),
114+
MavenRepo("spaceAroundCredentials", "http://maven.imagej.net/content/repositories/snapshots", "user", "pass"),
115+
MavenRepo("spaceAroundCredentials2", "http://maven.imagej.net/content/repositories/snapshots", "user", "pass")
107116
)
108117

109118
collectDependencies() shouldBe listOf(
@@ -116,7 +125,6 @@ class Tests {
116125
}
117126

118127

119-
120128
// combine kotlin opts spread over multiple lines
121129
@Test
122130
fun optsCollect() {

0 commit comments

Comments
 (0)