Moose -> Moo
[Packaging-Tools.git] / lib / Packaging / Tools / Plugin.pm
blob9205c2238296f7fdcce3f0c1f355ab49c3dc6161
1 package Packaging::Tools::Plugin;
3 use 5.014;
5 use strict;
6 use warnings;
8 =head1 NAME
10 Packaging::Tools::Plugin - base class for Packaging::Tools plugins to define API
12 =cut
14 use Carp qw/carp croak/;
16 our $VERSION = '0.001';
18 require File::Basename;
19 require File::Spec;
21 use Moo;
22 use namespace::clean;
24 has '_column_defs' => (
25 is => 'ro',
26 isa => 'ArrayRef[Str]',
27 builder => '_default_column_defs',
28 init_arg => undef
30 has 'packaged_modules' => (
31 is => 'ro',
32 isa => 'HashRef',
33 lazy => 1,
34 builder => 'find_packaged_modules',
35 init_arg => undef
37 has 'installed_packages' => (
38 is => 'ro',
39 isa => 'HashRef',
40 lazy => 1,
41 builder => 'find_installed_packages',
42 init_arg => undef
45 eval { require Packaging::Tools::Plugin::Role::Cache; }
46 and with "Packaging::Tools::Plugin::Role::Cache";
48 =head1 SYNOPSIS
50 =head1 SUBROUTINES/METHODS
52 =cut
54 sub _default_column_defs
56 return [
57 qw(PKG_IDENT DIST_NAME DIST_VERSION DIST_FILE),
58 qw(PKG_NAME PKG_VERSION PKG_MAINTAINER PKG_INSTALLED),
59 qw(PKG_LOCATION PKG_HOMEPAGE PKG_LICENSE PKG_MASTER_SITES)
63 =head2 find_installed_packages
65 =cut
67 sub find_installed_packages { ... }
69 sub _find_packaged_modules { ... }
71 =head2 find_packaged_modules
73 =cut
75 sub find_packaged_modules
77 my ($self) = @_;
79 return $self->_find_packaged_modules();
82 sub _get_pkg_details { ... }
84 =head2 get_pkg_details($pkg_ident,@var_names)
86 Returns package details.
88 =over 4
90 =item * I<$pkg_ident>
92 One value from the list of L</find_packaged_modules|packaged modules>.
94 =item * I<@var_names>
96 Any combination of:
98 =over 8
100 =item DIST_NAME
102 =item DIST_VERSION
104 =item DIST_FILE
106 =item PKG_NAME
108 =item PKG_VERSION
110 =item PKG_MAINTAINER
112 =item PKG_LOCATION
114 =item PKG_LICENSE
116 =item PKG_HOMEPAGE
118 =item PKG_INSTALLED
120 =item PKG_MASTER_SITES
122 =back
124 =back
126 =cut
128 sub get_pkg_details
130 my ( $self, $pkg_ident, @var_names ) = @_;
132 defined( $self->packaged_modules()->{$pkg_ident} )
133 or $self->_get_pkg_details( $pkg_ident, @var_names );
135 if ($have_cache)
137 $self->_cache->{dbh} or $self->_open_cache();
139 my $dbh = $self->_cache->{dbh};
140 defined( $self->packaged_modules()->{$pkg_ident} )
141 and $have_cache
142 and $self->_update_cache( $self->_column_defs()->[0] => $pkg_ident,
143 %{ $self->packaged_modules()->{$pkg_ident} } );
144 delete $self->_cache->{stale_pkg_idents}->{$pkg_ident};
146 if ( $self->_cache->{_last_scan}
147 and scalar( keys %{ $self->_cache->{stale_pkg_idents} } ) == 0 )
149 $self->_cache->{last_scan} = delete $self->_cache->{_last_scan};
150 my ( $sql, $sth );
151 $sql = "DELETE FROM LAST_SCAN";
152 $sth = $dbh->prepare($sql);
153 $sth->execute() or carp( $dbh->errstr() );
155 $sql = "INSERT INTO LAST_SCAN (LAST_SCAN) VALUES(" . $self->_cache->{last_scan} . ")";
156 $sth = $dbh->prepare($sql);
157 $sth->execute() or carp( $dbh->errstr() );
161 return @{ $self->packaged_modules()->{$pkg_ident} }{@var_names};
164 sub _fetch_all_pkgs_details
166 my $self = shift;
168 defined( $self->_cache ) and $self->_cache->{last_scan} and return;
169 $have_cache and !$self->_cache->{dbh} and $self->_open_cache();
171 my @stale_pkgs;
172 if ( $have_cache and $self->_cache->{stale_pkg_idents} )
174 @stale_pkgs = keys %{ $self->_cache->{stale_pkg_idents} };
176 else
178 my %stale_tmp = map { $_ => 1 } $self->find_packaged_modules();
179 delete @stale_tmp{ keys %{ $self->{pkg_details} } };
182 foreach my $stale_pkg (@stale_pkgs)
184 $self->get_pkg_details( $stale_pkg, qw(DIST_NAME) );
187 return;
190 sub find_package
192 my ( $self, %match ) = @_;
194 scalar( keys %match ) > 0 or croak("find_package( key => value )");
195 $self->_fetch_all_pkgs_details();
196 my @found;
198 if ($have_cache)
200 my $dbh = $self->_cache->{dbh};
201 my $sql = sprintf(
202 "SELECT DISTINCT %s FROM DIST_PKGS WHERE %s",
203 $self->_column_defs()->[0],
204 join(
205 " AND ", map { $_ . "=" . $dbh->quote( $match{$_} ) } keys %match
208 @found = $dbh->selectrow_array($sql);
210 else
212 my @keys = keys %match;
213 my @values = @match{@keys};
214 my $pkgs = $self->packaged_modules;
215 @found = grep { @values ~~ @{ $pkgs->{$_} }{@keys}; } keys %{$pkgs};
218 1 == scalar(@found) and return $found[0];
219 return;
222 has 'template_directories' => (
223 is => 'ro',
224 isa => 'ArrayRef[Str]',
225 lazy => 1,
226 init_arg => undef,
227 builder => ''
230 sub _get_template_directories { return; }
232 sub DESTROY
234 my $self = $_[0];
236 $self->_cache->{dbh} and $self->_cache->{dbh}->disconnect();
237 $self->_clear_cache();
239 return;
242 no Moo;