|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use 5.006002; |
| 4 | + |
| 5 | +use strict; |
| 6 | +use warnings; |
| 7 | + |
| 8 | +use Getopt::Long; |
| 9 | +use version; |
| 10 | + |
| 11 | +use constant THIS_PERL => version->parse( $] ); |
| 12 | +use constant PERL_5_10 => version->parse( 'v5.10.0' ); |
| 13 | +use constant PERL_5_10_1 => version->parse( 'v5.10.1' ); |
| 14 | +use constant PERL_5_12 => version->parse( 'v5.12.0' ); |
| 15 | + |
| 16 | +unless ( caller ) { |
| 17 | + my %opt; |
| 18 | + |
| 19 | + GetOptions( \%opt, |
| 20 | + qw{ verbose }, |
| 21 | + ) or die "Bad option\n"; |
| 22 | + |
| 23 | + |
| 24 | + my $env_hash = compute_environment(); |
| 25 | + |
| 26 | + my $env_text; |
| 27 | + $env_text .= "$_=$env_hash->{$_}\n" for sort keys %{ $env_hash }; |
| 28 | + |
| 29 | + $opt{verbose} and print $env_text; |
| 30 | + |
| 31 | + defined $ENV{GITHUB_ENV} |
| 32 | + and $ENV{GITHUB_ENV} ne '' |
| 33 | + or die "Environment variable GITHUB_ENV undefined or empty\n"; |
| 34 | + open my $fh, '>>:encoding(utf-8)', $ENV{GITHUB_ENV} |
| 35 | + or die "Can not open $ENV{GITHUB_ENV}: $!\n"; |
| 36 | + |
| 37 | + print { $fh } $env_text; |
| 38 | + |
| 39 | + close $fh; |
| 40 | +} |
| 41 | + |
| 42 | +sub compute_environment { |
| 43 | + my $is_windows = { |
| 44 | + MSWin32 => 1, |
| 45 | + dos => 1, |
| 46 | + }->{$^O} || ''; |
| 47 | + my $is_unix = $is_windows ? '' : 1; |
| 48 | + my $my_home; |
| 49 | + { |
| 50 | + local $@ = undef; |
| 51 | + eval { |
| 52 | + require File::HomeDir; |
| 53 | + $my_home = File::HomeDir->my_home(); |
| 54 | + print "Home from File::HomeDir\n"; |
| 55 | + 1; |
| 56 | + } or do { |
| 57 | + $my_home = $ENV{HOME}; |
| 58 | + print "Home from \$HOME\n"; |
| 59 | + }; |
| 60 | + } |
| 61 | + my @want_modules = want_modules(); |
| 62 | + my %env = ( |
| 63 | + MY_HOME => $my_home, |
| 64 | + MY_IS_GITHUB_ACTION => 1, |
| 65 | + MY_IS_UNIX => $is_unix, |
| 66 | + MY_IS_WINDOWS => $is_windows, |
| 67 | + MY_TOOLCHAIN_EUMM => -e 'Makefile.PL' ? 1 : '', |
| 68 | + MY_TOOLCHAIN_MB => -e 'Build.PL' ? 1 : '', |
| 69 | + MY_WANT_MODULES => "@want_modules", |
| 70 | + ); |
| 71 | + |
| 72 | + $is_windows |
| 73 | + and @env{ qw{ LINES COLUMNS } } = ( 24, 80 ); |
| 74 | + |
| 75 | + return \%env; |
| 76 | +} |
| 77 | + |
| 78 | +# FIXME Not all my distros need HTML-Tagset. Figure out a way to handle |
| 79 | +# this other than by bashing this script. This is not straightforward |
| 80 | +# because HTML-Tagset is probably not a direct dependency. |
| 81 | +sub want_modules { |
| 82 | + my ( $perl_ver ) = @_; |
| 83 | + defined $perl_ver |
| 84 | + or $perl_ver = THIS_PERL; |
| 85 | + if ( $perl_ver >= PERL_5_12 ) { |
| 86 | + return; |
| 87 | + } elsif ( $perl_ver >= PERL_5_10_1 ) { |
| 88 | + # NOTE that, thus far, XSLoader, Test-Deep, and Perl-Critic are |
| 89 | + # only needed for Perl-Critic policies, and maybe not all of |
| 90 | + # those. |
| 91 | + # NOTE that, thus far, HTML-Tagset is only needed for modules |
| 92 | + # that make use of libwww-perl, directly or indirectly. |
| 93 | + return qw{ |
| 94 | + RRA/podlators-4.14.tar.gz |
| 95 | + }; |
| 96 | + } elsif ( $perl_ver >= PERL_5_10 ) { |
| 97 | + return qw{ |
| 98 | + RRA/podlators-4.14.tar.gz |
| 99 | + }; |
| 100 | + } else { |
| 101 | + return qw{ |
| 102 | + SMUELLER/ExtUtils-ParseXS-3.30.tar.gz |
| 103 | + RRA/podlators-4.14.tar.gz |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +1; |
| 109 | + |
| 110 | +__END__ |
| 111 | +
|
| 112 | +=head1 TITLE |
| 113 | +
|
| 114 | +environment.PL - Customize GitHub Actions environment |
| 115 | +
|
| 116 | +=head1 SYNOPSIS |
| 117 | +
|
| 118 | + .github/workflows/environment.PL |
| 119 | + .github/workflows/environment.PL --verbose |
| 120 | +
|
| 121 | +=head1 OPTIONS |
| 122 | +
|
| 123 | +=head2 --verbose |
| 124 | +
|
| 125 | +If this Boolean option is asserted, the environment variables defiend |
| 126 | +are written to standard output. |
| 127 | +
|
| 128 | +The default is C<--no-verbose>. |
| 129 | +
|
| 130 | +=head1 DETAILS |
| 131 | +
|
| 132 | +This Perl script adds environment variables to the GitHub Actions |
| 133 | +environment. The following environment variables are added: |
| 134 | +
|
| 135 | +=head2 MY_HOME |
| 136 | +
|
| 137 | +The job's home directory, as determined by |
| 138 | +L<File::HomeDir|File::HomeDir>. |
| 139 | +
|
| 140 | +=head2 MY_IS_GITHUB_ACTION |
| 141 | +
|
| 142 | +Always true (i.e. C<'1'>). |
| 143 | +
|
| 144 | +=head2 MY_IS_UNIX |
| 145 | +
|
| 146 | +True (i.e. C<1>) if running under some flavor of Unix, and false (i.e. |
| 147 | +C<''>) otherwise. At the moment this is the Boolean inverse of |
| 148 | +L<MY_IS_WINDOWS|/MY_IS_WINDOWS>. |
| 149 | +
|
| 150 | +=head2 MY_IS_WINDOWS |
| 151 | +
|
| 152 | +True (i.e. C<1>) if running under Windows, and false (i.e. C<''>) |
| 153 | +othewise. At the moment this is true if C<$^O> is C<'MSWin32'> or |
| 154 | +C<'dos'>. |
| 155 | +
|
| 156 | +=head2 MY_WANT_MODUKES |
| 157 | +
|
| 158 | +A space-delimited list of modules or repositories to install B<before> |
| 159 | +installing dependencies. This is computed in an admittedly ad-hoc manner |
| 160 | +based on the version of Perl being used, and is intended to cover the |
| 161 | +case where old Perls (typically before C<5.10> can not execute the |
| 162 | +current version of a module. |
| 163 | +
|
| 164 | +=head1 INVOCATION AS MODULINO |
| 165 | +
|
| 166 | +This script can also be used (or required) as a modulino. When you do |
| 167 | +this, the following subroutines get loaded into C<main::>: |
| 168 | +
|
| 169 | +=head2 compute_environment |
| 170 | +
|
| 171 | +This subroutine takes no arguments. It returns a reference to a hash |
| 172 | +that describes the environment variables to be added to the environment. |
| 173 | +
|
| 174 | +=head2 want_modules |
| 175 | +
|
| 176 | +This subroutine takes an optional L<version|version> object representing |
| 177 | +a Perl version, and returns a list of modules or distributions to be |
| 178 | +installed that version B<before> module prerequisites are |
| 179 | +processed. If no argument is provided, the version of the |
| 180 | +currently-running Perl is assumed. |
| 181 | +
|
| 182 | +=head1 AUTHOR |
| 183 | +
|
| 184 | +Thomas R. Wyant, III F<wyant at cpan dot org> |
| 185 | +
|
| 186 | +=head1 COPYRIGHT AND LICENSE |
| 187 | +
|
| 188 | +Copyright (C) 2022 by Thomas R. Wyant, III |
| 189 | +
|
| 190 | +This program is free software; you can redistribute it and/or modify it |
| 191 | +under the same terms as Perl 5.10.0. For more details, see the full text |
| 192 | +of the licenses in the directory LICENSES. |
| 193 | +
|
| 194 | +This program is distributed in the hope that it will be useful, but |
| 195 | +without any warranty; without even the implied warranty of |
| 196 | +merchantability or fitness for a particular purpose. |
| 197 | +
|
| 198 | +=cut |
| 199 | +
|
| 200 | +# ex: set ts=8 sts=4 sw=4 tw=72 ft=perl expandtab shiftround : |
0 commit comments