Author: dagolden
Date: Tue Nov 24 07:53:27 2009
New Revision: 13599

Modified:
   Module-Build/trunk/Changes
   Module-Build/trunk/lib/Module/Build/Base.pm
   Module-Build/trunk/t/mymeta.t

Log:
Base MYMETA on META if it exists

Modified: Module-Build/trunk/Changes
==============================================================================
--- Module-Build/trunk/Changes  (original)
+++ Module-Build/trunk/Changes  Tue Nov 24 07:53:27 2009
@@ -18,6 +18,12 @@
    We now try to detect such failures, prepend 'lib' to @INC and try again.
    [David Golden]
 
+ - MYMETA.yml used to be generated from scratch, overriding any
+   customizations used to create META.yml.  Now, if META.yml exists, that
+   will be used as the base for MYMETA and only prereq fields will be
+   updated (to reflect any dynamic configuration); also, 'dynamic_config'
+   will be set to false and 'generated_by' will be updated [David Golden]
+
 0.35_09 - Thu Nov 19 01:30:42 EST 2009
 
  Bug fixes:

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 Tue Nov 24 07:53:27 2009
@@ -1753,17 +1753,45 @@
 EOF
 }
 
-sub create_build_script {
+sub create_mymeta {
   my ($self) = @_;
-  $self->write_config;
-
-  # Create MYMETA.yml
   my $mymetafile = $self->mymetafile;
+  my $metafile = $self->metafile;
+
+  # cleanup
   if ( $self->delete_filetree($mymetafile) ) {
     $self->log_verbose("Removed previous '$mymetafile'\n");
   }
   $self->log_info("Creating new '$mymetafile' with configuration results\n");
-  $self->write_metafile( $mymetafile, $self->prepare_metadata( fatal => 0 ) );
+
+  # use old meta and update prereqs, if possible
+  my $mymeta;
+  if ( -f $metafile ) {
+    $mymeta = $self->read_metafile( $self->metafile );
+    my $prereqs = $self->_normalize_prereqs;
+    for my $t ( keys %$prereqs ) {
+        $mymeta->{$t} = $prereqs->{$t};
+    }
+  }
+  # but generate from scratch, ignoring errors if META doesn't exist
+  else {
+    $mymeta = $self->prepare_metadata( fatal => 0 );
+  }
+
+  # MYMETA is always static
+  $mymeta->{dynamic_config} = 0;
+  # Note which M::B created it
+  $mymeta->{generated_by} = "Module::Build version $Module::Build::VERSION";
+
+  $self->write_metafile( $mymetafile, $mymeta );
+  return 1;
+}
+
+sub create_build_script {
+  my ($self) = @_;
+
+  $self->write_config;
+  $self->create_mymeta;
 
   # Create Build
   my ($build_script, $dist_name, $dist_version)
@@ -4138,6 +4166,21 @@
   return 1;
 }
 
+sub read_metafile {
+  my $self = shift;
+  my ($metafile) = @_;
+  my $yaml;
+
+  my $class = $self->_mb_feature('YAML_support')
+            ? 'YAML::Tiny' : 'Module::Build::YAML' ;
+
+  eval "require $class; 1" or die $@;
+  my $meta = $class->read($metafile)
+    or $self->log_warn( "Error reading '$metafile': " . $class->errstr . "\n");
+
+  return $meta->[0] || {};
+}
+
 sub write_metafile {
   my $self = shift;
   my ($metafile, $node) = @_;
@@ -4175,6 +4218,23 @@
   return $version;
 }
 
+sub _normalize_prereqs {
+  my ($self) = @_;
+  my $p = $self->{properties};
+
+  # copy prereq data structures so we can modify them before writing to META
+  my %prereq_types;
+  for my $type ( 'configure_requires', @{$self->prereq_action_types} ) {
+    if (exists $p->{$type}) {
+      for my $mod ( keys %{ $p->{$type} } ) {
+        $prereq_types{$type}{$mod} =
+          $self->normalize_version($p->{$type}{$mod});
+      }
+    }
+  }
+  return \%prereq_types;
+}
+
 sub prepare_metadata {
   my ($self, %args) = @_;
   my $fatal = $args{fatal} || 0;
@@ -4226,19 +4286,10 @@
     # XXX we are silently omitting the url for any unknown license
   }
 
-  # copy prereq data structures so we can modify them before writing to META
-  my %prereq_types;
-  for my $type ( 'configure_requires', @{$self->prereq_action_types} ) {
-    if (exists $p->{$type}) {
-      for my $mod ( keys %{ $p->{$type} } ) {
-        $prereq_types{$type}{$mod} =
-          $self->normalize_version($p->{$type}{$mod});
-      }
-    }
-  }
 
-  for my $t ( keys %prereq_types ) {
-      $add_node->($t, $prereq_types{$t});
+  my $prereqs = $self->_normalize_prereqs;
+  for my $t ( keys %$prereqs ) {
+      $add_node->($t, $prereqs->{$t});
   }
 
   if (exists $p->{dynamic_config}) {

Modified: Module-Build/trunk/t/mymeta.t
==============================================================================
--- Module-Build/trunk/t/mymeta.t       (original)
+++ Module-Build/trunk/t/mymeta.t       Tue Nov 24 07:53:27 2009
@@ -3,7 +3,7 @@
 use strict;
 use lib 't/lib';
 use MBTest;
-plan tests => 23;
+plan tests => 24;
 
 blib_load('Module::Build');
 blib_load('Module::Build::YAML');
@@ -50,9 +50,12 @@
   my $output = stdout_of sub { $dist->run_build('distmeta') };
   like($output, qr/Creating META.yml/,
     "Ran Build distmeta to create META.yml");
-  my $meta = Module::Build::YAML->read('META.yml');
-  my $mymeta = Module::Build::YAML->read('MYMETA.yml');
-  is_deeply( $meta, $mymeta, "Generated MYMETA matches generated META" );
+  my $meta = Module::Build::YAML->read('META.yml')->[0];
+  my $mymeta = Module::Build::YAML->read('MYMETA.yml')->[0];
+  is( delete $mymeta->{dynamic_config}, 0,
+    "MYMETA 'dynamic_config' is 0"
+  );
+  is_deeply( $meta, $mymeta, "Other generated MYMETA matches generated META" );
   $output = stdout_stderr_of sub { $dist->run_build('realclean') };
   like( $output, qr/Cleaning up/, "Ran realclean");
   ok( ! -e 'Build', "Build file removed" );
@@ -65,9 +68,9 @@
     "Ran Build.PL with dynamic config"
   );
   ok( -e "MYMETA.yml", "MYMETA.yml exists" );
-  $mymeta = Module::Build::YAML->read('MYMETA.yml');
-  isnt(   $meta->[0]{requires}{'File::Spec'},
-        $mymeta->[0]{requires}{'File::Spec'},
+  $mymeta = Module::Build::YAML->read('MYMETA.yml')->[0];
+  isnt(   $meta->{requires}{'File::Spec'},
+        $mymeta->{requires}{'File::Spec'},
         "MYMETA requires differs from META"
   );
   $output = stdout_stderr_of sub { $dist->run_build('realclean') };
@@ -76,15 +79,16 @@
   ok( ! -e 'MYMETA.yml', "MYMETA file removed" );
 
   # manually change META and check that changes are preserved
-  $meta->[0]{author} = ['John Gault'];
-  ok( $meta->write('META.yml'), "Wrote manually modified META.yml" );
+  $meta->{author} = ['John Gault'];
+  ok( Module::Build::YAML->new($meta)->write('META.yml'),
+    "Wrote manually modified META.yml" );
 
   $output = stdout_of sub { $dist->run_build_pl };
   like($output, qr/Creating new 'MYMETA.yml' with configuration results/,
     "Ran Build.PL"
   );
-  my $mymeta2 = Module::Build::YAML->read('MYMETA.yml');
-  is_deeply( $mymeta2->[0]{author}, [ 'John Gault' ],
+  my $mymeta2 = Module::Build::YAML->read('MYMETA.yml')->[0];
+  is_deeply( $mymeta2->{author}, [ 'John Gault' ],
     "MYMETA preserved META modifications"
   );
 

Reply via email to