| 1 | package Imager::CommandLine::NewTemplate { | ||
| 2 | use MooseX::App; | ||
| 3 | use MooseX::Types::Path::Class; | ||
| 4 | use Path::Class; | ||
| 5 | use Cwd; | ||
| 6 | use Template; | ||
| 7 | use v5.10; | ||
| 8 | |||
| 9 | option version => ( | ||
| 10 | is => 'ro', | ||
| 11 | isa => 'Int', | ||
| 12 | documentation => 'The version of the template type to use', | ||
| 13 | lazy => 1, | ||
| 14 | default => sub { | ||
| 15 | my $self = shift; | ||
| 16 | my $dir = dir ( $self->template_dir, $self->type ); | ||
| 17 | my @dirs = sort map { $_->basename } grep { $_->is_dir } $dir->children; | ||
| 18 | pop @dirs; | ||
| 19 | }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has template_dir => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Path::Class::Dir', | ||
| 25 | lazy => 1, | ||
| 26 | default => sub { | ||
| 27 | dir ( $FindBin::Bin, '..', 'templates', 'ami' ); | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | has _template_base => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'Path::Class::Dir', | ||
| 34 | lazy => 1, | ||
| 35 | default => sub { | ||
| 36 | my $self = shift; | ||
| 37 | # template_dir, template_type, template_ | ||
| 38 | dir ( $self->template_dir, $self->type, $self->version ) | ||
| 39 | } | ||
| 40 | ); | ||
| 41 | |||
| 42 | has _files => ( | ||
| 43 | is => 'ro', | ||
| 44 | lazy => 1, | ||
| 45 | isa => 'ArrayRef[Path::Class::File]', | ||
| 46 | traits => [ 'Array' ], | ||
| 47 | handles => { | ||
| 48 | files => 'elements', | ||
| 49 | }, | ||
| 50 | default => sub { | ||
| 51 | my $self = shift; | ||
| 52 | my @files; | ||
| 53 | while (my $file = $self->_template_base->next) { | ||
| 54 | next unless -f $file; | ||
| 55 | push @files, $file; | ||
| 56 | } | ||
| 57 | return \@files; | ||
| 58 | } | ||
| 59 | ); | ||
| 60 | |||
| 61 | option type => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => 'Str', | ||
| 64 | documentation => 'The template type to use', | ||
| 65 | required => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | parameter name => ( | ||
| 69 | is => 'ro', | ||
| 70 | isa => 'Str', | ||
| 71 | documentation => 'The name for the AMI Class', | ||
| 72 | required => 1, | ||
| 73 | ); | ||
| 74 | |||
| 75 | has destination_dir => ( | ||
| 76 | is => 'ro', | ||
| 77 | isa => 'Str', | ||
| 78 | default => sub { | ||
| 79 | my $self = shift; | ||
| 80 | getcwd; | ||
| 81 | } | ||
| 82 | ); | ||
| 83 | |||
| 84 | has _name_for_path => ( | ||
| 85 | is => 'ro', | ||
| 86 | isa => 'Path::Class::File', | ||
| 87 | lazy => 1, | ||
| 88 | default => sub { | ||
| 89 | my $self = shift; | ||
| 90 | my $file_name = $self->name; | ||
| 91 | $file_name =~ s|::|/|g; | ||
| 92 | file($file_name); | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | |||
| 96 | has _template_engine => ( | ||
| 97 | is => 'ro', | ||
| 98 | default => sub { | ||
| 99 | Template->new( | ||
| 100 | INTERPOLATE => 0, | ||
| 101 | EVAL_PERL => 0, | ||
| 102 | ) || die Template->error(); | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | |||
| 106 | sub run { | ||
| 107 | my ($self) = @_; | ||
| 108 | |||
| 109 | say "Spawning template for ", $self->type; | ||
| 110 | foreach my $file ($self->files) { | ||
| 111 | my $rebase_from = $self->_template_base; | ||
| 112 | my $rebase_to = $self->destination_dir; | ||
| 113 | my $file_path = $file->stringify; | ||
| 114 | |||
| 115 | $file_path =~ s/^$rebase_from/$rebase_to/; | ||
| 116 | $file_path =~ s/NAME/$self->_name_for_path/ge; | ||
| 117 | |||
| 118 | $file_path = file($file_path); | ||
| 119 | |||
| 120 | my $input = $file->slurp; | ||
| 121 | my $output; | ||
| 122 | $self->_template_engine->process( | ||
| 123 | \$input, | ||
| 124 | { name => $self->name, | ||
| 125 | name_path => $self->_name_for_path, | ||
| 126 | type => $self->type, | ||
| 127 | version => $self->version, | ||
| 128 | }, | ||
| 129 | \$output | ||
| 130 | ); | ||
| 131 | # Ensure the path exists | ||
| 132 | $file_path->parent->mkpath; | ||
| 133 | if (-e $file_path) { | ||
| 134 | warn "Not overwritting $file_path"; | ||
| 135 | } else { | ||
| 136 | $file_path->spew($output); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; |