Skip to content
This repository

File munge api #24

Open
wants to merge 4 commits into from

1 participant

Kent Fredric
Kent Fredric

This is mostly a request for a review of ideas and implementation.

If you like the concept , or think it needs work, get back to me =).

the idea is to unify the way people munge files, which currently has to be done differently depending on whether its a file from code, or a string file.

This doesn't appear to be a problem much at the moment, but if you try making a test or anything that gets moved/munged a File::FromCode, it will explode in a ball of fire when the munger tries to do

$file->content( $foo ) and the FromCode file is immutable in that context.

The Munge API simplifies this by making consumers interact with the content via a callback fashion, forcing a sub-ref, so the underlying infrastructure can do the right thing, and either modify the content now, or use coderef chaining so the files content is aggregated when its needed.

$file->munge(sub{ 
    my( $fileself, $content ) = @_; 
    return munged($content);
});

On normal files, the code runs immediately, on FromCode files, it chains it so its more like this:

my $orig = $file->{coderef}; 
$file->{coderef} = sub { 
     my $content = $orig->(); 
     return munge( $content );
};
Kent Fredric

I have thrown a bunch of tuits together and whipped up https://metacpan.org/module/KENTNL/Dist-Zilla-Util-SimpleMunge-0.1.2/lib/Dist/Zilla/Util/SimpleMunge.pm

A complimentary interface for munging files in a homogenous matter which has space in the code for defaulting to the changes suggested by this branch if wanted.

Currently covers the basic use case of simpifying the effort of munging any file in a consistent way, but will also have the option later to coerce lazy from-codes to immediate inmemories and vice versa depending on need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
12  lib/Dist/Zilla/File/FromCode.pm
... ...
@@ -1,7 +1,8 @@
1 1
 package Dist::Zilla::File::FromCode;
2 2
 # ABSTRACT: a file whose content is (re-)built on demand
3 3
 use Moose;
4  
-
  4
+use Params::Util qw( _CODELIKE );
  5
+use Carp qw();
5 6
 use namespace::autoclean;
6 7
 
7 8
 =head1 DESCRIPTION
@@ -31,6 +32,15 @@ sub content {
31 32
   return $self->$code;
32 33
 }
33 34
 
  35
+sub munge {
  36
+  my ( $self, $munger ) = @_ ;
  37
+  Carp::croak('->munge( munger )  was not passed a coderef' ) unless _CODELIKE($munger);
  38
+  my $orig = $self->code;
  39
+  $self->code(sub {
  40
+    return $munger->( $self, $self->$orig );
  41
+  });
  42
+}
  43
+
34 44
 with 'Dist::Zilla::Role::File';
35 45
 __PACKAGE__->meta->make_immutable;
36 46
 1;
