Skip to content

Commit

Permalink
#2
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic committed Feb 20, 2024
1 parent 9cd2b55 commit 287d019
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package org.jetbrains.kotlin.gradle.targets.js.npm

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.logging.Logger
Expand All @@ -24,6 +27,7 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinProjectNpmResol
import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinRootNpmResolver
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.SingleActionPerProject
import java.io.File
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

Expand Down Expand Up @@ -87,14 +91,16 @@ abstract class KotlinNpmResolutionManager : BuildService<KotlinNpmResolutionMana
logger: Logger,
nodeJsEnvironment: NodeJsEnvironment,
environment: PackageManagerEnvironment,
) = prepareIfNeeded(logger = logger, nodeJsEnvironment, environment)
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
) = prepareIfNeeded(logger = logger, nodeJsEnvironment, environment, resolvedConfiguration)

internal fun installIfNeeded(
args: List<String> = emptyList(),
services: ServiceRegistry,
logger: Logger,
nodeJsEnvironment: NodeJsEnvironment,
packageManagerEnvironment: PackageManagerEnvironment,
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): Unit? {
synchronized(this) {
if (state is ResolutionState.Installed) {
Expand All @@ -106,7 +112,7 @@ abstract class KotlinNpmResolutionManager : BuildService<KotlinNpmResolutionMana
}

return try {
val installation: Installation = prepareIfNeeded(logger = logger, nodeJsEnvironment, packageManagerEnvironment)
val installation: Installation = prepareIfNeeded(logger = logger, nodeJsEnvironment, packageManagerEnvironment, resolvedConfiguration)
installation.install(args, services, logger, nodeJsEnvironment, packageManagerEnvironment)
state = ResolutionState.Installed()
} catch (e: Exception) {
Expand All @@ -120,6 +126,7 @@ abstract class KotlinNpmResolutionManager : BuildService<KotlinNpmResolutionMana
logger: Logger,
nodeJsEnvironment: NodeJsEnvironment,
packageManagerEnvironment: PackageManagerEnvironment,
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): Installation {
val state0 = this.state
return when (state0) {
Expand All @@ -137,7 +144,8 @@ abstract class KotlinNpmResolutionManager : BuildService<KotlinNpmResolutionMana
logger,
nodeJsEnvironment,
packageManagerEnvironment,
this
this,
resolvedConfiguration
).also {
this.state = ResolutionState.Prepared(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ package org.jetbrains.kotlin.gradle.targets.js.npm

import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin.Companion.kotlinNodeJsExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmProject.Companion.PACKAGE_JSON
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution
import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinRootNpmResolver
import org.jetbrains.kotlin.gradle.utils.getFile
import org.jetbrains.kotlin.gradle.utils.property
import java.io.File
Expand All @@ -26,11 +32,17 @@ abstract class PublicPackageJsonTask :
DefaultTask(),
UsesKotlinNpmResolutionManager {

private val nodeJs: NodeJsRootExtension
get() = project.rootProject.kotlinNodeJsExtension

@get:Internal
abstract val compilationDisambiguatedName: Property<String>

private val projectPath = project.path

private val rootResolver: KotlinRootNpmResolver
get() = nodeJs.resolver

@get:Input
val projectVersion = project.version.toString()

Expand Down Expand Up @@ -60,12 +72,27 @@ abstract class PublicPackageJsonTask :
}
}

@get:Internal
internal val components by lazy {
rootResolver.allConfigurations
}

// @get:Input
// internal abstract val components: Property<ResolvedComponentResult>

// @get:Input
// internal abstract val map: MapProperty<ComponentArtifactIdentifier, File>

private val compilationResolution: PreparedKotlinCompilationNpmResolution
get() = npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
.getResolutionOrPrepare(
npmResolutionManager.get(),
logger
)
get() = run {
// val resolvedConfiguration = components.get() to map.get().map { (key, value) -> key.componentIdentifier to value }.toMap()
npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
.getResolutionOrPrepare(
npmResolutionManager.get(),
logger,
components
)
}

@get:Input
val externalDependencies: Collection<NpmDependencyDeclaration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

package org.jetbrains.kotlin.gradle.targets.js.npm.resolved

import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.logging.Logger
import org.gradle.api.provider.Provider
import org.gradle.internal.service.ServiceRegistry
import org.jetbrains.kotlin.gradle.targets.js.nodejs.PackageManagerEnvironment
import org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager
import org.jetbrains.kotlin.gradle.targets.js.npm.NodeJsEnvironment
import org.jetbrains.kotlin.gradle.targets.js.npm.resolver.KotlinCompilationNpmResolution
import java.io.File
import java.io.Serializable

class KotlinRootNpmResolution(
Expand All @@ -27,6 +33,7 @@ class KotlinRootNpmResolution(
nodeJsEnvironment: NodeJsEnvironment,
packageManagerEnvironment: PackageManagerEnvironment,
npmResolutionManager: KotlinNpmResolutionManager,
aggregatedConfigurations: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): Installation {
synchronized(projects) {
npmResolutionManager.parameters.gradleNodeModulesProvider.get().close()
Expand All @@ -36,7 +43,8 @@ class KotlinRootNpmResolution(
.map {
it.close(
npmResolutionManager,
logger
logger,
aggregatedConfigurations
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@
package org.jetbrains.kotlin.gradle.targets.js.npm.resolver

import org.gradle.api.Action
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.FileCollectionDependency
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.internal.artifacts.DefaultProjectComponentIdentifier
import org.gradle.api.logging.Logger
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.bundling.Zip
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.isMain
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.nodejs.TasksRequirements
import org.jetbrains.kotlin.gradle.targets.js.npm.*
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.KotlinRootNpmResolution
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution
import org.jetbrains.kotlin.gradle.utils.CompositeProjectComponentArtifactMetadata
import org.jetbrains.kotlin.gradle.utils.getFile
import org.jetbrains.kotlin.gradle.utils.`is`
import org.jetbrains.kotlin.gradle.utils.topRealPath
import java.io.File
import java.io.Serializable

class KotlinCompilationNpmResolution(
// val componentResult: ResolvedComponentResult,
// val map: Map<ComponentArtifactIdentifier, File>,
// var internalDependencies: Collection<InternalDependency>,
// var internalCompositeDependencies: Collection<CompositeDependency>,
// var externalGradleDependencies: Collection<FileExternalGradleDependency>,
// var externalNpmDependencies: Collection<NpmDependencyDeclaration>,
// var fileCollectionDependencies: Collection<FileCollectionExternalGradleDependency>,
// val resolvedConfiguration: Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>,
val npmDeps: Set<NpmDependencyDeclaration>,
val fileDeps: Set<FileCollectionExternalGradleDependency>,
val projectPath: String,
val compilationDisambiguatedName: String,
val npmProjectName: String,
val npmProjectVersion: String,
val tasksRequirements: TasksRequirements,
) : Serializable {

var internalDependencies: Collection<InternalDependency>? = null
var internalCompositeDependencies: Collection<CompositeDependency>? = null
var externalGradleDependencies: Collection<FileExternalGradleDependency>? = null
var externalNpmDependencies: Collection<NpmDependencyDeclaration>? = null
var fileCollectionDependencies: Collection<FileCollectionExternalGradleDependency>? = null

// val inputs: PackageJsonProducerInputs
// get() = PackageJsonProducerInputs(
// internalDependencies.map { it.projectName },
Expand All @@ -63,18 +63,14 @@ class KotlinCompilationNpmResolution(
fun prepareWithDependencies(
npmResolutionManager: KotlinNpmResolutionManager,
logger: Logger,
resolvedConfiguration: Pair<ResolvedComponentResult, Map<ComponentIdentifier, File>>,
npmDeps: Set<NpmDependencyDeclaration>,
fileDeps: Set<FileCollectionExternalGradleDependency>
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): PreparedKotlinCompilationNpmResolution {
check(resolution == null) { "$this already resolved" }

return createPreparedResolution(
npmResolutionManager,
logger,
resolvedConfiguration,
npmDeps,
fileDeps,
resolvedConfiguration
).also {
resolution = it
}
Expand All @@ -84,46 +80,45 @@ class KotlinCompilationNpmResolution(
fun getResolutionOrPrepare(
npmResolutionManager: KotlinNpmResolutionManager,
logger: Logger,
resolvedConfiguration: Pair<ResolvedComponentResult, Map<ComponentIdentifier, File>>? = null,
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): PreparedKotlinCompilationNpmResolution {

return resolution ?: prepareWithDependencies(
npmResolutionManager,
logger,
resolvedConfiguration!!,
null!!,
null!!
resolvedConfiguration,
)
}

@Synchronized
fun close(
npmResolutionManager: KotlinNpmResolutionManager,
logger: Logger,
resolvedConfiguration: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): PreparedKotlinCompilationNpmResolution {
check(!closed) { "$this already closed" }
closed = true
return getResolutionOrPrepare(npmResolutionManager, logger)
return getResolutionOrPrepare(npmResolutionManager, logger, resolvedConfiguration)
}

fun createPreparedResolution(
private fun createPreparedResolution(
npmResolutionManager: KotlinNpmResolutionManager,
logger: Logger,
resolvedConfiguration: Pair<ResolvedComponentResult, Map<ComponentIdentifier, File>>,
npmDeps: Set<NpmDependencyDeclaration>,
fileDeps: Set<FileCollectionExternalGradleDependency>,
allConfigurations: Map<String, Map<String, Pair<Provider<ResolvedComponentResult>, Provider<Map<ComponentArtifactIdentifier, File>>>>>,
): PreparedKotlinCompilationNpmResolution {
val rootResolver = npmResolutionManager.parameters.resolution.get()

val visitor = ConfigurationVisitor(rootResolver)
visitor.visit(resolvedConfiguration.first to resolvedConfiguration.second)
val configuration = allConfigurations.getValue(projectPath).getValue(compilationDisambiguatedName)
visitor.visit(configuration.first.get() to configuration.second.get().map { (key, value) -> key.componentIdentifier to value }.toMap())

val internalNpmDependencies = visitor.internalDependencies
.map {
val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName]
compilationNpmResolution.getResolutionOrPrepare(
npmResolutionManager,
logger
logger,
allConfigurations
)
}
.flatMap { it.externalNpmDependencies }
Expand Down Expand Up @@ -283,7 +278,7 @@ class KotlinCompilationNpmResolution(

private fun visitArtifact(
dependency: ComponentIdentifier,
artifact: File
artifact: File,
) {
// val artifactId = artifact.id
// val componentIdentifier = dependency.id
Expand Down Expand Up @@ -326,11 +321,12 @@ class KotlinCompilationNpmResolution(
// }

private fun visitProjectDependency(
componentIdentifier: ProjectComponentIdentifier
componentIdentifier: ProjectComponentIdentifier,
) {
val dependentProject = rootResolution[componentIdentifier.projectPath]

val dependentCompilation = dependentProject.npmProjects.single { it.compilationDisambiguatedName.contains("main", ignoreCase = true) }
val dependentCompilation =
dependentProject.npmProjects.single { it.compilationDisambiguatedName.contains("main", ignoreCase = true) }

internalDependencies.add(
InternalDependency(
Expand All @@ -353,8 +349,6 @@ class KotlinCompilationNpmResolution(
// )




// ================
// private val internalDependencies = mutableSetOf<InternalDependency>()
// private val internalCompositeDependencies = mutableSetOf<CompositeDependency>()
Expand Down
Loading

0 comments on commit 287d019

Please sign in to comment.