-
Notifications
You must be signed in to change notification settings - Fork 57
221 lines (204 loc) · 8.84 KB
/
draw-zmk.yml
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Reusable workflow for drawing and committing an automated keymap diagram
# for ZMK config repos using https://github.com/caksoylar/keymap-drawer
name: Draw ZMK keymaps
on:
workflow_call:
inputs:
keymap_patterns:
description: 'Path specification for keymaps to be parsed'
default: 'config/*.keymap'
required: false
type: string
config_path:
description: 'Path to the keymap-drawer configuration file, ignored if non-existent'
default: 'keymap_drawer.config.yaml'
required: false
type: string
west_config_path:
description: 'Path to the folder containing west.yml, e.g. `config`. If specified, west will be initialized to fetch modules defined in west.yml'
default: ''
required: false
type: string
output_folder:
description: 'Output folder for SVG and YAML files'
default: 'keymap-drawer'
required: false
type: string
json_path:
description: 'Path containing <keymap>.json physical layout description files, ignored if non-existent'
default: 'config'
required: false
type: string
parse_args:
description: "Map of keyboard names to extra `keymap parse` args, e.g. `corne:'--layer-names Def Lwr Rse Fun'`"
default: ''
required: false
type: string
draw_args:
description: "Map of keyboard names to extra `keymap draw` args, e.g. `corne:'-k corne_rotated -l LAYOUT_split_3x5_3'`"
default: ''
required: false
type: string
commit_message:
description: 'Commit message for updated images. Ignored if `amend_commit` is `true`.'
default: 'keymap-drawer render'
required: false
type: string
amend_commit:
description: 'Whether to amend the last commit instead of creating a new one. Make sure you understand the implications of rewriting the branch history if you use this option!'
default: false
required: false
type: boolean
install_branch:
description: 'Install keymap-drawer from a git branch, use empty for pypi release (default)'
default: ''
required: false
type: string
install_repo:
description: 'Install keymap-drawer from a different git remote, primarily for testing changes using a keymap-drawer fork. Ignored if `install_branch` is unset/empty.'
default: 'https://github.com/caksoylar/keymap-drawer.git'
required: false
type: string
destination:
description: 'Add the output files to a commit, as artifacts or both, values: `commit`, `artifact`, `both`'
default: 'commit'
required: false
type: string
artifact_name:
description: 'Name of the produced artifact containing SVG and YAML outputs. Ignored if `destination` is `commit`.'
default: 'drawings'
required: false
type: string
fail_on_error:
description: 'Fail the action if an error occurs during parse/draw'
default: false
required: false
type: boolean
outputs:
drawings:
description: 'Archive with keymap in YAML and drawing in SVG formats'
value: ${{ jobs.draw.outputs.drawings }}
jobs:
draw:
runs-on: ubuntu-latest
outputs:
drawings: ${{ steps.artifact-upload-step.outputs.artifact-id }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# So the reference to the parent commit is available when amending
# See:
# - https://github.com/stefanzweifel/git-auto-commit-action#using---amend-and---no-edit-as-commit-options
# - https://github.com/stefanzweifel/git-auto-commit-action/issues/159#issuecomment-845347950
# - https://github.com/actions/checkout
fetch-depth: ${{ (inputs.amend_commit == true && 2) || 1 }}
submodules: recursive
- name: Install keymap-drawer (pypi)
if: inputs.install_branch == ''
run: python3 -m pip install keymap-drawer
- name: Install keymap-drawer (git)
if: inputs.install_branch != ''
run: python3 -m pip install "git+${{ inputs.install_repo }}@${{ inputs.install_branch }}"
- name: Install west
if: inputs.west_config_path != ''
run: python3 -m pip install west
- name: Cache west modules
if: inputs.west_config_path != ''
uses: actions/cache@v4
continue-on-error: true
env:
cache_name: cache-zephyr-3.5.0-modules
with:
path: |
modules/
tools/
zephyr/
bootloader/
zmk/
key: ${{ runner.os }}-build-${{ env.cache_name }}-${{ hashFiles('**/west.yml', '**/build.yaml') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache_name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: West Init
if: inputs.west_config_path != ''
run: west init -l ${{ inputs.west_config_path }}
- name: West Update
if: inputs.west_config_path != ''
run: west update
- name: Draw keymaps
id: draw
run: |
get_args() {
local keyboard=$2
eval set -- "$1"
for arg; do
local key=${arg%%:*}
local val=${arg#*:}
if [ "$key" = "$keyboard" ]; then
echo "$val"
break
fi
done
}
declare -a DRAWINGS
error_occurred=0
mkdir -p "${{ inputs.output_folder }}"
config_path="${{ inputs.config_path }}"
[ -e "$config_path" ] && config_arg=(-c "$config_path") || config_arg=()
echo "INFO: using config args: ${config_arg[@]}"
for keymap_file in ${{ inputs.keymap_patterns }}; do
keyboard=$(basename -s .keymap "$keymap_file")
echo "INFO: drawing for $keyboard"
parse_args=$(get_args "${{ inputs.parse_args }}" "$keyboard")
echo "INFO: got extra parse args: $parse_args"
draw_args=$(get_args "${{ inputs.draw_args }}" "$keyboard")
echo "INFO: got extra draw args: $draw_args"
json_path="${{ inputs.json_path }}"
if [ -f "$json_path/${keyboard}.json" ]; then
echo "INFO: found $json_path/${keyboard}.json";
draw_args+=" -j $json_path/${keyboard}.json"
fi
keymap "${config_arg[@]}" parse -z "$keymap_file" $parse_args >"${{ inputs.output_folder }}/$keyboard.yaml" \
&& keymap "${config_arg[@]}" draw "${{ inputs.output_folder }}/$keyboard.yaml" $draw_args >"${{ inputs.output_folder }}/$keyboard.svg" \
|| {
echo "ERROR: parsing or drawing failed for $keyboard!"
error_occurred=1
}
DRAWINGS+=(\"${{ inputs.output_folder }}/$keyboard.yaml\" \"${{ inputs.output_folder }}/$keyboard.svg\")
done
IFS=','
echo "DRAWINGS=[${DRAWINGS[*]}]" >> $GITHUB_OUTPUT
unset IFS
if [ "${{ inputs.fail_on_error }}" == "true" ] && [ $error_occurred -eq 1 ]; then
exit 1
fi
- name: Get last commit message
id: last_commit_message
if: inputs.amend_commit == true && (inputs.destination == 'commit' || inputs.destination == 'both')
run: |
echo "msg=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
- name: Commit updated images
if: ( inputs.destination == 'commit' || inputs.destination == 'both' )
uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: '${{ inputs.output_folder }}/*.svg ${{ inputs.output_folder }}/*.yaml'
# So the previous commit is amended instead of creating a new one when desired
# See:
# - https://github.com/stefanzweifel/git-auto-commit-action#using---amend-and---no-edit-as-commit-options
# - https://github.com/stefanzweifel/git-auto-commit-action/issues/159#issuecomment-845347950
# - https://github.com/actions/checkout
commit_message: '${{ (inputs.amend_commit == true && steps.last_commit_message.outputs.msg) || inputs.commit_message }}'
commit_options: "${{ (inputs.amend_commit == true && '--amend --no-edit') || '' }}"
push_options: "${{ (inputs.amend_commit == true && '--force-with-lease') || '' }}"
skip_fetch: ${{ inputs.amend_commit == true }}
- name: Artifact upload
id: artifact-upload-step
if: ( inputs.destination == 'artifact' || inputs.destination == 'both' )
uses: actions/upload-artifact@v4
with:
name: '${{ inputs.artifact_name }}'
path: |
${{ join(fromJSON(steps.draw.outputs.DRAWINGS), '
') }}