[Gradle, JS] Add task for separate usage of Dukat with project npm dependencies
#KT-38331 fixed
This commit is contained in:
+27
-4
@@ -17,7 +17,8 @@ abstract class AbstractDukatTask(
|
|||||||
@Internal
|
@Internal
|
||||||
override val compilation: KotlinJsCompilation
|
override val compilation: KotlinJsCompilation
|
||||||
) : AbstractTask(), RequiresNpmDependencies {
|
) : AbstractTask(), RequiresNpmDependencies {
|
||||||
private val nodeJs get() = NodeJsRootPlugin.apply(project.rootProject)
|
@get:Internal
|
||||||
|
protected val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
|
||||||
|
|
||||||
@get:Internal
|
@get:Internal
|
||||||
override val nodeModulesRequired: Boolean
|
override val nodeModulesRequired: Boolean
|
||||||
@@ -27,6 +28,16 @@ abstract class AbstractDukatTask(
|
|||||||
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency>
|
override val requiredNpmDependencies: Set<RequiredKotlinJsDependency>
|
||||||
get() = setOf(nodeJs.versions.dukat)
|
get() = setOf(nodeJs.versions.dukat)
|
||||||
|
|
||||||
|
@get:Internal
|
||||||
|
val dts by lazy {
|
||||||
|
val resolvedCompilation = nodeJs.npmResolutionManager.requireInstalled()[project][compilation]
|
||||||
|
val dtsResolver = DtsResolver(resolvedCompilation.npmProject)
|
||||||
|
dtsResolver.getAllDts(
|
||||||
|
resolvedCompilation.externalNpmDependencies,
|
||||||
|
considerGeneratingFlag
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Package name for the generated file (by default filename.d.ts renamed to filename.d.kt)
|
* Package name for the generated file (by default filename.d.ts renamed to filename.d.kt)
|
||||||
*/
|
*/
|
||||||
@@ -38,13 +49,21 @@ abstract class AbstractDukatTask(
|
|||||||
* Collection of d.ts files
|
* Collection of d.ts files
|
||||||
*/
|
*/
|
||||||
@get:Internal
|
@get:Internal
|
||||||
abstract val dTsFiles: List<File>
|
val dTsFiles: List<File>
|
||||||
|
get() = dts.map { it.file }
|
||||||
|
|
||||||
|
@get:Input
|
||||||
|
val inputs
|
||||||
|
get() = dts.map { it.inputKey }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destination directory for files with converted declarations
|
* Destination directory for files with converted declarations
|
||||||
*/
|
*/
|
||||||
@get:OutputDirectory
|
@get:OutputDirectory
|
||||||
abstract val destDir: File
|
abstract var destDir: File
|
||||||
|
|
||||||
|
@get:Internal
|
||||||
|
internal abstract val considerGeneratingFlag: Boolean
|
||||||
|
|
||||||
@get:Internal
|
@get:Internal
|
||||||
val operation: String = "Generating Kotlin/JS external declarations"
|
val operation: String = "Generating Kotlin/JS external declarations"
|
||||||
@@ -53,6 +72,8 @@ abstract class AbstractDukatTask(
|
|||||||
open fun run() {
|
open fun run() {
|
||||||
nodeJs.npmResolutionManager.checkRequiredDependencies(this)
|
nodeJs.npmResolutionManager.checkRequiredDependencies(this)
|
||||||
|
|
||||||
|
destDir.deleteRecursively()
|
||||||
|
|
||||||
DukatRunner(
|
DukatRunner(
|
||||||
compilation,
|
compilation,
|
||||||
dTsFiles,
|
dTsFiles,
|
||||||
@@ -62,4 +83,6 @@ abstract class AbstractDukatTask(
|
|||||||
operation
|
operation
|
||||||
).execute()
|
).execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal const val DUKAT_TASK_GROUP = "Dukat"
|
||||||
+5
-2
@@ -18,10 +18,13 @@ class DtsResolver(val npmProject: NpmProject) {
|
|||||||
indexFileSuffixes = listOf(".d.ts")
|
indexFileSuffixes = listOf(".d.ts")
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getAllDts(externalNpmDependencies: Collection<NpmDependency>): List<Dts> {
|
fun getAllDts(
|
||||||
|
externalNpmDependencies: Collection<NpmDependency>,
|
||||||
|
considerGeneratingFlag: Boolean = true
|
||||||
|
): List<Dts> {
|
||||||
return externalNpmDependencies
|
return externalNpmDependencies
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.filter { it.generateExternals }
|
.filter { !considerGeneratingFlag || it.generateExternals }
|
||||||
.filter { it.scope == NORMAL || it.scope == OPTIONAL }
|
.filter { it.scope == NORMAL || it.scope == OPTIONAL }
|
||||||
.mapNotNullTo(mutableSetOf()) { typeModules.resolve(it.key)?.let { file -> Dts(file.canonicalFile, it) } }
|
.mapNotNullTo(mutableSetOf()) { typeModules.resolve(it.key)?.let { file -> Dts(file.canonicalFile, it) } }
|
||||||
.sortedBy { it.inputKey }
|
.sortedBy { it.inputKey }
|
||||||
|
|||||||
+16
-7
@@ -6,7 +6,6 @@
|
|||||||
package org.jetbrains.kotlin.gradle.targets.js.dukat
|
package org.jetbrains.kotlin.gradle.targets.js.dukat
|
||||||
|
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.disambiguateName
|
import org.jetbrains.kotlin.gradle.plugin.mpp.disambiguateName
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmDependency
|
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmDependency
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.npm.plugins.CompilationResolverPlugin
|
import org.jetbrains.kotlin.gradle.targets.js.npm.plugins.CompilationResolverPlugin
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.KotlinRootNpmResolution
|
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.KotlinRootNpmResolution
|
||||||
@@ -20,21 +19,31 @@ internal class DukatCompilationResolverPlugin(
|
|||||||
val nodeJs get() = resolver.nodeJs
|
val nodeJs get() = resolver.nodeJs
|
||||||
val npmProject get() = resolver.npmProject
|
val npmProject get() = resolver.npmProject
|
||||||
val compilation get() = npmProject.compilation
|
val compilation get() = npmProject.compilation
|
||||||
val taskName = npmProject.compilation.disambiguateName("generateExternals")
|
val integratedTaskName = npmProject.compilation.disambiguateName("generateExternalsIntegrated")
|
||||||
|
val separateTaskName = npmProject.compilation.disambiguateName("generateExternals")
|
||||||
|
|
||||||
init {
|
init {
|
||||||
compilation.defaultSourceSet.kotlin.srcDir(npmProject.externalsDir)
|
compilation.defaultSourceSet.kotlin.srcDir(npmProject.externalsDir)
|
||||||
|
|
||||||
val task = project.registerTask<DukatTask>(
|
val integratedTask = project.registerTask<DukatTask>(
|
||||||
taskName,
|
integratedTaskName,
|
||||||
listOf(compilation)
|
listOf(compilation)
|
||||||
) {
|
) {
|
||||||
it.group = NodeJsRootPlugin.TASKS_GROUP_NAME
|
it.group = DUKAT_TASK_GROUP
|
||||||
it.description = "Generate Kotlin/JS external declarations for .d.ts files in ${compilation}"
|
it.description = "Integrated generation Kotlin/JS external declarations for .d.ts files in ${compilation}"
|
||||||
it.dependsOn(nodeJs.npmInstallTask, npmProject.packageJsonTask)
|
it.dependsOn(nodeJs.npmInstallTask, npmProject.packageJsonTask)
|
||||||
}
|
}
|
||||||
|
|
||||||
compilation.compileKotlinTask.dependsOn(task)
|
compilation.compileKotlinTask.dependsOn(integratedTask)
|
||||||
|
|
||||||
|
project.registerTask<SeparateDukatTask>(
|
||||||
|
separateTaskName,
|
||||||
|
listOf(compilation)
|
||||||
|
) {
|
||||||
|
it.group = DUKAT_TASK_GROUP
|
||||||
|
it.description = "Generate Kotlin/JS external declarations for .d.ts files of all NPM dependencies in ${compilation}"
|
||||||
|
it.dependsOn(nodeJs.npmInstallTask, npmProject.packageJsonTask)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hookDependencies(
|
override fun hookDependencies(
|
||||||
|
|||||||
+5
-20
@@ -5,12 +5,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.gradle.targets.js.dukat
|
package org.jetbrains.kotlin.gradle.targets.js.dukat
|
||||||
|
|
||||||
import org.gradle.api.tasks.Input
|
|
||||||
import org.gradle.api.tasks.Internal
|
|
||||||
import org.gradle.api.tasks.OutputDirectory
|
import org.gradle.api.tasks.OutputDirectory
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||||
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.utils.property
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -19,26 +17,13 @@ open class DukatTask
|
|||||||
constructor(
|
constructor(
|
||||||
compilation: KotlinJsCompilation
|
compilation: KotlinJsCompilation
|
||||||
) : AbstractDukatTask(compilation) {
|
) : AbstractDukatTask(compilation) {
|
||||||
private val nodeJs get() = NodeJsRootPlugin.apply(project.rootProject)
|
|
||||||
|
|
||||||
@get:Internal
|
override val considerGeneratingFlag: Boolean = true
|
||||||
val dts by lazy {
|
|
||||||
val resolvedCompilation = nodeJs.npmResolutionManager.requireInstalled()[project][compilation]
|
|
||||||
val dtsResolver = DtsResolver(resolvedCompilation.npmProject)
|
|
||||||
dtsResolver.getAllDts(resolvedCompilation.externalNpmDependencies)
|
|
||||||
}
|
|
||||||
|
|
||||||
@get:Internal
|
|
||||||
override val dTsFiles: List<File>
|
|
||||||
get() = dts.map { it.file }
|
|
||||||
|
|
||||||
@get:Input
|
|
||||||
val inputs
|
|
||||||
get() = dts.map { it.inputKey }
|
|
||||||
|
|
||||||
@get:OutputDirectory
|
@get:OutputDirectory
|
||||||
override val destDir: File
|
override var destDir: File by property {
|
||||||
get() = compilation.npmProject.externalsDir
|
compilation.npmProject.externalsDir
|
||||||
|
}
|
||||||
|
|
||||||
private val executor by lazy {
|
private val executor by lazy {
|
||||||
DukatExecutor(nodeJs, dts, compilation.npmProject, true, compareInputs = false)
|
DukatExecutor(nodeJs, dts, compilation.npmProject, true, compareInputs = false)
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 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.dukat
|
||||||
|
|
||||||
|
import org.gradle.api.tasks.OutputDirectory
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
|
||||||
|
import org.jetbrains.kotlin.gradle.utils.property
|
||||||
|
import java.io.File
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
open class SeparateDukatTask
|
||||||
|
@Inject
|
||||||
|
constructor(
|
||||||
|
compilation: KotlinJsCompilation
|
||||||
|
) : AbstractDukatTask(compilation) {
|
||||||
|
override val considerGeneratingFlag: Boolean = false
|
||||||
|
|
||||||
|
@get:OutputDirectory
|
||||||
|
override var destDir: File by property {
|
||||||
|
project.projectDir.resolve("externals")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user