|
| 1 | + |
| 2 | + |
| 3 | +def is-windows [] { |
| 4 | + $nu.os-info | get "family" | str starts-with "windows" |
| 5 | +} |
| 6 | + |
| 7 | + |
| 8 | +def --wrapped run-cmd [...cmd: string] { |
| 9 | + print $"\n(ansi blue)Running(ansi reset) ($cmd | str join ' ')" |
| 10 | + let elapsed = timeit {|| ^($cmd | first) ...($cmd | skip 1)} |
| 11 | + print $"(ansi magenta)($cmd | first) took ($elapsed)(ansi reset)" |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +def flush-artifacts [ |
| 16 | + build_dir: string, dirty: bool |
| 17 | +] { |
| 18 | + if ($build_dir | path exists) { |
| 19 | + if $dirty == false { |
| 20 | + print $"(ansi yellow)Removing artifacts(ansi reset) ($build_dir)" |
| 21 | + rm -r $build_dir |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +# Build the docs. |
| 28 | +# |
| 29 | +# Note, there is no check against what |
| 30 | +# version of doxygen is used. |
| 31 | +def "nur docs" [ |
| 32 | + --dirty (-d) # Do not flush previous build artifacts |
| 33 | + --open (-o) # Open the built docs in your default browser |
| 34 | +] { |
| 35 | + let build_dir = "docs/html" |
| 36 | + flush-artifacts $build_dir $dirty |
| 37 | + cd docs |
| 38 | + run-cmd doxygen |
| 39 | + if $open { |
| 40 | + let root_pg = $nur.project-path | path join $"($build_dir)/index.html" |
| 41 | + start $root_pg |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def changed-files [] { |
| 47 | + let status = git status --short | lines |
| 48 | + if ($status | length) == 0 { |
| 49 | + print $"(ansi red)No file changes detected via (ansi cyan)`git status`(ansi reset)" |
| 50 | + print $"Perhaps you want to format all files? (ansi cyan)`nur fmt -a`" |
| 51 | + return [] |
| 52 | + } |
| 53 | + let changed = ( |
| 54 | + $status |
| 55 | + | each {$in | str trim | split column --regex '\s+' -n 2} |
| 56 | + | flatten |
| 57 | + | rename "state" "name" |
| 58 | + ) |
| 59 | + # print $changed |
| 60 | + let result = ( |
| 61 | + $changed |
| 62 | + | where {$in.state | split chars | each {$in in ['A' 'M' 'R']} | any {$in}} |
| 63 | + | get "name" |
| 64 | + | each { |
| 65 | + if ($in | str contains " -> ") { |
| 66 | + $in | parse "{a} -> {b}" | get "b" | $in.0 |
| 67 | + } else { $in } |
| 68 | + } |
| 69 | + ) |
| 70 | + # print $result |
| 71 | + $result |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +def get-clang-format-version [bin_name: string] { |
| 76 | + if (which $bin_name | is-empty) { |
| 77 | + null |
| 78 | + } else { |
| 79 | + let version = ( |
| 80 | + ^$bin_name --version |
| 81 | + | split column ' ' |
| 82 | + | values |
| 83 | + | flatten |
| 84 | + | last |
| 85 | + ) |
| 86 | + print $"($bin_name) --version: ($version)" |
| 87 | + $version | parse "{major}.{minor}.{patch}" |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +def match-version [ |
| 93 | + bin_name: string, |
| 94 | + expected: string = "14", |
| 95 | + --throw |
| 96 | +] { |
| 97 | + let version = get-clang-format-version $bin_name |
| 98 | + if ($version | is-empty) { |
| 99 | + if $throw { |
| 100 | + error make { |
| 101 | + msg: $"($bin_name) not found. Ensure it is installed and added to your PATH." |
| 102 | + } |
| 103 | + } |
| 104 | + false |
| 105 | + } else { |
| 106 | + if ($version.major.0 == $expected) { |
| 107 | + true |
| 108 | + } else if $throw { |
| 109 | + error make { |
| 110 | + msg: $"Failed to find clang-format v($expected).x" |
| 111 | + } |
| 112 | + } else { |
| 113 | + false |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +# Run clang-format on C++ files. |
| 120 | +# |
| 121 | +# By default, only changed C++ sources are formatted (uses `git status`). |
| 122 | +# The clang-format version is expected to be v14. |
| 123 | +# If v14 is not found, then an error is thrown. |
| 124 | +def "nur fmt" [ |
| 125 | + --all (-a) # Format all C++ sources |
| 126 | +] { |
| 127 | + let all_files = glob "**/*.{h,cpp,c,ino}" --exclude [ |
| 128 | + "**/build/**" |
| 129 | + "utility/**" |
| 130 | + "clock-arch.*" |
| 131 | + "Dns.*" |
| 132 | + ] | path relative-to $nur.project-path |
| 133 | + let files = if $all { |
| 134 | + $all_files |
| 135 | + } else { |
| 136 | + let changes = changed-files |
| 137 | + ( |
| 138 | + $all_files |
| 139 | + | where { |
| 140 | + ($in | path split) in ( |
| 141 | + $changes | each {$in | path split} |
| 142 | + ) |
| 143 | + } |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + let bin_name = if (is-windows) { |
| 148 | + let bin_name = "clang-format" |
| 149 | + let is_expected = match-version $bin_name --throw |
| 150 | + "clang-format" |
| 151 | + } else { |
| 152 | + let bin_name = "clang-format-14" |
| 153 | + let is_expected = match-version $bin_name |
| 154 | + if ($is_expected == false) { |
| 155 | + let bin_name = "clang-format" |
| 156 | + let is_expected = match-version $bin_name --throw |
| 157 | + $bin_name |
| 158 | + } else { |
| 159 | + $bin_name |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if ($files | length) > 0 { |
| 164 | + print $files |
| 165 | + let elapsed = timeit {|| $files | par-each {|f| ^$bin_name "-i" "--style" "file" $f}} |
| 166 | + print $"clang-format took ($elapsed) using parallelism!" |
| 167 | + } |
| 168 | + print $"(ansi blue)Applied clang-format to ($files | length) files(ansi reset)" |
| 169 | +} |
0 commit comments