Added File::ShareDir support

David Golden <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.module-build
Message-ID <[email protected]>
I know some people were looking into this but it turned out to be fairly
easy. Needs live testing though.

David

---------- Forwarded message ----------
From: <[email protected]>
Date: Aug 28, 2009 6:30 PM
Subject: [svn:Module-Build] r13232 - in Module-Build/trunk: .
lib/Module/Build t
To: <[email protected]>

Author: dagolden
Date: Fri Aug 28 15:29:59 2009
New Revision: 13232

Modified:
  Module-Build/trunk/Changes
  Module-Build/trunk/lib/Module/Build/API.pod
  Module-Build/trunk/lib/Module/Build/Base.pm
  Module-Build/trunk/t/extend.t
  Module-Build/trunk/t/share_dir.t

Log:
share_dir installation support complete

share_dirs now install, hopefully correctly.  This
needs offline verification or optional testing only
if File::ShareDir is installed.

Todo: add File::ShareDir to requires automatically
with an appropriate minimum version for "new" style
share directory layout.


Modified: Module-Build/trunk/Changes
==============================================================================
--- Module-Build/trunk/Changes  (original)
+++ Module-Build/trunk/Changes  Fri Aug 28 15:29:59 2009
@@ -6,6 +6,9 @@
 - Generates MYMETA.yml during Build.PL (new standard protocol for
   communicating configuration results between toolchain components)
   [David Golden]
+ - Added 'share_dir' property top provide File::ShareDir support;
+   set automatically if a directory called 'share' exists
+   [David Golden]

 Bug fixes:
 - Fix the t/destinations.t fix. [David Golden, with thanks to Eric Wilhelm]

Modified: Module-Build/trunk/lib/Module/Build/API.pod
==============================================================================
--- Module-Build/trunk/lib/Module/Build/API.pod (original)
+++ Module-Build/trunk/lib/Module/Build/API.pod Fri Aug 28 15:29:59 2009
@@ -737,6 +737,32 @@
 instead of C<script_files>.  Please consider this usage deprecated,
 though it will continue to exist for several version releases.

+=item share_dir
+
+[version 0.36]
+
+An optional parameter specifying directories of static data files to
+be installed as read-only files for use with L<File::ShareDir>.  The
+C<share_dir> property supports both distribution-level and
+module-level share files.
+
+The default when C<share_dir> is not set is for any files in a F<share>
+directory at the top level of the distribution to be installed in
+distribution-level share directory.  Alternatively, C<share_dir> can be set
to
+a directory name or an arrayref of directory names containing files to be
+installed in the distribution-level share directory.
+
+If C<share_dir> is a hashref, it may have C<dist> or C<module> keys
+providing full flexibility in defining share directories to install.
+
+  share_dir => {
+    dist => [ 'examples', 'more_examples' ],
+    module => {
+      Foo::Templates => ['share/html', 'share/text'],
+      Foo::Config    => 'share/config',
+    }
+  }
+
 =item sign

 [version 0.16]

Modified: Module-Build/trunk/lib/Module/Build/Base.pm
==============================================================================
--- Module-Build/trunk/lib/Module/Build/Base.pm (original)
+++ Module-Build/trunk/lib/Module/Build/Base.pm Fri Aug 28 15:29:59 2009
@@ -831,7 +831,7 @@
 __PACKAGE__->add_property(auto_configure_requires => 1);
 __PACKAGE__->add_property(blib => 'blib');
 __PACKAGE__->add_property(build_class => 'Module::Build');
-__PACKAGE__->add_property(build_elements => [qw(PL support pm xs pod
script)]);
+__PACKAGE__->add_property(build_elements => [qw(PL support pm xs share_dir
pod script)]);
 __PACKAGE__->add_property(build_script => 'Build');
 __PACKAGE__->add_property(build_bat => 0);
 __PACKAGE__->add_property(config_dir => '_build');
@@ -2459,6 +2459,59 @@
  }
 }