13  lib/Dist/Zilla/Plugin/ExtraTests.pm
@@ -51,10 +51,11 @@ sub _rewrite {
51 51
   (my $name = $file->name) =~ s{^xt/([^/]+)/}{t/$1-};
52 52
 
53 53
   $file->name($name);
54  
-
55  
-  my @lines = split /\n/, $file->content;
56  
-  my $after = $lines[0] =~ /\A#!/ ? 1 : 0;
57  
-  splice @lines, $after, 0, qq|
  54
+  $file->munge(sub{
  55
+    my( $self, $content ) = @_ ;
  56
+    my @lines = split /\n/, $content;
  57
+    my $after = $lines[0] =~ /\A#!/ ? 1 : 0;
  58
+    splice @lines, $after, 0, qq|
58 59
 BEGIN {
59 60
   unless (\$ENV{$env}) {
60 61
     require Test::More;
@@ -62,8 +63,8 @@ BEGIN {
62 63
   }
63 64
 }
64 65
 |;
65  
-
66  
-  $file->content(join "\n", @lines, '');
  66
+    return join "\n", @lines, '';
  67
+  });
67 68
 }
68 69
 
69 70
 __PACKAGE__->meta->make_immutable;
25  lib/Dist/Zilla/Plugin/NextRelease.pm
@@ -64,17 +64,20 @@ sub munge_files {
64 64
   my ($file) = grep { $_->name eq $self->filename } @{ $self->zilla->files };
65 65
   return unless $file;
66 66
 
67  
-  my $content = $self->fill_in_string(
68  
-    $file->content,
69  
-    {
70  
-      dist    => \($self->zilla),
71  
-      version => \($self->zilla->version),
72  
-      NEXT    => \($self->section_header),
73  
-    },
74  
-  );
75  
-
76  
-  $self->log_debug([ 'updating contents of %s in memory', $file->name ]);
77  
-  $file->content($content);
  67
+  $file->munge(sub{
  68
+    my( $selffile, $content ) = @_;
  69
+    $content = $self->fill_in_string(
  70
+      $content,
  71
+      {
  72
+        dist    => \($self->zilla),
  73
+        version => \($self->zilla->version),
  74
+        NEXT    => \($self->section_header),
  75
+      },
  76
+    );
  77
+
  78
+    $self->log_debug([ 'updating contents of %s in memory', $file->name ]);
  79
+    return $content;
  80
+  });
78 81
 }
79 82
 
80 83
 # new release is part of distribution history, let's record that.
73  lib/Dist/Zilla/Plugin/PodVersion.pm
@@ -34,42 +34,45 @@ sub munge_file {
34 34
 sub munge_pod {
35 35
   my ($self, $file) = @_;
36 36
 
37  
-  my @content = split /\n/, $file->content;
38  
-
39  
-  require List::MoreUtils;
40  
-  if (List::MoreUtils::any(sub { $_ =~ /^=head1 VERSION\b/ }, @content)) {
41  
-    $self->log($file->name . ' already has a VERSION section in POD');
42  
-    return;
43  
-  }
44  
-
45  
-  for (0 .. $#content) {
46  
-    next until $content[$_] =~ /^=head1 NAME/;
47  
-
48  
-    $_++; # move past the =head1 line itself
49  
-    $_++ while $content[$_] =~ /^\s*$/;
50  
-
51  
-    $_++; # move past the line with the abstract
52  
-    $_++ while $content[$_] =~ /^\s*$/;
53  
-
54  
-    splice @content, $_ - 1, 0, (
55  
-      q{},
56  
-      "=head1 VERSION",
57  
-      q{},
58  
-      "version " . $self->zilla->version . q{},
59  
-    );
60  
-
61  
-    $self->log_debug([ 'adding VERSION Pod section to %s', $file->name ]);
62  
-
63  
-    my $content = join "\n", @content;
64  
-    $content .= "\n" if length $content;
65  
-    $file->content($content);
66  
-    return;
67  
-  }
68  
-
69  
-  $self->log([
  37
+  $file->munge(sub{
  38
+    my  ($selffile, $content ) = @_;
  39
+    my @content = split /\n/, $content;
  40
+
  41
+    require List::MoreUtils;
  42
+    if (List::MoreUtils::any(sub { $_ =~ /^=head1 VERSION\b/ }, @content)) {
  43
+      $self->log($file->name . ' already has a VERSION section in POD');
  44
+      return;
  45
+    }
  46
+
  47
+    for (0 .. $#content) {
  48
+      next until $content[$_] =~ /^=head1 NAME/;
  49
+
  50
+      $_++; # move past the =head1 line itself
  51
+      $_++ while $content[$_] =~ /^\s*$/;
  52
+
  53
+      $_++; # move past the line with the abstract
  54
+      $_++ while $content[$_] =~ /^\s*$/;
  55
+
  56
+      splice @content, $_ - 1, 0, (
  57
+        q{},
  58
+        "=head1 VERSION",
  59
+        q{},
  60
+        "version " . $self->zilla->version . q{},
  61
+      );
  62
+
  63
+      $self->log_debug([ 'adding VERSION Pod section to %s', $file->name ]);
  64
+      my $content = join "\n", @content;
  65
+      $content .= "\n" if length $content;
  66
+      return $content;
  67
+    }
  68
+
  69
+    $self->log([
70 70
     "couldn't find '=head1 NAME' in %s, not adding '=head1 VERSION'",
71  
-    $file->name,
72  
-  ]);
  71
+      $file->name,
  72
+    ]);
  73
+
  74
+    return $content;
  75
+  });
73 76
 }
74 77
 
75 78
 __PACKAGE__->meta->make_immutable;
10  lib/Dist/Zilla/Role/File.pm
@@ -3,7 +3,8 @@ package Dist::Zilla::Role::File;
3 3
 use Moose::Role;
4 4
 
5 5
 use Moose::Util::TypeConstraints;
6  
-
  6
+use Params::Util qw( _CODELIKE );
  7
+use Carp qw();
7 8
 use namespace::autoclean;
8 9
 
9 10
 =head1 DESCRIPTION
@@ -55,4 +56,11 @@ has mode => (
55 56
 
56 57
 requires 'content';
57 58
 
  59
+sub munge {
  60
+  my ( $self, $code ) = @_;
  61
+  Carp::croak("->munge( code ) was not passed a coderef") unless _CODELIKE($code);
  62
+  my $content = $self->content;
  63
+  $self->content( $code->($self,$content ) );
  64
+}
  65
+
58 66
 1;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.