Skip to content

Commit 42a40f3

Browse files
authored
Merge pull request #214 from per1234/check-shell
Use template "Check Shell" workflow (Task)
2 parents 2670f2a + 8379743 commit 42a40f3

File tree

7 files changed

+419
-211
lines changed

7 files changed

+419
-211
lines changed

Diff for: .editorconfig

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
1-
# http://editorconfig.org
2-
3-
root = true
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/general/.editorconfig
2+
# See: https://editorconfig.org/
3+
# The formatting style defined in this file is the official standardized style to be used in all Arduino Tooling
4+
# projects and should not be modified.
5+
# Note: indent style for each file type is defined even when it matches the universal config in order to make it clear
6+
# that this type has an official style.
47

58
[*]
69
charset = utf-8
10+
end_of_line = lf
11+
indent_size = 2
12+
indent_style = space
713
insert_final_newline = true
814
trim_trailing_whitespace = true
915

10-
[*.go]
16+
[*.{adoc,asc,asciidoc}]
17+
indent_size = 2
18+
indent_style = space
19+
20+
[*.{bash,sh}]
21+
indent_size = 2
22+
indent_style = space
23+
24+
[*.{c,cc,cp,cpp,cxx,h,hh,hpp,hxx,ii,inl,ino,ixx,pde,tpl,tpp,txx}]
25+
indent_size = 2
26+
indent_style = space
27+
28+
[*.{go,mod}]
1129
indent_style = tab
30+
31+
[*.java]
32+
indent_size = 2
33+
indent_style = space
34+
35+
[*.{js,jsx,json,jsonc,json5,ts,tsx}]
36+
indent_size = 2
37+
indent_style = space
38+
39+
[*.{md,mdx,mkdn,mdown,markdown}]
40+
indent_size = unset
41+
indent_style = space
42+
43+
[*.proto]
44+
indent_size = 2
45+
indent_style = space
46+
47+
[*.py]
1248
indent_size = 4
49+
indent_style = space
1350

14-
[*.{yml,yaml}]
51+
[*.svg]
52+
indent_size = 2
1553
indent_style = space
54+
55+
[*.{yaml,yml}]
1656
indent_size = 2
57+
indent_style = space
58+
59+
[.gitmodules]
60+
indent_style = tab

Diff for: .github/workflows/check-formatting.yml

-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ jobs:
3434
repo-token: ${{ secrets.GITHUB_TOKEN }}
3535
version: 3.x
3636

37-
- name: Check shell script formatting
38-
# https://github.com/mvdan/sh
39-
run: |
40-
docker run --volume "$GITHUB_WORKSPACE/libraries/spell-check":/mnt --workdir /mnt mvdan/shfmt:latest -w .
41-
git diff --color --exit-code
42-
4337
- name: Check documentation formatting
4438
run: task docs:check-formatting
4539