+sub process_share_dir_files {
+  my $self = shift;
+  my $files = $self->_find_share_dir_files;
+  return unless $files;
+
+  # XXX need to add File::ShareDir as 'requires'
+
+  # root for all File::ShareDir paths
+  my $share_prefix = File::Spec->catdir($self->blib, qw/lib auto share/);
+
+  # copy all share files to blib
+  while (my ($file, $dest) = each %$files) {
+    $self->copy_if_modified(
+      from => $file, to => File::Spec->catfile( $share_prefix, $dest )
+    );
+  }
+}
+
+sub _find_share_dir_files {
+  my $self = shift;
+  my $share_dir = $self->share_dir;
+  return unless $share_dir;
+
+  my @file_map;
+  if ( $share_dir->{dist} ) {
+    my $prefix = "dist/" . $self->dist_name;
+    push @file_map, $self->_share_dir_map( $prefix, $share_dir->{dist} );
+  }
+
+  if ( $share_dir->{module} ) {
+    for my $mod ( keys %{ $share_dir->{module} } ) {
+      (my $altmod = $mod) =~ s{::}{-}g;
+      my $prefix = "module/$altmod";
+      push @file_map, $self->_share_dir_map($prefix,
$share_dir->{module}{$mod});
+    }
+  }
+
+  return { @file_map };
+}
+
+sub _share_dir_map {
+  my ($self, $prefix, $list) = @_;
+  my %files;
+  for my $dir ( @$list ) {
+    for my $f ( @{ $self->rscan_dir( $dir, sub {-f} )} ) {
+      $files{$f} = File::Spec->catfile(
+        $prefix, File::Spec->abs2rel( $f, $dir )
+      );
+    }
+  }
+  return %files;
+}
+
 sub process_PL_files {
  my ($self) = @_;
  my $files = $self->find_PL_files;

Modified: Module-Build/trunk/t/extend.t
==============================================================================
--- Module-Build/trunk/t/extend.t       (original)
+++ Module-Build/trunk/t/extend.t       Fri Aug 28 15:29:59 2009
@@ -79,7 +79,7 @@

  $mb->add_build_element('foo');
  $mb->add_build_element('foo');
-  is_deeply $mb->build_elements, [qw(PL support pm xs pod script foo)],
+  is_deeply $mb->build_elements, [qw(PL support pm xs share_dir pod script
foo)],
      'The foo element should be in build_elements only once';

  $mb->dispatch('build');

Modified: Module-Build/trunk/t/share_dir.t
==============================================================================
--- Module-Build/trunk/t/share_dir.t    (original)
+++ Module-Build/trunk/t/share_dir.t    Fri Aug 28 15:29:59 2009
@@ -9,7 +9,7 @@
 # Begin testing
 #--------------------------------------------------------------------------#

-plan tests => 12;
+plan tests => 19;

 require_ok('Module::Build');
 ensure_blib('Module::Build');
@@ -21,7 +21,7 @@
 my $tmp = MBTest->tmpdir;

 use DistGen;
-my $dist = DistGen->new( dir => $tmp );
+my $dist = DistGen->new( dir => $tmp, name => 'Simple::Share' );

 $dist->regen;
 END{ $dist->remove }
@@ -44,11 +44,12 @@
 $dist->add_file('share/foo.txt',<< '---');
 This is foo.txt
 ---
-$dist->add_file('other/bar.txt',<< '---');
+$dist->add_file('other/share/bar.txt',<< '---');
 This is bar.txt
 ---
 $dist->regen;
 ok( -e catfile(qw/share foo.txt/), "Created 'share' directory" );
+ok( -e catfile(qw/other share bar.txt/), "Created 'other/share' directory"
);

 # Check default when share_dir is not given
 $mb = $dist->new_from_context;
@@ -119,7 +120,7 @@
    license             => 'perl',
    share_dir           => {
      dist => 'share',
-      module => { $dist->name =>  'other'  },
+      module => { $dist->name =>  'other/share'  },
    },
  }
 );
@@ -127,7 +128,7 @@
 $mb = $dist->new_from_context;
 is_deeply( $mb->share_dir,
  { dist => [ 'share' ],
-    module => { $dist->name => ['other']  },
+    module => { $dist->name => ['other/share']  },
  },
  "Hashref share_dir w/ both dist and module shares (scalar-form)"
 );
@@ -139,7 +140,7 @@
    license             => 'perl',
    share_dir           => {
      dist => [ 'share' ],
-      module => { $dist->name =>  ['other']  },
+      module => { $dist->name =>  ['other/share']  },
    },
  }
 );
@@ -147,11 +148,63 @@
 $mb = $dist->new_from_context;
 is_deeply( $mb->share_dir,
  { dist => [ 'share' ],
-    module => { $dist->name => ['other']  },
+    module => { $dist->name => ['other/share']  },
  },
  "Hashref share_dir w/ both dist and module shares (array-form)"
 );

+#--------------------------------------------------------------------------#
+# test constructing to/from mapping
+#--------------------------------------------------------------------------#
+
+is_deeply( $mb->_find_share_dir_files,
+  {
+    'share/foo.txt' => 'dist/Simple-Share/foo.txt',
+    'other/share/bar.txt' => 'module/Simple-Share/bar.txt',
+  },
+  "share_dir filemap for copying to lib complete"
+);
+
+#--------------------------------------------------------------------------#
+# test moving files to blib
+#--------------------------------------------------------------------------#

+$mb->dispatch('build');
+
+ok( -d 'blib', "Build ran and blib exists" );
+ok( -d 'blib/lib/auto/share', "blib/lib/auto/share exists" );
+
+my $share_list = Module::Build->rscan_dir('blib/lib/auto/share', sub {-f});
+
+is_deeply(
+  $share_list, [
+    'blib/lib/auto/share/dist/Simple-Share/foo.txt',
+    'blib/lib/auto/share/module/Simple-Share/bar.txt',
+  ],
+  "share_dir files copied to blib"
+);
+
+#--------------------------------------------------------------------------#
+# test installing
+#--------------------------------------------------------------------------#
+
+my $temp_install = 'temp_install';
+mkdir $temp_install;
+ok( -d $temp_install, "temp install dir created" );
+
+$mb->install_base($temp_install);
+stdout_of( sub { $mb->dispatch('install') } );
+
+$share_list = Module::Build->rscan_dir(
+  "$temp_install/lib/perl5/auto/share", sub {-f}
+);
+
+is_deeply(
+  $share_list, [
+    "$temp_install/lib/perl5/auto/share/dist/Simple-Share/foo.txt",
+    "$temp_install/lib/perl5/auto/share/module/Simple-Share/bar.txt",
+  ],
+  "share_dir files correctly installed"
+);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.