[boxer] 13/33: Add Boxer::File::WithSkeleton class.

Jonas Smedegaard dr at jones.dk
Sun Nov 5 18:28:17 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/1.1.5-1
in repository boxer.

commit f9d1a20f34cfa8465cf97aa75466a9aff4c01d87
Author: Jonas Smedegaard <dr at jones.dk>
Date:   Sun Jul 3 22:21:21 2016 +0200

    Add Boxer::File::WithSkeleton class.
---
 lib/Boxer/File/WithSkeleton.pm | 160 +++++++++++++++++++++++++++++++++++++++++
 lib/Boxer/Task/Serialize.pm    |  53 ++++++--------
 lib/Boxer/Types.pm             |  10 ++-
 meta/makefile.pret             |   2 +-
 4 files changed, 191 insertions(+), 34 deletions(-)

diff --git a/lib/Boxer/File/WithSkeleton.pm b/lib/Boxer/File/WithSkeleton.pm
new file mode 100644
index 0000000..48eebca
--- /dev/null
+++ b/lib/Boxer/File/WithSkeleton.pm
@@ -0,0 +1,160 @@
+package Boxer::File::WithSkeleton;
+
+=encoding UTF-8
+
+=cut
+
+use v5.14;
+use utf8;
+use strictures 2;
+use version;
+use Role::Commons -all;
+
+use Path::Tiny;
+use Template::Tiny;
+use File::ShareDir qw(dist_dir);
+
+use Moo;
+use Types::Standard qw(Maybe);
+use Types::TypeTiny qw(HashLike);
+use Types::Path::Tiny qw(Dir File Path);
+use Boxer::Types qw(SkelDir Basename);
+
+use namespace::clean;
+
+=head1 VERSION
+
+Version v1.1.4
+
+=cut
+
+our $VERSION = version->declare("v1.1.4");
+
+# permit callers to sloppily pass undefined values
+sub BUILDARGS
+{
+	my ( $class, %args ) = @_;
+	delete @args{ grep !defined( $args{$_} ), keys %args };
+	return {%args};
+}
+
+has basename => (
+	is  => 'ro',
+	isa => Basename,
+);
+
+has file => (
+	is  => 'lazy',
+	isa => Basename,
+
+	default => sub {
+		if ( $_[0]->basename ) {
+			return $_[0]->basename;
+		}
+		elsif ( $_[0]->skeleton_suffix ) {
+			return $_[0]->skeleton_path->basename( $_[0]->skeleton_suffix );
+		}
+	},
+);
+
+has file_path => (
+	is       => 'lazy',
+	isa      => Path,
+	required => 1,
+	default  => sub {
+		if ( $_[0]->file_dir and $_[0]->file ) {
+			return $_[0]->file_dir->child( $_[0]->file );
+		}
+	},
+);
+
+has file_dir => (
+	is      => 'lazy',
+	isa     => Dir,
+	default => sub { path('.') },
+);
+
+has skeleton => (
+	is      => 'lazy',
+	isa     => Basename,
+	default => sub {
+		if (    $_[0]->basename
+			and $_[0]->skeleton_dir
+			and $_[0]->skeleton_suffix )
+		{
+			return $_[0]->skeleton_dir->child(
+				$_[0]->basename . $_[0]->skeleton_suffix )->basename;
+		}
+	},
+);
+
+has skeleton_path => (
+	is       => 'lazy',
+	isa      => File,
+	required => 1,
+	default  => sub {
+		if ( $_[0]->skeleton_dir and $_[0]->skeleton ) {
+			return $_[0]->skeleton_dir->child( $_[0]->skeleton );
+		}
+	},
+);
+
+has skeleton_dir => (
+	is      => 'lazy',
+	isa     => SkelDir,
+	default => sub { path( dist_dir('Boxer'), 'skel' ) },
+);
+
+has skeleton_suffix => (
+	is      => 'ro',
+	isa     => Basename,
+	default => '.in',
+);
+
+has vars => (
+	is       => 'ro',
+	isa      => HashLike,
+	required => 1,
+);
+
+sub create
+{
+	my $self = shift;
+
+	my $template = Template::Tiny->new(
+		TRIM => 1,
+	);
+
+	my $content = '';
+	$template->process(
+		\$self->skeleton_path->slurp,
+		$self->vars,
+		\$content
+	);
+	$self->file_path->spew( $content . "\n" );
+}
+
+=head1 AUTHOR
+
+Jonas Smedegaard C<< <dr at jones.dk> >>.
+
+=cut
+
+our $AUTHORITY = 'cpan:JONASS';
+
+=head1 COPYRIGHT AND LICENCE
+
+Copyright © 2013-2016 Jonas Smedegaard
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+
+=cut
+
+1;
diff --git a/lib/Boxer/Task/Serialize.pm b/lib/Boxer/Task/Serialize.pm
index 0d11779..edcafb6 100644
--- a/lib/Boxer/Task/Serialize.pm
+++ b/lib/Boxer/Task/Serialize.pm
@@ -10,11 +10,10 @@ use strictures 2;
 use version;
 use Role::Commons -all;
 use autodie;
-use Carp qw<croak>;
 
 use Path::Tiny;
-use Template::Tiny;
 use File::ShareDir qw(dist_dir);
+use Boxer::File::WithSkeleton;
 
 use Moo;
 use Types::Standard qw( Bool Str Undef InstanceOf );
