-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.sh
executable file
·207 lines (171 loc) · 4.28 KB
/
commands.sh
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
#! /bin/bash
# A helper script to help build and upload firmware projects based on PlatformIO
function list_environments(){
pio project config|grep ^env:|sed "s,env:,,"
}
function generate_vscode_tasks() {
# Lists all environments, and creates a vscode build and upload tasks for each environment
# This is useful for creating a vscode task.json file
for env in $(list_environments); do
echo "
// Upload ${env}
{
\"label\": \"upload ${env}\",
\"type\": \"shell\",
\"command\": \"pio run -t upload -e ${env}\",
},
// Build ${env}
{
\"label\": \"build ${env}\",
\"type\": \"shell\",
\"command\": \"pio run -e ${env}\",
},"
done
}
function update_vscode_tasks_json () {
# Updates the vscode tasks.json file with the tasks generated by generate_vscode_tasks
if [ ! -f .vscode/tasks.json ]; then
echo "tasks.json file not found. Please run this command from the root of the project"
exit 1
fi
# Backup the tasks.json file
cp .vscode/tasks.json .vscode/tasks.json.bak
# Create the top part
echo ' {
// This file is auto generated by the commands.sh script. Any edits will be overwritten automatically.
"version": "2.0.0",
"tasks": [
// Build all
{
"label": "Build all",
"type": "shell",
"command": "pio run",
"group": {
"kind": "build",
"isDefault": true
}
},
// Make compiledb to allow LSP to work
{
"label": "Build compiledb database for LSP / clangd",
"type": "shell",
"command": "pio run -t compiledb",
},
' > .vscode/tasks.json
# Generate the tasks
generate_vscode_tasks >> .vscode/tasks.json
# Close the tasks.json file
echo "] }" >> .vscode/tasks.json
echo "tasks.json updated successfully"
}
function print_help() {
echo "Usage: $0 <command>"
echo "Commands:"
echo " list"
echo " show"
echo " build-all"
echo " build <environment>"
echo " upload <environment>"
echo " add_board"
echo " fzf_boards"
echo " update_vscode_tasks"
exit 1
}
function build() {
if [ $# -ne 1 ]; then
echo "Usage: $0 build <environment>"
echo "See list of available environments by running $0 list"
exit 1
fi
pio run -e $1
}
function build_all() {
pio run
}
function upload() {
if [ $# -ne 1 ]; then
echo "Usage: $0 upload <environment>"
echo "See list of available environments by running $0 list"
exit 1
fi
pio run -e $1 --target upload
}
function show() {
pio project --list-targets
}
function fzf_boards() {
# Fuzzy search boards and return the board id
# Check if fzf installed
if ! command -v fzf &> /dev/null; then
echo "fzf is not installed. Please install fzf to use this feature."
exit 1
fi
pio boards --json-output | jq -r '.[] | "id:" + .id + ", name:" + .name + ", platform:" + .platform' | fzf
}
# Gets new environemnt using fzf and adds it to the platformio.ini file
function add_new_board() {
new_env=$(fzf_boards)
if [ -z "$new_env" ]; then
echo "No board selected. Exiting"
exit 1
fi
echo "Adding new environment $new_env"
# Get the board id
board_id=$(echo $new_env | awk -F',' '{print $1}' | awk -F':' '{print $2}' | xargs)
board_name=$(echo $new_env | awk -F',' '{print $2}' | awk -F':' '{print $2}' | xargs)
board_platform=$(echo $new_env | awk -F',' '{print $3}' | awk -F':' '{print $2}' | xargs)
# Add the new environment to the platformio.ini file
echo "Adding new environment to platformio.ini"
echo "
; $board_name
[env:$board_id]
platform = $board_platform
board = $board_id
; Additional build flags for $board_name here
build_flags =
\${env.build_flags}
; Additional libraries for $board_name here
lib_deps =
\${env.lib_deps}" >> platformio.ini
update_vscode_tasks_json
}
function main() {
# parse the command line arguments
if [ $# -eq 0 ]; then
print_help
exit 1
fi
case $1 in
list)
list_environments
;;
show)
show
;;
upload)
shift
upload $@
;;
build-all)
build_all
;;
build)
shift
build $@
;;
add_board)
add_new_board
;;
fzf_boards)
fzf_boards
;;
update_vscode_tasks)
update_vscode_tasks_json
;;
*)
print_help
exit 1
;;
esac
}
main $@