| 1 | package Dist::Inkt::Role::WriteLICENSE; | ||
| 2 | |||
| 3 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 4 | our $VERSION = '0.026'; | ||
| 5 | |||
| 6 | use Moose::Role; | ||
| 7 | use Software::LicenseUtils; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | after BUILD => sub { | ||
| 11 | my $self = shift; | ||
| 12 | unshift @{ $self->targets }, 'LICENSE'; | ||
| 13 | }; | ||
| 14 | |||
| 15 | # I've submitted this hash as a patch for Software::LicenseUtils. | ||
| 16 | # Maybe I won't need to hard-code it here. | ||
| 17 | # | ||
| 18 | my %meta_keys = ( | ||
| 19 | ## CPAN::Meta::Spec 2.0 | ||
| 20 | ## | ||
| 21 | agpl_3 => 'AGPL_3', | ||
| 22 | apache_1_1 => 'Apache_1_1', | ||
| 23 | apache_2_0 => 'Apache_2_0', | ||
| 24 | artistic_1 => 'Artistic_1_0', | ||
| 25 | artistic_2 => 'Artistic_2_0', | ||
| 26 | bsd => 'BSD', | ||
| 27 | freebsd => 'FreeBSD', | ||
| 28 | gfdl_1_2 => 'GFDL_1_2', | ||
| 29 | gfdl_1_3 => 'GFDL_1_3', | ||
| 30 | gpl_1 => 'GPL_1', | ||
| 31 | gpl_2 => 'GPL_2', | ||
| 32 | gpl_3 => 'GPL_3', | ||
| 33 | lgpl_2_1 => 'LGPL_2_1', | ||
| 34 | lgpl_3_0 => 'LGPL_3_0', | ||
| 35 | mit => 'MIT', | ||
| 36 | mozilla_1_0 => 'Mozilla_1_0', | ||
| 37 | mozilla_1_1 => 'Mozilla_1_1', | ||
| 38 | openssl => 'OpenSSL', | ||
| 39 | perl_5 => 'Perl_5', | ||
| 40 | qpl_1_0 => 'QPL_1_0', | ||
| 41 | ssleay => 'SSLeay', | ||
| 42 | sun => 'Sun', | ||
| 43 | zlib => 'Zlib', | ||
| 44 | # open_source | ||
| 45 | restricted => 'None', | ||
| 46 | # unrestricted | ||
| 47 | # unknown | ||
| 48 | |||
| 49 | ## META-spec 1.4 | ||
| 50 | ## | ||
| 51 | apache => [ map { "Apache_$_" } qw(1_1 2_0) ], | ||
| 52 | # apache_1_1 | ||
| 53 | perl => 'Perl_5', | ||
| 54 | artistic => 'Artistic_1_0', | ||
| 55 | # artistic_2 | ||
| 56 | # bsd | ||
| 57 | gpl => [ map { "GPL_$_" } qw(1 2 3) ], | ||
| 58 | lgpl => [ map { "LGPL_$_" } qw(2_1 3_0) ], | ||
| 59 | # mit | ||
| 60 | mozilla => [ map { "Mozilla_$_" } qw(1_0 1_1 2_0) ], | ||
| 61 | # open_source | ||
| 62 | restrictive => 'None', | ||
| 63 | # unrestricted | ||
| 64 | # unknown | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub Build_LICENSE | ||
| 68 | { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $file = $self->targetfile('LICENSE'); | ||
| 72 | $file->exists and return $self->log('Skipping %s; it already exists', $file); | ||
| 73 | $self->log('Writing %s', $file); | ||
| 74 | |||
| 75 | my $L = $self->metadata->{license}; | ||
| 76 | unless (@{ $L || [] }==1) | ||
| 77 | { | ||
| 78 | $self->log('WARNING: did not find exactly one licence; found %d', scalar(@{ $L || [] })); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $class; | ||
| 83 | unless ($class = $meta_keys{$L->[0]}) | ||
| 84 | { | ||
| 85 | $self->log("WARNING: could not grok licence '%s'", @$L); | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | if (ref $class) | ||
| 90 | { | ||
| 91 | $self->log("WARNING: ambiguous licence '%s'", @$L); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $holders = Moose::Util::english_list( | ||
| 96 | $self->can('doap_project') | ||
| 97 | ? map($_->to_string('compact'), @{$self->doap_project->maintainer}) | ||
| 98 | : @{$self->metadata->{author}} | ||
| 99 | ); | ||
| 100 | |||
| 101 | $class = "Software::License::$class"; | ||
| 102 | eval "require $class;"; | ||
| 103 | my $licence = $class->new({ | ||
| 104 | year => [localtime]->[5] + 1900, | ||
| 105 | holder => $holders, | ||
| 106 | }); | ||
| 107 | |||
| 108 | $file->spew_utf8( $licence->fulltext ); | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |