summaryrefslogtreecommitdiff
path: root/vendor/update
blob: ca113ff29c29801a7f4297546080026b3bb3be61 (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
#!/usr/bin/env perl
#
# InspIRCd -- Internet Relay Chat Daemon
#
#   Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
#
# This file is part of InspIRCd.  InspIRCd is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


use v5.10.0;
use strict;
use warnings FATAL => qw(all);

use File::Basename        qw(basename dirname);
use File::Copy            qw(move);
use File::Spec::Functions qw(abs2rel catdir catfile);
use File::Temp            qw(tempdir);
use FindBin               qw($RealDir);
use POSIX                 qw(strftime);
use TOML                  qw(from_toml);

use lib dirname $RealDir;
use make::common;
use make::console;

my $config = catfile $RealDir, 'update.toml';
open(my $fh, $config) or print_error "unable to read $config: $!";
my $contents = do { local $/; <$fh> };
close $fh;

my ($data, $error) = from_toml $contents;
print_error "unable to parse $config: $!" if $error;

while (my ($name, $info) = each %{$data}) {
	print_format "Updating <|GREEN $name|> ...\n";

	my $unpackdir = File::Temp->newdir;
	my $vendordir = catdir $RealDir, $name;
	my $success = 0;
	if (defined $info->{git}) {
		$success ||= execute 'git', 'clone', $info->{git}, $unpackdir;
		chomp(my $tag = `git -C $unpackdir describe --abbrev=0 --tags HEAD 2>/dev/null`) unless $success;
		$success ||= execute 'git', '-C', $unpackdir, 'checkout', $tag if $tag;
		chomp($info->{version} = `git -C $unpackdir describe --always --tags HEAD 2>/dev/null`);
	} elsif (defined $info->{tarball}) {
		my $tarball = catfile $unpackdir, basename $info->{tarball};
		$success ||= execute 'wget', '--output-document', $tarball, $info->{tarball};
		$success ||= execute 'tar', 'fx', $tarball, '-C', $unpackdir, '--strip-components', 1;
	} else {
		print_error "unable to update $name; no git or tarball specified!";
	}
	print_error "unable to update $name: download failed!" if $success;

	unlink $vendordir;
	my $glob = $info->{files} or print_error "unable to update $name: no file glob specified!";
	for my $file (glob catfile $unpackdir, $glob) {
		my $pathname = abs2rel $file, $unpackdir;
		for (my $i = 0; $i < ($info->{depth} // 0); ++$i) {
			$pathname =~ s/[^\/]+\///;
		}
		my $filename = catfile $vendordir, $pathname;
		my $dirname = dirname $filename;
		create_directory $dirname, 0750;
		move $file, $filename;
	}
}

my $readme = catfile $RealDir, 'README.md';
open($fh, $readme) or print_error "unable to read $readme: $!";
$contents = do { local $/; <$fh> };
close $fh;

open($fh, '>', $readme) or print_error "unable to write $readme: $!";
print $fh $contents =~ s/\n\#\#.*//rs;
for my $name (sort keys %{$data}) {
	my $info = $data->{$name};
	printf $fh "\n## %s\n\n", $name;
	printf $fh "**Author** &mdash; [%s](mailto:%s)\n\n", $info->{author}, $info->{email} if $info->{email};
	printf $fh "**Author** &mdash; %s\n\n", $info->{author} unless $info->{email};
	printf $fh "**License** &mdash; %s\n\n", $info->{license};
	printf $fh "**Version** &mdash; %s\n\n", $info->{version};
	my $website = $info->{website} // $info->{git};
	printf $fh "**Website** &mdash; [%s](%s)\n", $website, $website;
}
close $fh;