Diff for: .github/workflows/check-shell-task.yml

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-shell-task.md
2+
name: Check Shell Scripts
3+
4+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/check-shell-task.ya?ml"
9+
- "Taskfile.ya?ml"
10+
- "**/.editorconfig"
11+
- "**.bash"
12+
- "**.sh"
13+
pull_request:
14+
paths:
15+
- ".github/workflows/check-shell-task.ya?ml"
16+
- "Taskfile.ya?ml"
17+
- "**/.editorconfig"
18+
- "**.bash"
19+
- "**.sh"
20+
schedule:
21+
# Run every Tuesday at 8 AM UTC to catch breakage caused by tool changes.
22+
- cron: "0 8 * * TUE"
23+
workflow_dispatch:
24+
repository_dispatch:
25+
26+
jobs:
27+
lint:
28+
name: ${{ matrix.configuration.name }}
29+
runs-on: ubuntu-latest
30+
31+
env:
32+
# See: https://github.com/koalaman/shellcheck/releases/latest
33+
SHELLCHECK_RELEASE_ASSET_SUFFIX: .linux.x86_64.tar.xz
34+
35+
strategy:
36+
fail-fast: false
37+
38+
matrix:
39+
configuration:
40+
- name: Generate problem matcher output
41+
# ShellCheck's "gcc" output format is required for annotated diffs, but inferior for humans reading the log.
42+
format: gcc
43+
# The other matrix job is used to set the result, so this job is configured to always pass.
44+
continue-on-error: true
45+
- name: ShellCheck
46+
# ShellCheck's "tty" output format is most suitable for humans reading the log.
47+
format: tty
48+
continue-on-error: false
49+
50+
steps:
51+
- name: Set environment variables
52+
run: |
53+
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
54+
echo "INSTALL_PATH=${{ runner.temp }}/shellcheck" >> "$GITHUB_ENV"
55+
56+
- name: Checkout repository
57+
uses: actions/checkout@v2
58+
59+
- name: Install Task
60+
uses: arduino/setup-task@v1
61+
with:
62+
repo-token: ${{ secrets.GITHUB_TOKEN }}
63+
version: 3.x
64+
65+
- name: Download latest ShellCheck release binary package
66+
id: download
67+
uses: MrOctopus/[email protected]
68+
with:
69+
repository: koalaman/shellcheck
70+
excludes: prerelease, draft
71+
asset: ${{ env.SHELLCHECK_RELEASE_ASSET_SUFFIX }}
72+
target: ${{ env.INSTALL_PATH }}
73+
74+
- name: Install ShellCheck
75+
run: |
76+
cd "${{ env.INSTALL_PATH }}"
77+
tar --extract --file="${{ steps.download.outputs.name }}"
78+
EXTRACTION_FOLDER="$(basename "${{ steps.download.outputs.name }}" "${{ env.SHELLCHECK_RELEASE_ASSET_SUFFIX }}")"
79+
# Add installation to PATH:
80+
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#adding-a-system-path
81+
echo "${{ env.INSTALL_PATH }}/$EXTRACTION_FOLDER" >> "$GITHUB_PATH"
82+
83+
- name: Run ShellCheck
84+
uses: liskin/gh-problem-matcher-wrap@v1
85+
continue-on-error: ${{ matrix.configuration.continue-on-error }}
86+
with:
87+
linters: gcc
88+
run: task --silent shell:check SHELLCHECK_FORMAT=${{ matrix.configuration.format }}
89+
90+
formatting:
91+
runs-on: ubuntu-latest
92+
93+
steps:
94+
- name: Set environment variables
95+
run: |
96+
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
97+
echo "SHFMT_INSTALL_PATH=${{ runner.temp }}/shfmt" >> "$GITHUB_ENV"
98+
99+
- name: Checkout repository
100+
uses: actions/checkout@v2
101+
102+
- name: Install Task
103+
uses: arduino/setup-task@v1
104+
with:
105+
repo-token: ${{ secrets.GITHUB_TOKEN }}
106+
version: 3.x
107+
108+
- name: Download shfmt
109+
id: download
110+
uses: MrOctopus/[email protected]
111+
with:
112+
repository: mvdan/sh
113+
excludes: prerelease, draft
114+
asset: _linux_amd64
115+
target: ${{ env.SHFMT_INSTALL_PATH }}
116+
117+
- name: Install shfmt
118+
run: |
119+
# Executable permissions of release assets are lost
120+
chmod +x "${{ env.SHFMT_INSTALL_PATH }}/${{ steps.download.outputs.name }}"
121+
# Standardize binary name
122+
mv "${{ env.SHFMT_INSTALL_PATH }}/${{ steps.download.outputs.name }}" "${{ env.SHFMT_INSTALL_PATH }}/shfmt"
123+
# Add installation to PATH:
124+
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#adding-a-system-path
125+
echo "${{ env.SHFMT_INSTALL_PATH }}" >> "$GITHUB_PATH"
126+
127+
- name: Format shell scripts
128+
run: task --silent shell:format
129+
130+
- name: Check formatting
131+
run: git diff --color --exit-code
132+
133+
executable:
134+
runs-on: ubuntu-latest
135+
136+
steps:
137+
- name: Checkout repository
138+
uses: actions/checkout@v2
139+
140+
- name: Install Task
141+
uses: arduino/setup-task@v1
142+
with:
143+
repo-token: ${{ secrets.GITHUB_TOKEN }}
144+
version: 3.x
145+
146+
- name: Check for non-executable scripts
147+
run: task --silent shell:check-mode

Diff for: .github/workflows/lint-shell.yml

