Skip to content

Commit

Permalink
3.0.1
Browse files Browse the repository at this point in the history
- Fix `"deprecated": false` flagging deps as deprecated (fixes #122)
- Fix empty computed registry throwing at fetch
- Log improvements
- Code improvements
- Update Gradle version and dependencies
  • Loading branch information
WarningImHack3r committed Sep 3, 2024
1 parent fea8953 commit 4a392b8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

## [Unreleased]

### Fixed

- Fix `false` deprecated field flagging dependencies as deprecated (#122)
- Fix registry affectation logic freezing the plugin

## [3.0.0] - 2024-08-01

### Removed
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.warningimhack3r.npmupdatedependencies
pluginName = npm-update-dependencies
pluginRepositoryUrl = https://github.com/WarningImHack3r/npm-update-dependencies
# SemVer format -> https://semver.org
pluginVersion = 3.0.0
pluginVersion = 3.0.1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 223
Expand All @@ -21,7 +21,7 @@ platformPlugins =
platformBundledPlugins =

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 8.9
gradleVersion = 8.10

# Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib
kotlin.stdlib.default.dependency = false
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ semver4j = "5.3.0"
# plugins
changelog = "2.2.1"
intellijPlatform = "2.0.1"
kotlin = "2.0.10"
kotlin = "2.0.20"
kover = "0.8.3"
qodana = "2024.1.9"
serialization = "1.7.1"
serialization = "1.7.2"

[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.warningimhack3r.npmupdatedependencies.backend.engine

import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asBoolean
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asJsonArray
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asJsonObject
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.asString
import com.github.warningimhack3r.npmupdatedependencies.backend.extensions.isBlankOrEmpty
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
Expand All @@ -28,10 +30,8 @@ class NPMJSClient(private val project: Project) {
log.info("Getting registry for package $packageName")
val state = NUDState.getInstance(project)
val shellRunner = ShellRunner.getInstance(project)
return state.packageRegistries[packageName].also {
if (it != null) {
log.debug("Registry for package $packageName found in cache: $it")
}
return state.packageRegistries[packageName]?.also {
log.debug("Registry for package $packageName found in cache: $it")
} ?: shellRunner.execute(
arrayOf("npm", "v", packageName, "dist.tarball")
)?.trim()?.let { dist ->
Expand All @@ -48,11 +48,13 @@ class NPMJSClient(private val project: Project) {
return@let null.also {
log.debug("No dist.tarball found for package $packageName in any registry")
}
}.substringBefore("/$packageName").ifEmpty {
log.debug("No registry found for package $packageName")
return@let null
}
val registry = computedRegistry.substringBefore("/$packageName")
log.info("Computed registry for package $packageName: $registry")
state.packageRegistries[packageName] = registry
registry
log.info("Computed registry for package $packageName: $computedRegistry")
state.packageRegistries[packageName] = computedRegistry
computedRegistry
} ?: NPMJS_REGISTRY.also {
log.info("Using default registry for package $packageName")
}
Expand Down Expand Up @@ -82,15 +84,13 @@ class NPMJSClient(private val project: Project) {
log.info("Getting latest version for package $packageName")
val registry = getRegistry(packageName)
val json = getBodyAsJSON("${registry}/$packageName/latest")
return json?.get("version")?.asString.also {
if (it != null) {
log.info("Latest version for package $packageName found in cache: $it")
}
return json?.get("version")?.asString?.also {
log.info("Latest version for package $packageName found online: $it")
} ?: ShellRunner.getInstance(project).execute(
arrayOf("npm", "v", packageName, "version", "--registry=$registry")
)?.trim()?.let { it.ifEmpty { null } }.also {
if (it != null) {
log.info("Latest version for package $packageName found: $it")
log.info("Latest version for package $packageName found locally: $it")
} else {
log.warn("Latest version for package $packageName not found")
}
Expand All @@ -101,11 +101,9 @@ class NPMJSClient(private val project: Project) {
log.info("Getting all versions for package $packageName")
val registry = getRegistry(packageName)
val json = getBodyAsJSON("${registry}/$packageName")
return json?.get("versions")?.asJsonObject?.keys?.toList().also {
if (it != null) {
log.info("All versions for package $packageName found in cache (${it.size} versions)")
log.debug("Versions in cache for $packageName: $it")
}
return json?.get("versions")?.asJsonObject?.keys?.toList()?.also {
log.info("All versions for package $packageName found in online (${it.size} versions)")
log.debug("Versions for $packageName: $it")
} ?: ShellRunner.getInstance(project).execute(
arrayOf("npm", "v", packageName, "versions", "--json", "--registry=$registry")
)?.trim()?.let { versions ->
Expand All @@ -122,27 +120,43 @@ class NPMJSClient(private val project: Project) {
} else {
listOf(versions.replace("\"", ""))
}
}.also { versions ->
if (versions != null) {
log.info("All versions for package $packageName found (${versions.size} versions)")
log.debug("Versions for $packageName: $versions")
}
}?.also { versions ->
log.info("All versions for package $packageName found locally (${versions.size} versions)")
log.debug("Local versions for $packageName: $versions")
}
}

fun getPackageDeprecation(packageName: String): String? {
log.info("Getting deprecation status for package $packageName")
val registry = getRegistry(packageName)
val json = getBodyAsJSON("${registry}/$packageName/latest")
return json?.get("deprecated")?.asString.also {
if (it != null) {
log.info("Deprecation status for package $packageName found in cache: $it")

fun deprecationStatus(deprecation: String, local: Boolean = false): String? {
log.debug("Deprecation status for package $packageName before transformation: $deprecation")
return with(deprecation) {
when {
local && isBlankOrEmpty() -> null
equals("true", ignoreCase = true) || (!local && isBlankOrEmpty()) -> "Deprecated"
equals("false", ignoreCase = true) -> null
else -> this
}
}.also { reason ->
log.debug("Deprecation status for package $packageName after transformation: $reason")
}
}

return json?.get("deprecated")?.let { deprecation ->
log.debug("Deprecation status for package $packageName found online: $deprecation")
if (deprecation.asBoolean == true) {
"Deprecated"
} else deprecation.asString?.let { deprecationStatus(it) }
}?.also { reason ->
log.info("Online deprecation after transformation: $reason")
} ?: ShellRunner.getInstance(project).execute(
arrayOf("npm", "v", packageName, "deprecated", "--registry=$registry")
)?.trim()?.let { it.ifEmpty { null } }.also {
if (it != null) {
log.info("Deprecation status for package $packageName found: $it")
)?.trim()?.let { deprecationStatus(it, local = true) }.also { reason ->
if (reason != null) {
log.info("Deprecation status for package $packageName found locally: $reason")
} else {
log.debug("No deprecation status found for package $packageName")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ShellRunner(private val project: Project) {

private val failedWindowsPrograms = mutableSetOf<String>()

private fun isWindows() = System.getProperty("os.name").lowercase().contains("win")
private fun isWindows() = System.getProperty("os.name").contains("win", ignoreCase = true)

fun execute(command: Array<String>): String? {
val program = command.firstOrNull() ?: return null.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.github.warningimhack3r.npmupdatedependencies.backend.extensions
import com.intellij.json.psi.JsonValue
import kotlinx.coroutines.*
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

fun <T> safeConversion(block: () -> T): T? = try {
block()
} catch (_: Exception) {
} catch (_: IllegalArgumentException) {
null
}

Expand All @@ -20,11 +22,16 @@ val JsonElement.asJsonArray
get() = safeConversion { jsonArray }

val JsonElement.asString
get() = safeConversion { jsonPrimitive.content }
get() = jsonPrimitive.contentOrNull

val JsonElement.asBoolean
get() = jsonPrimitive.booleanOrNull

fun JsonValue.stringValue(): String = text.replace("\"", "")

// Credit: https://jivimberg.io/blog/2018/05/04/parallel-map-in-kotlin/
fun <T, R> Iterable<T>.parallelMap(mapper: suspend (T) -> R) = runBlocking(SupervisorJob() + Dispatchers.Default) {
coroutineScope { map { async { mapper(it) } }.awaitAll() }
}

fun String.isBlankOrEmpty() = isBlank() || isEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GitHubErrorReportSubmitter : ErrorReportSubmitter() {
var causedByLastIndex = -1
val splitStackTrace = stackTrace.split("\n")
splitStackTrace.reversed().forEachIndexed { index, s ->
if (s.lowercase().startsWith("caused by")) {
if (s.startsWith("caused by", ignoreCase = true)) {
causedByLastIndex = splitStackTrace.size - index
return@forEachIndexed
}
Expand All @@ -77,10 +77,10 @@ class GitHubErrorReportSubmitter : ErrorReportSubmitter() {
&& !line.startsWith("at kotlinx.")
&& !line.startsWith("at com.intellij.")
}.joinToString("\n"),
/*"device-os" to with(System.getProperty("os.name").lowercase()) {
/*"device-os" to with(System.getProperty("os.name")) {
when { // Windows, macOS or Linux
startsWith("windows") -> "Windows"
startsWith("mac") -> "macOS"
startsWith("windows", ignoreCase = true) -> "Windows"
startsWith("mac", ignoreCase = true) -> "macOS"
else -> "Linux"
}
},*/ // currently cannot be set (https://github.com/orgs/community/discussions/44983)
Expand Down

0 comments on commit 4a392b8

Please sign in to comment.