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