-
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.
fix(Bazel): Support local registries
The current implementation of Bazel only supports remote registries. This commit extends the implementation to also support registries on the local filesystem. They are called "Index Registries" (see [1]). The variable "%workspace%" present in the test data is a built-in variable of Bazel (see [2]). [1]: https://docs.bazel.build/versions/5.1.0/bzlmod.html#index-registry [2]: https://bazel.build/run/bazelrc#bazelrc-syntax-semantics Signed-off-by: Nicolas Nobelis <[email protected]>
- Loading branch information
1 parent
50c0512
commit 5968180
Showing
9 changed files
with
1,805 additions
and
20 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
clients/bazel-module-registry/src/main/kotlin/BazelModuleRegistryService.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,46 @@ | ||
/* | ||
* 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.clients.bazelmoduleregistry | ||
|
||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonNamingStrategy | ||
|
||
/** | ||
* The JSON (de-)serialization object used by this client. | ||
*/ | ||
internal val JSON = Json { | ||
ignoreUnknownKeys = true | ||
namingStrategy = JsonNamingStrategy.SnakeCase | ||
} | ||
|
||
/** | ||
* A Bazel registry which is either local or remote. | ||
*/ | ||
interface BazelModuleRegistryService { | ||
/** | ||
* Retrieve the metadata for a module. | ||
*/ | ||
suspend fun getModuleMetadata(name: String): ModuleMetadata | ||
|
||
/** | ||
* Retrieve information about the source code for a specific version of a module. | ||
*/ | ||
suspend fun getModuleSourceInfo(name: String, version: String): ModuleSourceInfo | ||
} |
67 changes: 67 additions & 0 deletions
67
clients/bazel-module-registry/src/main/kotlin/LocalBazelModuleRegistryService.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,67 @@ | ||
/* | ||
* 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.clients.bazelmoduleregistry | ||
|
||
import java.io.File | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.decodeFromStream | ||
|
||
/** | ||
* A Bazel registry which is located on the local file system. | ||
*/ | ||
class LocalBazelModuleRegistryService(directory: File) : BazelModuleRegistryService { | ||
private val moduleDirectory: File | ||
|
||
init { | ||
val registryFile = directory.resolve("bazel_registry.json") | ||
|
||
require(registryFile.isFile) { | ||
"The Bazel registry file bazel_registry.json does not exist in '${directory.canonicalPath}'." | ||
} | ||
|
||
val bazelRegistry = registryFile.inputStream().use { | ||
JSON.decodeFromStream<BazelRegistry>(it) | ||
} | ||
|
||
moduleDirectory = registryFile.resolve(bazelRegistry.moduleBasePath) | ||
} | ||
|
||
override suspend fun getModuleMetadata(name: String): ModuleMetadata { | ||
val metadataJson = moduleDirectory.resolve(name).resolve("metadata.json") | ||
require(metadataJson.isFile) | ||
return metadataJson.inputStream().use { | ||
JSON.decodeFromStream<ModuleMetadata>(it) | ||
} | ||
} | ||
|
||
override suspend fun getModuleSourceInfo(name: String, version: String): ModuleSourceInfo { | ||
val sourceJson = moduleDirectory.resolve(name).resolve(version).resolve("source.json") | ||
require(sourceJson.isFile) | ||
return sourceJson.inputStream().use { | ||
JSON.decodeFromStream<ModuleSourceInfo>(it) | ||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
private data class BazelRegistry( | ||
val moduleBasePath: String | ||
) |
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
3 changes: 3 additions & 0 deletions
3
...ackage-managers/bazel/src/funTest/assets/projects/synthetic/bazel-local-registry/.bazelrc
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,3 @@ | ||
common --registry=file://%workspace%/tools/bazelbuild/registry/ | ||
|
||
common --registry=https://raw.githubusercontent.com/bazelbuild/bazel-central-registry/main/ |
Empty file.
Oops, something went wrong.