bazelbuild / buildtools

A bazel BUILD file formatter and editor
Apache License 2.0
1.01k stars 415 forks source link

Add dict_replace_if_equal command to buildozer. #1273

Closed tsell closed 4 months ago

tsell commented 4 months ago

This is useful for making modifications at scale, where you only want to replace certain dictionary values. To illustrate, suppose you have a BUILD file containing many targets, some of which use some "old_value" which you wish to replace with "new_value" in a dictionary, and some of which use other values that you don't want to modify:

macro(
  name = "foo",
  macro_values = {
    "key": "old_value",
  },
)

macro(
  name = "bar",
  macro_values = {
    "key": "neutral_value",
  },
)

With this new command, these can all be modified like so:

buildozer "dict_replace_if_equal macro_values key old_value new_value" //path/to/these/targets:*

Resulting in the desired change:

macro(
  name = "foo",
  macro_values = {
    "key": "new_value",
  },
)

macro(
  name = "bar",
  macro_values = {
    "key": "neutral_value",
  },
)