Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
136 lines (123 loc) · 4.75 KB
/
Copy pathaction.yml
File metadata and controls
136 lines (123 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
name: "lint"
description: "Run lint on the files changed by a commit or pull request."
author: "Constantine Molchanov"
inputs:
config:
description: "Path to the lint config file."
required: false
default: "linter.nim"
nim-version:
description: "Nim version used to build the config."
required: false
default: "stable"
semantic:
description: "Run semantic checks in addition to ordinary checks."
required: false
default: "false"
setup:
description: "Command to prepare project dependencies for semantic analysis; LINT_ROOT points to this action's checkout. Empty to skip."
required: false
default: 'nimble setup --requires:"file://$LINT_ROOT" -y'
runs:
using: composite
steps:
- name: Set up Nim
uses: jiro4989/setup-nim-action@v2
with:
nim-version: ${{ inputs.nim-version }}
- name: Set up project dependencies
if: inputs.semantic == 'true' && inputs.setup != ''
shell: bash
env:
SETUP: ${{ inputs.setup }}
LINT_ROOT: ${{ github.action_path }}
run: bash -eo pipefail -c "$SETUP"
- name: Find local linters
id: local-linters
shell: bash
env:
CONFIG: ${{ inputs.config }}
run: |
# The config may import linters that live in this repository, and those
# are compiled into the binary, so they belong in the cache key.
#
# Only `import a/b/c` is detected: a module path made of letters,
# digits, underscores, and slashes. Relative imports (`import ./a`,
# `import ../a`), bracket groups (`import a/[b, c]`), `from x import y`,
# and `include` are not handled. Write such imports as plain
# `import a/b` lines to have them picked up.
dir=$(dirname "$CONFIG")
paths=""
for mod in $(grep -oE '^import [a-zA-Z0-9_/]+' "$CONFIG" | sed 's/^import //'); do
path="${mod%.nim}.nim"
if [ -f "$dir/$path" ]; then
paths="$paths,$(realpath --relative-to=. "$dir/$path")"
fi
done
echo "paths=${paths#,}" >> "$GITHUB_OUTPUT"
- name: Restore the compiled linter from cache
uses: actions/cache@v4
with:
path: .lint-cache
key: lint-${{ runner.os }}-${{ inputs.nim-version }}-${{ hashFiles(inputs.config, '**/*.nimble', 'nimble.lock', steps.local-linters.outputs.paths) }}
- name: Build the linter if it was not cached
shell: bash
env:
LINT_SRC: ${{ github.action_path }}/src
run: |
# if we have a cached executable, use it
if [ -x .lint-cache/linter ]; then
echo "Using the cached linter."
exit 0
fi
# install lint from the source
(cd "${{ github.action_path }}" && nimble install -y)
# lint uses `import compiler/...` so the compiler sources
# must be accessible
nim_root=$(dirname "$(dirname "$(command -v nim)")")
compiler_path=""
for candidate in "$nim_root" "$nim_root/lib"; do
if [ -d "$candidate/compiler" ]; then
compiler_path="$candidate"
break
fi
done
if [ -z "$compiler_path" ]; then
echo "Cannot find the Nim compiler sources next to $(command -v nim)"
exit 1
fi
# build the executable and cache it
mkdir -p .lint-cache
nim c -d:release --hints:off \
--path:"$compiler_path" \
--path:"$LINT_SRC" \
--out:.lint-cache/linter "${{ inputs.config }}"
- name: Lint changed files
shell: bash
env:
# The commit to diff against, per event: a pull request's base, a merge
# queue's base, or the previous commit on a push. Without the merge
# queue case the diff is empty and the job passes without linting.
BASE: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha || github.event.before }}
# A PR checkout is a synthetic merge that may include newer base-branch
# changes. Select files from the PR head, not that merge commit.
HEAD: ${{ github.event.pull_request.head.sha || github.sha }}
SEMANTIC: ${{ inputs.semantic }}
run: |
if [ -z "$BASE" ] || [ "$BASE" = "0000000000000000000000000000000000000000" ]; then
echo "No base commit to compare against, nothing to do."
exit 0
fi
files=$(git diff --name-only --diff-filter=ACMR "$BASE...$HEAD" -- '*.nim')
if [ -z "$files" ]; then
echo "No Nim files changed."
exit 0
fi
echo "Linting:"
echo "$files"
args=(--format=github)
if [ "$SEMANTIC" = "true" ]; then
args+=(--semantic)
fi
# shellcheck disable=SC2086
.lint-cache/linter "${args[@]}" $files