« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App.pm »
| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use FindBin; | ||
| 5 | use lib "${FindBin::RealBin}/../lib"; | ||
| 6 | use lib "${FindBin::RealBin}/../local/lib/perl5"; | ||
| 7 | use Cwd qw/abs_path/; | ||
| 8 | use Data::Section::Simple; | ||
| 9 | use File::Path 'mkpath'; | ||
| 10 | use Getopt::Long; | ||
| 11 | use Pod::Usage 'pod2usage'; | ||
| 12 | use String::CamelCase qw(decamelize); | ||
| 13 | use Text::Xslate; | ||
| 14 | use Dwarf::Util qw(write_file); | ||
| 15 | |||
| 16 | my $opts = { output => $FindBin::RealBin . '/../lib' }; | ||
| 17 | GetOptions($opts, 'name=s', 'output=s', 'help'); | ||
| 18 | |||
| 19 | if (@ARGV) { | ||
| 20 | $opts->{name} = $ARGV[0]; | ||
| 21 | $opts->{output} = $ARGV[1] if $ARGV[1]; | ||
| 22 | } | ||
| 23 | |||
| 24 | if (not $opts->{name} or $opts->{help}) { | ||
| 25 | pod2usage; | ||
| 26 | } | ||
| 27 | |||
| 28 | # App が付いてなければ補完 | ||
| 29 | unless ($opts->{name} =~ /^App::/) { | ||
| 30 | $opts->{name} = "App::" . $opts->{name}; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $type = 'Model.pm'; | ||
| 34 | $type = 'Api.pm' if $opts->{name} =~ /::Api/; | ||
| 35 | $type = 'Cli.pm' if $opts->{name} =~ /::Cli/; | ||
| 36 | $type = 'Web.pm' if $opts->{name} =~ /::Web/; | ||
| 37 | |||
| 38 | my $reader = Data::Section::Simple->new('View'); | ||
| 39 | my $tmpl = $reader->get_data_section($type); | ||
| 40 | |||
| 41 | my $tx = Text::Xslate->new; | ||
| 42 | my $content = $tx->render_string($tmpl, $opts); | ||
| 43 | |||
| 44 | my $dst = $opts->{output} . "/" . find_path($opts->{name}); | ||
| 45 | write_file($dst, $content); | ||
| 46 | |||
| 47 | print "created " . abs_path($dst) . "\n"; | ||
| 48 | |||
| 49 | # Cli な場合はラッパーシェルスクリプトも作る | ||
| 50 | if ($opts->{name} =~ /::Cli/) { | ||
| 51 | $opts->{name2} = find_script_name($opts->{name}); | ||
| 52 | $tmpl = $reader->get_data_section('script.sh'); | ||
| 53 | $content = $tx->render_string($tmpl, $opts); | ||
| 54 | $dst = join "/", $FindBin::RealBin, split " ", find_path($opts->{name2}, 'sh'); | ||
| 55 | write_file($dst, $content); | ||
| 56 | print "created $dst\n"; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub find_path { | ||
| 60 | my ($name, $ext) = @_; | ||
| 61 | $ext //= 'pm'; | ||
| 62 | $name =~ s/::/\//g; | ||
| 63 | $name .= "." . $ext; | ||
| 64 | return $name; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub find_script_name { | ||
| 68 | my $name = shift; | ||
| 69 | return $name unless $name =~ /^.+::Cli::(.+)/; | ||
| 70 | my @a = map { decamelize $_ } split "::", $1; | ||
| 71 | return join " ", @a; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 SYNOPSIS | ||
| 75 | |||
| 76 | ./generate.pl CLASS_NAME [-o OUTPUT_DIR] | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | package View; | ||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | __DATA__ |