Skip to content

Commit

Permalink
Update apply-formatting.yml (#520)
Browse files Browse the repository at this point in the history
* Add ability to format python code with black through github actions (auto checks and par-hermes command) and CMake target

Co-authored-by: Philipp Grete <gretephi@msu.edu>
Co-authored-by: Jonas Lippuner <jlippuner@lanl.gov>
Co-authored-by: Philipp Grete <gretephi@msu.edu>
  • Loading branch information
3 people authored Jun 15, 2021
1 parent a54c9b8 commit 80a62ba
Show file tree
Hide file tree
Showing 27 changed files with 8,440 additions and 7,033 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/apply-formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
types: [created]
jobs:
apply-formatting:
name: Format C++ Code
name: Format Py and C++ Code
runs-on: ubuntu-latest
if: startsWith(github.event.comment.body, '@par-hermes format')
steps:
- uses: AndrewGaspar/cpp-auto-formatter/command@v0.1
- uses: JoshuaSBrown/cpp-py-formatter/command@v0.2.3
with:
botName: par-hermes
clangFormatVersion: 8
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/check-formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Check Formatting
on: push
jobs:
check-formatting:
name: Check C++ Formatting
name: Check Python and C++ Formatting
runs-on: ubuntu-latest
steps:
- uses: AndrewGaspar/cpp-auto-formatter/check@v0.1
- uses: JoshuaSBrown/cpp-py-formatter/check@v0.2.3
with:
clangFormatVersion: 8
token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [[PR 518]](https://github.com/lanl/parthenon/pull/518) Added MPI performance regression tests to CI performance app
- [[PR 530]](https://github.com/lanl/parthenon/pull/530) Fixed issue with ci plotting the oldest 5 commit metrics for each test, also cleaned up legend formatting.
- [[PR 536]](https://github.com/lanl/parthenon/pull/536) Updated to latest Kokkos release.
- [[PR 520]](https://github.com/lanl/parthenon/pull/520) Add black python formatter to github actions

### Removed (removing behavior/API/varaibles/...)
- [[PR 498]](https://github.com/lanl/parthenon/pull/498) Cleanup unused user hooks and variables
Expand Down
10 changes: 9 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ cmake -DPARTHENON_LINT_DEFAULT=ON .
```

### Formatting Code
We use clang-format to automatically format the code. If you have clang-format installed
We use clang-format to automatically format the C++ code. If you have clang-format installed
locally, you can always execute `make format` or `cmake --build . --target format` from
your build directory to automatically format the code.

Expand Down Expand Up @@ -159,6 +159,14 @@ run something equivalent to
`git fetch origin && git reset --hard origin/$(git branch --show-current)` to update your
local tracking branch.

In addition to clang-format, black is used to enforce formatting on python scripts.
Running:
```
@par-hermes format
```

Will also format all the ".py" files found in the repository.

## Test Suite

### Continuous Testing/Integration Environment
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![codecov](https://codecov.io/gh/lanl/parthenon/branch/master/graph/badge.svg)](https://codecov.io/gh/lanl/parthenon)
[![testing](https://gitlab.com/theias/hpc/jmstone/athena-parthenon/parthenon-ci-mirror/badges/develop/pipeline.svg)](https://gitlab.com/theias/hpc/jmstone/athena-parthenon/parthenon-ci-mirror/-/commits/develop)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Parthenon performance portable AMR framework

Expand Down
47 changes: 41 additions & 6 deletions cmake/Format.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ find_program(
clang-format-mp-8.0 # MacPorts
clang-format # Default name
)

find_program(BLACK NAMES black)

if (CLANG_FORMAT AND NOT CLANG_FORMAT_VERSION)
# Get clang-format --version
execute_process(
Expand All @@ -32,6 +35,16 @@ if (CLANG_FORMAT AND NOT CLANG_FORMAT_VERSION)
endif()
endif()

if (BLACK)
# Get black --version
execute_process(
COMMAND ${BLACK} --version
OUTPUT_VARIABLE BLACK_VERSION_OUTPUT)

message(STATUS "black --version: " ${BLACK_VERSION_OUTPUT})

endif()

if (NOT CLANG_FORMAT_VERSION)
message(
WARNING
Expand All @@ -55,10 +68,32 @@ set(
${PROJECT_SOURCE_DIR}/example/[^\.]*.cpp ${PROJECT_SOURCE_DIR}/example/[^\.]*.hpp
)

if (CMAKE_VERSION VERSION_LESS "3.12.0")
file(GLOB_RECURSE FORMAT_SOURCES ${GLOBS})
else()
file(GLOB_RECURSE FORMAT_SOURCES CONFIGURE_DEPENDS ${GLOBS})
endif()
# Specifically trying to exclude external here - I'm not sure if there's a better way
set(
PY_GLOBS
${parthenon_SOURCE_DIR}/scripts/[^\.]*.py
${parthenon_SOURCE_DIR}/tst/[^\.]*.py
${parthenon_SOURCE_DIR}/example/[^\.]*.py
)

file(GLOB_RECURSE FORMAT_SOURCES CONFIGURE_DEPENDS ${GLOBS})
file(GLOB_RECURSE PY_FORMAT_SOURCES CONFIGURE_DEPENDS ${PY_GLOBS})

add_custom_target(format ${CLANG_FORMAT} -i ${FORMAT_SOURCES})
if (CLANG_FORMAT)
if (BLACK)
add_custom_target(format
COMMAND ${CLANG_FORMAT} -i ${FORMAT_SOURCES}
COMMAND ${BLACK} ${PY_FORMAT_SOURCES}
VERBATIM)
else()
add_custom_target(format
COMMAND echo "Only C++ files will be formatted black was not found"
COMMAND ${CLANG_FORMAT} -i ${FORMAT_SOURCES}
VERBATIM)
endif()
elseif (BLACK)
add_custom_target(format
COMMAND echo "Only Python files will be formatted clang_format was not found"
COMMAND ${BLACK} ${PY_FORMAT_SOURCES}
VERBATIM)
endif()
Loading

0 comments on commit 80a62ba

Please sign in to comment.