Giter Site home page Giter Site logo

kberov / mojolicious-command-generate-resources Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 117 KB

Routes from database for your application

License: Artistic License 2.0

Perl 100.00%
mojolicious perl mojolicious-command web web-framework crud rest generator command

mojolicious-command-generate-resources's Introduction

NAME

Mojolicious::Command::Author::generate::resources - Generate MVC & OpenAPI RESTful API files from database tables

SYNOPSIS

Usage: APPLICATION generate resources [OPTIONS]

  my_app.pl generate help resources # help with all available options
  my_app.pl generate resources --tables users,groups
  my_app.pl generate resources --tables users,groups -D dbx

PERL REQUIREMENTS

This command uses "signatures" in feature, therefore Perl 5.20 is required.

DESCRIPTION

An usable release...

Mojolicious::Command::generate::resources generates directory structure for a fully functional MVC set of files, routes and RESTful API specification in OpenAPI format based on existing tables in your application's database.

The purpose of this tool is to promote RAD by generating the boilerplate code for model (M), templates (V) and controller (C) and help programmers to quickly create well structured, fully functional applications. It assumes that you already have tables created in a database and you just want to generate CRUD actions for them.

In the generated actions you will find eventually working code for reading, creating, updating and deleting records from the tables you specified on the command-line. The generated code is just boilerplate to give you a jump start, so you can concentrate on writing your business-specific code. It is assumed that you will modify the generated code to suit your specific needs. All the generated code is produced from templates. You can copy the folder with the templates, push it to @{$app->renderer->paths} and modify to your taste. Please look into the t/blog folder of this distribution for examples.

The command expects to find and will use one of the commonly used helpers pg, mysql sqlite. The supported wrappers are respectively Mojo::Pg, Mojo::mysql and Mojo::SQLite.

OPTIONS

Below are the options this command accepts, described in Getopt::Long notation. Both short and long variants are shown as well as the types of values they accept. All of them, beside --tables, are guessed from your application and usually do not need to be specified.

H|home_dir=s

Optional. Defaults to app->home (which is MyApp home directory). Used to set the root directory to which the files will be dumped. If you set this option, respectively the lib and api folders will be created under the new home_dir. If you want them elsewhere, set these options explicitly.

L|lib=s

Optional. Defaults to app->home/lib (relative to the --home_dir directory). If you installed MyApp in some custom path and you wish to generate your controllers into e.g. site_lib, set this option.

api_dir=s

Optional. Directory where the OpenAPI json file will be generated. Defaults to app->home/api (relative to the --home_dir directory). If you installed MyApp in some custom path and you wish to generate your OpenApi files into for example site_lib/MyApp/etc/api, set this option explicitly.

C|controller_namespace=s

Optional. The namespace for the controller classes to be generated. Defaults to app->routes->namespaces->[0], usually MyApp::Controller, where MyApp is the name of your application. If you decide to use another namespace for the controllers, do not forget to add it to the list app->routes->namespaces in myapp.conf or your plugin configuration file. Here is an example.

# Setting the Controller class from which all controllers must inherit.
# See /perldoc/Mojolicious/#controller_class
# See /perldoc/Mojolicious/Guides/Growing#Controller-class
app->controller_class('MyApp::C');

# Namespace(s) to load controllers from
# See /perldoc/Mojolicious#routes
app->routes->namespaces(['MyApp::C']);

M|model_namespace=s

Optional. The namespace for the model classes to be generated. Defaults to MyApp::Model.

T|templates_root=s

Optional. Defaults to app->renderer->paths->[0]. This is usually app->home/templates directory. If you want to use another directory, do not forget to add it to the app->renderer->paths list in your configuration file. Here is how to add a new directory to app->renderer->paths in myapp.conf.

# Application/site specific templates
# See /perldoc/Mojolicious/Renderer#paths
unshift @{app->renderer->paths}, $home->rel_file('site_templates');

D|db_helper=s

Optional. If passed, this method name will be used when generating Model classes and helpers. The application is still expected to support the unified API of the supported database adapters. This feature helps to generate code for an application that wants to support all the three adaptors or if for example tomorrow suddenly appears a Mojo::Oracle tiny wrapper around DBD::Oracle.

t|tables=s@

Mandatory. List of tables separated by commas for which controllers should be generated.

SUPPORT

Please report bugs, contribute and make merge requests on Github.

ATTRIBUTES

Mojolicious::Command::generate::resources inherits all attributes from Mojolicious::Command and implements the following new ones.

args

Used for storing arguments from the command-line.

my $args = $self->args;

description

my $description = $command->description;
$command        = $command->description('Foo!');

Short description of this command, used for the ~$ mojo generate commands list.

routes

$self->routes;

Returns an ARRAY reference containing routes, prepared after $self->args->{tables}. Suggested Perl code for the routes is dumped in a file named TODO in --homedir so you can copy and paste into your application code.

usage

my $usage = $command->usage;
$command  = $command->usage('Foo!');

Usage information for this command, used for the help screen.

METHODS

Mojolicious::Command::generate::resources inherits all methods from Mojolicious::Command and implements the following new ones.

run

Mojolicious::Command::generate::resources->new(app=>$app)->run(@ARGV);

Run this command.

render_template_to_file

Renders a template from a file to a file using Mojo::Template. Parameters: $tmpl_file - full path tho the template file; $target_file - full path to the file to be written; $template_args - a hash reference containing the arguments to the template. See also "render_to_file" in Mojolicious::Command.

$self->render_template_to_file($tmpl_file, $target_file, $template_args);

generate_formfields

