[Gradle, JS] Add distribution DSL
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.targets.js.dsl
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
interface Distribution {
|
||||||
|
var directory: File?
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.targets.js.dsl
|
||||||
|
|
||||||
|
@Experimental(level = Experimental.Level.WARNING)
|
||||||
|
@Target(AnnotationTarget.FUNCTION)
|
||||||
|
annotation class ExperimentalDistributionDsl
|
||||||
+10
@@ -54,6 +54,16 @@ interface KotlinJsBrowserDsl : KotlinJsSubTargetDsl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExperimentalDistributionDsl
|
||||||
|
fun distribution(body: Distribution.() -> Unit)
|
||||||
|
|
||||||
|
@ExperimentalDistributionDsl
|
||||||
|
fun distribution(fn: Closure<*>) {
|
||||||
|
distribution {
|
||||||
|
ConfigureUtil.configure(fn, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun webpackTask(body: KotlinWebpack.() -> Unit)
|
fun webpackTask(body: KotlinWebpack.() -> Unit)
|
||||||
fun webpackTask(fn: Closure<*>) {
|
fun webpackTask(fn: Closure<*>) {
|
||||||
webpackTask {
|
webpackTask {
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.targets.js.subtargets
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.targets.js.dsl.Distribution
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class BrowserDistribution : Distribution {
|
||||||
|
override var directory: File? = null
|
||||||
|
}
|
||||||
+30
-17
@@ -7,16 +7,15 @@ package org.jetbrains.kotlin.gradle.targets.js.subtargets
|
|||||||
|
|
||||||
import org.gradle.api.NamedDomainObjectContainer
|
import org.gradle.api.NamedDomainObjectContainer
|
||||||
import org.gradle.api.Task
|
import org.gradle.api.Task
|
||||||
|
import org.gradle.api.plugins.BasePluginConvention
|
||||||
|
import org.gradle.api.tasks.Copy
|
||||||
import org.gradle.api.tasks.TaskProvider
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce
|
import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.BuildVariant
|
import org.jetbrains.kotlin.gradle.targets.js.dsl.*
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.BuildVariantKind
|
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDceDsl
|
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBrowserDsl
|
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||||
@@ -37,6 +36,7 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
private val commonWebpackConfigurations: MutableList<KotlinWebpack.() -> Unit> = mutableListOf()
|
private val commonWebpackConfigurations: MutableList<KotlinWebpack.() -> Unit> = mutableListOf()
|
||||||
private val commonRunConfigurations: MutableList<KotlinWebpack.() -> Unit> = mutableListOf()
|
private val commonRunConfigurations: MutableList<KotlinWebpack.() -> Unit> = mutableListOf()
|
||||||
private val dceConfigurations: MutableList<KotlinJsDce.() -> Unit> = mutableListOf()
|
private val dceConfigurations: MutableList<KotlinJsDce.() -> Unit> = mutableListOf()
|
||||||
|
private val distribution: Distribution = BrowserDistribution()
|
||||||
|
|
||||||
private lateinit var buildVariants: NamedDomainObjectContainer<BuildVariant>
|
private lateinit var buildVariants: NamedDomainObjectContainer<BuildVariant>
|
||||||
|
|
||||||
@@ -53,6 +53,11 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
commonRunConfigurations.add(body)
|
commonRunConfigurations.add(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExperimentalDistributionDsl
|
||||||
|
override fun distribution(body: Distribution.() -> Unit) {
|
||||||
|
distribution.body()
|
||||||
|
}
|
||||||
|
|
||||||
override fun webpackTask(body: KotlinWebpack.() -> Unit) {
|
override fun webpackTask(body: KotlinWebpack.() -> Unit) {
|
||||||
commonWebpackConfigurations.add(body)
|
commonWebpackConfigurations.add(body)
|
||||||
}
|
}
|
||||||
@@ -143,6 +148,22 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
|
|
||||||
val compileKotlinTask = compilation.compileKotlinTask
|
val compileKotlinTask = compilation.compileKotlinTask
|
||||||
|
|
||||||
|
val basePluginConvention = project.convention.plugins["base"] as BasePluginConvention?
|
||||||
|
|
||||||
|
distribution.directory = distribution.directory ?: project.buildDir.resolve(basePluginConvention!!.distsDirName)
|
||||||
|
|
||||||
|
val distributionTask = project.registerTask<Copy>(
|
||||||
|
disambiguateCamelCased(
|
||||||
|
DISTRIBUTION_TASK_NAME
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
it.from(compilation.output.resourcesDir)
|
||||||
|
it.into(distribution.directory!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
val assembleTask = project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME)
|
||||||
|
assembleTask.dependsOn(distributionTask)
|
||||||
|
|
||||||
buildVariants.all { buildVariant ->
|
buildVariants.all { buildVariant ->
|
||||||
val kind = buildVariant.kind
|
val kind = buildVariant.kind
|
||||||
val webpackTask = project.registerTask<KotlinWebpack>(
|
val webpackTask = project.registerTask<KotlinWebpack>(
|
||||||
@@ -154,13 +175,15 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
) {
|
) {
|
||||||
it.dependsOn(
|
it.dependsOn(
|
||||||
nodeJs.npmInstallTask,
|
nodeJs.npmInstallTask,
|
||||||
target.project.tasks.getByName(compilation.processResourcesTaskName)
|
target.project.tasks.getByName(compilation.processResourcesTaskName),
|
||||||
|
distributionTask
|
||||||
)
|
)
|
||||||
|
|
||||||
it.configureOptimization(kind)
|
it.configureOptimization(kind)
|
||||||
|
|
||||||
it.compilation = compilation
|
it.compilation = compilation
|
||||||
it.description = "build webpack ${kind.name.toLowerCase()} bundle"
|
it.description = "build webpack ${kind.name.toLowerCase()} bundle"
|
||||||
|
it.destinationDirectory = distribution.directory
|
||||||
|
|
||||||
when (kind) {
|
when (kind) {
|
||||||
BuildVariantKind.PRODUCTION -> {
|
BuildVariantKind.PRODUCTION -> {
|
||||||
@@ -180,21 +203,10 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
commonWebpackConfigurations.forEach { configure ->
|
commonWebpackConfigurations.forEach { configure ->
|
||||||
it.configure()
|
it.configure()
|
||||||
}
|
}
|
||||||
|
|
||||||
it.doLast { _ ->
|
|
||||||
val resourcesDir = compilation.output.resourcesDir
|
|
||||||
if (resourcesDir.exists()) {
|
|
||||||
resourcesDir
|
|
||||||
.copyRecursively(
|
|
||||||
target = it.destinationDirectory!!,
|
|
||||||
overwrite = true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kind == BuildVariantKind.PRODUCTION) {
|
if (kind == BuildVariantKind.PRODUCTION) {
|
||||||
project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).dependsOn(webpackTask)
|
assembleTask.dependsOn(webpackTask)
|
||||||
project.registerTask<Task>(disambiguateCamelCased(WEBPACK_TASK_NAME)) {
|
project.registerTask<Task>(disambiguateCamelCased(WEBPACK_TASK_NAME)) {
|
||||||
it.dependsOn(webpackTask)
|
it.dependsOn(webpackTask)
|
||||||
}
|
}
|
||||||
@@ -274,5 +286,6 @@ open class KotlinBrowserJs @Inject constructor(target: KotlinJsTarget) :
|
|||||||
const val DEVELOPMENT = "development"
|
const val DEVELOPMENT = "development"
|
||||||
|
|
||||||
private const val WEBPACK_TASK_NAME = "webpack"
|
private const val WEBPACK_TASK_NAME = "webpack"
|
||||||
|
private const val DISTRIBUTION_TASK_NAME = "distribution"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-10
@@ -101,16 +101,6 @@ open class KotlinWebpack : DefaultTask(), RequiresNpmDependencies {
|
|||||||
open val evaluatedConfigFile: File
|
open val evaluatedConfigFile: File
|
||||||
@OutputFile get() = reportDir.resolve("webpack.config.evaluated.js")
|
@OutputFile get() = reportDir.resolve("webpack.config.evaluated.js")
|
||||||
|
|
||||||
val resourcesDir: File?
|
|
||||||
@Optional
|
|
||||||
@InputDirectory
|
|
||||||
get() {
|
|
||||||
val resourcesDir = compilation.output.resourcesDir
|
|
||||||
return if (resourcesDir.exists()) {
|
|
||||||
resourcesDir
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
@Input
|
@Input
|
||||||
var bin: String = "webpack/bin/webpack.js"
|
var bin: String = "webpack/bin/webpack.js"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user