-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathnurfile
More file actions
284 lines (250 loc) · 7.51 KB
/
nurfile
File metadata and controls
284 lines (250 loc) · 7.51 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def is-windows [] {
$nu.os-info | get "family" | str starts-with "windows"
}
def --wrapped run-cmd [...cmd: string] {
print $"\n(ansi blue)Running(ansi reset) ($cmd | str join ' ')"
let elapsed = timeit {|| ^($cmd | first) ...($cmd | skip 1)}
print $"(ansi magenta)($cmd | first) took ($elapsed)(ansi reset)"
}
def flush-artifacts [
build_dir: string, dirty: bool
] {
if ($build_dir | path exists) {
if $dirty == false {
print $"(ansi yellow)Removing artifacts(ansi reset) ($build_dir)"
rm -r $build_dir
}
}
}
# Build the docs.
#
# Note, there is no check against what
# version of doxygen is used.
def "nur docs" [
--dirty (-d) # Do not flush previous build artifacts
--open (-o) # Open the built docs in your default browser
] {
let build_dir = "docs/html"
flush-artifacts $build_dir $dirty
cd docs
run-cmd doxygen
if $open {
let root_pg = $nur.project-path | path join $"($build_dir)/index.html"
start $root_pg
}
}
def find-built-examples [path: string] {
let len = $nur.project-path | str length
let example_path_len = ($path | str length) + 2
let sources = (
glob $"($path)/**/*.cpp" --exclude [
"**/build/**"
]
| each {$in | str substring ($len + $example_path_len)..-5}
)
let binaries = (
glob $"($path)/build/**/*" --exclude [
"**/CMakeFiles/**"
]
| each {$in | str substring ($len + $example_path_len + 6)..}
| filter {$in in $sources}
)
$binaries
}
# Build the Linux examples.
#
# This task expects the library to be installed.
# CMake will build any ncurses examples if
# the libncurses5-dev package is installed
def "nur examples" [
--dirty (-d) # Reuse previous build env
...cmake_opts: string # additional args passed to cmake when configuring the build env
] {
let src_dir = "examples_RPi"
let build_dir = $"($src_dir)/build"
flush-artifacts $build_dir $dirty
if $dirty == false {
run-cmd cmake -B $build_dir $src_dir ...$cmake_opts
} else if ($build_dir | path exists) == false {
run-cmd cmake -B $build_dir $src_dir ...$cmake_opts
}
run-cmd cmake --build $build_dir
print $"(ansi green)Built the following examples:(ansi reset)"
let binaries = (
find-built-examples $src_dir
| each {$"($build_dir)/($in)"}
)
print $binaries
}
# Build/install the library.
#
# Note, this may ask for the password to
# install the library with super-user privileges.
def --wrapped "nur lib" [
--dirty (-d) # Reuse previous build env
--no-install # Do not install the library (useful for testing compilation)
...cmake_opts: string # additional args passed to cmake when configuring the build env
] {
let build_dir = "build"
flush-artifacts $build_dir $dirty
if $dirty == false {
run-cmd cmake -B build -S . ...$cmake_opts
}
run-cmd cmake --build $build_dir
if $no_install == false {
run-cmd sudo cmake --install $build_dir
}
}
# Build the Pico SDK examples.
#
# If building on Windows, then `-G Ninja` is
# automatically passed to CMake when configuring the
# build environment.
def --wrapped "nur pico" [
--dirty (-d) # Reuse previous build env
...cmake_opts: string # additional args passed to cmake when configuring the build env
] {
let src_dir = "examples_pico"
let build_dir = $"($src_dir)/build"
flush-artifacts $build_dir $dirty
let use_ninja = '-G Ninja'
let opts = if (is-windows) {
$cmake_opts | append $use_ninja
} else { $cmake_opts }
if $dirty == false {
run-cmd cmake -B $build_dir $src_dir ...$opts
} else if ($build_dir | path exists) == false {
run-cmd cmake -B $build_dir $src_dir ...$opts
}
run-cmd cmake --build $build_dir
}
# Install the python wrapper.
#
# Note, this task requires the library
# (& boost.python) to be installed.
def "nur py" [
--dirty (-d) # Reuse previous build env
] {
let src_dir = "pyRF24Mesh"
let artifacts = glob $"($src_dir)/{build,*.egg-info}"
if ($artifacts | length) > 0 {
if $dirty == false {
print $"(ansi yellow)Removing artifacts(ansi reset) ($artifacts | str join ' ')"
rm -r ...$artifacts
}
}
run-cmd pip install -v $"./($src_dir)"
}
def changed-files [] {
let status = git status --short | lines
if ($status | length) == 0 {
print $"(ansi red)No file changes detected via (ansi cyan)`git status`(ansi reset)"
print $"Perhaps you want to format all files? (ansi cyan)`nur fmt -a`"
return []
}
let changed = (
$status
| each {$in | str trim | split column --regex '\s+' -n 2}
| flatten
| rename "state" "name"
)
# print $changed
let result = (
$changed
| where {$in.state | split chars | each {$in in ['A' 'M' 'R']} | any {$in}}
| get "name"
| each {
if ($in | str contains " -> ") {
$in | parse "{a} -> {b}" | get "b" | $in.0
} else { $in }
}
)
# print $result
$result
}
def get-clang-format-version [bin_name: string] {
if (which $bin_name | is-empty) {
null
} else {
let version = (
^$bin_name --version
| split column ' '
| values
| flatten
| last
)
print $"($bin_name) --version: ($version)"
$version | parse "{major}.{minor}.{patch}"
}
}
def match-version [
bin_name: string,
expected: string = "14",
--throw
] {
let version = get-clang-format-version $bin_name
if ($version | is-empty) {
if $throw {
error make {
msg: $"($bin_name) not found. Ensure it is installed and added to your PATH."
}
}
false
} else {
if ($version.major.0 == $expected) {
true
} else if $throw {
error make {
msg: $"Failed to find clang-format v($expected).x"
}
} else {
false
}
}
}
# Run clang-format on C++ files.
#
# By default, only changed C++ sources are formatted (uses `git status`).
# The clang-format version is expected to be v14.
# If v14 is not found, then an error is thrown.
def "nur fmt" [
--all (-a) # Format all C++ sources
] {
let all_files = glob "**/*.{h,cpp,c,ino}" --exclude [
"**/build/**"
] | path relative-to $nur.project-path
let files = if $all {
$all_files
} else {
let changes = changed-files
(
$all_files
| where {
($in | path split) in (
$changes | each {$in | path split}
)
}
)
}
let bin_name = if (is-windows) {
let bin_name = "clang-format"
let is_expected = match-version $bin_name --throw
"clang-format"
} else {
let bin_name = "clang-format-14"
let is_expected = match-version $bin_name
if ($is_expected == false) {
let bin_name = "clang-format"
let is_expected = match-version $bin_name --throw
$bin_name
} else {
$bin_name
}
}
if ($files | length) > 0 {
print $files
let elapsed = timeit {|| $files | par-each {|f| ^$bin_name "-i" "--style" "file" $f}}
print $"clang-format took ($elapsed) using parallelism!"
}
print $"(ansi blue)Applied clang-format to ($files | length) files(ansi reset)"
}