3 # InspIRCd -- Internet Relay Chat Daemon
5 # Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
7 # This file is part of InspIRCd. InspIRCd is free software: you can
8 # redistribute it and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation, version 2.
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 use warnings FATAL => qw(all);
25 use File::Basename qw(basename dirname);
26 use File::Copy qw(move);
27 use File::Spec::Functions qw(abs2rel catdir catfile);
28 use File::Temp qw(tempdir);
29 use FindBin qw($RealDir);
30 use POSIX qw(strftime);
31 use TOML qw(from_toml);
33 use lib dirname $RealDir;
37 my $config = catfile $RealDir, 'update.toml';
38 open(my $fh, $config) or print_error "unable to read $config: $!";
39 my $contents = do { local $/; <$fh> };
42 my ($data, $error) = from_toml $contents;
43 print_error "unable to parse $config: $!" if $error;
45 while (my ($name, $info) = each %{$data}) {
46 say console_format "Updating <|GREEN $name|> ...";
48 my $unpackdir = File::Temp->newdir;
49 my $vendordir = catdir $RealDir, $name;
51 if (defined $info->{git}) {
52 $success ||= execute 'git', 'clone', $info->{git}, $unpackdir;
53 chomp(my $tag = `git -C $unpackdir describe --abbrev=0 --tags HEAD 2>/dev/null`) unless $success;
54 $success ||= execute 'git', '-C', $unpackdir, 'checkout', $tag if $tag;
55 chomp($info->{version} = `git -C $unpackdir describe --always --tags HEAD 2>/dev/null`);
56 } elsif (defined $info->{tarball}) {
57 my $tarball = catfile $unpackdir, basename $info->{tarball};
58 $success ||= execute 'wget', '--output-document', $tarball, $info->{tarball};
59 $success ||= execute 'tar', 'fx', $tarball, '-C', $unpackdir, '--strip-components', 1;
61 print_error "unable to update $name; no git or tarball specified!";
63 print_error "unable to update $name: download failed!" if $success;
66 my $glob = $info->{files} or print_error "unable to update $name: no file glob specified!";
67 for my $file (glob catfile $unpackdir, $glob) {
68 my $pathname = abs2rel $file, $unpackdir;
69 for (my $i = 0; $i < ($info->{depth} // 0); ++$i) {
70 $pathname =~ s/[^\/]+\///;
72 my $filename = catfile $vendordir, $pathname;
73 my $dirname = dirname $filename;
74 create_directory $dirname, 0750;
75 move $file, $filename;
79 my $readme = catfile $RealDir, 'README.md';
80 open($fh, $readme) or print_error "unable to read $readme: $!";
81 $contents = do { local $/; <$fh> };
84 open($fh, '>', $readme) or print_error "unable to write $readme: $!";
85 print $fh $contents =~ s/\n\#\#.*//rs;
86 for my $name (sort keys %{$data}) {
87 my $info = $data->{$name};
88 printf $fh "\n## %s\n\n", $name;
89 printf $fh "**Author** — [%s](mailto:%s)\n\n", $info->{author}, $info->{email} if $info->{email};
90 printf $fh "**Author** — %s\n\n", $info->{author} unless $info->{email};
91 printf $fh "**License** — %s\n\n", $info->{license};
92 printf $fh "**Version** — %s\n\n", $info->{version};
93 my $website = $info->{website} // $info->{git};
94 printf $fh "**Website** — [%s](%s)\n", $website, $website;