| 1 | package Data::Password::zxcvbn::AuthorTools::PackageWriter; | ||
| 2 | use v5.26; | ||
| 3 | use Types::Path::Tiny qw(Dir); | ||
| 4 | use Types::Common::String qw(NonEmptySimpleStr NonEmptyStr); | ||
| 5 | use Types::PerlVersion qw(PerlVersion); | ||
| 6 | use Types::Standard qw(Maybe); | ||
| 7 | use Data::Dumper qw(Dumper); | ||
| 8 | use Moo::Role; | ||
| 9 | our $VERSION = '1.0.2'; # VERSION | ||
| 10 | # ABSTRACT: common role for tools that generate packages | ||
| 11 | |||
| 12 | |||
| 13 | has output_dir => ( | ||
| 14 | is => 'ro', | ||
| 15 | default => $ARGV[1] || 'lib/', | ||
| 16 | isa => Dir, | ||
| 17 | coerce => 1, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has package_name => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => NonEmptySimpleStr, | ||
| 23 | required => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has package_version => ( | ||
| 27 | is => 'ro', | ||
| 28 | defaul => $ARGV[0], | ||
| 29 | isa => Maybe[PerlVersion], | ||
| 30 | coerce => 1, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has package_abstract => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => NonEmptyStr, | ||
| 36 | required => 1, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has package_description => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => NonEmptyStr, | ||
| 42 | required => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | requires 'hash_variable_name'; | ||
| 46 | |||
| 47 | sub write_out { | ||
| 48 | my ($self, $hashref) = @_; | ||
| 49 | |||
| 50 | my $data_str = Data::Dumper->new([$hashref]) | ||
| 51 | ->Indent(1) | ||
| 52 | ->Trailingcomma(1) | ||
| 53 | ->Purity(1) | ||
| 54 | ->Useqq(0) | ||
| 55 | ->Terse(1) | ||
| 56 | ->Quotekeys(1) | ||
| 57 | ->Sortkeys(1) | ||
| 58 | ->Dump; | ||
| 59 | $data_str =~ s[^\{][(]; | ||
| 60 | $data_str =~ s[\};?$][)]; | ||
| 61 | |||
| 62 | my $package_name = $self->package_name; | ||
| 63 | |||
| 64 | my $file = $self->output_dir->child( ($package_name =~ s{::}{/}gr) . '.pm'); | ||
| 65 | my $dir = $file->parent; | ||
| 66 | -d $dir or $file->parent->mkpath or die "failed to create output path $dir: $!"; | ||
| 67 | |||
| 68 | my $fh = $file->openw_utf8; | ||
| 69 | |||
| 70 | print $fh <<"END"; | ||
| 71 | package $package_name; | ||
| 72 | use strict; | ||
| 73 | use warnings; | ||
| 74 | END | ||
| 75 | |||
| 76 | my $package_abstract = $self->package_abstract; | ||
| 77 | my $package_description = $self->package_description; | ||
| 78 | my $hash_variable_name = $self->hash_variable_name; | ||
| 79 | |||
| 80 | if (my $version = $self->package_version) { | ||
| 81 | print $fh <<"END"; | ||
| 82 | our \$VERSION = '$version'; | ||
| 83 | |||
| 84 | =head1 NAME | ||
| 85 | |||
| 86 | $package_name - $package_abstract | ||
| 87 | |||
| 88 | END | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | print $fh <<"END"; | ||
| 92 | # VERSION | ||
| 93 | # ABSTRACT: $package_abstract | ||
| 94 | |||
| 95 | END | ||
| 96 | } | ||
| 97 | |||
| 98 | print $fh <<"EOF"; | ||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | $package_description | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | our %${hash_variable_name} = $data_str; | ||
| 106 | |||
| 107 | 1; | ||
| 108 | EOF | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | __END__ |