Skip to content

Instantly share code, notes, and snippets.

@stagnation
Created June 14, 2023 09:58
Show Gist options
  • Save stagnation/7f8ad0f260e6456bbd80fb5d05e70ada to your computer and use it in GitHub Desktop.
Save stagnation/7f8ad0f260e6456bbd80fb5d05e70ada to your computer and use it in GitHub Desktop.
"""Crude pretty-printer for providers and output groups. [0/1844]
$ bazel cquery --output=starlark --starlark:file=providers.cquery //path/to:target
# Evaluate it again with each change to this file
# https://github.com/eradman/entr
$ echo "output_groups.cquery" \
| entr \
bazel cquery --output=starlark --starlark:file=providers.cquery //path/to:target
"""
LIST_SEP = " - "
LINE_SEP = "\n" + LIST_SEP
_SEP = "\n "
def mapPath(x):
if x == None:
return "None"
else:
return x.path
def header(x):
return "\n\n" + x + ":\n"
def format(target):
_providers = providers(target)
output = _providers.get("OutputGroupInfo")
__providers = LIST_SEP + LINE_SEP.join(_providers.keys())
_output_groups = LIST_SEP + LINE_SEP.join(dir(output))
res = "\n".join(["providers:", __providers, "", "output_groups:", _output_groups])
key = 'FileProvider'
if key in _providers:
val = _providers[key]
res += header(key)
joined = LINE_SEP.join([f.path for f in val.files_to_build.to_list()])
res += (LIST_SEP + joined) if joined else "\n"
key = 'FilesToRunProvider'
if key in _providers:
val = _providers[key]
res += header(key)
res += "{sep}{a}\n{sep}{b}\n".format(
sep = LIST_SEP,
a = mapPath(val.executable),
b = mapPath(val.runfiles_manifest)
)
# NB: You may iterate over all output groups and print them here. I just check default.
group = "default"
if group in output:
res += header("OutputGroupInfo " + group)
res += ", ".join([f.path for f in output[group].to_list()])
res += "\n"
key = 'ToolchainInfo'
if key in _providers:
val = _providers[key]
res += header(key)
res += LINE_SEP + "info.tool: " + val.info.tool.path
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment