Skip to content

Commit

Permalink
vendoring.bzl: Adding a way to dynamically choose whether to use vend…
Browse files Browse the repository at this point in the history
…ored repo or pull it from git.

Needed to build in environments that do not support git submodules, because all our vendored repos are actually not vendored, but are submodules.

Currently not used for any of the packages we have.
  • Loading branch information
ivucica committed Sep 20, 2024
1 parent 8125a2e commit 5f3b001
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*-Python-*-
workspace(name="yatc")

load(":vendoring.bzl", "vendored_git_repository", "new_vendored_git_repository")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
Expand Down
66 changes: 66 additions & 0 deletions vendoring.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- mode: python; -*-
# vim: set syntax=python:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")

def _vendored_git_repository_impl(repository_ctx):
url = repository_ctx.attr.url
schemaless_url = url.split("://", 1)[1]
local_path = repository_ctx.workspace_root.get_child("vendor", schemaless_url)
if local_path.get_child("WORKSPACE").exists or local_path.get_child("WORKSPACE.bazel").exists:
# git submodule init --update was run.

if repository_ctx.attr.build_file_content:
native.new_local_repository(
name = repository_ctx.attr.name,
path = local_path,
build_file_content = repository_ctx.attr.build_file_content,
)
else:
native.local_repository(
name = repository_ctx.attr.name,
path = local_path,
)
else:
# Vendored path is not a valid Bazel repo
# Assume it's a submodule for now.
# TODO: read .gitmodules to avoid the need for commit to be passed, and to detect an invalid submodule being defined.
# TODO: if github, fetch with http_archive / http_file.

if repository_ctx.attr.build_file_content:
new_git_repository(
name = repository_ctx.attr.name,
url = repository_ctx.attr.url,
commit = repository_ctx.attr.commit,
tag = repository_ctx.attr.tag,
build_file_content = repository_ctx.attr.build_file_content,
)
else:
git_repository(
name = repository_ctx.attr.name,
url = repository_ctx.attr.url,
commit = repository_ctx.attr.commit,
tag = repository_ctx.attr.tag,
)


_vendored_git_repository_rule = repository_rule(
implementation = _vendored_git_repository_impl,
attrs = {
"url": attr.string(),
# "sha256": attr.string(), # Only applicable if we use http_archive.
"commit": attr.string(),
"tag": attr.string(),

"build_file_content": attr.string(), # If empty, assume not new_*_repository.
},
)


def vendored_git_repository(name, url, commit = None, tag = None):
_vendored_git_repository_rule(name = name, url = url, commit = commit, tag = tag)


def new_vendored_git_repository(name, url, build_file_content, commit = None, tag = None):
_vendored_git_repository_rule(name = name, url = url, commit = commit, tag = tag, build_file_content = build_file_content)

0 comments on commit 5f3b001

Please sign in to comment.