forked from paritytech/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-duplicate-dependencies.awk
executable file
·44 lines (44 loc) · 1.2 KB
/
find-duplicate-dependencies.awk
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
#!/usr/bin/env -S awk -E
# Find duplicate dependencies in a Cargo.lock file
function do_substr(str, len, end) {
return substr(str, len + 1, length(str) - len - end)
}
function assert(cond, msg) {
if (!cond) {
print msg > "/dev/stderr"
exit 2
}
}
BEGIN {
num_duplicates = 0
for (i = 1; i < ARGC; ++i) {
if (ARGV[i] !~ /^[/.]/) {
ARGV[i] = "./" ARGV[i]
}
}
}
/^name = "[[:alnum:]_-]+"$/ {
pkg_name = do_substr($0, 8, 1)
line_num = NR
next
}
/^version = "[0-9]+\.[0-9]+\.[0-9]+([+-][[:alnum:]_.-]*)?"$/ {
version = do_substr($0, length("version = \""), 1)
assert(NR == line_num + 1, "Found version line at wrong time?")
line_num = 0
if (pkg_name in versions && versions[pkg_name] != version) {
print ("Duplicate package " pkg_name ": found versions " versions[pkg_name] " and " version)
num_duplicates += 1
}
versions[pkg_name] = version
next
}
{ assert(!line_num, "missing version line " NR) }
END {
if (num_duplicates) {
print "\033[1;31mFound", num_duplicates, "duplicate dependencies 😨\033[0m"
exit 1
} else {
print "\033[1;32mYou only depend on one version of each dependency. Yay! 🙂\033[0m"
}
}