Skip to content

Commit

Permalink
Revamp style-json commands (#41101)
Browse files Browse the repository at this point in the history
* Revamp json-styling commands in Makefile

* style-json now checks all non-blacklisted files, rather than stopping
  after the first failure.
* Dependencies fixed such that it doesn't compile the json_formatter
  executable every time you use these targets.

* Reduce json formatted spam

Stop printing anything for files that are well-formatted.  These lines
just clutter the CI output.

* Document some suggestions for git pre-commit

Show how to check json and code style in a pre-commit hook.
  • Loading branch information
jbytheway authored Jun 6, 2020
1 parent 0423c66 commit 000ecc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
14 changes: 5 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1102,22 +1102,18 @@ else
@echo Cannot run an astyle check, your system either does not have astyle, or it is too old.
endif

JSON_FILES = $(shell find data -name "*.json" | sed "s|^\./||")
JSON_WHITELIST = $(filter-out $(shell cat json_blacklist), $(JSON_FILES))

style-json: $(JSON_WHITELIST)

$(JSON_WHITELIST): json_blacklist json_formatter
style-json: json_blacklist $(JSON_FORMATTER_BIN)
ifndef CROSS
@$(JSON_FORMATTER_BIN) $@
find data -name "*.json" -print0 | grep -v -z -F -f json_blacklist | \
xargs -0 -L 1 $(JSON_FORMATTER_BIN)
else
@echo Cannot run json formatter in cross compiles.
endif

style-all-json: json_formatter
style-all-json: $(JSON_FORMATTER_BIN)
find data -name "*.json" -print0 | xargs -0 -L 1 $(JSON_FORMATTER_BIN)

json_formatter: $(JSON_FORMATTER_SOURCES)
$(JSON_FORMATTER_BIN): $(JSON_FORMATTER_SOURCES)
$(CXX) $(CXXFLAGS) $(TOOL_CXXFLAGS) -Itools/format -Isrc \
$(JSON_FORMATTER_SOURCES) -o $(JSON_FORMATTER_BIN)

Expand Down
15 changes: 15 additions & 0 deletions doc/DEVELOPER_TOOLING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## Pre-commit hook

If you have all the relevant tools installed, you can have git automatically
check the style of code and json by adding these commands to your git
pre-commit hook (typically at `.git/hooks/pre-commit`):

```BASH
git diff --cached --name-only -z HEAD | grep -z 'data/.*\.json' | \
xargs -r -0 -L 1 ./tools/format/json_formatter.[ce]* || exit 1

make astyle-check || exit 1
```

More details below on how to make these work and other ways to invoke these tools.

## Code style (astyle)

Automatic formatting of source code is performed by [Artistic Style](http://astyle.sourceforge.net/).
Expand Down
2 changes: 0 additions & 2 deletions tools/format/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,9 @@ int main( int argc, char *argv[] )
#else
bool supports_color = isatty( STDOUT_FILENO );
#endif
std::string color_good = supports_color ? "\x1b[32m" : std::string();
std::string color_bad = supports_color ? "\x1b[31m" : std::string();
std::string color_end = supports_color ? "\x1b[0m" : std::string();
if( in_str == out.str() ) {
std::cout << color_good << "Well formatted: " << color_end << filename << std::endl;
exit( EXIT_SUCCESS );
} else {
std::ofstream fout( filename, std::ios::binary | std::ios::trunc );
Expand Down

0 comments on commit 000ecc6

Please sign in to comment.