Skip to content

Commit

Permalink
refactor(package-managers): Align on Lockfile instead of LockFile
Browse files Browse the repository at this point in the history
Also rename function and classes in line with recent renaming done by
6c6ddbf and 1ddb89b.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Mar 19, 2024
1 parent 38f9dde commit e89a499
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion plugins/package-managers/cargo/src/main/kotlin/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Cargo(
return emptyMap()
}

val contents = lockfile.reader().use { toml.decodeFromNativeReader<CargoLockFile>(it) }
val contents = lockfile.reader().use { toml.decodeFromNativeReader<CargoLockfile>(it) }
return when (contents.version) {
3 -> {
contents.packages.mapNotNull { pkg ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlinx.serialization.Serializable
* See https://docs.rs/cargo-lock/latest/cargo_lock/struct.Lockfile.html.
*/
@Serializable
internal data class CargoLockFile(
internal data class CargoLockfile(
val version: Int = 1,

@SerialName("package")
Expand Down
10 changes: 5 additions & 5 deletions plugins/package-managers/composer/src/main/kotlin/Composer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Composer(
return listOf(result)
}

val lockfile = ensureLockFile(workingDir)
val lockfile = ensureLockfile(workingDir)

logger.info { "Parsing lockfile at '$lockfile'..." }

Expand Down Expand Up @@ -277,12 +277,12 @@ class Composer(
return packages
}

private fun ensureLockFile(workingDir: File): File {
private fun ensureLockfile(workingDir: File): File {
val lockfile = workingDir.resolve(COMPOSER_LOCK_FILE)

val hasLockFile = lockfile.isFile
requireLockfile(workingDir) { hasLockFile }
if (hasLockFile) return lockfile
val hasLockfile = lockfile.isFile
requireLockfile(workingDir) { hasLockfile }
if (hasLockfile) return lockfile

val composerVersion = Semver(getVersion(workingDir))
val args = listOfNotNull(
Expand Down
4 changes: 2 additions & 2 deletions plugins/package-managers/conan/src/main/kotlin/Conan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Conan(

override fun command(workingDir: File?) = "conan"

private fun hasLockFile(file: String) = File(file).isFile
private fun hasLockfile(file: String) = File(file).isFile

override fun transformVersion(output: String) =
// Conan could report version strings like:
Expand Down Expand Up @@ -154,7 +154,7 @@ class Conan(

// TODO: Support lockfiles which are located in a different directory than the definition file.
val lockfileName = options[OPTION_LOCKFILE_NAME]
requireLockfile(workingDir) { lockfileName?.let { hasLockFile(workingDir.resolve(it).path) } ?: false }
requireLockfile(workingDir) { lockfileName?.let { hasLockfile(workingDir.resolve(it).path) } ?: false }

val jsonFile = createOrtTempDir().resolve("info.json")
if (lockfileName != null) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/go/src/main/kotlin/GoDep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class GoDep(
run("ensure", workingDir = workingDir, environment = mapOf("GOPATH" to gopath.path))
}

val contents = lockfile.reader().use { toml.decodeFromNativeReader<GoDepLockFile>(it) }
val contents = lockfile.reader().use { toml.decodeFromNativeReader<GoDepLockfile>(it) }
if (contents.projects.isEmpty()) {
logger.warn { "The lockfile '$lockfile' does not contain any projects." }
return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlinx.serialization.Serializable
* See https://golang.github.io/dep/docs/Gopkg.lock.html.
*/
@Serializable
internal data class GoDepLockFile(
internal data class GoDepLockfile(
val projects: List<Project> = emptyList()
) {
@Serializable
Expand Down
6 changes: 3 additions & 3 deletions plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ open class Npm(

private val npmViewCache = ConcurrentHashMap<String, Deferred<JsonNode>>()

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

/**
* Check if [this] represents a workspace within a `node_modules` directory.
Expand Down Expand Up @@ -567,7 +567,7 @@ open class Npm(
* Install dependencies using the given package manager command.
*/
private fun installDependencies(workingDir: File): List<Issue> {
requireLockfile(workingDir) { hasLockFile(workingDir) }
requireLockfile(workingDir) { hasLockfile(workingDir) }

// Install all NPM dependencies to enable NPM to list dependencies.
val process = runInstall(workingDir)
Expand Down Expand Up @@ -596,7 +596,7 @@ open class Npm(
"--legacy-peer-deps".takeIf { legacyPeerDeps }
)

val subcommand = if (hasLockFile(workingDir)) "ci" else "install"
val subcommand = if (hasLockfile(workingDir)) "ci" else "install"
return ProcessCapture(workingDir, command(workingDir), subcommand, *options.toTypedArray())
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/node/src/main/kotlin/Pnpm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Pnpm(
) = Pnpm(type, analysisRoot, analyzerConfig, repoConfig)
}

override fun hasLockFile(projectDir: File) = NodePackageManager.PNPM.hasLockFile(projectDir)
override fun hasLockfile(projectDir: File) = NodePackageManager.PNPM.hasLockfile(projectDir)

override fun File.isWorkspaceDir() = realFile() in findWorkspaceSubmodules(analysisRoot)

Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/node/src/main/kotlin/Yarn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Yarn(
) = Yarn(type, analysisRoot, analyzerConfig, repoConfig)
}

override fun hasLockFile(projectDir: File) = NodePackageManager.YARN.hasLockFile(projectDir)
override fun hasLockfile(projectDir: File) = NodePackageManager.YARN.hasLockfile(projectDir)

override fun command(workingDir: File?) = if (Os.isWindows) "yarn.cmd" else "yarn"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ enum class NodePackageManager(
val workspaceFileName: String = NodePackageManager.DEFINITION_FILE
) {
NPM("package-lock.json", markerFileName = "npm-shrinkwrap.json") {
override fun hasLockFile(projectDir: File): Boolean =
super.hasLockFile(projectDir) || hasNonEmptyFile(projectDir, markerFileName)
override fun hasLockfile(projectDir: File): Boolean =
super.hasLockfile(projectDir) || hasNonEmptyFile(projectDir, markerFileName)
},

PNPM("pnpm-lock.yaml", workspaceFileName = "pnpm-workspace.yaml") {
Expand All @@ -137,7 +137,7 @@ enum class NodePackageManager(
YARN("yarn.lock") {
private val lockfileMarker = "# yarn lockfile v1"

override fun hasLockFile(projectDir: File): Boolean {
override fun hasLockfile(projectDir: File): Boolean {
val lockfile = projectDir.resolve(lockfileName)
if (!lockfile.isFile) return false

Expand All @@ -150,7 +150,7 @@ enum class NodePackageManager(
YARN2("yarn.lock", markerFileName = ".yarnrc.yml") {
private val lockfileMarker = "__metadata:"

override fun hasLockFile(projectDir: File): Boolean {
override fun hasLockfile(projectDir: File): Boolean {
val lockfile = projectDir.resolve(lockfileName)
if (!lockfile.isFile) return false

Expand Down Expand Up @@ -190,7 +190,7 @@ enum class NodePackageManager(
/**
* Return true if the [projectDir] contains a lockfile for this package manager, or return false otherwise.
*/
open fun hasLockFile(projectDir: File): Boolean = hasNonEmptyFile(projectDir, lockfileName)
open fun hasLockfile(projectDir: File): Boolean = hasNonEmptyFile(projectDir, lockfileName)

/**
* If the [projectDir] contains a workspace file for this package manager, return the list of package patterns, or
Expand Down Expand Up @@ -229,7 +229,7 @@ enum class NodePackageManager(
*/
fun getFileScore(projectDir: File): Int =
listOf(
hasLockFile(projectDir),
hasLockfile(projectDir),
hasNonEmptyFile(projectDir, markerFileName),
// Only count the presence of an additional workspace file if it is not the definition file.
workspaceFileName != DEFINITION_FILE && hasNonEmptyFile(projectDir, workspaceFileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class NpmDetectionTest : WordSpec({
writeText("")
}

it.hasLockFile(lockfile.parentFile) shouldBe false
it.hasLockfile(lockfile.parentFile) shouldBe false
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ class NpmDetectionTest : WordSpec({
writeText("{}")
}

NPM.hasLockFile(lockfile.parentFile) shouldBe true
NPM.hasLockfile(lockfile.parentFile) shouldBe true
}

"parse workspace files" {
Expand Down Expand Up @@ -175,7 +175,7 @@ class NpmDetectionTest : WordSpec({
writeText("lockfileVersion: '6.0'")
}

PNPM.hasLockFile(lockfile.parentFile) shouldBe true
PNPM.hasLockfile(lockfile.parentFile) shouldBe true
}

"parse workspace files" {
Expand Down Expand Up @@ -208,7 +208,7 @@ class NpmDetectionTest : WordSpec({
writeText(YARN_LOCK_FILE_HEADER)
}

YARN.hasLockFile(lockfile.parentFile) shouldBe true
YARN.hasLockfile(lockfile.parentFile) shouldBe true
}

"parse workspace files" {
Expand Down Expand Up @@ -238,7 +238,7 @@ class NpmDetectionTest : WordSpec({
writeText(YARN2_LOCK_FILE_HEADER)
}

YARN2.hasLockFile(lockfile.parentFile) shouldBe true
YARN2.hasLockfile(lockfile.parentFile) shouldBe true
}

"parse workspace files" {
Expand Down
44 changes: 22 additions & 22 deletions plugins/package-managers/pub/src/main/kotlin/Pub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,22 @@ class Pub(
// dependency tree.
if (packageName in processedPackages) return@forEach

val pkgInfoFromLockFile = lockfile["packages"][packageName]
val pkgInfoFromLockfile = lockfile["packages"][packageName]
// If the package is marked as SDK (e.g. flutter, flutter_test, dart) we cannot resolve it correctly as
// it is not stored in .pub-cache. For now, we just ignore those SDK packages.
if (pkgInfoFromLockFile == null || pkgInfoFromLockFile["source"].textValueOrEmpty() == "sdk") return@forEach
if (pkgInfoFromLockfile == null || pkgInfoFromLockfile["source"].textValueOrEmpty() == "sdk") return@forEach

val id = Identifier(
type = managerName,
namespace = "",
name = packageName,
version = pkgInfoFromLockFile["version"].textValueOrEmpty()
version = pkgInfoFromLockfile["version"].textValueOrEmpty()
)

val packageInfo = packages[id] ?: throw IOException("Could not find package info for $packageName")

try {
val dependencyYamlFile = readPackageInfoFromCache(pkgInfoFromLockFile, workingDir)
val dependencyYamlFile = readPackageInfoFromCache(pkgInfoFromLockfile, workingDir)
val requiredPackages =
dependencyYamlFile["dependencies"]?.fieldNames()?.asSequence()?.toList().orEmpty()

Expand All @@ -390,7 +390,7 @@ class Pub(
// dependencies for each pub dependency manually, as the analyzer will only scan the
// projectRoot, but not the packages in the ".pub-cache" directory.
if (containsFlutter && !pubDependenciesOnly) {
scanAndroidPackages(pkgInfoFromLockFile, labels, workingDir).forEach { resultAndroid ->
scanAndroidPackages(pkgInfoFromLockfile, labels, workingDir).forEach { resultAndroid ->
packageReferences += packageInfo.toReference(
dependencies = resultAndroid.project.scopes
.find { it.name == "releaseCompileClasspath" }
Expand Down Expand Up @@ -526,21 +526,21 @@ class Pub(
var containsFlutter = false

listOf("packages"/*, "packages-dev"*/).forEach {
lockfile[it]?.fields()?.forEach { (packageName, pkgInfoFromLockFile) ->
lockfile[it]?.fields()?.forEach { (packageName, pkgInfoFromLockfile) ->
try {
val version = pkgInfoFromLockFile["version"].textValueOrEmpty()
val version = pkgInfoFromLockfile["version"].textValueOrEmpty()
var description = ""
var rawName = ""
var homepageUrl = ""
var vcs = VcsInfo.EMPTY
var authors = emptySet<String>()

val source = pkgInfoFromLockFile["source"].textValueOrEmpty()
val source = pkgInfoFromLockfile["source"].textValueOrEmpty()

when {
source == "path" -> {
rawName = packageName
val path = pkgInfoFromLockFile["description"]["path"].textValueOrEmpty()
val path = pkgInfoFromLockfile["description"]["path"].textValueOrEmpty()
vcs = VersionControlSystem.forDirectory(workingDir.resolve(path))?.getInfo() ?: run {
logger.warn {
"Invalid path of package $rawName: " +
Expand All @@ -551,7 +551,7 @@ class Pub(
}

source == "git" -> {
val pkgInfoFromYamlFile = readPackageInfoFromCache(pkgInfoFromLockFile, workingDir)
val pkgInfoFromYamlFile = readPackageInfoFromCache(pkgInfoFromLockfile, workingDir)

rawName = pkgInfoFromYamlFile["name"]?.textValue() ?: packageName
description = pkgInfoFromYamlFile["description"].textValueOrEmpty().trim()
Expand All @@ -560,15 +560,15 @@ class Pub(

vcs = VcsInfo(
type = VcsType.GIT,
url = normalizeVcsUrl(pkgInfoFromLockFile["description"]["url"].textValueOrEmpty()),
revision = pkgInfoFromLockFile["description"]["resolved-ref"].textValueOrEmpty(),
path = pkgInfoFromLockFile["description"]["path"].textValueOrEmpty()
url = normalizeVcsUrl(pkgInfoFromLockfile["description"]["url"].textValueOrEmpty()),
revision = pkgInfoFromLockfile["description"]["resolved-ref"].textValueOrEmpty(),
path = pkgInfoFromLockfile["description"]["path"].textValueOrEmpty()
)
}

// For now, we ignore SDKs like the Dart SDK and the Flutter SDK in the analyzer.
source != "sdk" -> {
val pkgInfoFromYamlFile = readPackageInfoFromCache(pkgInfoFromLockFile, workingDir)
val pkgInfoFromYamlFile = readPackageInfoFromCache(pkgInfoFromLockfile, workingDir)

rawName = pkgInfoFromYamlFile["name"].textValueOrEmpty()
description = pkgInfoFromYamlFile["description"].textValueOrEmpty().trim()
Expand All @@ -583,7 +583,7 @@ class Pub(
vcs = VcsHost.parseUrl(repositoryUrl).copy(revision = "")
}

pkgInfoFromLockFile["description"].textValueOrEmpty() == "flutter" -> {
pkgInfoFromLockfile["description"].textValueOrEmpty() == "flutter" -> {
// Set Flutter flag, which triggers another scan for iOS and Android native dependencies.
containsFlutter = true
// Set hardcoded package details.
Expand All @@ -592,7 +592,7 @@ class Pub(
description = "Flutter SDK"
}

pkgInfoFromLockFile["description"].textValueOrEmpty() == "flutter_test" -> {
pkgInfoFromLockfile["description"].textValueOrEmpty() == "flutter_test" -> {
// Set hardcoded package details.
rawName = "flutter_test"
homepageUrl = "https://github.com/flutter/flutter/tree/master/packages/flutter_test"
Expand All @@ -604,10 +604,10 @@ class Pub(
logger.warn { "No version information found for package $rawName." }
}

val hostUrl = pkgInfoFromLockFile["description"]["url"].textValueOrEmpty()
val hostUrl = pkgInfoFromLockfile["description"]["url"].textValueOrEmpty()

val sourceArtifact = if (source == "hosted" && hostUrl.isNotEmpty() && version.isNotEmpty()) {
val sha256 = pkgInfoFromLockFile["description"]["sha256"].textValueOrEmpty()
val sha256 = pkgInfoFromLockfile["description"]["sha256"].textValueOrEmpty()

RemoteArtifact(
url = "$hostUrl/packages/$rawName/versions/$version.tar.gz",
Expand Down Expand Up @@ -640,7 +640,7 @@ class Pub(
} catch (e: JacksonYAMLParseException) {
e.showStackTrace()

val packageVersion = pkgInfoFromLockFile["version"].textValueOrEmpty()
val packageVersion = pkgInfoFromLockfile["version"].textValueOrEmpty()
issues += createAndLogIssue(
source = managerName,
message = "Failed to parse $PUBSPEC_YAML for package $packageName:$packageVersion: " +
Expand All @@ -654,9 +654,9 @@ class Pub(
// each Pub dependency manually, as the analyzer will only analyze the projectRoot, but not the packages in
// the ".pub-cache" directory.
if (containsFlutter && !pubDependenciesOnly) {
lockfile["packages"]?.forEach { pkgInfoFromLockFile ->
lockfile["packages"]?.forEach { pkgInfoFromLockfile ->
// As this package contains Flutter, trigger Gradle manually for it.
scanAndroidPackages(pkgInfoFromLockFile, labels, workingDir).forEach { result ->
scanAndroidPackages(pkgInfoFromLockfile, labels, workingDir).forEach { result ->
result.collectPackagesByScope("releaseCompileClasspath").forEach { pkg ->
packages[pkg.id] = pkg
}
Expand All @@ -665,7 +665,7 @@ class Pub(
}

// As this package contains Flutter, trigger CocoaPods manually for it.
scanIosPackages(pkgInfoFromLockFile, workingDir)?.let { result ->
scanIosPackages(pkgInfoFromLockfile, workingDir)?.let { result ->
result.packages.forEach { pkg ->
packages[pkg.id] = pkg
}
Expand Down

0 comments on commit e89a499

Please sign in to comment.