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

Make Pnpm separate from Npm #9274

Open
wants to merge 4 commits 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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"
sschuberth marked this conversation as resolved.
Show resolved Hide resolved
sschuberth marked this conversation as resolved.
Show resolved Hide resolved
dependencies:
- id: "NPM::dom-serializer:0.1.1"
dependencies:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ analyzer:
- id: "NPM::json-stable-stringify:1.0.1"
dependencies:
- id: "NPM::jsonify:0.0.0"
- id: "PNPM::pnpm-workspaces:1.0.1"
linkage: "PROJECT_DYNAMIC"
dependencies:
- id: "NPM::json-stable-stringify:1.0.1"
dependencies:
- id: "NPM::jsonify:0.0.0"
- id: "PNPM::pnpm-workspaces:1.0.1"
linkage: "PROJECT_DYNAMIC"
- id: "PNPM::testing-pnpm-package-a:1.0.2"
definition_file_path: "plugins/package-managers/node/src/funTest/assets/projects/synthetic/pnpm/workspaces/src/packages/package-a/package.json"
authors:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.plugins.packagemanagers.node
package org.ossreviewtoolkit.plugins.packagemanagers.node.pnpm

import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.should
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.plugins.packagemanagers.node
package org.ossreviewtoolkit.plugins.packagemanagers.node.yarn2

import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.should
Expand Down
95 changes: 46 additions & 49 deletions plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import org.apache.logging.log4j.kotlin.logger

import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.getFallbackProjectName
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.processPackageVcs
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.processProjectVcs
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VcsHost
import org.ossreviewtoolkit.downloader.VersionControlSystem
Expand Down Expand Up @@ -120,11 +122,6 @@ open class Npm(

protected open fun hasLockfile(projectDir: File) = NodePackageManager.NPM.hasLockfile(projectDir)

/**
* Check if [this] represents a workspace within a `node_modules` directory.
*/
protected open fun File.isWorkspaceDir() = isSymbolicLink()

/**
* Load the submodule directories of the project defined in [moduleDir].
*/
Expand Down Expand Up @@ -182,7 +179,7 @@ open class Npm(

if (installIssues.any { it.severity == Severity.ERROR }) {
val project = runCatching {
parseProject(definitionFile)
parseProject(definitionFile, analysisRoot, managerName)
}.getOrElse {
logger.error { "Failed to parse project information: ${it.collectMessages()}" }
Project.EMPTY
Expand All @@ -199,7 +196,7 @@ open class Npm(
}

val project = runCatching {
parseProject(projectDir.resolve("package.json"))
parseProject(projectDir.resolve("package.json"), analysisRoot, managerName)
}.getOrElse {
issues += createAndLogIssue(
source = managerName,
Expand Down Expand Up @@ -261,7 +258,7 @@ open class Npm(
/**
* Find the directories which are defined as submodules of the project within [moduleDir].
*/
protected fun findWorkspaceSubmodules(moduleDir: File): Set<File> =
private fun findWorkspaceSubmodules(moduleDir: File): Set<File> =
submodulesCache.getOrPut(moduleDir.absolutePath) {
loadWorkspaceSubmodules(moduleDir)
}
Expand Down Expand Up @@ -392,47 +389,6 @@ open class Npm(
)
}

private fun parseProject(packageJsonFile: File): Project {
logger.debug { "Parsing project info from '$packageJsonFile'." }

val packageJson = parsePackageJson(packageJsonFile)

val rawName = packageJson.name.orEmpty()
val (namespace, name) = splitNpmNamespaceAndName(rawName)

val projectName = name.ifBlank {
getFallbackProjectName(analysisRoot, packageJsonFile).also {
logger.warn { "'$packageJsonFile' does not define a name, falling back to '$it'." }
}
}

val version = packageJson.version.orEmpty()
if (version.isBlank()) {
logger.warn { "'$packageJsonFile' does not define a version." }
}

val declaredLicenses = packageJson.licenses.mapNpmLicenses()
val authors = parseNpmAuthor(packageJson.authors.firstOrNull()) // TODO: parse all authors.
val homepageUrl = packageJson.homepage.orEmpty()
val projectDir = packageJsonFile.parentFile.realFile()
val vcsFromPackage = parseNpmVcsInfo(packageJson)

return Project(
id = Identifier(
type = managerName,
namespace = namespace,
name = projectName,
version = version
),
definitionFilePath = VersionControlSystem.getPathInfo(packageJsonFile.realFile()).path,
authors = authors,
declaredLicenses = declaredLicenses,
vcs = vcsFromPackage,
vcsProcessed = processProjectVcs(projectDir, vcsFromPackage, homepageUrl),
homepageUrl = homepageUrl
)
}

/**
* Install dependencies using the given package manager command.
*/
Expand Down Expand Up @@ -641,3 +597,44 @@ internal fun parsePackage(

return module
}

internal fun parseProject(packageJsonFile: File, analysisRoot: File, managerName: String): Project {
Npm.logger.debug { "Parsing project info from '$packageJsonFile'." }

val packageJson = parsePackageJson(packageJsonFile)

val rawName = packageJson.name.orEmpty()
val (namespace, name) = splitNpmNamespaceAndName(rawName)

val projectName = name.ifBlank {
getFallbackProjectName(analysisRoot, packageJsonFile).also {
Npm.logger.warn { "'$packageJsonFile' does not define a name, falling back to '$it'." }
}
}

val version = packageJson.version.orEmpty()
if (version.isBlank()) {
Npm.logger.warn { "'$packageJsonFile' does not define a version." }
}

val declaredLicenses = packageJson.licenses.mapNpmLicenses()
val authors = parseNpmAuthor(packageJson.authors.firstOrNull()) // TODO: parse all authors.
val homepageUrl = packageJson.homepage.orEmpty()
val projectDir = packageJsonFile.parentFile.realFile()
val vcsFromPackage = parseNpmVcsInfo(packageJson)

return Project(
id = Identifier(
type = managerName,
namespace = namespace,
name = projectName,
version = version
),
definitionFilePath = VersionControlSystem.getPathInfo(packageJsonFile.realFile()).path,
authors = authors,
declaredLicenses = declaredLicenses,
vcs = vcsFromPackage,
vcsProcessed = processProjectVcs(projectDir, vcsFromPackage, homepageUrl),
homepageUrl = homepageUrl
)
}
85 changes: 0 additions & 85 deletions plugins/package-managers/node/src/main/kotlin/Pnpm.kt

This file was deleted.

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,

Check warning on line 32 in plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt#L30-L32

Added lines #L30 - L32 were not covered by tests
val path: String,
val private: Boolean,

Check warning on line 34 in plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt#L34

Added line #L34 was not covered by tests
val dependencies: Map<String, Dependency> = emptyMap(),
val devDependencies: Map<String, Dependency> = emptyMap(),
val optionalDependencies: Map<String, Dependency> = emptyMap()
) {
@Serializable
data class Dependency(

Check warning on line 40 in plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt#L38-L40

Added lines #L38 - L40 were not covered by tests
val from: String,
val version: String,
val resolved: String? = null,

Check warning on line 43 in plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt#L43

Added line #L43 was not covered by tests
val path: String,
val dependencies: Map<String, Dependency> = emptyMap(),
val optionalDependencies: Map<String, Dependency> = emptyMap()
)

Check warning on line 47 in plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/ModuleInfo.kt#L47

Added line #L47 was not covered by tests
}
Loading
Loading