override suspend fun run()

in sources/amper-cli/src/org/jetbrains/amper/tasks/web/WebCompileKlibTask.kt [90:187]


    override suspend fun run(
        dependenciesResult: List<TaskResult>,
        executionContext: TaskGraphExecutionContext,
    ): TaskResult {
        val fragments = module.fragments.filter {
            it.platforms.contains(platform) && it.isTest == isTest
        }
        if (fragments.isEmpty()) {
            error("Zero fragments in module ${module.userReadableName} for platform $platform isTest=$isTest")
        }

        // TODO The IR compiler needs recursive dependencies
        val externalDependencies = dependenciesResult
            .filterIsInstance<ResolveExternalDependenciesTask.Result>()
            .flatMap { it.compileClasspath }
            .distinct()
            .filterKLibs()
            .toList()

        logger.debug(
            "" +
                    "${expectedPlatform.name} compile ${module.userReadableName} -- collected external dependencies" +
                    if (externalDependencies.isNotEmpty()) "\n" else "" +
                            externalDependencies.sorted().joinToString("\n").prependIndent("  ")
        )

        val compileModuleDependencies = dependenciesResult.filterIsInstance<Result>()

        val compiledKlibModuleDependencies = compileModuleDependencies
            .mapNotNull { it.compiledKlib }
            .toList()

        val kotlinUserSettings = fragments.singleLeafFragment().serializableKotlinSettings()

        logger.debug("${expectedPlatform.name} compile klib '${module.userReadableName}' -- ${fragments.joinToString(" ") { it.name }}")

        val jdk = jdkProvider.getJdkOrUserError(module.jdkSettings)

        val libraryPaths = compiledKlibModuleDependencies + externalDependencies

        val additionalSources = additionalKotlinJavaSourceDirs.map { artifact ->
            SourceRoot(
                fragmentName = artifact.fragmentName,
                path = artifact.path,
            )
        }

        val productionCompileResult = if (isTest) {
            compileModuleDependencies.firstOrNull { it.module == module && !it.isTest }
                ?: error("${expectedPlatform.name} compilation result from production compilation result was not found for module=${module.userReadableName}, task=$taskName")
        } else null

        val sources = fragments.flatMap { it.sourceRoots } + additionalSources.map { it.path }

        val artifact = incrementalCache.execute(
            key = taskName.name,
            inputValues = mapOf(
                "kotlin.settings" to Json.encodeToString(kotlinUserSettings),
                "task.output.root" to taskOutputRoot.path.pathString,
            ),
            inputFiles = sources + libraryPaths,
        ) {
            cleanDirectory(taskOutputRoot.path)

            val artifact = taskOutputRoot.path

            // in Kotlin >= 2.2, we need to list all source files (not just dirs)
            val sourceFiles = sources.flatMap { dir ->
                // Kotlin IR compiler only accepts *.kt files, and we need to align with fragment arguments
                dir.walk().filter { it.extension in validSourceFileExtensions }
            }
            if (sourceFiles.isEmpty()) {
                logger.debug("No sources were found for ${fragments.identificationPhrase()}, skipping compilation")
                return@execute IncrementalCache.ExecutionResult(emptyList())
            }

            compileSources(
                jdk = jdk,
                kotlinUserSettings = kotlinUserSettings,
                sourceFiles = sourceFiles,
                fragments = fragments,
                additionalSourceRoots = additionalSources,
                librariesPaths = libraryPaths,
                friendPaths = listOfNotNull(productionCompileResult?.compiledKlib),
            )

            logger.info("Compiling module '${module.userReadableName}' for platform '${platform.pretty}'...")

            return@execute IncrementalCache.ExecutionResult(listOf(artifact))
        }.outputFiles.singleOrNull()

        return Result(
            compiledKlib = artifact,
            module = module,
            isTest = isTest,
            taskName = taskName,
        )
    }