Debian Codegraph @

script/pandoc-version

raw
1 #!/usr/bin/env perl
2 use v5.14;
3 use Pod::Usage;
4 use Pandoc;
5 use Pandoc::Version;
6 use Pandoc::Release;
7
8 my $verbose = grep /^(-v|--verbose)$/, @ARGV;
9 my @args = grep {!/^(-v|--verbose)$/} @ARGV;
10 pod2usage(-exitval => 0) if grep /^-{0,2}h(elp)?$/, @args;
11
12 my $version = eval { Pandoc::Version->new($args[0]) };
13 my $command = $version ? 'use' : $args[0] // 'version';
14
15 if ($command =~ /^v(ersion)?$/) {
16
17 # TODO: catch if no pandoc installed
18 say pandoc->version;
19
20 } elsif ($command =~ /^l(ist)?$/) {
21
22 # print installed versions
23 my $dir = pandoc_data_dir( 'bin' );
24 opendir(DIR, $dir);
25 my @files = grep { -e "$dir/$_" } grep(/^pandoc-/,readdir(DIR));
26 my @versions = map { /^pandoc-(\d+(\.\d+)*)$/ ? $1 : () } @files;
27 say $_ for reverse sort @versions;
28 closedir(DIR);
29
30 } elsif ($command =~ /^i(nstall)?$/) {
31 my $pandoc;
32
33 $version = Pandoc::Version->new($args[1]) if $args[1];
34 if ($version) {
35 say "Installing release $version of Pandoc...";
36 $pandoc = Pandoc::Release->get($version);
37 } else {
38 say "Installing latest release of Pandoc...";
39 $pandoc = Pandoc::Release->latest;
40 }
41
42 $pandoc->download->symlink( verbose => 1 );
43
44 } elsif ($command =~ /^u(se)?$/) {
45 $version //= eval { Pandoc::Version->new($args[1]) }
46 // die "missing or malformed version number!\n";
47
48 if (my $pandoc = eval { Pandoc->new($version) }) {
49 $pandoc->symlink( verbose => $verbose );
50 } else {
51 say STDERR "Pandoc version $version is not installed!";
52 exit 1;
53 }
54 }
55
56 exit;
57
58 __END__
59
60 =head1 NAME
61
62 pandoc-version - switch pandoc executable
63
64 =head1 SYNOPSIS
65
66 pandoc-version [-v|--verbose] [command] [version]
67
68 Commands:
69 v|version show current version
70 l|list list installed versions
71 u|use switch version
72 i|nstall install latest or specified release
73 h|help show help
74
75 =head1 DESCRIPTION
76
77 This command line can be used to show the current executable version of Pandoc,
78 and to switch installed versions.
79
80 Full usage is limited to Debian based operating systems.
81
82 =head1 SEE ALSO
83
84 L<Pandoc::Release>
85
86 =cut