]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/blob - lib/CPANPLUS/Dist/Gentoo.pm
Add an INSTALLATION paragraph to the doc
[perl/modules/CPANPLUS-Dist-Gentoo.git] / lib / CPANPLUS / Dist / Gentoo.pm
1 package CPANPLUS::Dist::Gentoo;
2
3 use strict;
4 use warnings;
5
6 use File::Copy qw/copy/;
7 use File::Path qw/mkpath/;
8 use File::Spec::Functions qw/catdir catfile/;
9
10 use IPC::Cmd qw/run can_run/;
11
12 use CPANPLUS::Error;
13
14 use base qw/CPANPLUS::Dist::Base/;
15
16 =head1 NAME
17
18 CPANPLUS::Dist::Gentoo - CPANPLUS backend generating Gentoo ebuilds.
19
20 =head1 VERSION
21
22 Version 0.02_01
23
24 =cut
25
26 our $VERSION = '0.02_01';
27
28 =head1 SYNOPSIS
29
30     cpan2dist --format=CPANPLUS::Dist::Gentoo \
31               --dist-opts overlay=/usr/local/portage \
32               --dist-opts distdir=/usr/portage/distfiles \
33               --dist-opts manifest=yes \
34               --dist-opts keywords=x86 \
35               Any::Module You::Like
36
37 =head1 DESCRPITON
38
39 This module is a CPANPLUS backend that recursively generates Gentoo ebuilds for a given package in the specified overlay (defaults to C</usr/local/portage>), update the manifest, and even emerge it (together with its dependencies) if the user requires it. You need write permissions on the directory where Gentoo fetches its source files (usually C</usr/portage/distfiles>).
40
41 The generated ebuilds are placed into the section C<perl-gcpanp>. They favour depending on C<perl-core> or C<dev-perl> rather than C<perl-gcpanp>.
42
43 =head1 INSTALLATION
44
45 After installing this module, you should append C<perl-gcpanp> to your F</etc/portage/categories> file.
46
47 =head1 METHODS
48
49 All the methods are inherited from L<CPANPLUS::Dist::Base>. Please refer to its perldoc for precise information on what's done at each step.
50
51 =cut
52
53 use constant CATEGORY => 'perl-gcpanp';
54
55 sub format_available {
56  for my $prog (qw/emerge ebuild/) {
57   unless (can_run($prog)) {
58    error "$prog is required to write ebuilds -- aborting";
59    return 0;
60   }
61  }
62  return 1;
63 }
64
65 sub init {
66  my ($self) = @_;
67  my $stat = $self->status;
68  $stat->mk_accessors(qw/name version dist desc uri src license deps
69                         eb_name eb_version eb_dir eb_file distdir fetched_arch
70                         keywords do_manifest/);
71
72  return 1;
73 }
74
75 my %gentooism = (
76  'Digest'          => 'digest-base',
77  'Locale-Maketext' => 'locale-maketext',
78  'Net-Ping'        => 'net-ping',
79  'PathTools'       => 'File-Spec',
80  'PodParser'       => 'Pod-Parser',
81 );
82
83 sub prepare {
84  my $self = shift;
85  my $mod  = $self->parent;
86  my $stat = $self->status;
87  my $int  = $mod->parent;
88  my $conf = $int->configure_object;
89
90  my %opts = @_;
91
92  my $keywords = delete $opts{'keywords'};
93  $keywords = 'x86' unless defined $keywords;
94  $keywords = [ split ' ', $keywords ];
95  $stat->keywords($keywords);
96
97  my $manifest = delete $opts{'manifest'};
98  $manifest = 1 unless defined $manifest;
99  $manifest = 0 if $manifest =~ /^\s*no?\s*$/i;
100  $stat->do_manifest($manifest);
101
102  my $overlay = catdir(delete($opts{'overlay'}) || '/usr/local/portage',
103                       CATEGORY);
104
105  $stat->distdir(delete($opts{'distdir'}) || '/usr/portage/distfiles');
106  if ($stat->do_manifest && !-w $stat->distdir) {
107   error 'distdir isn\'t writable -- aborting';
108   return 0;
109  }
110  $stat->fetched_arch($mod->status->fetch);
111
112  my $name = $mod->package_name;
113  $stat->name($name);
114
115  my $version = $mod->package_version;
116  $stat->version($version);
117  $stat->dist($name . '-' . $version);
118  my $f = 1;
119  $version =~ s/_+/$f ? do { $f = 0; '_p' } : ''/ge;
120  1 while $version =~ s/(_p[^.]*)\.+/$1/;
121  $stat->eb_version($version);
122
123  $stat->eb_name($gentooism{$stat->name} || $stat->name);
124  $stat->eb_dir(catdir($overlay, $stat->eb_name));
125  $stat->eb_file(catfile($stat->eb_dir,
126                         $stat->eb_name . '-' . $stat->eb_version . '.ebuild'));
127  if (-r $stat->eb_file) {
128   msg 'Ebuild already generated for ' . $stat->dist . ' -- skipping';
129   $stat->prepared(1);
130   $stat->created(1);
131   return 1;
132  }
133
134  $self->SUPER::prepare(%opts);
135
136  my $desc = $mod->description;
137  ($desc = $name) =~ s/-+/::/g unless $desc;
138  $stat->desc($desc);
139  $stat->uri('http://search.cpan.org/dist/' . $name);
140  unless ($name =~ /^([^-]+)/) {
141   error 'Wrong distribution name -- aborting';
142   return 0;
143  }
144  $stat->src('mirror://cpan/modules/by-module/' . $1 . '/' . $mod->package);
145  $stat->license([ qw/Artistic GPL-2/ ]);
146
147  my $prereqs = $mod->status->prereqs;
148  my @depends;
149  for my $prereq (sort keys %$prereqs) {
150   my $obj = $int->module_tree($prereq);
151   unless ($obj) {
152    error 'Wrong module object -- aborting';
153    return 0;
154   }
155   next if $obj->package_is_perl_core;
156   {
157    my $version;
158    if ($prereqs->{$prereq}) {
159     if ($obj->installed_version && $obj->installed_version < $obj->version) {
160      $version = $obj->installed_version;
161     } else {
162      $version = $obj->package_version;
163     }
164    }
165    push @depends, [ $obj , $version ];
166   }
167  }
168  $stat->deps(\@depends);
169
170  return 1;
171 }
172
173 sub create {
174  my $self = shift;
175  my $stat = $self->status;
176
177  unless ($stat->prepared) {
178   error 'Can\'t create ' . $stat->dist . ' since it was never prepared -- aborting';
179   return 0;
180  }
181
182  if ($stat->created) {
183   msg $stat->dist . ' was already created -- skipping';
184   return 1;
185  }
186
187  $self->SUPER::create(@_);
188
189  my $dir = $stat->eb_dir;
190  unless (-d $dir) {
191   eval { mkpath $dir };
192   if ($@) {
193    error "mkpath($dir): $@";
194    return 0;
195   }
196  }
197
198  my $d = "# Generated by CPANPLUS::Dist::Gentoo\n\ninherit perl-module\n\n";
199  $d   .= 'S="${WORKDIR}/' . $stat->dist . "\"\n";
200  $d   .= 'DESCRIPTION="' . $stat->desc . "\"\n";
201  $d   .= 'HOMEPAGE="' . $stat->uri . "\"\n";
202  $d   .= 'SRC_URI="' . $stat->src . "\"\n";
203  $d   .= "SLOT=\"0\"\n";
204  $d   .= 'LICENSE="|| ( ' . join(' ', sort @{$stat->license}) . " )\"\n";
205  $d   .= 'KEYWORDS="' . join(' ', sort @{$stat->keywords}) . "\"\n";
206  $d   .= 'DEPEND="' . join "\n",
207   'dev-lang/perl',
208   map {
209    my $a = $_->[0]->package_name;
210    my $x = '';
211    if (defined $_->[1]) {
212     $x  = '>=';
213     $a .= '-' . $_->[1];
214    }
215    '|| ( ' . join(' ', map "$x$_/$a",
216                            qw/perl-core dev-perl perl-gcpan/, CATEGORY)
217            . ' )';
218   } @{$stat->deps};
219  $d   .= "\"\n";
220
221  my $file = $stat->eb_file;
222  open my $eb, '>', $file or do {
223   error "open($file): $! -- aborting";
224   return 0;
225  };
226  print $eb $d;
227  close $eb;
228
229  if ($stat->do_manifest) {
230   unless (copy $stat->fetched_arch, $stat->distdir) {
231    error "Couldn\'t copy the distribution file to distdir ($!) -- aborting";
232    1 while unlink $file;
233    return 0;
234   }
235
236   msg 'Adding Manifest entry for ' . $stat->dist;
237   unless (scalar run command => [ 'ebuild', $file, 'manifest' ], verbose => 0) {
238    error 'ebuild manifest failed -- aborting';
239    1 while unlink $file;
240    return 0;
241   }
242  }
243
244  return 1;
245 }
246
247 sub install {
248  my $self = shift;
249  my $stat = $self->status;
250  my $conf = $self->parent->parent->configure_object;
251
252  my $sudo = $conf->get_program('sudo');
253  my @cmd = ('emerge', '=' . $stat->eb_name . '-' . $stat->eb_version);
254  unshift @cmd, $sudo if $sudo;
255
256  unless (run command => \@cmd, verbose => 1) {
257   error 'emerge failed -- aborting';
258   return 0;
259  }
260
261  return 1;
262 }
263
264 sub uninstall {
265  my $self = shift;
266  my $stat = $self->status;
267  my $conf = $self->parent->parent->configure_object;
268
269  my $sudo = $conf->get_program('sudo');
270  my @cmd = ('emerge', '-C', '=' . $stat->eb_name . '-' . $stat->eb_version);
271  unshift @cmd, $sudo if $sudo;
272
273  unless (run command => \@cmd, verbose => 1) {
274   error 'emerge -C failed -- aborting';
275   return 0;
276  }
277
278  return 1;
279 }
280
281 =head1 DEPENDENCIES
282
283 Gentoo (L<http://gentoo.org>).
284
285 L<CPANPLUS>, L<IPC::Cmd> (core modules since 5.9.5).
286
287 L<File::Path> (since 5.001), L<File::Copy> (5.002), L<File::Spec::Functions> (5.00504).
288
289 =head1 SEE ALSO
290
291 L<cpan2dist>.
292
293 L<CPANPLUS::Dist::Base>, L<CPANPLUS::Dist::Deb>, L<CPANPLUS::Dist::Mdv>.
294
295 =head1 AUTHOR
296
297 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
298
299 =head1 BUGS
300
301 Please report any bugs or feature requests to C<bug-cpanplus-dist-gentoo at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPANPLUS-Dist-Gentoo>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
302
303 =head1 SUPPORT
304
305 You can find documentation for this module with the perldoc command.
306
307     perldoc CPANPLUS::Dist::Gentoo
308
309 =head1 ACKNOWLEDGEMENTS
310
311 The module is to some extend cargo-culted from L<CPANPLUS::Dist::Deb> and L<CPANPLUS::Dist::Mdv>.
312
313 =head1 COPYRIGHT & LICENSE
314
315 Copyright 2008 Vincent Pit, all rights reserved.
316
317 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
318
319 =cut
320
321 1; # End of CPANPLUS::Dist::Gentoo