summaryrefslogtreecommitdiff
path: root/plugins/kotlin/gradle/gradle-tooling/src/org/jetbrains/kotlin/idea/gradleTooling/builders/KotlinGradleFragmentBuilder.kt
blob: aabf75a446ad95c43ddf39419721e98476acabc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.gradleTooling.builders

import org.jetbrains.kotlin.idea.gradleTooling.KotlinFragmentImpl
import org.jetbrains.kotlin.idea.gradleTooling.KotlinProjectModelImportingContext
import org.jetbrains.kotlin.idea.gradleTooling.KotlinVariantImpl
import org.jetbrains.kotlin.idea.gradleTooling.reflect.KotlinFragmentReflection
import org.jetbrains.kotlin.idea.gradleTooling.reflect.KotlinVariantReflection
import org.jetbrains.kotlin.idea.projectModel.KotlinFragment
import org.jetbrains.kotlin.idea.projectModel.KotlinKPMModule.Companion.TEST_MODULE_NAME
import java.io.File

internal object KotlinGradleFragmentBuilder : KotlinProjectModelComponentBuilder<KotlinFragmentReflection, KotlinFragment> {
    override fun buildComponent(
        origin: KotlinFragmentReflection,
        importingContext: KotlinProjectModelImportingContext
    ): KotlinFragment? {
        val fragmentName = origin.fragmentName ?: return null
        val moduleIdentifier = origin.containingModule?.moduleIdentifier ?: return null
        val kotlinModuleIdentifier = KotlinModuleIdentifierBuilder.buildComponent(moduleIdentifier) ?: return null

        return importingContext.fragmentCache.withCache(kotlinModuleIdentifier, fragmentName) {
            val isTestModule = origin.containingModule?.name == TEST_MODULE_NAME
            val sourceDirs = origin.kotlinSourceSourceRoots?.srcDirs ?: emptySet()

            //TODO replace with a proper way to compute resources
            val resourceDirs: Set<File> = setOfNotNull(sourceDirs.singleOrNull()?.parentFile?.resolve("resources"))

            val kotlinLanguageSettings = origin.languageSettings?.let { KotlinLanguageSettingsBuilder.buildComponent(it) }

            val directRefinesDependencies = origin.directRefinesDependencies.orEmpty()
                .mapNotNull { dep -> buildComponent(dep, importingContext) }

            val resolvedDependencies = KotlinFragmentDependencyResolutionBuilder.buildComponent(origin, importingContext)

            val fragment = KotlinFragmentImpl(
                fragmentName = fragmentName,
                isTestFragment = isTestModule,
                moduleIdentifier = kotlinModuleIdentifier,
                languageSettings = kotlinLanguageSettings,
                directRefinesFragments = directRefinesDependencies,
                sourceDirs = sourceDirs,
                resourceDirs = resourceDirs,
                resolvedDependencies = resolvedDependencies
            )

            if (origin is KotlinVariantReflection) {
                val variantAttributes = origin.variantAttributes.orEmpty()
                val compilationOutputs = origin.compilationOutputs?.let { KotlinCompilationOutputBuilder.buildComponent(it) }

                return@withCache KotlinVariantImpl(
                    fragment,
                    variantAttributes = variantAttributes,
                    compilationOutputs = compilationOutputs
                )
            }

            return@withCache fragment
        }
    }
}