« lib/CallBackery.pm
↑ Summary
blib/lib/Mojolicious/Command/generate/callbackery_app/lib/App/GuiPlugin/Song.pm »
| 1 | package Mojolicious::Command::generate::callbackery_app; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | use File::Basename; | ||
| 4 | use Mojo::Util qw(class_to_file class_to_path); | ||
| 5 | use Mojo::File; | ||
| 6 | |||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Cwd 'getcwd'; | ||
| 9 | use File::Spec::Functions qw(catdir catfile); | ||
| 10 | |||
| 11 | has description => 'Generate CallBackery web application directory structure.'; | ||
| 12 | has usage => sub { shift->extract_usage }; | ||
| 13 | |||
| 14 | has cwd => sub { | ||
| 15 | getcwd(); | ||
| 16 | }; | ||
| 17 | |||
| 18 | sub rel_dir { | ||
| 19 | my $self=shift; | ||
| 20 | catdir $self->cwd, split('/', pop) | ||
| 21 | } | ||
| 22 | |||
| 23 | sub rel_file { | ||
| 24 | my $self =shift; | ||
| 25 | catfile $self->cwd, split('/', pop); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub run { | ||
| 29 | my ($self, $app) = @_; | ||
| 30 | $app ||= 'MyCallBackeryApp'; | ||
| 31 | my @dir = split /\//, $app; | ||
| 32 | my $class = pop @dir; | ||
| 33 | |||
| 34 | die <<EOF unless $class =~ /^[A-Z](?:\w|::)+$/; | ||
| 35 | Your application name has to be a well formed (CamelCase) Perl module name | ||
| 36 | like "MyApp". | ||
| 37 | EOF | ||
| 38 | $self->cwd(join '/', @dir) if @dir; | ||
| 39 | |||
| 40 | my $name = class_to_file $class; | ||
| 41 | my $qxclass = $name; | ||
| 42 | $name =~ s/_/-/g; | ||
| 43 | my $class_path = class_to_path $class; | ||
| 44 | # Configure Main Dir | ||
| 45 | my $file = { | ||
| 46 | 'configure.ac' => 'configure.ac', | ||
| 47 | 'bootstrap' => 'bootstrap', | ||
| 48 | 'PERL_MODULES' => 'PERL_MODULES', | ||
| 49 | 'VERSION' => 'VERSION', | ||
| 50 | 'README' => 'README', | ||
| 51 | 'AUTHORS' => 'AUTHORS', | ||
| 52 | '.gitignore' => '.gitignore', | ||
| 53 | 'LICENSE' => 'LICENSE', | ||
| 54 | 'COPYRIGHT' => 'COPYRIGHT', | ||
| 55 | 'CHANGES' => 'CHANGES', | ||
| 56 | 'Makefile.am' => 'Makefile.am', | ||
| 57 | 'bin/Makefile.am' => 'bin/Makefile.am', | ||
| 58 | 'thirdparty/Makefile.am' => 'thirdparty/Makefile.am', | ||
| 59 | 'etc/Makefile.am' => 'etc/Makefile.am', | ||
| 60 | 'etc/app.cfg' => 'etc/'.$name.'.cfg', | ||
| 61 | 'bin/script.pl' => 'bin/'.$name.'.pl', | ||
| 62 | 'bin/source-mode.sh' => 'bin/'.$name.'-source-mode.sh', | ||
| 63 | 'lib/App.pm' => 'lib/'.$class_path, | ||
| 64 | 'lib/Makefile.am' => 'lib/Makefile.am', | ||
| 65 | 'lib/App/GuiPlugin/Song.pm' => 'lib/'.$class.'/GuiPlugin/Song.pm', | ||
| 66 | 'lib/App/GuiPlugin/SongForm.pm' => 'lib/'.$class.'/GuiPlugin/SongForm.pm', | ||
| 67 | 'frontend/Makefile.am' => 'frontend/Makefile.am', | ||
| 68 | 'frontend/Manifest.json' => 'frontend/Manifest.json', | ||
| 69 | 'frontend/config.json' => 'frontend/config.json', | ||
| 70 | 'frontend/source/class/app/Application.js' => 'frontend/source/class/'.$qxclass.'/Application.js', | ||
| 71 | 'frontend/source/class/app/__init__.js' => 'frontend/source/class/'.$qxclass.'/__init__.js', | ||
| 72 | 'frontend/source/class/app/theme/Theme.js' => 'frontend/source/class/'.$qxclass.'/theme/Theme.js', | ||
| 73 | 'frontend/source/index.html' => 'frontend/source/index.html', | ||
| 74 | 't/basic.t' => 't/basic.t', | ||
| 75 | }; | ||
| 76 | |||
| 77 | my ($userName,$fullName) = (getpwuid $<)[0,6]; | ||
| 78 | $fullName =~ s/,.+//g; | ||
| 79 | chomp(my $domain = `hostname -d`); | ||
| 80 | my $email = $userName.'@'.$domain; | ||
| 81 | |||
| 82 | if ( -r $ENV{HOME} . '/.gitconfig' ){ | ||
| 83 | my $in = Mojo::File->new($ENV{HOME} . '/.gitconfig')->slurp; | ||
| 84 | $in =~ /name\s*=\s*(\S.+\S)/ and $fullName = $1; | ||
| 85 | $in =~ /email\s*=\s*(\S+)/ and $email = $1; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | for my $key (keys %$file){ | ||
| 90 | $self->render_to_rel_file($key, $name.'/'.$file->{$key}, { | ||
| 91 | class => $class, | ||
| 92 | name => $name, | ||
| 93 | qxclass => $qxclass, | ||
| 94 | class_path => $class_path, | ||
| 95 | year => (localtime time)[5]+1900, | ||
| 96 | email => $email, | ||
| 97 | fullName => $fullName, | ||
| 98 | userName => $userName, | ||
| 99 | date => strftime('%Y-%m-%d',localtime(time)), | ||
| 100 | }); | ||
| 101 | } | ||
| 102 | |||
| 103 | $self->chmod_rel_file("$name/bootstrap", 0755); | ||
| 104 | $self->chmod_rel_file("$name/bin/".$name.".pl", 0755); | ||
| 105 | $self->chmod_rel_file("$name/bin/".$name."-source-mode.sh", 0755); | ||
| 106 | |||
| 107 | $self->create_rel_dir("$name/public"); | ||
| 108 | $self->create_rel_dir("$name/templates"); | ||
| 109 | $self->create_rel_dir("$name/frontend/source/resource/$name"); | ||
| 110 | $self->create_rel_dir("$name/frontend/source/translation"); | ||
| 111 | chdir $self->cwd.'/'.$name; | ||
| 112 | system "./bootstrap"; | ||
| 113 | |||
| 114 | say "** Generated App $class in ".$self->cwd.'/'.$name; | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | sub render_data { | ||
| 119 | my ($self, $name) = (shift, shift); | ||
| 120 | Mojo::Template->new->name("template $name") | ||
| 121 | ->render(Mojo::File->new(dirname($INC{'Mojolicious/Command/generate/callbackery_app.pm'}).'/callbackery_app/'.$name)->slurp, @_); | ||
| 122 | } | ||
| 123 | 1; | ||
| 124 | |||
| 125 | =encoding utf8 | ||
| 126 | |||
| 127 | =head1 NAME | ||
| 128 | |||
| 129 | Mojolicious::Command::generate::callbackery_app - Calbackery App generator command | ||
| 130 | |||
| 131 | =head1 SYNOPSIS | ||
| 132 | |||
| 133 | Usage: mojo generate callbackery_app [OPTIONS] [NAME] | ||
| 134 | |||
| 135 | mojo generate callbackery_app | ||
| 136 | mojo generate callbackery_app [/full/path/]TestApp | ||
| 137 | |||
| 138 | Options: | ||
| 139 | -h, --help Show this summary of available options | ||
| 140 | |||
| 141 | =head1 DESCRIPTION | ||
| 142 | |||
| 143 | L<Mojolicious::Command::generate::callbackery_app> generates application directory | ||
| 144 | structures for fully functional L<CallBackery> applications. | ||
| 145 | |||
| 146 | =head1 ATTRIBUTES | ||
| 147 | |||
| 148 | L<Mojolicious::Command::generate::callbackery_app> inherits all attributes from | ||
| 149 | L<Mojolicious::Command> and implements the following new ones. | ||
| 150 | |||
| 151 | =head2 description | ||
| 152 | |||
| 153 | my $description = $app->description; | ||
| 154 | $app = $app->description('Foo'); | ||
| 155 | |||
| 156 | Short description of this command, used for the command list. | ||
| 157 | |||
| 158 | =head2 usage | ||
| 159 | |||
| 160 | my $usage = $app->usage; | ||
| 161 | $app = $app->usage('Foo'); | ||
| 162 | |||
| 163 | Usage information for this command, used for the help screen. | ||
| 164 | |||
| 165 | =head1 METHODS | ||
| 166 | |||
| 167 | L<Mojolicious::Command::generate::callbackery_app> inherits all methods from | ||
| 168 | L<Mojolicious::Command> and implements the following new ones. | ||
| 169 | |||
| 170 | =head2 run | ||
| 171 | |||
| 172 | $app->run(@ARGV); | ||
| 173 | |||
| 174 | Run this command. | ||
| 175 | |||
| 176 | =head1 SEE ALSO | ||
| 177 | |||
| 178 | L<CallBackery>, L<http://callbackery.org>. | ||
| 179 | |||
| 180 | =cut |