Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ Using annotations instead of comment directives to configure scripts is cleaner

// also protected artifact repositories are supported, see <https://github.com/holgerbrandl/kscript/blob/master/test/TestsReadme.md#manual-testing>
// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password")

// You can use environment variables for user and password when string surrounded by double {} brackets
// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="{{ARTIFACTORY_USER}}", password="{{ARTIFACTORY_PASSWORD}}")
// will be use 'ARTIFACTORY_USER' and 'ARTIFACTORY_PASSWORD' environment variables
// if the value doesn't found in the script environment will fail

// Include helper scripts without deployment or prior compilation
@file:Include("util.kt")
Expand Down
17 changes: 15 additions & 2 deletions src/main/kotlin/kscript/app/DependencyUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.sonatype.aether.artifact.Artifact
import org.sonatype.aether.repository.Authentication
import org.sonatype.aether.repository.RemoteRepository
import org.sonatype.aether.util.artifact.DefaultArtifact
import org.sonatype.aether.util.artifact.JavaScopes.COMPILE
import org.sonatype.aether.util.artifact.JavaScopes.RUNTIME
import java.io.File

Expand Down Expand Up @@ -68,12 +67,26 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
}
}

fun decodeEnv(value: String): String {
return if (value.startsWith("{{") && value.endsWith("}}")) {
val envKey = value.substring(2, value.length - 2)
val envValue = System.getenv()[envKey]
if (null == envValue) {
errorMsg("Could not resolve environment variable {{$envKey}} in maven repository credentials")
quit(1)
}
envValue
} else {
value
}
}

fun resolveDependenciesViaAether(depIds: List<String>, customRepos: List<MavenRepo>, loggingEnabled: Boolean): List<Artifact> {
val jcenter = RemoteRepository("jcenter", "default", "http://jcenter.bintray.com/")
val customRemoteRepos = customRepos.map { mavenRepo ->
RemoteRepository(mavenRepo.id, "default", mavenRepo.url).apply {
if (!mavenRepo.user.isNullOrEmpty() && !mavenRepo.password.isNullOrEmpty()) {
authentication = Authentication(mavenRepo.user, mavenRepo.password)
authentication = Authentication(decodeEnv(mavenRepo.user), decodeEnv(mavenRepo.password))
}
}
}
Expand Down