@@ -42,48 +41,36 @@ has skeldir => (
 	is       => 'ro',
 	isa      => SkelDir,
 	coerce   => 1,
-	required => 1,
-	default  => sub { path( dist_dir('Boxer'), 'skel' ) },
 );
 
 has infile => (
-	is       => 'lazy',
+	is       => 'ro',
 	isa      => File,
 	coerce   => File->coercion,
-	required => 1,
-	default  => sub { $_[0]->skeldir->child('preseed.cfg.in') },
 );
 
 has altinfile => (
-	is       => 'lazy',
+	is       => 'ro',
 	isa      => File,
 	coerce   => File->coercion,
-	required => 1,
-	default  => sub { $_[0]->skeldir->child('script.sh.in') },
 );
 
 has outdir => (
 	is       => 'ro',
 	isa      => Dir,
 	coerce   => Dir->coercion,
-	required => 1,
-	default  => sub { path('.') },
 );
 
 has outfile => (
-	is       => 'lazy',
+	is       => 'ro',
 	isa      => Path,
 	coerce   => Path->coercion,
-	required => 1,
-	default  => sub { $_[0]->outdir->child('preseed.cfg') },
 );
 
 has altoutfile => (
-	is       => 'lazy',
+	is       => 'ro',
 	isa      => Path,
 	coerce   => Path->coercion,
-	required => 1,
-	default  => sub { $_[0]->outdir->child('script.sh') },
 );
 
 has node => (
@@ -111,12 +98,6 @@ sub run
 	my $pkgautolist = join( ' ',      sort @{ $world->pkgs_auto } );
 	my $tweaklist   = join( ";\\\n ", @{ $world->tweaks } );
 
-	$self->outdir->mkpath;
-
-	my $template = Template::Tiny->new(
-		TRIM => 1,
-	);
-
 	my %vars = (
 		node        => $self->node,
 		suite       => $world->epoch,
@@ -127,6 +108,15 @@ sub run
 		pkgautolist => $pkgautolist,
 	);
 
+	Boxer::File::WithSkeleton->new(
+		basename      => 'preseed.cfg',
+		skeleton_dir  => $self->skeldir,
+		skeleton_path => $self->infile,
+		file_dir      => $self->outdir,
+		file_path     => $self->outfile,
+		vars          => \%vars,
+	)->create;
+
 	my %altvars = %vars;
 	$altvars{tweaklist} =~ s,chroot\s+/target\s+,,g;
 	$altvars{tweaklist} =~ s,/target/,/,g;
@@ -134,13 +124,14 @@ sub run
 	# TODO: maybe move below (or only $''{ part?) to reclass parser
 	$altvars{tweaklist} =~ s/\\\K''(?=n)|\$\K''(?=\{)//g;
 
-	my $altcontent = '';
-	$template->process( \$self->altinfile->slurp, \%altvars, \$altcontent );
-	$self->altoutfile->spew( $altcontent . "\n" );
-
-	my $content = '';
-	$template->process( \$self->infile->slurp, \%vars, \$content );
-	$self->outfile->spew( $content . "\n" );
+	Boxer::File::WithSkeleton->new(
+		basename      => 'script.sh',
+		skeleton_dir  => $self->skeldir,
+		skeleton_path => $self->altinfile,
+		file_dir      => $self->outdir,
+		file_path     => $self->altoutfile,
+		vars          => \%altvars,
+	)->create;
 }
 
 =head1 AUTHOR
diff --git a/lib/Boxer/Types.pm b/lib/Boxer/Types.pm
index 871d18c..a5bb131 100644
--- a/lib/Boxer/Types.pm
+++ b/lib/Boxer/Types.pm
@@ -10,11 +10,13 @@ use strictures 2;
 use version;
 use Role::Commons -all;
 
+use Path::Tiny;
+
 use Type::Library -base,
-	-declare => qw( DataDir ClassDir NodeDir SkelDir Suite );
+	-declare => qw( DataDir ClassDir NodeDir SkelDir Basename Suite );
 use Type::Utils -all;
+use Types::Common::String qw(NonEmptySimpleStr LowerCaseSimpleStr);
 use Types::Path::Tiny qw(Dir);
-use Types::Common::String qw(LowerCaseSimpleStr);
 
 use namespace::clean;
 
@@ -42,6 +44,10 @@ declare SkelDir, as Dir,
 	coercion => 1,
 	message {'Must be an existing directory containing boxer skeleton files'};
 
+declare Basename, as NonEmptySimpleStr,
+	where { $_ eq path($_)->basename },
+	message {'Must be a bare filename with no directory parts'};
+
 declare Suite, as LowerCaseSimpleStr,
 	coercion => 1,
 	message {'Must be a single lowercase word'};
diff --git a/meta/makefile.pret b/meta/makefile.pret
index 1a90aa1..03f357e 100644
--- a/meta/makefile.pret
+++ b/meta/makefile.pret
@@ -17,7 +17,7 @@
 	:runtime-requirement  [ :on "Log::Any::Adapter::Screen 0"^^:CpanId ];
 	:runtime-requirement  [ :on "namespace::clean 0"^^:CpanId ];
 	:runtime-requirement  [ :on "MooX::Types::MooseLike::Base 0"^^:CpanId ];
-	:runtime-requirement  [ :on "Path::Tiny 0"^^:CpanId ];
+	:runtime-requirement  [ :on "Path::Tiny 0.054"^^:CpanId ];
 	:runtime-requirement  [ :on "Role::Commons 0"^^:CpanId ];
 	:runtime-requirement  [ :on "Template::Tiny 0.11"^^:CpanId ];
 	:runtime-requirement  [ :on "Try::Tiny 0"^^:CpanId ];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/boxer.git



More information about the Pkg-perl-cvs-commits mailing list