Generates form-fields from columns information found in the respective table. The result is put into _form.html.ep. The programmer can then modify the generated form-fields.

$form_fields = $self->generate_formfields($table_name);

generate_openapi

Generates Open API file in json format. The generated file is put in "--api_dir". The filename is api.json. This is the file which will be loaded by MyApp.

generate_path_api

Generates API definitions and paths for each table. Invoked in "generate_openapi". Paramaters: $t - the table name; $api_defs_object - the object API definition, based on the table name; $tmpl_args - the arguments for the templates. $api_defs_object and $tmpl_args will be enriched with additional key-value pairs as required by the OpenAPI specification. Returns void.

generate_validation

Generates code for the _validation method in the respective controler.

$validation_code = $self->generate_validation($table_name);

TODO

The work on the features may not go in the same order specified here. Some parts may be fully implemented while others may be left for later.

- Improve documentation.
- Add initial documentation stub to the generated classes.
- Improve templates to generate code to which is more ready to use.
- Append to the existing api.json if it already exists. More tests.

AUTHOR

Красимир Беров
CPAN ID: BEROV
[email protected]

COPYRIGHT

This program is free software licensed under

Artistic License 2.0

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO

Mojolicious::Command::generate, Mojolicious::Command, Mojolicious, Mojolicious::Plugin::OpenAPI, Perl.

mojolicious-command-generate-resources's People

Contributors

kberov avatar reneeb avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

reneeb

mojolicious-command-generate-resources's Issues

Undeclared dependency Mojo::SQLite

t/002_crud.t fails if Mojo::SQLite is not installed:

Useless use of anonymous hash ({}) in void context at t/002_crud.t line 94.
Can't locate Mojo/SQLite.pm in @INC (you may need to install the Mojo::SQLite module) (@INC contains: /var/tmp/cpansmoker-1023/2018011206/resourcesNuZA/blog/lib /home/cpansand/.cpan/build/2018011206/Mojolicious-Command-generate-resources-0.04-0/blib/lib /home/cpansand/.cpan/build/2018011206/Mojolicious-Command-generate-resources-0.04-0/blib/arch /home/cpansand/.cpan/build/2018011206/Mojolicious-7.60-0/blib/arch /home/cpansand/.cpan/build/2018011206/Mojolicious-7.60-0/blib/lib /home/cpansand/.cpan/build/2018011206/Mojolicious-7.60-0/blib/arch /home/cpansand/.cpan/build/2018011206/Mojolicious-7.60-0/blib/lib /usr/perl5.26.1p/lib/site_perl/5.26.1/amd64-freebsd /usr/perl5.26.1p/lib/site_perl/5.26.1 /usr/perl5.26.1p/lib/5.26.1/amd64-freebsd /usr/perl5.26.1p/lib/5.26.1 .) at /var/tmp/cpansmoker-1023/2018011206/resourcesNuZA/blog/lib/Blog.pm line 5.
BEGIN failed--compilation aborted at /var/tmp/cpansmoker-1023/2018011206/resourcesNuZA/blog/lib/Blog.pm line 5.
Compilation failed in require at t/002_crud.t line 40.
# Tests were run but no plan was declared and done_testing() was not seen.
# Looks like your test exited with 2 just after 1.
t/002_crud.t .. 
Dubious, test returned 2 (wstat 512, 0x200)
All 1 subtests passed 

Test suite started to fail (with Mojolicious 8.27?)

A sample fail report: http://www.cpantesters.org/cpan/report/d93a4d48-3e13-11ea-9df8-533a5b1f184e

Statistical analysis suggests that the breakage started with Mojolicious 8.27:

****************************************************************
Regression 'mod:Mojolicious'
****************************************************************
Name           	       Theta	      StdErr	 T-stat
[0='const']    	      1.0000	      0.0000	55661404870717376.00
[1='eq_8.02']  	      0.0000	      0.0000	   0.00
[2='eq_8.05']  	      0.0000	      0.0000	   2.05
[3='eq_8.07']  	      0.0000	      0.0000	   2.39
[4='eq_8.08']  	      0.0000	      0.0000	   1.22
[5='eq_8.09']  	      0.0000	      0.0000	   0.24
[6='eq_8.12']  	     -0.0000	      0.0000	  -0.34
[7='eq_8.13']  	     -0.0000	      0.0000	  -0.34
[8='eq_8.14']  	     -0.0000	      0.0000	  -0.83
[9='eq_8.15']  	      0.0000	      0.0000	   1.36
[10='eq_8.16'] 	     -0.0000	      0.0000	  -2.44
[11='eq_8.17'] 	      0.0000	      0.0000	   2.73
[12='eq_8.26'] 	     -0.0000	      0.0000	  -1.47
[13='eq_8.27'] 	     -1.0000	      0.0000	-18763455754174240.00
[14='eq_8.30'] 	     -1.0000	      0.0000	-12293547766686428.00
[15='eq_8.31'] 	     -1.0000	      0.0000	-16976569330977352.00
[16='eq_8.32'] 	     -1.0000	      0.0000	-31857886981972700.00

R^2= 1.000, N= 104, K= 17
****************************************************************

Test suite started to fail

I see the following new test failures on my smokers:

Unknown command "resources", maybe you need to install it?
t/002_crud.t ...... 
Dubious, test returned 2 (wstat 512, 0x200)
No subtests run 
Unknown command "resources", maybe you need to install it?
t/003_open_api.t .. 
Dubious, test returned 2 (wstat 512, 0x200)
No subtests run 

Statistical analysis shows two dependencies which might cause the failure:

  • Mojolicious >= 8.01
  • Mojolicious::Plugin::OpenAPI = 2.00

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.