Skip to content

Commit 51a5137

Browse files
committed
Also use grep-highlighter for buildozer outputs.
That way it is possible to maybe only edit a subset.
1 parent e55a7f0 commit 51a5137

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

bant/bant.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ static int usage(const char *prog, const char *message, int exit_code) {
8888
-f <format> : Output format, support depends on command. One of
8989
: native (default), s-expr, plist, json, csv
9090
Unique prefix ok, so -fs , -fp, -fj or -fc is sufficient.
91-
-g <regex> : 'grep' filter output with regular expression. Can be
91+
-g <regex> : 'grep'-filter output with regular expression. Can be
9292
provided multiple times to narrow match ('and' semantics).
93-
Outputs are highlit with different colors (also see '-O')
94-
-O : 'or' smentics for for `-g`. Instead of requiring all
95-
regexs to match for a record, require one of them.
93+
Matches are highlit with different colors (also see '-O').
94+
-i : (with `-g`): Treat regex case insensitively.
95+
-O : (with `-g`): 'or' match smentics. Instead of requiring all
96+
regexs to match for a record, require at least one of them.
9697
-r : Follow dependencies recursively starting from pattern.
9798
Without numeric parameter, follows dependencies to the end.
9899
An optional parameter allows to limit the nesting depth,
@@ -110,9 +111,7 @@ Commands (unique prefix sufficient):
110111
-a : print all toplevel items in packages, not just rules.
111112
-e : elaborate; light eval: expand variables, concat etc.
112113
-b : elaborate including expansion of built-in macros.
113-
-g <regex> : 'grep' - only print targets where any string
114-
matches regex.
115-
-i If '-g' is given: case insensitive
114+
-g, -i, -O work here and print the whole item on match.
116115
parse : Parse all BUILD files from pattern. Follow deps with -r
117116
Emit parse errors. Silent otherwise: No news are good news.
118117
-v : some stats.

bant/cli-commands.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ CliStatus RunCommand(Session &session, Command cmd,
279279
}
280280
break;
281281
}
282+
282283
case Command::kExpandedLibraryHeaders: //
283284
bant::PrintProvidedSources(
284285
session, "header", print_pattern,
@@ -316,21 +317,27 @@ CliStatus RunCommand(Session &session, Command cmd,
316317
ExtractGeneratedFromGenrule(project, session.info()));
317318
break;
318319

319-
case Command::kDWYU:
320+
case Command::kDWYU: {
321+
auto highlighter = CreateGrepHighlighterFromFlags(session);
322+
if (!highlighter) return CliStatus::kExitFailure;
320323
if (bant::CreateDependencyEdits(
321324
session, project, patterns,
322-
CreateBuildozerDepsEditCallback(session.out())) > 0) {
325+
CreateBuildozerDepsEditCallback(session.out(), *highlighter)) > 0) {
323326
return CliStatus::kExitCleanupFindings;
324327
}
325328
break;
329+
}
326330

327-
case Command::kCanonicalizeDeps:
331+
case Command::kCanonicalizeDeps: {
332+
auto highlighter = CreateGrepHighlighterFromFlags(session);
333+
if (!highlighter) return CliStatus::kExitFailure;
328334
if (CreateCanonicalizeEdits(
329335
session, project, patterns,
330-
CreateBuildozerDepsEditCallback(session.out())) > 0) {
336+
CreateBuildozerDepsEditCallback(session.out(), *highlighter)) > 0) {
331337
return CliStatus::kExitCleanupFindings;
332338
}
333339
break;
340+
}
334341

335342
case Command::kListPackages: {
336343
auto highlighter = CreateGrepHighlighterFromFlags(session);
@@ -342,7 +349,8 @@ CliStatus RunCommand(Session &session, Command cmd,
342349
printer->AddRow({std::string(parsed->name()), package.ToString()});
343350
}
344351
printer->Finish();
345-
} break;
352+
break;
353+
}
346354

347355
case Command::kListLeafs:
348356
case Command::kListTargets: {
@@ -369,7 +377,8 @@ CliStatus RunCommand(Session &session, Command cmd,
369377
});
370378
}
371379
printer->Finish();
372-
} break;
380+
break;
381+
}
373382

374383
case Command::kListWorkkspace:
375384
PrintMatchingWorkspaceExternalRepos(session, project, patterns);

bant/tool/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cc_library(
1111
hdrs = ["edit-callback.h"],
1212
deps = [
1313
"//bant:types-bazel",
14+
"//bant/util:grep-highlighter",
1415
],
1516
)
1617

bant/tool/edit-callback.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@
1818
#include "bant/tool/edit-callback.h"
1919

2020
#include <ostream>
21+
#include <sstream>
2122
#include <string_view>
2223

2324
#include "bant/types-bazel.h"
25+
#include "bant/util/grep-highlighter.h"
2426

2527
namespace bant {
26-
EditCallback CreateBuildozerDepsEditCallback(std::ostream &out) {
27-
return [&out](EditRequest edit, const BazelTarget &target,
28-
std::string_view before, std::string_view after) {
28+
EditCallback CreateBuildozerDepsEditCallback(std::ostream &out,
29+
const GrepHighlighter &grepper) {
30+
return [&out, &grepper](EditRequest edit, const BazelTarget &target,
31+
std::string_view before, std::string_view after) {
32+
std::stringstream tmp_out;
2933
switch (edit) {
3034
case EditRequest::kRemove:
31-
out << "buildozer 'remove deps " << before << "' " << target << "\n";
35+
tmp_out << "'remove deps " << before << "' " << target;
3236
break;
3337
case EditRequest::kAdd:
34-
out << "buildozer 'add deps " << after << "' " << target << "\n";
38+
tmp_out << "'add deps " << after << "' " << target;
3539
break;
3640
case EditRequest::kRename:
37-
out << "buildozer 'replace deps " << before << " " << after << "' "
38-
<< target << "\n";
41+
tmp_out << "'replace deps " << before << " " << after << "' " << target;
3942
break;
4043
}
44+
grepper.EmitMatch(tmp_out.str(), out, "buildozer ", "\n");
4145
};
4246
}
4347

bant/tool/edit-callback.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <string_view>
2424

2525
#include "bant/types-bazel.h"
26+
#include "bant/util/grep-highlighter.h"
2627

2728
namespace bant {
2829

@@ -47,8 +48,11 @@ using EditCallback =
4748
std::function<void(EditRequest op, const BazelTarget &target,
4849
std::string_view before, std::string_view after)>;
4950

50-
// Create an EditCallback function that writes "buildozer" dep-edits to "out".
51-
EditCallback CreateBuildozerDepsEditCallback(std::ostream &out);
51+
// Create an EditCallback function that writes "buildozer" dep-edits to "out"
52+
// The GrepHighlighter needs to be alive for the duration of the use of
53+
// the callback.
54+
EditCallback CreateBuildozerDepsEditCallback(std::ostream &out,
55+
const GrepHighlighter &grepper);
5256

5357
} // namespace bant
5458
#endif // BANT_TOOL_EDIT_CALLBACK_

0 commit comments

Comments
 (0)