forked from vircadia/vircadia-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaint
executable file
·169 lines (123 loc) · 3.6 KB
/
maint
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
#!/usr/bin/perl -w
use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
use Getopt::Long;
use Data::Dumper qw(Dumper);
use VircadiaBuilder::Common;
my ($cmd_help, $cmd_cleanup, $cmd_compare);
my $confpath = "$FindBin::Bin/distros";
$Data::Dumper::Sortkeys=1;
$Data::Dumper::Terse=1;
$| = 1;
GetOptions(
"cleanup" => \$cmd_cleanup,
"compare" => \$cmd_compare,
"help" => \$cmd_help,
);
if ( $cmd_cleanup ) {
foreach my $file (@ARGV) {
info("Processing $file... ");
info("reading... ");
my $orig_data = read_file($file);
my $d;
info("parsing... ");
eval {
$d = eval($orig_data);
};
if ( $@ ) {
fatal("Failed to parse $file: $@");
}
info("cleaning up... ");
$d->{appimage_dependencies} = [sort { $a cmp $b } @{$d->{appimage_dependencies}}] if (exists($d->{appimage_dependencies}));
$d->{qt_patches} = [sort { $a cmp $b } @{$d->{qt_patches}}] if (exists($d->{qt_patches}));
$d->{qt_source_dependencies} = [sort { $a cmp $b } @{$d->{qt_source_dependencies}}] if (exists($d->{qt_source_dependencies}));
$d->{source_dependencies} = [sort { $a cmp $b } @{$d->{source_dependencies}}] if (exists($d->{source_dependencies}));
my $new_data = "#!/usr/bin/perl\n" . Dumper($d);
if ( $orig_data ne $new_data ) {
important("changes made, writing... ");
open(my $fh, ">", $file) or die "Can't create $file: $!";
print $fh "#!/usr/bin/perl\n";
print $fh Dumper($d);
close $fh;
important("done.\n");
} else {
info_ok("no cleanup needed, all good.\n");
}
}
} elsif ( $cmd_compare ) {
my ($file_a, $file_b) = @ARGV;
my $file_a_content = read_file($file_a);
my $file_b_content = read_file($file_b);
my $d_a = eval($file_a_content);
my $d_b = eval($file_b_content);
# First list all scalars
foreach my $field (sort grep({ ref($d_a->{$_}) ne "ARRAY" } keys %$d_a)) {
compare($d_a, $d_b, $field);
}
# Then all arrays. Just for the sake of readability.
foreach my $field (sort grep({ ref($d_a->{$_}) eq "ARRAY" } keys %$d_a)) {
compare($d_a, $d_b, $field);
}
} elsif ( $cmd_help ) {
show_help();
}
sub compare {
my ($a, $b, $field) = @_;
info("$field" . (" " x (30-length($field))) . ": ");
if ( ref($a->{$field}) eq "ARRAY" ) {
my @removed = missing_in_b($a->{$field}, $b->{$field});
my @added = missing_in_b($b->{$field}, $a->{$field});
if ( @added || @removed ) {
info("\n");
}
if ( @added ) {
important("\tAdded : ");
important(join(', ', @added) . "\n");
}
if ( @removed ) {
important("\tRemoved: ");
warning(join(', ', @removed) . "\n");
}
unless (@added || @removed) {
info_ok("same\n");
}
} else {
if ( $a->{$field} eq $b->{$field} ) {
info_ok("same\n");
} else {
important("'$a->{$field}' => '$b->{$field}'\n");
}
}
}
sub missing_in_b {
my ($arr_a, $arr_b) = @_;
my %map_b = map { $_ => 1 } @$arr_b;
my @missing;
foreach my $val (@$arr_a) {
push @missing, $val unless exists $map_b{$val};
}
return sort @missing;
}
sub read_file {
my ($file) = @_;
open(my $fh, "<", $file) or die "Can't open $file: $!";
local $/;
undef $/;
my $data = <$fh>;
close $fh;
return $data;
}
sub show_help {
print <<HELP;
$0 [options]
Performs maintainance operations for VircadiaBuilder distro files.
This tool is intended to make it easier to develop support for new distros,
and to make it easier to submit cleaner patches.
Options:
--cleanup FILES Runs cleanup on all the files, replacing
them with indented, sorted versions.
--compare FILEA FILEB Compares configurations FILEA and FILEB.
--help Shows this text.
HELP
}