-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtoolkit.nu
72 lines (67 loc) · 2.57 KB
/
toolkit.nu
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
# Prints input string with highlighted span
#
# Useful for debugging parser output
export def span [span_start: int, span_end: int]: string -> nothing {
let s = $in | encode utf8
let pre = $s | bytes at ..($span_start - 1) | decode
let highlighted = $s | bytes at $span_start..($span_end - 1) | decode
let post = $s | bytes at ($span_end).. | decode
print ($pre + $'(ansi ru)($highlighted)(ansi reset)' + $post)
}
# Runs the parser on all nu scripts for given list of files and directories.
# When directory is passed, all *.nu scripts are found recursively.
#
# Returns a table with a row for every file that was parsed, the exit_code of
# the parser and the time it took to parse the source.
#
# Example usage:
# $ parse-all example.nu ../nu_scripts/
export def parse-all [
--verbose (-v)=false # Print the file name before parsing (useful when parser hangs)
--binary (-b)='./target/debug/new-nu-parser' # The path to new-nu-parser binary
...sources: string # The list of nu scripts and directories to parse
] (nothing -> list<record<file: string, exit_code: int, time: duration>>) {
let files = $sources | each { |$source|
if ($source | path type) == "dir" {
glob ($source | path join "**/*.nu")
} else {
[$source]
}
} | flatten
$files | each { |file|
if $verbose {
print $"Analyzing ($file) ..."
}
mut output = {}
let time = timeit {
let ret = (^$"($binary)" $file | complete)
$output = $ret
}
{
file: $file,
exit_code: $output.exit_code
time: $time,
}
}
}
# Summarizes the output produced by `parse-all` command.
#
# Example usage:
# $ parse-all ../nu_scripts/ | summary
export def summary [] (list<record<file: string, exit_code: int, time: duration>> -> string) {
let report = $in;
let errors = $report | where exit_code == 1
let crashes = $report | where exit_code != 0 and exit_code != 1
let slowest = $report | sort-by time | last
print $"Total files: ($report | length)"
print $"Errors: ($report | where exit_code == 1 | length)"
print $"Crashes: ($report | where exit_code != 1 and exit_code != 0 | length)"
print $"Successes: ($report | where exit_code == 0 | length)"
if ($errors | length) > 0 {
print $"Sample error file: ($errors | first | get file)"
}
if ($crashes | length) > 0 {
print $"Sample crash file: ($crashes | first | get file)"
}
print $"Slowest file: ($slowest | get time) ($slowest | get file)"
}