summaryrefslogtreecommitdiffstats
path: root/_testing/travis/update-scripts-yaml.pl
blob: e554f3359423bbbca5e5e174a3bf53d9dde1ce01 (plain)
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
use strict; use warnings;
use YAML::Tiny 1.59;

my $config = YAML::Tiny::LoadFile('_testing/config.yml');
my @yaml_keys;
if ($config) {
    @yaml_keys = @{ $config->{scripts_yaml_keys}//[] };
}
die "no keys defined in config.yaml\n" unless @yaml_keys;

my @docs;
{ open my $ef, '<:utf8', '_data/scripts.yaml' or die $!;
  @docs = Load(do { local $/; <$ef> });
}

my %oldmeta;
for (@{$docs[0]//[]}) {
    $oldmeta{$_->{filename}} = $_;
}

my %newmeta;
for my $file (<scripts/*.pl>) {
    my ($filename, $base) =
	$file =~ m,^scripts/((.*)\.pl)$,;
    my $info_file = "Test/$base/info.yml";
    my @cdoc;
    if (-f $info_file && open my $ef, '<:utf8', $info_file) {
	local $@;
	@cdoc = eval { Load(do { local $/; <$ef> }); };
	if ($@) {
	    print "ERROR $base: $@\n";
	    @cdoc=();
	}
    }
    if (@cdoc) {
	$newmeta{$filename} = $cdoc[0][0];
	for my $copykey (qw(modified version)) {
	    unless (defined $newmeta{$filename}{$copykey}) {
		$newmeta{$filename}{$copykey}
		    = $oldmeta{$filename}{$copykey}
			if defined $oldmeta{$filename}{$copykey};
	    }
	}
	$newmeta{$filename}{filename} = $filename;
	my $modules = delete $newmeta{$filename}{modules};
	$newmeta{$filename}{modules}
	    = join ' ', @$modules
		if 'ARRAY' eq ref $modules;
	my $commands = delete $newmeta{$filename}{commands};
	my @commands = grep { !/ / } @$commands
		if 'ARRAY' eq ref $commands;
	$newmeta{$filename}{commands} = "@commands"
	    if @commands;
    }
    elsif (exists $oldmeta{$filename}) {
	print "META-INF FOR $base NOT FOUND\n";
	system "ls 'Test/$base/'*";
	$newmeta{$filename} = $oldmeta{$filename};
    }
    else {
	print "MISSING META FOR $base\n";
    }
}
my @newdoc = map {
    my $v = $newmeta{$_};
    +{
        map {
            exists $v->{$_}
                ? ($_ => $v->{$_})
                : ()
        } sort @yaml_keys
    }
} sort keys %newmeta;
YAML::Tiny::DumpFile('_data/scripts.yaml', \@newdoc);

if ($config && @{$config->{whitelist}//[]}) {
    my $changed;
    my @wl;
    for my $sf (@{$config->{whitelist}}) {
	if (-s "Test/$sf:passed") {
	    $changed = 1;
	}
	else {
	    push @wl, $sf;
	}
    }
    if ($changed) {
	$config->{whitelist} = \@wl;
	YAML::Tiny::DumpFile('_testing/config.yml', $config);
    }
}

if (exists $ENV{REPO_LOGIN_TOKEN} && exists $ENV{TRAVIS_REPO_SLUG}) {
    { open my $cred, '>', "$ENV{HOME}/.git-credentials" or die $!;
      print $cred "https://$ENV{REPO_LOGIN_TOKEN}:x-oauth-basic\@github.com\n";
    }
    system qq[
git config user.email "scripts\@irssi.org"
git config user.name "Irssi Scripts Helper"
git config credential.helper store
git config remote.origin.url https://github.com/$ENV{TRAVIS_REPO_SLUG}
git checkout '$ENV{TRAVIS_BRANCH}'
if [ "\$(git log -1 --format=%an)" != "\$(git config user.name)" -a \\
     "\$(git log -1 --format=%cn)" != "\$(git config user.name)" ]; then
    git add _data/scripts.yaml
    git commit -m 'automatic scripts database update for $ENV{TRAVIS_COMMIT}

[skip ci]'
    git config push.default simple
    git push origin
fi
];
}