-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pnpm): Make
Pnpm
separate from Npm
Stop inheriting from `Npm` and rely entirely on the output of `pnpm` commands to figure out the necessary information. For example, do not re-construct the dependency from the file structure within `node_modles`, but rely on `pnpm list` instead. This reduces complexity and makes the implementation easy to understand, maintain or change. From the diff of the expected result files it becomes aparent that this change contains the following fixes: 1. htmlparser2 now dependends on domutils 1.7.0 instead of 1.5.1, which is inline with the dependency tree output by `pnpm list --depth 1000` 2. The cyclic reference from the workspace root project to itself is now included. Also note that it good to drop the `--shamefully-hoist` command line option, because it is not needed and its use is discouraged. Omiting it may also reduce the amount of warnings. Signed-off-by: Frank Viernau <[email protected]>
- Loading branch information
Showing
6 changed files
with
270 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ project: | |
- id: "NPM::domhandler:2.4.2" | ||
dependencies: | ||
- id: "NPM::domelementtype:1.3.1" | ||
- id: "NPM::domutils:1.5.1" | ||
- id: "NPM::domutils:1.7.0" | ||
dependencies: | ||
- id: "NPM::dom-serializer:0.1.1" | ||
dependencies: | ||
|
@@ -477,6 +477,36 @@ packages: | |
url: "https://github.com/FB55/domutils.git" | ||
revision: "7d4bd16cd36ffce62362ef91616806ea27e30d95" | ||
path: "" | ||
- id: "NPM::domutils:1.7.0" | ||
purl: "pkg:npm/[email protected]" | ||
authors: | ||
- "Felix Boehm" | ||
declared_licenses: | ||
- "BSD-2-Clause" | ||
declared_licenses_processed: | ||
spdx_expression: "BSD-2-Clause" | ||
description: "utilities for working with htmlparser2's dom" | ||
homepage_url: "https://github.com/FB55/domutils#readme" | ||
binary_artifact: | ||
url: "" | ||
hash: | ||
value: "" | ||
algorithm: "" | ||
source_artifact: | ||
url: "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz" | ||
hash: | ||
value: "56ea341e834e06e6748af7a1cb25da67ea9f8c2a" | ||
algorithm: "SHA-1" | ||
vcs: | ||
type: "Git" | ||
url: "git://github.com/FB55/domutils.git" | ||
revision: "34f193ca17d11a98d9310b1965efe5f73d32d79f" | ||
path: "" | ||
vcs_processed: | ||
type: "Git" | ||
url: "https://github.com/FB55/domutils.git" | ||
revision: "34f193ca17d11a98d9310b1965efe5f73d32d79f" | ||
path: "" | ||
- id: "NPM::eachr:3.3.0" | ||
purl: "pkg:npm/[email protected]" | ||
authors: | ||
|
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
48 changes: 48 additions & 0 deletions
48
plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.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,48 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.node.pnpm | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
private val JSON = Json { ignoreUnknownKeys = true } | ||
|
||
internal fun parsePnpmList(json: String): List<ModuleInfo> = JSON.decodeFromString(json) | ||
|
||
@Serializable | ||
internal data class ModuleInfo( | ||
val name: String, | ||
val version: String, | ||
val path: String, | ||
val private: Boolean, | ||
val dependencies: Map<String, Dependency> = emptyMap(), | ||
val devDependencies: Map<String, Dependency> = emptyMap(), | ||
val optionalDependencies: Map<String, Dependency> = emptyMap() | ||
) { | ||
@Serializable | ||
data class Dependency( | ||
val from: String, | ||
val version: String, | ||
val resolved: String? = null, | ||
val path: String, | ||
val dependencies: Map<String, Dependency> = emptyMap(), | ||
val optionalDependencies: Map<String, Dependency> = emptyMap() | ||
) | ||
} |
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
81 changes: 81 additions & 0 deletions
81
plugins/package-managers/node/src/main/kotlin/pnpm/PnpmDependencyHandler.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,81 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.node.pnpm | ||
|
||
import java.io.File | ||
|
||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.Issue | ||
import org.ossreviewtoolkit.model.Package | ||
import org.ossreviewtoolkit.model.PackageLinkage | ||
import org.ossreviewtoolkit.model.utils.DependencyHandler | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackage | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackageJson | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.pnpm.ModuleInfo.Dependency | ||
import org.ossreviewtoolkit.utils.common.realFile | ||
|
||
internal class PnpmDependencyHandler(private val pnpm: Pnpm) : DependencyHandler<Dependency> { | ||
private val workspaceModuleDirs = mutableSetOf<File>() | ||
private val packageJsonCache = mutableMapOf<File, PackageJson>() | ||
|
||
private fun Dependency.isProject() = File(path).realFile().absoluteFile in workspaceModuleDirs | ||
|
||
fun setWorkspaceModuleDirs(dirs: Collection<File>) { | ||
workspaceModuleDirs.apply { | ||
clear() | ||
addAll(dirs) | ||
} | ||
} | ||
|
||
override fun identifierFor(dependency: Dependency): Identifier { | ||
val type = pnpm.managerName.takeIf { dependency.isProject() } ?: "NPM" | ||
val namespace = dependency.from.substringBeforeLast("/", "") | ||
val name = dependency.from.substringAfterLast("/") | ||
val version = if (dependency.isProject()) { | ||
readPackageJson(dependency.packageJsonFile).version.orEmpty() | ||
} else { | ||
dependency.version.takeUnless { it.startsWith("link:") || it.startsWith("file:") }.orEmpty() | ||
} | ||
|
||
return Identifier(type, namespace, name, version) | ||
} | ||
|
||
override fun dependenciesFor(dependency: Dependency): List<Dependency> = | ||
(dependency.dependencies + dependency.optionalDependencies).values.toList() | ||
|
||
override fun linkageFor(dependency: Dependency): PackageLinkage = | ||
PackageLinkage.DYNAMIC.takeUnless { dependency.isProject() } ?: PackageLinkage.PROJECT_DYNAMIC | ||
|
||
override fun createPackage(dependency: Dependency, issues: MutableCollection<Issue>): Package? = | ||
dependency.takeUnless { it.isProject() }?.let { | ||
parsePackage( | ||
workingDir = it.workingDir, | ||
packageJsonFile = it.packageJsonFile, | ||
getRemotePackageDetails = pnpm::getRemotePackageDetails | ||
) | ||
} | ||
|
||
private fun readPackageJson(packageJsonFile: File): PackageJson = | ||
packageJsonCache.getOrPut(packageJsonFile.realFile()) { parsePackageJson(packageJsonFile) } | ||
} | ||
|
||
private val Dependency.workingDir: File get() = File(path) | ||
private val Dependency.packageJsonFile: File get() = File(path, "package.json") |