forked from check-spelling/check-spelling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-state.sh
executable file
·96 lines (96 loc) · 3.18 KB
/
update-state.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
#!/bin/bash
Q='"'
q="'"
B='```'
b='`'
n="
"
N="$n$n"
strip_lead() {
perl -ne 's/^\s+(\S)/$1/; print'
}
strip_blanks() {
perl -ne 'next unless /./; print'
}
strip_lead_and_blanks() {
strip_lead | strip_blanks
}
generate_instructions() {
instructions=$(mktemp)
if [ -z "$skip_wrapping" ]; then
echo '(cd $(git rev-parse --show-toplevel)' >> "$instructions"
to_retrieve_expect >> "$instructions"
fi
if [ -s "$instructions_preamble" ]; then
cat "$instructions_preamble" >> "$instructions"
fi
if [ -n "$patch_remove" ]; then
if [ -z "$expect_files" ]; then
expect_files="$expect_file"
fi
echo 'perl -e '"$q"'
my @expect_files=qw('"$q$Q$expect_files$Q$q"');
@ARGV=@expect_files;
my @stale=qw('"$q$Q$patch_remove$Q$q"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">", $ARGV); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'"$q" |
strip_lead >> "$instructions"
fi
if [ -n "$patch_add" ]; then
echo 'perl -e '"$q"'
my $new_expect_file="'"$new_expect_file"'";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$q$Q$patch_add$Q$q"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'"$q" |
strip_lead >> "$instructions"
fi
if [ -n "$should_exclude_patterns" ]; then
echo "(cat $q$excludes_file$q - 2> /dev/null <<EOF
$should_exclude_patterns
EOF
) |grep .|
sort -f |
uniq > $q$excludes_file.temp$q &&
mv $q$excludes_file.temp$q $q$excludes_file$q" | strip_lead >> "$instructions"
fi
if [ -z "$skip_wrapping" ]; then
echo ')' >> "$instructions"
fi
echo "$instructions"
}
patch_variables() {
if [ -n "$patch_remove" ]; then
echo '
patch_remove=$(perl -ne '"$q"'next unless s{^</summary>(.*)</details>$}{$1}; print'"$q"' < '"$1"')
' | strip_lead
fi
if [ -n "$patch_add" ]; then
echo '
patch_add=$(perl -e '"$q"'$/=undef; $_=<>; if (m{Unrecognized words[^<]*</summary>\n*'"$B"'\n*([^<]*)'"$B"'\n*</details>$}m) { print "$1" } elsif (m{Unrecognized words[^<]*\n\n((?:\w.*\n)+)\n}m) { print "$1" };'"$q"' < '"$1"')
' | strip_lead
fi
if [ -n "$should_exclude_patterns" ]; then
echo '
should_exclude_patterns=$(perl -e '"$q"'$/=undef;
$_=<>;
exit unless s{(?:You should consider excluding directory paths|You should consider adding them to).*}{}s;
s{.*These sample patterns would exclude them:}{}s;
s{.*\`\`\`([^`]*)\`\`\`.*}{$1}m;
print'"$q"' < '"$1"' | grep . || true)
' | strip_lead
fi
}