| 1 | package Dist::Zilla::Plugin::MetaResourcesFromGit; | ||
| 2 | BEGIN { | ||
| 3 | $Dist::Zilla::Plugin::MetaResourcesFromGit::VERSION = '1.103620'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | extends 'Dist::Zilla::Plugin::MetaResources'; | ||
| 8 | |||
| 9 | # both available due to Dist::Zilla | ||
| 10 | use Path::Class 'dir'; | ||
| 11 | use Config::INI::Reader; | ||
| 12 | |||
| 13 | our %transform = ( | ||
| 14 | 'lc' => sub { lc shift }, | ||
| 15 | 'uc' => sub { uc shift }, | ||
| 16 | deb => sub { 'lib'. (lc shift) .'-perl' }, | ||
| 17 | '' => sub { shift }, | ||
| 18 | ); | ||
| 19 | |||
| 20 | use String::Formatter method_stringf => { | ||
| 21 | -as => '_format_string', | ||
| 22 | codes => { | ||
| 23 | a => sub { $_[0]->_github->{'account'} }, | ||
| 24 | r => sub { $_[0]->_github->{'project'} }, | ||
| 25 | N => sub { $transform{$_[1] || ''}->( $_[0]->name ) }, | ||
| 26 | }, | ||
| 27 | }; | ||
| 28 | |||
| 29 | has name => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Str', | ||
| 32 | lazy => 1, | ||
| 33 | default => sub { $_[0]->zilla->name }, | ||
| 34 | ); | ||
| 35 | |||
| 36 | has remote => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Str', | ||
| 39 | default => 'origin', | ||
| 40 | ); | ||
| 41 | |||
| 42 | has homepage => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Str', | ||
| 45 | default => 'https://github.com/%a/%r/wiki', | ||
| 46 | ); | ||
| 47 | |||
| 48 | has bugtracker_web => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Str', | ||
| 51 | default => 'https://rt.cpan.org/Public/Dist/Display.html?Name=%N', | ||
| 52 | ); | ||
| 53 | |||
| 54 | has repository_url => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => 'Str', | ||
| 57 | default => 'git://github.com/%a/%r.git', | ||
| 58 | ); | ||
| 59 | |||
| 60 | has repository_web => ( | ||
| 61 | is => 'ro', | ||
| 62 | isa => 'Str', | ||
| 63 | default => 'https://github.com/%a/%r', | ||
| 64 | ); | ||
| 65 | |||
| 66 | has _github => ( | ||
| 67 | is => 'ro', | ||
| 68 | isa => 'HashRef[Str]', | ||
| 69 | lazy_build => 1, | ||
| 70 | ); | ||
| 71 | |||
| 72 | sub _build__github { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $root = dir('.git'); | ||
| 76 | my $ini = $root->file('config'); | ||
| 77 | |||
| 78 | die "GitHubMeta: need a .git/config file, and you don't have one\n" | ||
| 79 | unless -e $ini; | ||
| 80 | |||
| 81 | my $fh = $ini->openr; | ||
| 82 | my $config = Config::INI::Reader->read_handle($fh); | ||
| 83 | |||
| 84 | my $remote = $self->remote; | ||
| 85 | die "GitHubMeta: no '$remote' remote found in .git/config\n" | ||
| 86 | unless exists $config->{qq{remote "$remote"}}; | ||
| 87 | |||
| 88 | my $url = $config->{qq{remote "$remote"}}->{'url'}; | ||
| 89 | die "GitHubMeta: no url found for remote '$remote'\n" | ||
| 90 | unless $url and length $url; | ||
| 91 | |||
| 92 | my ($account, $project) = ($url =~ m{[:/](.+)/(.+)\.git$}); | ||
| 93 | |||
| 94 | die "GitHubMeta: no github account name found in .git/config\n" | ||
| 95 | unless $account and length $account; | ||
| 96 | die "GitHubMeta: no github repository (project) found in .git/config\n" | ||
| 97 | unless $project and length $project; | ||
| 98 | |||
| 99 | return { account => $account, project => $project }; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub BUILD { | ||
| 103 | my ($self, $params) = @_; | ||
| 104 | |||
| 105 | if (eval {exists $params->{resources}->{homepage}}) { | ||
| 106 | # if param is customized by the user, format it | ||
| 107 | $params->{resources}->{homepage} | ||
| 108 | &&= _format_string($params->{resources}->{homepage}, $self); | ||
| 109 | # else user has asked for param to be disabled | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | # use our default | ||
| 113 | $params->{resources}->{homepage} | ||
| 114 | = _format_string($self->homepage, $self); | ||
| 115 | } | ||
| 116 | |||
| 117 | if (eval {exists $params->{resources}->{bugtracker}->{web}}) { | ||
| 118 | $params->{resources}->{bugtracker}->{web} | ||
| 119 | &&= _format_string($params->{resources}->{bugtracker}->{web}, $self); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | $params->{resources}->{bugtracker}->{web} | ||
| 123 | = _format_string($self->bugtracker_web, $self); | ||
| 124 | } | ||
| 125 | |||
| 126 | if (eval {exists $params->{resources}->{repository}->{url}}) { | ||
| 127 | $params->{resources}->{repository}->{url} | ||
| 128 | &&= _format_string($params->{resources}->{repository}->{url}, $self); | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | $params->{resources}->{repository}->{url} | ||
| 132 | = _format_string($self->repository_url, $self); | ||
| 133 | } | ||
| 134 | |||
| 135 | if (eval {$params->{resources}->{repository}->{url}}) { | ||
| 136 | $params->{resources}->{repository}->{type} = 'git'; | ||
| 137 | |||
| 138 | if (eval {exists $params->{resources}->{repository}->{web}}) { | ||
| 139 | $params->{resources}->{repository}->{web} | ||
| 140 | &&= _format_string($params->{resources}->{repository}->{web}, $self); | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | $params->{resources}->{repository}->{web} | ||
| 144 | = _format_string($self->repository_web, $self); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | return $params; | ||
| 149 | } | ||
| 150 | |||
| 151 | no Moose; | ||
| 152 | __PACKAGE__->meta->make_immutable; | ||
| 153 | 1; | ||
| 154 | |||
| 155 | # ABSTRACT: Metadata resource URLs from Git configuration | ||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | __END__ |