Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm libs in version catalogs #671

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ internal fun Dependency.npmModuleId(): ModuleId.Npm {
}

internal fun Dependency.matches(moduleId: ModuleId): Boolean {
return moduleId.group == group && moduleId.name == name
Comment on lines 25 to -26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leads to an allocation on every single matches check, that's the reason why I didn't just use the conversion trick.

Could you revert this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I can not, otherwise it does not work anymore, that was not just a cosmetic change:

moduleId.group == group && moduleId.name == name
actions == null && cache == @actions/cache
moduleId == moduleId()
Npm(group=actions, name=cache) == Npm(group=actions, name=cache)

I can try to reprogram it so that still the necessary calculation is done but no allocation necessary maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention that I did. :-)

return if ((moduleId is ModuleId.Npm) && (moduleId.group != null)) {
moduleId.let { "@${it.group}/${it.name}" } == name
} else {
moduleId.group == group && moduleId.name == name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package de.fayard.refreshVersions.core.internal

import de.fayard.refreshVersions.core.ModuleId
import de.fayard.refreshVersions.core.extensions.gradle.moduleId
import de.fayard.refreshVersions.core.extensions.gradle.npmModuleId
import de.fayard.refreshVersions.core.extensions.gradle.tryExtractingSimpleVersion
import de.fayard.refreshVersions.core.internal.VersionManagementKind.Match
import de.fayard.refreshVersions.core.internal.VersionManagementKind.NoMatch
import org.gradle.api.artifacts.Dependency
Expand Down Expand Up @@ -89,15 +91,28 @@ private fun Dependency.hasVersionInVersionCatalog(
versionsCatalogLibraries: Collection<MinimalExternalModuleDependency>,
versionsCatalogPlugins: Set<PluginDependencyCompat> = emptySet()
): Boolean {
if (this !is ExternalDependency) return false
when {
this::class.simpleName == "NpmDependency" -> {
return versionsCatalogLibraries.any {
val moduleId = npmModuleId()
it.module.group == (moduleId.group ?: "<unscoped>") && it.module.name == moduleId.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "<unscoped>" thing is a a convention you defined, that isn't standard, right?
Isn't it possible to just have no group (i.e. null) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a convention I invented and also suggested in the mentioned https://youtrack.jetbrains.com/issue/KT-56416/KJS-Gradle-npm-compatibility-with-Gradle-version-catalogs ticket.
I hope for it to become the official standard once that ticket maybe gets attention.

Just having semver = { module = "semver", version.ref = "semver" } does not work, as the version catalog parser complains it is not valid syntax.
Same for semver = { module = ":semver", version.ref = "semver" }, invalid syntax.
So you need the colon and you need something before it.
semver = { module = "null:semver", version.ref = "semver" } would be valid in the version catalog, but actually "null" would be a valid scope too and could also easily confuse users, so I decided to use something that clearly says it is an unscoped package and is very unlikely to be used as actual scope if it is even a valid scope name, hence "<unscoped>".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I would hope is for Gradle to allow null group as they do when not using version catalogs, that'd cut down the noise. On the other hand, unscoped dependencies are not that great of a thing, so maybe making it super obvious might be a good thing… 🤔

&& it.versionConstraint.tryExtractingSimpleVersion() == version
}
}

val matchingLib = versionsCatalogLibraries.any {
it.module.group == group && it.module.name == name && it.versionConstraint == versionConstraint
}
if (matchingLib) return true
this is ExternalDependency -> {
val matchingLib = versionsCatalogLibraries.any {
it.module.group == group && it.module.name == name
&& it.versionConstraint == versionConstraint
}
if (matchingLib) return true

if (name.endsWith(".gradle.plugin").not()) return false
if (name.endsWith(".gradle.plugin").not()) return false

val pluginId = name.substringBeforeLast(".gradle.plugin")
return versionsCatalogPlugins.any { it.pluginId == pluginId && it.version == versionConstraint }
val pluginId = name.substringBeforeLast(".gradle.plugin")
return versionsCatalogPlugins.any { it.pluginId == pluginId && it.version == versionConstraint }
}

else -> return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal class VersionsCatalogUpdater(
}.mapNotNull { libOrPlugin ->
dependenciesUpdates.firstOrNull {
val moduleId = it.moduleId
(moduleId.name == libOrPlugin.name) && (moduleId.group == libOrPlugin.group)
(moduleId.name == libOrPlugin.name) && ((moduleId.group ?: "<unscoped>") == libOrPlugin.group)
}
}.firstOrNull()
}
Expand Down