Skip to content

Commit

Permalink
Enables the disabling of automatic workspace edits
Browse files Browse the repository at this point in the history
While modifying the user’s workspace can help with the ergonomics of first time setup, it should be possible to disable these non-essential options.
  • Loading branch information
csmulhern committed Dec 20, 2023
1 parent eac41ee commit 936ae08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 6 additions & 2 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,12 @@ def _ensure_cwd_is_workspace_root():

def main():
_ensure_cwd_is_workspace_root()
_ensure_gitignore_entries_exist()
_ensure_external_workspaces_link_exists()

if {update_gitignore}:
_ensure_gitignore_entries_exist()

if {link_external}:
_ensure_external_workspaces_link_exists()

target_flag_pairs = [
# Begin: template filled by Bazel
Expand Down
22 changes: 21 additions & 1 deletion refresh_compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ refresh_compile_commands(
# exclude_headers = "external",
# Still not fast enough?
# Make sure you're specifying just the targets you care about by setting `targets`, above.
# refresh_compile_commands will automatically update .git/info/exclude by default.
# You can disable this behavior with update_gitignore = False.
# refresh_compile_commands will automatically create a symlink for external workspaces at /external.
# You can disable this behavior with link_external = False.
```
"""

Expand All @@ -63,6 +69,8 @@ def refresh_compile_commands(
targets = None,
exclude_headers = None,
exclude_external_sources = False,
update_gitignore = True,
link_external = True,
**kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes.
# Convert the various, acceptable target shorthands into the dictionary format
# In Python, `type(x) == y` is an antipattern, but [Starlark doesn't support inheritance](https://bazel.build/rules/language), so `isinstance` doesn't exist, and this is the correct way to switch on type.
Expand All @@ -88,7 +96,15 @@ def refresh_compile_commands(

# Generate the core, runnable python script from refresh.template.py
script_name = name + ".py"
_expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs)
_expand_template(
name = script_name,
labels_to_flags = targets,
exclude_headers = exclude_headers,
exclude_external_sources = exclude_external_sources,
update_gitignore = update_gitignore,
link_external = link_external,
**kwargs
)

# Combine them so the wrapper calls the main script
native.py_binary(
Expand All @@ -115,6 +131,8 @@ def _expand_template_impl(ctx):
" {windows_default_include_paths}": "\n".join([" %r," % path for path in find_cpp_toolchain(ctx).built_in_include_directories]), # find_cpp_toolchain is from https://docs.bazel.build/versions/main/integrating-with-rules-cc.html
"{exclude_headers}": repr(ctx.attr.exclude_headers),
"{exclude_external_sources}": repr(ctx.attr.exclude_external_sources),
"{update_gitignore}": repr(ctx.attr.update_gitignore),
"{link_external}": repr(ctx.attr.link_external),
},
)
return DefaultInfo(files = depset([script]))
Expand All @@ -124,6 +142,8 @@ _expand_template = rule(
"labels_to_flags": attr.string_dict(mandatory = True), # string keys instead of label_keyed because Bazel doesn't support parsing wildcard target patterns (..., *, :all) in BUILD attributes.
"exclude_external_sources": attr.bool(default = False),
"exclude_headers": attr.string(values = ["all", "external", ""]), # "" needed only for compatibility with Bazel < 3.6.0
"update_gitignore": attr.bool(default = True),
"link_external": attr.bool(default = True),
"_script_template": attr.label(allow_single_file = True, default = "refresh.template.py"),
# For Windows INCLUDE. If this were eliminated, for example by the resolution of https://github.com/clangd/clangd/issues/123, we'd be able to just use a macro and skylib's expand_template rule: https://github.com/bazelbuild/bazel-skylib/pull/330
# Once https://github.com/bazelbuild/bazel/pull/17108 is widely released, we should be able to eliminate this and get INCLUDE directly. Perhaps for 7.0? Should be released in the sucessor to 6.0
Expand Down

0 comments on commit 936ae08

Please sign in to comment.