Skip to content

Commit

Permalink
feat(package-manager): Add initial support for Bazel
Browse files Browse the repository at this point in the history
This is based on #264.

Signed-off-by: Haiko Schol <[email protected]>
  • Loading branch information
haikoschol committed Mar 14, 2024
1 parent 67315ee commit ed029a9
Show file tree
Hide file tree
Showing 26 changed files with 3,620 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
* 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.
Expand All @@ -24,13 +24,13 @@ import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.shouldBe

class BazelModuleRegistryClientTest : WordSpec({
val service = BazelModuleRegistryClient.create()
class BazelModuleRegistryClientFunTest : WordSpec({
val client = BazelModuleRegistryClient.create()
val repoUrl = "https://github.com/google/glog"

"getModuleMetadata" should {
"include a homepage URL and version 0.5.0 for the 'glog' module" {
val metadata = service.getModuleMetadata("glog")
val metadata = client.getModuleMetadata("glog")

metadata.homepage.toString() shouldBe repoUrl
metadata.versions.size shouldBeGreaterThan 1
Expand All @@ -40,7 +40,7 @@ class BazelModuleRegistryClientTest : WordSpec({

"getModuleSource" should {
"include URL and hash data" {
val sourceInfo = service.getModuleSourceInfo("glog", "0.5.0")
val sourceInfo = client.getModuleSourceInfo("glog", "0.5.0")

sourceInfo.url.toString() shouldBe "$repoUrl/archive/refs/tags/v0.5.0.tar.gz"
sourceInfo.integrity shouldBe "sha256-7t5x8oNxvzmqabRd4jsynTchQBbiBVJps7Xnz9QLWfU="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,22 @@ private val JSON = Json {
namingStrategy = JsonNamingStrategy.SnakeCase
}

/**
* The service uses the Bazel Central Registry by default.
*/
private const val DEFAULT_URL = "https://bcr.bazel.build"

/**
* Interface for a Bazel Module Registry, based on the directory structure of https://bcr.bazel.build
* and the git repository it is based on (https://github.com/bazelbuild/bazel-central-registry/).
*/
interface BazelModuleRegistryClient {
companion object {
/**
* The service uses the Bazel Central Registry by default.
*/
const val DEFAULT_URL = "https://bcr.bazel.build"

/**
* Create a Bazel Module Registry service instance for communicating with a server running at the given [url],
* defaulting to the Bazel Central Registry, optionally with a pre-built OkHttp [client].
*/
fun create(
url: String? = null,
client: OkHttpClient? = null
): BazelModuleRegistryClient {
fun create(url: String? = null, client: OkHttpClient? = null): BazelModuleRegistryClient {
val bmrClient = client ?: OkHttpClient()

val contentType = "application/json".toMediaType()
Expand Down
4 changes: 2 additions & 2 deletions clients/bazel-module-registry/src/main/kotlin/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ data class ModuleMetadata(
val repository: List<String>? = null,
val versions: List<String>,
// The key in the map is the version, the value the reason for yanking it.
val yankedVersions: Map<String, String>,
val yankedVersions: Map<String, String>
) {
@Serializable
data class Maintainer(
Expand All @@ -61,5 +61,5 @@ data class ModuleSourceInfo(
val patchStrip: Int? = null,
val patches: Map<String, String>? = null,
val stripPrefix: String? = null,
@Serializable(URISerializer::class) val url: URI,
@Serializable(URISerializer::class) val url: URI
)
60 changes: 60 additions & 0 deletions plugins/package-managers/bazel/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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
*/

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")

// Apply third-party plugins.
alias(libs.plugins.kotlinSerialization)
}

dependencies {
api(projects.analyzer)
api(projects.model)
api(projects.utils.commonUtils) {
because("This is a CommandLineTool.")
}

api(libs.semver4j) {
because("This is a CommandLineTool.")
}

implementation(projects.downloader)
implementation(projects.clients.bazelModuleRegistryClient)
implementation(projects.utils.ortUtils)
implementation(projects.utils.spdxUtils)

implementation(libs.bundles.kotlinxSerialization)
implementation(libs.kotlinx.coroutines)

funTestImplementation(testFixtures(projects.analyzer))
}

tasks.withType<KotlinCompile>().configureEach {
val customCompilerArgs = listOf(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
)

compilerOptions {
freeCompilerArgs.addAll(customCompilerArgs)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module(name="bazel-test-module", version="0.42.23", compatibility_level=0)

bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog")
bazel_dep(name = "googletest", version = "1.14.0", repo_name = "com_google_googletest", dev_dependency = True)

Loading

0 comments on commit ed029a9

Please sign in to comment.