Skip to content

Commit

Permalink
Fix CopySamples: make task work for both gradle and gradle.kts files
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelPunegov committed Dec 3, 2020
1 parent 69a224a commit e373aa0
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions build-tools/src/main/kotlin/org/jetbrains/kotlin/CopySamples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ package org.jetbrains.kotlin
import groovy.lang.Closure
import org.gradle.api.Task
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.InputDirectory
import java.io.File

/**
* A task that copies samples and replaces direct repository URLs with ones provided by the cache-redirector service.
*/
open class CopySamples: Copy() {

var samplesDir = project.file("samples")

init {
configureReplacements()
}

fun samplesDir(path: Any) {
samplesDir = project.file(path)
}
@InputDirectory
var samplesDir: File = project.file("samples")

private fun configureReplacements() {
from(samplesDir) {
it.exclude("**/*.gradle.kts")
it.exclude("**/*.gradle")
}
from(samplesDir) {
it.include("**/*.gradle")
val replacements = replacementsWithWrapper { s -> "maven { url '$s' }" }
it.filter { line ->
replacements.forEach { (repo, replacement) ->
if (line.contains(repo)) {
return@filter line.replace(repo, replacement)
}
}
return@filter line
val repo = line.trim()
replacements[repo]?.let { r -> line.replace(repo, r) } ?: line
}
}
from(samplesDir) {
it.include("**/*.gradle.kts")
val replacements = replacementsWithWrapper { s -> "maven(\"$s\")"}
it.filter { line ->
val repo = line.trim()
replacements[repo]?.let { r -> line.replace(repo, r) } ?: line
}
}
}
Expand All @@ -42,14 +42,22 @@ open class CopySamples: Copy() {
return this
}

companion object {
val replacements = listOf(
"mavenCentral()" to "maven { url 'https://cache-redirector.jetbrains.com/maven-central' }",
"jcenter()" to "maven { url 'https://cache-redirector.jetbrains.com/jcenter' }",
"https://dl.bintray.com/kotlin/kotlin-dev" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-dev",
"https://dl.bintray.com/kotlin/kotlin-eap" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-eap",
"https://dl.bintray.com/kotlin/ktor" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/ktor",
"https://plugins.gradle.org/m2" to "https://cache-redirector.jetbrains.com/plugins.gradle.org/m2"
)
}
private fun replacementsWithWrapper(wrap: (String) -> String) =
urlReplacements.map { entry ->
Pair(wrap(entry.key), wrap(entry.value))
}.toMap() + centralReplacements.map { entry ->
Pair(entry.key, wrap(entry.value))
}.toMap()

private val urlReplacements = mapOf(
"https://dl.bintray.com/kotlin/kotlin-dev" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-dev",
"https://dl.bintray.com/kotlin/kotlin-eap" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-eap",
"https://dl.bintray.com/kotlin/ktor" to "https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/ktor",
"https://plugins.gradle.org/m2" to "https://cache-redirector.jetbrains.com/plugins.gradle.org/m2"
)

private val centralReplacements = mapOf(
"mavenCentral()" to "https://cache-redirector.jetbrains.com/maven-central",
"jcenter()" to "https://cache-redirector.jetbrains.com/jcenter",
)
}

0 comments on commit e373aa0

Please sign in to comment.