Skip to content

Commit

Permalink
precommit: Add shfmt to format shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Feb 17, 2021
1 parent 1a5f6ed commit 2e2218d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions tools/precommit/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FROM ubuntu:20.04
# https://clang.llvm.org/docs/ClangFormat.html
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
apt-get update -qq && apt-get install -qq --no-install-recommends \
ca-certificates \
clang-format \
git \
less \
Expand All @@ -17,6 +18,7 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
python3-wheel \
python3-setuptools \
shellcheck \
wget \
&& rm -rf /var/lib/apt/lists/*

# Install markdownlint-cli and dependencies.
Expand All @@ -32,6 +34,13 @@ RUN ln -s /opt/cp2k-precommit/node_modules/markdownlint-cli/markdownlint.js /usr
COPY requirements.txt .
RUN pip3 install --quiet -r requirements.txt

# Install shfmt.
# https://github.com/mvdan/sh
RUN wget -q https://github.com/mvdan/sh/releases/download/v3.2.2/shfmt_v3.2.2_linux_amd64 && \
echo '3a32a69286a19491a81fcd854154f0d886c379ff28d99e32d5594490b8bbef4b shfmt_v3.2.2_linux_amd64' | sha256sum --check && \
chmod +x shfmt_v3.2.2_linux_amd64 && \
ln -s /opt/cp2k-precommit/shfmt_v3.2.2_linux_amd64 /usr/bin/shfmt

# Clone cp2k repository (needed for CI mode).
RUN git clone --quiet --recursive --single-branch -b master https://github.com/cp2k/cp2k.git /workspace/cp2k

Expand Down
2 changes: 2 additions & 0 deletions tools/precommit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ source code:
to format C and Cuda files.
- [black](https://github.com/psf/black)
to format Python scripts.
- [shfmt](https://github.com/mvdan/sh)
to format Shell scripts.
- [shellcheck](https://github.com/koalaman/shellcheck)
to analyze Shell scripts.
- [markdownlint](https://github.com/DavidAnson/markdownlint)
Expand Down
5 changes: 3 additions & 2 deletions tools/precommit/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def main():

# Find eligible files and sort by size as larger ones will take longer to process.
eligible_file_pattern = re.compile(
r"(./data/.*POTENTIALS?)|(.*/PACKAGE)|(.*\.(F|fypp|c|cu|h|py|md))$"
r"(./data/.*POTENTIALS?)|(.*/PACKAGE)|(.*\.(F|fypp|c|cu|h|py|md|sh))$"
)
file_list = [fn for fn in candidate_file_list if eligible_file_pattern.match(fn)]
file_list.sort(reverse=True, key=lambda fn: os.path.getsize(fn))
Expand Down Expand Up @@ -189,7 +189,8 @@ def process_file(fn, allow_modifications):
run_analyze_src(fn)

elif re.match(r".*\.sh$", fn):
run_remote_tool("shellcheck", fn)
run_remote_tool("shfmt", fn)
# run_remote_tool("shellcheck", fn)

elif re.match(r".*\.md$", fn):
run_remote_tool("markdownlint", fn)
Expand Down
6 changes: 6 additions & 0 deletions tools/precommit/precommit_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def black():
return run_tool(["black"])


# ======================================================================================
@app.route("/shfmt", methods=["POST"])
def shfmt():
return run_tool(["shfmt", "-i=2", "-ci", "-sr", "-w"])


# ======================================================================================
@app.route("/shellcheck", methods=["POST"])
def shellcheck():
Expand Down

0 comments on commit 2e2218d

Please sign in to comment.