[Gradle, JS] Forbid create peer dependency with directory
#KT-38683 fixed
This commit is contained in:
+89
-22
@@ -31,7 +31,7 @@ internal fun Project.addNpmDependencyExtension() {
|
|||||||
|
|
||||||
values()
|
values()
|
||||||
.forEach { scope ->
|
.forEach { scope ->
|
||||||
val extension = scope.name
|
val scopePrefix = scope.name
|
||||||
.removePrefix(NORMAL.name)
|
.removePrefix(NORMAL.name)
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
|
|
||||||
@@ -40,16 +40,21 @@ internal fun Project.addNpmDependencyExtension() {
|
|||||||
PEER -> NpmDependencyExtension::class.java
|
PEER -> NpmDependencyExtension::class.java
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val extension = when (scope) {
|
||||||
|
NORMAL, DEV, OPTIONAL -> DefaultNpmDependencyExtension(this, scope)
|
||||||
|
PEER -> NpmDependencyWithoutDirectoryExtension(this, scope)
|
||||||
|
}
|
||||||
|
|
||||||
extensions
|
extensions
|
||||||
.add(
|
.add(
|
||||||
TypeOf.typeOf<NpmDependencyExtension>(type),
|
TypeOf.typeOf<NpmDependencyExtension>(type),
|
||||||
lowerCamelCaseName(extension, "npm"),
|
lowerCamelCaseName(scopePrefix, "npm"),
|
||||||
DefaultNpmDependencyExtension(this, scope)
|
extension
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DefaultNpmDependencyExtension(
|
private abstract class AbstractNpmDependencyExtension(
|
||||||
private val project: Project,
|
private val project: Project,
|
||||||
private val scope: NpmDependency.Scope
|
private val scope: NpmDependency.Scope
|
||||||
) : NpmDependencyExtension,
|
) : NpmDependencyExtension,
|
||||||
@@ -66,14 +71,6 @@ private class DefaultNpmDependencyExtension(
|
|||||||
scope = scope
|
scope = scope
|
||||||
)
|
)
|
||||||
|
|
||||||
override operator fun invoke(name: String, directory: File): NpmDependency =
|
|
||||||
directoryNpmDependency(
|
|
||||||
project = project,
|
|
||||||
name = name,
|
|
||||||
directory = directory,
|
|
||||||
scope = scope
|
|
||||||
)
|
|
||||||
|
|
||||||
override operator fun invoke(directory: File): NpmDependency =
|
override operator fun invoke(directory: File): NpmDependency =
|
||||||
invoke(
|
invoke(
|
||||||
name = moduleName(directory),
|
name = moduleName(directory),
|
||||||
@@ -89,11 +86,12 @@ private class DefaultNpmDependencyExtension(
|
|||||||
name = arg,
|
name = arg,
|
||||||
args = *args
|
args = *args
|
||||||
)
|
)
|
||||||
is File -> invoke(arg)
|
else -> processNonStringFirstArgument(args)
|
||||||
else -> npmDeclarationException(args)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract fun processNonStringFirstArgument(arg: Any?, vararg args: Any?): NpmDependency
|
||||||
|
|
||||||
private fun withName(name: String, vararg args: Any?): NpmDependency {
|
private fun withName(name: String, vararg args: Any?): NpmDependency {
|
||||||
val arg = if (args.size > 1) args[1] else null
|
val arg = if (args.size > 1) args[1] else null
|
||||||
return when (arg) {
|
return when (arg) {
|
||||||
@@ -104,6 +102,65 @@ private class DefaultNpmDependencyExtension(
|
|||||||
name = name,
|
name = name,
|
||||||
version = arg
|
version = arg
|
||||||
)
|
)
|
||||||
|
else -> processNonStringNameArgument(name, arg, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun processNonStringNameArgument(
|
||||||
|
name: String,
|
||||||
|
arg: Any?,
|
||||||
|
vararg args: Any?
|
||||||
|
): NpmDependency
|
||||||
|
|
||||||
|
protected fun npmDeclarationException(args: Array<out Any?>): Nothing {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"""
|
||||||
|
|Unable to add NPM with scope '$scope' dependency by ${args.joinToString()}
|
||||||
|
|Possible variants:
|
||||||
|
|${possibleVariants().joinToString("\n") { "- ${it.first} -> ${it.second}" }}
|
||||||
|
""".trimMargin()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun possibleVariants(): List<Pair<String, String>> {
|
||||||
|
return listOf("npm('name', 'version')" to "name:version")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DefaultNpmDependencyExtension(
|
||||||
|
private val project: Project,
|
||||||
|
private val scope: NpmDependency.Scope
|
||||||
|
) : AbstractNpmDependencyExtension(project, scope) {
|
||||||
|
override fun invoke(name: String): NpmDependency =
|
||||||
|
onlyNameNpmDependency(name)
|
||||||
|
|
||||||
|
override operator fun invoke(name: String, directory: File): NpmDependency =
|
||||||
|
directoryNpmDependency(
|
||||||
|
project = project,
|
||||||
|
name = name,
|
||||||
|
directory = directory,
|
||||||
|
scope = scope
|
||||||
|
)
|
||||||
|
|
||||||
|
override operator fun invoke(directory: File): NpmDependency =
|
||||||
|
invoke(
|
||||||
|
name = moduleName(directory),
|
||||||
|
directory = directory
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun processNonStringFirstArgument(arg: Any?, vararg args: Any?): NpmDependency {
|
||||||
|
return when (arg) {
|
||||||
|
is File -> invoke(arg)
|
||||||
|
else -> npmDeclarationException(args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun processNonStringNameArgument(
|
||||||
|
name: String,
|
||||||
|
arg: Any?,
|
||||||
|
vararg args: Any?
|
||||||
|
): NpmDependency {
|
||||||
|
return when (arg) {
|
||||||
is File -> invoke(
|
is File -> invoke(
|
||||||
name = name,
|
name = name,
|
||||||
directory = arg
|
directory = arg
|
||||||
@@ -112,14 +169,24 @@ private class DefaultNpmDependencyExtension(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun npmDeclarationException(args: Array<out Any?>): Nothing {
|
override fun possibleVariants(): List<Pair<String, String>> {
|
||||||
throw IllegalArgumentException(
|
return super.possibleVariants() + listOf(
|
||||||
"""
|
"npm(File)" to "File.name:File",
|
||||||
Unable to add NPM dependency by $args
|
"npm('name', File)" to "name:File"
|
||||||
- npm('name', 'version') -> name:version
|
|
||||||
- npm(File) -> File.name:File
|
|
||||||
- npm('name', File) -> name:File
|
|
||||||
""".trimIndent()
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NpmDependencyWithoutDirectoryExtension(
|
||||||
|
project: Project,
|
||||||
|
scope: NpmDependency.Scope
|
||||||
|
) : AbstractNpmDependencyExtension(project, scope) {
|
||||||
|
override fun invoke(name: String, directory: File): NpmDependency =
|
||||||
|
npmDeclarationException(arrayOf(name, directory))
|
||||||
|
|
||||||
|
override fun processNonStringFirstArgument(arg: Any?, vararg args: Any?): NpmDependency =
|
||||||
|
npmDeclarationException(args)
|
||||||
|
|
||||||
|
override fun processNonStringNameArgument(name: String, arg: Any?, vararg args: Any?): NpmDependency =
|
||||||
|
npmDeclarationException(args)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user