Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Introduce Kotlin dependency helpers
Browse files Browse the repository at this point in the history
 * embeddedKotlinVersion
 * DependencyHandler.kotlinModule(module: String, version: String?)
 * RepositoryHandler.gradleScriptKotlin()

Resolves #94
Resolves #95
  • Loading branch information
bamboo committed Jul 18, 2016
1 parent b364b37 commit ee30b3a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
14 changes: 12 additions & 2 deletions build.gradle
@@ -1,7 +1,8 @@
buildscript {
ext.kotlinVersion = file('kotlin-version.txt').text.trim()
ext.kotlinRepo = 'https://repo.gradle.org/gradle/repo'
repositories {
maven { url 'https://repo.gradle.org/gradle/repo' }
maven { url kotlinRepo }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
Expand All @@ -14,9 +15,18 @@ apply from: 'build.gradle.kts'

// --- Enable automatic generation of API extensions -------------------
File apiExtensionsOutputDir = file("src/generated/kotlin")
task generateApiExtensions(type: codegen.GenerateConfigurationExtensions) {
task generateConfigurationExtensions(type: codegen.GenerateConfigurationExtensions) {
outputFile = new File(apiExtensionsOutputDir, "org/gradle/script/lang/kotlin/ConfigurationExtensions.kt")
}
task generateKotlinDependencyExtensions(type: codegen.GenerateKotlinDependencyExtensions) {
outputFile = new File(apiExtensionsOutputDir, "org/gradle/script/lang/kotlin/KotlinDependencyExtensions.kt")
embeddedKotlinVersion = kotlinVersion
gradleScriptKotlinRepository = kotlinRepo
}
task generateApiExtensions {
dependsOn generateConfigurationExtensions
dependsOn generateKotlinDependencyExtensions
}
sourceSets {
main.kotlin.srcDirs += apiExtensionsOutputDir
}
Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package codegen

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

import java.io.File

open class GenerateKotlinDependencyExtensions : DefaultTask() {

@get:OutputFile
var outputFile: File? = null

@get:Input
var embeddedKotlinVersion: String? = null

@get:Input
var gradleScriptKotlinRepository: String? = null

@TaskAction
fun generate() {
outputFile!!.writeText(
"""/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.script.lang.kotlin
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.ArtifactRepository
/**
* The version of the Kotlin compiler embedded in gradle-script-kotlin (currently _${embeddedKotlinVersion}_).
*/
val embeddedKotlinVersion = "$embeddedKotlinVersion"
/**
* Adds the remote repository containing the Kotlin libraries embedded in gradle-script-kotlin.
*/
fun RepositoryHandler.gradleScriptKotlin(): ArtifactRepository =
maven { it.setUrl("$gradleScriptKotlinRepository") }
/**
* Builds the dependency notation for the named Kotlin [module] at the given [version].
*
* @param module simple name of the Kotlin module, for example "reflect".
* @param version desired version, null implies [embeddedKotlinVersion].
*/
fun DependencyHandler.kotlinModule(module: String, version: String? = null): Any =
"org.jetbrains.kotlin:kotlin-${'$'}module:${'$'}{version ?: embeddedKotlinVersion}"
""")
}
}

0 comments on commit ee30b3a

Please sign in to comment.