From e89a4997218d0b28fe00b8ea7080e23fc87979fb Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Tue, 19 Mar 2024 10:39:42 +0100 Subject: [PATCH] refactor(package-managers): Align on `Lockfile` instead of `LockFile` Also rename function and classes in line with recent renaming done by 6c6ddbf and 1ddb89b. Signed-off-by: Frank Viernau --- .../cargo/src/main/kotlin/Cargo.kt | 2 +- .../{CargoLockFile.kt => CargoLockfile.kt} | 2 +- .../composer/src/main/kotlin/Composer.kt | 10 ++--- .../conan/src/main/kotlin/Conan.kt | 4 +- .../go/src/main/kotlin/GoDep.kt | 2 +- .../{GoDepLockFile.kt => GoDepLockfile.kt} | 2 +- .../node/src/main/kotlin/Npm.kt | 6 +-- .../node/src/main/kotlin/Pnpm.kt | 2 +- .../node/src/main/kotlin/Yarn.kt | 2 +- .../src/main/kotlin/utils/NpmDetection.kt | 12 ++--- .../src/test/kotlin/utils/NpmDetectionTest.kt | 10 ++--- .../pub/src/main/kotlin/Pub.kt | 44 +++++++++---------- 12 files changed, 49 insertions(+), 49 deletions(-) rename plugins/package-managers/cargo/src/main/kotlin/{CargoLockFile.kt => CargoLockfile.kt} (97%) rename plugins/package-managers/go/src/main/kotlin/{GoDepLockFile.kt => GoDepLockfile.kt} (97%) diff --git a/plugins/package-managers/cargo/src/main/kotlin/Cargo.kt b/plugins/package-managers/cargo/src/main/kotlin/Cargo.kt index 40e56a2f5ec0f..363755fcef659 100644 --- a/plugins/package-managers/cargo/src/main/kotlin/Cargo.kt +++ b/plugins/package-managers/cargo/src/main/kotlin/Cargo.kt @@ -109,7 +109,7 @@ class Cargo( return emptyMap() } - val contents = lockfile.reader().use { toml.decodeFromNativeReader(it) } + val contents = lockfile.reader().use { toml.decodeFromNativeReader(it) } return when (contents.version) { 3 -> { contents.packages.mapNotNull { pkg -> diff --git a/plugins/package-managers/cargo/src/main/kotlin/CargoLockFile.kt b/plugins/package-managers/cargo/src/main/kotlin/CargoLockfile.kt similarity index 97% rename from plugins/package-managers/cargo/src/main/kotlin/CargoLockFile.kt rename to plugins/package-managers/cargo/src/main/kotlin/CargoLockfile.kt index fa5c999b57d90..bed72c195864b 100644 --- a/plugins/package-managers/cargo/src/main/kotlin/CargoLockFile.kt +++ b/plugins/package-managers/cargo/src/main/kotlin/CargoLockfile.kt @@ -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") diff --git a/plugins/package-managers/composer/src/main/kotlin/Composer.kt b/plugins/package-managers/composer/src/main/kotlin/Composer.kt index cca66b95ae29f..21b0a459ab38e 100644 --- a/plugins/package-managers/composer/src/main/kotlin/Composer.kt +++ b/plugins/package-managers/composer/src/main/kotlin/Composer.kt @@ -128,7 +128,7 @@ class Composer( return listOf(result) } - val lockfile = ensureLockFile(workingDir) + val lockfile = ensureLockfile(workingDir) logger.info { "Parsing lockfile at '$lockfile'..." } @@ -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( diff --git a/plugins/package-managers/conan/src/main/kotlin/Conan.kt b/plugins/package-managers/conan/src/main/kotlin/Conan.kt index ef37e6e0c546e..e66ec1df16c19 100644 --- a/plugins/package-managers/conan/src/main/kotlin/Conan.kt +++ b/plugins/package-managers/conan/src/main/kotlin/Conan.kt @@ -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: @@ -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) { diff --git a/plugins/package-managers/go/src/main/kotlin/GoDep.kt b/plugins/package-managers/go/src/main/kotlin/GoDep.kt index 776f7e04836e3..4218687a24133 100644 --- a/plugins/package-managers/go/src/main/kotlin/GoDep.kt +++ b/plugins/package-managers/go/src/main/kotlin/GoDep.kt @@ -230,7 +230,7 @@ class GoDep( run("ensure", workingDir = workingDir, environment = mapOf("GOPATH" to gopath.path)) } - val contents = lockfile.reader().use { toml.decodeFromNativeReader(it) } + val contents = lockfile.reader().use { toml.decodeFromNativeReader(it) } if (contents.projects.isEmpty()) { logger.warn { "The lockfile '$lockfile' does not contain any projects." } return emptyList() diff --git a/plugins/package-managers/go/src/main/kotlin/GoDepLockFile.kt b/plugins/package-managers/go/src/main/kotlin/GoDepLockfile.kt similarity index 97% rename from plugins/package-managers/go/src/main/kotlin/GoDepLockFile.kt rename to plugins/package-managers/go/src/main/kotlin/GoDepLockfile.kt index da8b3fd772478..16c43ea0c0e9e 100644 --- a/plugins/package-managers/go/src/main/kotlin/GoDepLockFile.kt +++ b/plugins/package-managers/go/src/main/kotlin/GoDepLockfile.kt @@ -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 = emptyList() ) { @Serializable diff --git a/plugins/package-managers/node/src/main/kotlin/Npm.kt b/plugins/package-managers/node/src/main/kotlin/Npm.kt index 0fc98f2106162..edc17dc328e38 100644 --- a/plugins/package-managers/node/src/main/kotlin/Npm.kt +++ b/plugins/package-managers/node/src/main/kotlin/Npm.kt @@ -129,7 +129,7 @@ open class Npm( private val npmViewCache = ConcurrentHashMap>() - 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. @@ -567,7 +567,7 @@ open class Npm( * Install dependencies using the given package manager command. */ private fun installDependencies(workingDir: File): List { - requireLockfile(workingDir) { hasLockFile(workingDir) } + requireLockfile(workingDir) { hasLockfile(workingDir) } // Install all NPM dependencies to enable NPM to list dependencies. val process = runInstall(workingDir) @@ -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()) } } diff --git a/plugins/package-managers/node/src/main/kotlin/Pnpm.kt b/plugins/package-managers/node/src/main/kotlin/Pnpm.kt index 9f77bae6611db..7d13cd69acc4c 100644 --- a/plugins/package-managers/node/src/main/kotlin/Pnpm.kt +++ b/plugins/package-managers/node/src/main/kotlin/Pnpm.kt @@ -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) diff --git a/plugins/package-managers/node/src/main/kotlin/Yarn.kt b/plugins/package-managers/node/src/main/kotlin/Yarn.kt index 46973cecc5192..da7ebde5e4ee5 100644 --- a/plugins/package-managers/node/src/main/kotlin/Yarn.kt +++ b/plugins/package-managers/node/src/main/kotlin/Yarn.kt @@ -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" diff --git a/plugins/package-managers/node/src/main/kotlin/utils/NpmDetection.kt b/plugins/package-managers/node/src/main/kotlin/utils/NpmDetection.kt index c631fe79b1c69..15e582d56c6bf 100644 --- a/plugins/package-managers/node/src/main/kotlin/utils/NpmDetection.kt +++ b/plugins/package-managers/node/src/main/kotlin/utils/NpmDetection.kt @@ -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") { @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/plugins/package-managers/node/src/test/kotlin/utils/NpmDetectionTest.kt b/plugins/package-managers/node/src/test/kotlin/utils/NpmDetectionTest.kt index 249374551ed2d..8e0f57889d53a 100644 --- a/plugins/package-managers/node/src/test/kotlin/utils/NpmDetectionTest.kt +++ b/plugins/package-managers/node/src/test/kotlin/utils/NpmDetectionTest.kt @@ -45,7 +45,7 @@ class NpmDetectionTest : WordSpec({ writeText("") } - it.hasLockFile(lockfile.parentFile) shouldBe false + it.hasLockfile(lockfile.parentFile) shouldBe false } } @@ -140,7 +140,7 @@ class NpmDetectionTest : WordSpec({ writeText("{}") } - NPM.hasLockFile(lockfile.parentFile) shouldBe true + NPM.hasLockfile(lockfile.parentFile) shouldBe true } "parse workspace files" { @@ -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" { @@ -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" { @@ -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" { diff --git a/plugins/package-managers/pub/src/main/kotlin/Pub.kt b/plugins/package-managers/pub/src/main/kotlin/Pub.kt index ba521fcedf17f..be69cb5318f93 100644 --- a/plugins/package-managers/pub/src/main/kotlin/Pub.kt +++ b/plugins/package-managers/pub/src/main/kotlin/Pub.kt @@ -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() @@ -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" } @@ -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() - 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: " + @@ -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() @@ -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() @@ -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. @@ -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" @@ -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", @@ -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: " + @@ -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 } @@ -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 }