Skip to content
Browse files

elaborate proof of concept?

  • Loading branch information...
0 parents commit f3854f7c675a11c6b3832dc111f72e6d705c09fa @rjbs committed May 27, 2008
1 MANIFEST.SKIP
@@ -0,0 +1 @@
+MANIFEST.SKIP
30 eg/self-dist
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use lib 'lib';
+use lib 't/lib';
+
+use Dist::Zilla;
+use Dist::Zilla::Plugin::ManifestSkip;
+use Dist::Zilla::Plugin::Manifest;
+use Dist::Zilla::Plugin::MakeMaker;
+use Dist::Zilla::Plugin::CreditTaker;
+
+use File::Temp qw(tempdir);
+use Moose::Autobox;
+
+my $root = $ARGV[0] || '.';
+my $target = $ARGV[1] || tempdir(DIR => '.');
+
+my $dist = Dist::Zilla->new({
+ plugins => [
+ Dist::Zilla::Plugin::ManifestSkip->new,
+ Dist::Zilla::Plugin::Manifest->new,
+ Dist::Zilla::Plugin::MakeMaker->new,
+ Dist::Zilla::Plugin::CreditTaker->new,
+ ],
+ root => Path::Class::dir($root),
+});
+
+$dist->build_dist($target);
120 lib/Dist/Zilla.pm
@@ -0,0 +1,120 @@
+package Dist::Zilla;
+use Moose;
+use Moose::Autobox;
+
+our $VERSION = '0.001';
+
+use File::Find::Rule;
+use Path::Class ();
+
+use Dist::Zilla::Config;
+
+use Dist::Zilla::Role::Plugin;
+
+# XXX: should come from config!
+sub name { 'Dist::Zilla' }
+
+has config => (
+ is => 'ro',
+ isa => 'HashRef',
+ lazy => 1,
+ default => sub { Dist::Zilla::Config->read_file('dist.ini') },
+);
+
+has plugins => (
+ is => 'ro',
+ isa => 'ArrayRef[Dist::Zilla::Role::Plugin]',
+ default => sub { [ ] },
+);
+
+has files => (
+ is => 'ro',
+ isa => 'ArrayRef',
+ lazy => 1,
+ default => sub {
+ my ($self) = @_;
+ my $root = $self->root;
+ my @files = File::Find::Rule
+ ->not( File::Find::Rule->name(qr/^\./) )
+ ->file
+ ->in($root);
+
+ return \@files;
+ },
+);
+
+sub plugins_with {
+ my ($self, $role) = @_;
+
+ $role =~ s/^-/Dist::Zilla::Role::/;
+ my $plugins = $self->plugins->grep(sub { $_->does($role) });
+
+ return $plugins;
+}
+
+has root => (
+ is => 'ro',
+ isa => 'Path::Class::Dir',
+ required => 1,
+);
+
+sub manifest {
+ my ($self) = @_;
+
+ my $files = [ $self->files->flatten ];
+
+ $_->prune_files($files) for $self->plugins_with(-FilePruner)->flatten;
+
+ return $files;
+}
+
+sub build_dist {
+ my ($self, $root) = @_;
+
+ my $build_root = Path::Class::dir($root);
+ $build_root->mkpath unless -d $build_root;
+
+ my $dist_root = $self->root;
+ my $manifest = $self->manifest;
+
+ for my $file (map { $dist_root->file($_) } $manifest->flatten) {
+ my $to_dir = $build_root->subdir( $file->dir );
+ $to_dir->mkpath unless -e $to_dir;
+ die "not a directory: $to_dir" unless -d $to_dir;
+
+ my $content = do {
+ open my $in_fh, '<', "$file" or die "couldn't open $file to read: $!";
+ local $/;
+ <$in_fh>;
+ };
+
+ my $to = $to_dir->file( $file->basename );
+ my $arg = { to => $to, content => $content };
+ $_->munge_file($arg) for $self->plugins_with(-FileMunger)->flatten;
+ $to = $arg->{to};
+
+ open my $out_fh, '>', "$to" or die "couldn't open $to to write: $!";
+ print { $out_fh } $arg->{content};
+ close $out_fh or die "error closing $to: $!";
+ }
+
+ for ($self->plugins_with(-FileWriter)->flatten) {
+ $_->write_files({
+ build_root => $build_root,
+ dist => $self,
+ manifest => $manifest,
+ });
+ }
+
+ for ($self->plugins_with(-AfterBuild)->flatten) {
+ $_->after_build({
+ build_root => $build_root,
+ dist => $self,
+ manifest => $manifest,
+ });
+ }
+}
+
+
+no Moose;
+1;
8 lib/Dist/Zilla/Config.pm
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+package Dist::Zilla::Config;
+use base 'Config::INI::Reader';
+
+
+
+1;
33 lib/Dist/Zilla/Plugin/MakeMaker.pm
@@ -0,0 +1,33 @@
+package Dist::Zilla::Plugin::MakeMaker;
+use Moose;
+use Moose::Autobox;
+with 'Dist::Zilla::Role::FileWriter';
+
+use Text::Template;
+
+my $template = <<'END_MAKEFILE';
+use strict;
+use warnings;
+
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => {{ $dist->name }},
+ VERSION_FROM => "{{ (grep { /.pm$/ } @{$dist->files})[0] }}",
+);
+END_MAKEFILE
+
+sub write_files {
+ my ($self, $arg) = @_;
+
+ my $content = Text::Template::fill_in_string(
+ $template,
+ HASH => { dist => \$arg->{dist} },
+ DELIMITERS => [ qw( {{ }} ) ],
+ );
+
+ $self->write_content_to($content, $arg->{build_root}->file('Makefile.PL'));
+ $arg->{manifest}->push('Makefile.PL');
+}
+
+1;
18 lib/Dist/Zilla/Plugin/Manifest.pm
@@ -0,0 +1,18 @@
+package Dist::Zilla::Plugin::Manifest;
+use Moose;
+use Moose::Autobox;
+with 'Dist::Zilla::Role::AfterBuild';
+
+sub after_build {
+ my ($self, $arg) = @_;
+
+ my $file = $arg->{build_root}->file('MANIFEST');
+ open my $fh, '>', $file or die "can't open $file for writing: $!";
+
+ print { $fh } "$_\n" for sort ($arg->{manifest}->flatten, 'MANIFEST');
+
+ close $fh or die "can't close $file: $!";
+}
+
+no Moose;
+1;
17 lib/Dist/Zilla/Plugin/ManifestSkip.pm
@@ -0,0 +1,17 @@
+package Dist::Zilla::Plugin::ManifestSkip;
+use Moose;
+with 'Dist::Zilla::Role::FilePruner';
+
+use ExtUtils::Manifest;
+
+sub prune_files {
+ my ($self, $files) = @_;
+
+ # XXX: Totally evil. Just mucking about. -- rjbs, 2008-05-27
+ my $skip = ExtUtils::Manifest::_maniskip;
+ @$files = grep { ! $skip->($_) } @$files;
+ return;
+}
+
+no Moose;
+1;
8 lib/Dist/Zilla/Role/AfterBuild.pm
@@ -0,0 +1,8 @@
+package Dist::Zilla::Role::AfterBuild;
+use Moose::Role;
+
+with 'Dist::Zilla::Role::Plugin';
+requires 'after_build';
+
+no Moose::Role;
+1;
8 lib/Dist/Zilla/Role/FileMunger.pm
@@ -0,0 +1,8 @@
+package Dist::Zilla::Role::FileMunger;
+use Moose::Role;
+
+with 'Dist::Zilla::Role::Plugin';
+requires 'munge_file';
+
+no Moose::Role;
+1;
8 lib/Dist/Zilla/Role/FilePruner.pm
@@ -0,0 +1,8 @@
+package Dist::Zilla::Role::FilePruner;
+use Moose::Role;
+
+with 'Dist::Zilla::Role::Plugin';
+requires 'prune_files';
+
+no Moose::Role;
+1;
19 lib/Dist/Zilla/Role/FileWriter.pm
@@ -0,0 +1,19 @@
+package Dist::Zilla::Role::FileWriter;
+use Moose::Role;
+
+with 'Dist::Zilla::Role::Plugin';
+requires 'write_files';
+
+sub write_content_to {
+ my ($self, $content, $file) = @_;
+
+ open my $out_fh, '>', "$file" or die "can't open $file for writing: $!";
+
+ print { $out_fh } (ref $content ? $$content : $content)
+ or die "error printing to $file: $!";
+
+ close $out_fh or die "error closing $file: $!";
+}
+
+no Moose::Role;
+1;
7 lib/Dist/Zilla/Role/Plugin.pm
@@ -0,0 +1,7 @@
+package Dist::Zilla::Role::Plugin;
+use Moose::Role;
+
+
+
+no Moose::Role;
+1;
15 t/lib/Dist/Zilla/Plugin/CreditTaker.pm
@@ -0,0 +1,15 @@
+package Dist::Zilla::Plugin::CreditTaker;
+use Moose;
+with 'Dist::Zilla::Role::FileMunger';
+
+sub munge_file {
+ my ($self, $arg) = @_;
+ return unless $arg->{to} =~ /\.pm/;
+
+ return if $arg->{content} =~ /\A# built by Dist::Zilla/;
+
+ my $credit = "# build by Dist::Zilla " . Dist::Zilla->VERSION . "\n";
+ $arg->{content} =~ s/\A/$credit/;
+}
+
+1;

0 comments on commit f3854f7

Please sign in to comment.
Something went wrong with that request. Please try again.