-28
This file was deleted.

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[![Docs Status](https://github.com/arduino/arduino-lint/workflows/Publish%20documentation/badge.svg)](https://github.com/arduino/arduino-lint/actions?workflow=Publish+documentation)
88
[![Codecov](https://codecov.io/gh/arduino/arduino-lint/branch/main/graph/badge.svg?token=nprqPQMbdh)](https://codecov.io/gh/arduino/arduino-lint)
99
[![Check General Formatting status](https://github.com/arduino/arduino-lint/actions/workflows/check-general-formatting-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-general-formatting-task.yml)
10+
[![Check Shell Scripts status](https://github.com/arduino/arduino-lint/actions/workflows/check-shell-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-shell-task.yml)
1011
[![Check Certificates status](https://github.com/arduino/arduino-lint/actions/workflows/check-certificates.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-certificates.yml)
1112

1213
**Arduino Lint** is a command line tool that checks for common problems in [Arduino](https://www.arduino.cc/) projects:

Diff for: Taskfile.yml

+63-13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ tasks:
4646
- task: config:check
4747
- task: general:check-formatting
4848
- task: check-spelling
49+
- task: shell:check-mode
4950

5051
lint:
5152
desc: Lint all files
@@ -266,24 +267,73 @@ tasks:
266267
cmds:
267268
- npx {{ .PRETTIER }} --write "**/*.md"
268269

269-
shell:lint:
270-
desc: Lint shell scripts
270+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-shell-task/Taskfile.yml
271+
shell:check:
272+
desc: Check for problems with shell scripts
271273
cmds:
272-
# https://github.com/koalaman/shellcheck
273274
- |
274-
shopt -s globstar # Needed to check all scripts recursively.
275-
shellcheck ./**/*.sh
276-
277-
shell:check-formatting:
278-
desc: Format shell scripts
275+
if ! which shellcheck &>/dev/null; then
276+
echo "shellcheck not installed or not in PATH. Please install: https://github.com/koalaman/shellcheck#installing"
277+
exit 1
278+
fi
279+
- |
280+
# There is something odd about shellcheck that causes the task to always exit on the first fail, despite any
281+
# measures that would prevent this with any other command. So it's necessary to call shellcheck only once with
282+
# the list of script paths as an argument. This could lead to exceeding the maximum command length on Windows if
283+
# the repository contained a large number of scripts, but it's unlikely to happen in reality.
284+
shellcheck \
285+
--format={{default "tty" .SHELLCHECK_FORMAT}} \
286+
$(
287+
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
288+
# \ characters special treatment on Windows in an attempt to support them as path separators.
289+
find . \
290+
-path ".git" -prune -or \
291+
\( \
292+
-regextype posix-extended \
293+
-regex '.*[.](bash|sh)' -and \
294+
-type f \
295+
\)
296+
)
297+
298+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-shell-task/Taskfile.yml
299+
shell:format:
300+
desc: Format shell script files
279301
cmds:
280-
# https://github.com/mvdan/sh#shfmt
281-
- shfmt -d .
302+
- |
303+
if ! which shfmt &>/dev/null; then
304+
echo "shfmt not installed or not in PATH. Please install: https://github.com/mvdan/sh#shfmt"
305+
exit 1
306+
fi
307+
- shfmt -w .
282308

283-
shell:format:
284-
desc: Format shell scripts
309+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-shell-task/Taskfile.yml
310+
shell:check-mode:
311+
desc: Check for non-executable shell scripts
285312
cmds:
286-
- shfmt -l -w .
313+
- |
314+
EXIT_STATUS=0
315+
while read -r nonExecutableScriptPath; do
316+
# The while loop always runs once, even if no file was found
317+
if [[ "$nonExecutableScriptPath" == "" ]]; then
318+
continue
319+
fi
320+
321+
echo "::error file=${nonExecutableScriptPath}::non-executable script file: $nonExecutableScriptPath";
322+
EXIT_STATUS=1
323+
done <<<"$(
324+
# The odd approach to escaping `.` in the regex is required for windows compatibility because mvdan.cc/sh
325+
# gives `\` characters special treatment on Windows in an attempt to support them as path separators.
326+
find . \
327+
-path ".git" -prune -or \
328+
\( \
329+
-regextype posix-extended \
330+
-regex '.*[.](bash|sh)' -and \
331+
-type f -and \
332+
-not -executable \
333+
-print \
334+
\)
335+
)"
336+
exit $EXIT_STATUS
287337
288338
config:check:
289339
desc: Lint and check formatting of configuration files

0 commit comments

Comments
 (0)