-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
364 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
.../kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/Npm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright 2010-2023 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.npm | ||
|
||
import org.gradle.api.logging.Logger | ||
import org.gradle.internal.service.ServiceRegistry | ||
import org.jetbrains.kotlin.gradle.internal.execWithProgress | ||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsEnv | ||
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution | ||
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnEnvironment | ||
import java.io.File | ||
|
||
class Npm : NpmApi<NpmEnvironment> { | ||
override fun preparedFiles(nodeJs: NodeJsEnvironment): Collection<File> { | ||
return listOf( | ||
nodeJs | ||
.rootPackageDir | ||
.resolve(NpmProject.PACKAGE_JSON) | ||
) | ||
} | ||
|
||
override fun prepareRootProject( | ||
nodeJs: NodeJsEnvironment, | ||
rootProjectName: String, | ||
rootProjectVersion: String, | ||
subProjects: Collection<PreparedKotlinCompilationNpmResolution>, | ||
resolutions: Map<String, String>, | ||
) { | ||
return prepareRootPackageJson( | ||
nodeJs, | ||
rootProjectName, | ||
rootProjectVersion, | ||
subProjects, | ||
resolutions | ||
) | ||
} | ||
|
||
private fun prepareRootPackageJson( | ||
nodeJs: NodeJsEnvironment, | ||
rootProjectName: String, | ||
rootProjectVersion: String, | ||
npmProjects: Collection<PreparedKotlinCompilationNpmResolution>, | ||
resolutions: Map<String, String> | ||
) { | ||
val rootPackageJsonFile = preparedFiles(nodeJs).single() | ||
|
||
saveRootProjectWorkspacesPackageJson( | ||
rootProjectName, | ||
rootProjectVersion, | ||
npmProjects, | ||
resolutions, | ||
rootPackageJsonFile | ||
) | ||
} | ||
|
||
override fun resolveRootProject( | ||
services: ServiceRegistry, | ||
logger: Logger, | ||
nodeJs: NodeJsEnvironment, | ||
environment: NpmEnvironment, | ||
npmProjects: Collection<PreparedKotlinCompilationNpmResolution>, | ||
cliArgs: List<String> | ||
) { | ||
val nodeJsWorldDir = nodeJs.rootPackageDir | ||
|
||
npmExec( | ||
services, | ||
logger, | ||
nodeJs, | ||
environment, | ||
nodeJsWorldDir, | ||
NpmApi.resolveOperationDescription("yarn"), | ||
cliArgs | ||
) | ||
} | ||
|
||
fun npmExec( | ||
services: ServiceRegistry, | ||
logger: Logger, | ||
nodeJs: NodeJsEnvironment, | ||
environment: NpmEnvironment, | ||
dir: File, | ||
description: String, | ||
args: List<String> | ||
) { | ||
services.execWithProgress(description) { exec -> | ||
val arguments = args | ||
.plus( | ||
if (logger.isDebugEnabled) "--verbose" else "" | ||
) | ||
.plus( | ||
if (environment.ignoreScripts) "--ignore-scripts" else "" | ||
).filter { it.isNotEmpty() } | ||
|
||
val nodeExecutable = nodeJs.nodeExecutable | ||
if (!environment.ignoreScripts) { | ||
val nodePath = if (nodeJs.isWindows) { | ||
File(nodeExecutable).parent | ||
} else { | ||
nodeExecutable | ||
} | ||
exec.environment( | ||
"PATH", | ||
"$nodePath${File.pathSeparator}${System.getenv("PATH")}" | ||
) | ||
} | ||
|
||
val command = environment.executable | ||
|
||
exec.executable = command | ||
exec.args = arguments | ||
|
||
exec.workingDir = dir | ||
} | ||
|
||
} | ||
|
||
private fun saveRootProjectWorkspacesPackageJson( | ||
rootProjectName: String, | ||
rootProjectVersion: String, | ||
npmProjects: Collection<PreparedKotlinCompilationNpmResolution>, | ||
resolutions: Map<String, String>, | ||
rootPackageJsonFile: File | ||
) { | ||
val nodeJsWorldDir = rootPackageJsonFile.parentFile | ||
val rootPackageJson = PackageJson(rootProjectName, rootProjectVersion) | ||
rootPackageJson.private = true | ||
|
||
val npmProjectWorkspaces = npmProjects.map { it.npmProjectDir.relativeTo(nodeJsWorldDir).path } | ||
val importedProjectWorkspaces = | ||
NpmImportedPackagesVersionResolver(npmProjects, nodeJsWorldDir).resolveAndUpdatePackages() | ||
|
||
rootPackageJson.workspaces = npmProjectWorkspaces + importedProjectWorkspaces | ||
rootPackageJson.resolutions = resolutions | ||
rootPackageJson.saveTo( | ||
rootPackageJsonFile | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...tlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmEnv.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright 2010-2023 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.npm | ||
|
||
data class NpmEnv( | ||
val executable: String, | ||
val ignoreScripts: Boolean, | ||
val overrides: List<NpmOverride> | ||
) |
21 changes: 21 additions & 0 deletions
21
...dle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmEnvironment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2010-2023 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.npm | ||
|
||
import java.io.Serializable | ||
|
||
data class NpmEnvironment( | ||
val executable: String, | ||
val ignoreScripts: Boolean, | ||
val overrides: List<NpmOverride> | ||
) : Serializable | ||
|
||
internal val NpmEnv.asNpmEnvironment | ||
get() = NpmEnvironment( | ||
executable, | ||
ignoreScripts, | ||
overrides | ||
) |
Oops, something went wrong.