Welcome Guest, Not a member yet? Register   Sign In
Rails-like Generator Script
#1

[eluser]kilishan[/eluser]
Hey all,

Here's a very early release of a generate and destroy script, much like what Ruby on Rails has for creating models, scaffolding, etc. I've been out of touch with CI for a while (lots of sites in Modx), but now have 2 large sites that I'm getting ready to start coding on with CI. I searched for a bit and couldn't find anything that I really liked to be able to speed up the development process, so I whipped this together over a couple of days last week.

If you are not familiar with the Rails generators, they allow for some pretty complex actions, but, at their core, are basically creating/modifying files and/or directories. This makes it easy to install complex modules or scaffold an entire app in seconds. (NOTE: the scaffolding feature is not in place, yet, though the framework to allow us to create that is.)

While I know this may turn some of you off, it is a command-line tool. It currently only ships with model and controller creators, and I know they're a little rough. Specifically, models currently don't do much other than create the model file. That will be expanded out shortly. But it's not the generators that ship with it that are the key, it's the generate and destroy scripts themselves that I wanted to give you a chance to play use. As time goes on, we can expand the number of generators available, and the power of the scripts themselves.

Download here.

To setup, unzip the file and place the script directory in your application folder. You'll want to modify the config.php file in that directory to reflect your setup. I keep a single CI folder out of my web-root that is shared between all of the sites, so I placed my generators directory in that CI directory to be shared with all sites. While preparing this zip, I may have screwed the generators path up, so make sure to check that.

To use, drop into the command line and navigate to your application folder. To generate a new model, type:

Code:
php script/generate.php model ModelName


The controller works the same, though it has more functionality right now. Any options placed after the ControllerName will be turned into functions within the controller. For example:

Code:
php script/generate.php controller ControllerName index function1 function2

will create a new controller with 3 functions already in place.

To get rid of that file, you can use:

Code:
php script/destroy.php controller ControllerName

Sure, it seems kind of pointless for this example to use the destroy script, but if you use my Ocular layout library, that's also included and a single command will either install all of the files and directories you need or remove it.

Like I said, these are bare-bones examples. The interesting part is the generator script itself. You can easily create your own scripts by extending the BaseGenerator and NamedGenerator classes. I haven't had a chance to pull together documentation yet, so just look at the example generators to get an idea of what can be done so far.

I'm hoping this is of interest to other people and we can all kick the tires together to create a great time-saver for all involved. My next project on this is the scaffolding creation (so you can actually edit the files), and possibly some form generators, though it looks like there have been some nice ones of those crop up while I was away. It may be a couple of weeks before I get back to it, though, since I have a site to get launched in the next week or two. I'm hoping that as you kick the tires, though, we can compile a list of items that can be improved on it, and be ready to go as soon as time frees up. Unless someone gets inspired and makes the changes before I get to it. Smile

Looking forward to hearing your comments and criticisms.
#2

[eluser]Dam1an[/eluser]
Sounds good, I actually started building a little something like this myself
If yours is similar to what I had anyway (the style of models, views and controllers) then we could maybe develop it abit more together (if you want)
#3

[eluser]kilishan[/eluser]
[quote author="Dam1an" date="1240947163"]Sounds good, I actually started building a little something like this myself
If yours is similar to what I had anyway (the style of models, views and controllers) then we could maybe develop it abit more together (if you want)[/quote]

I'm all for getting help on this. That's part of why I posted. Smile Well, that and because I know that I've seen interest in this type of a thing before.
#4

[eluser]The Wizard[/eluser]
would love if i could be a help in a way, though i don't have the time right now, i would
definitely look forward for helping.

http://ellislab.com/forums/viewthread/112969/
#5

[eluser]The Wizard[/eluser]
i've sent you an email. i hope you receive it fast Smile
take care
#6

[eluser]The Wizard[/eluser]
Hello Smile
While loonie was sleeping, i was able to build a generator that builds a form
and a controller based on a table like RoR Smile

here is the output that is automatically generated:
Code:
class Test extends Controller
{

    function index()
    {
        echo 'test';
    }    


    function create( )
    {
      
        switch ( $_SERVER ['REQUEST_METHOD'] )
        {
    
            case 'GET':
    
                $data['values']['id'];
                $data['values']['test'];
                $data['values']['foo'];
                $data['values']['bar'];

    
    
                $this->load->view( 'test', $data );  
    
            break;
    
            case 'POST':
    
                /* we set the rules */
                /* dont forget to edit these */
    
                $this->form_validation->set_rules( 'id', lang('id'), 'required' );
                $this->form_validation->set_rules( 'test', lang('test'), 'required' );
                $this->form_validation->set_rules( 'foo', lang('foo'), 'required' );
                $this->form_validation->set_rules( 'bar', lang('bar'), 'required' );

    
    
                if ( $this->form_validation->run() == FALSE )
                {
    
                    $data['values']['id'] = set_value( 'id' );
                    $data['values']['test'] = set_value( 'test' );
                    $data['values']['foo'] = set_value( 'foo' );
                    $data['values']['bar'] = set_value( 'bar' );

    
                    $this->load->view( 'test', $data );
                }
                else
                {
    
                    $data_post['id'] = $this->input->post( 'id' );
                    $data_post['test'] = $this->input->post( 'test' );
                    $data_post['foo'] = $this->input->post( 'foo' );
                    $data_post['bar'] = $this->input->post( 'bar' );

    
                    die('Process Data. ( $data['values'] ) ');
                    //TODO: insert
                    redirect('redirect_url');
    
                }
    
    
            break;
            
    
            default:
            break;
        }

                
    }

}

what do you guys think?
#7

[eluser]Jonas G[/eluser]
Thanks for the contribution. I like your tool as I hate copy-pasting the same stuff over and over again.

Even though you have a config file that lets you select your application folder path, the script uses

Code:
$this->dest_dir     = substr(dirname(__FILE__), 0, -22);
in generator.class.php line 33. I had to change that to
Code:
$this->dest_dir     = substr(dirname(__FILE__), 0, -10);
because I rename application to app.

Looking forward to future updates
Jonas
#8

[eluser]The Wizard[/eluser]
ok here we go Smile
this is my element of generation

hopefully it will join with this project and we have a cool big generator
that does a lot of stuff.


Code Generator v0.8
#9

[eluser]kilishan[/eluser]
herrkaleun - I'll look at it as I have time this week. It's going to be a busy week though, since I've got a site launch on Friday that still has a bit that has to be tested and tweaked.

Jonas - Good catch. That was a hacky way for me to implement that, wasn't it? Smile I'll fix it as I'm working on the next release.

For those that are watching this project, here's the roadmap, if we're calling my previous version 0.1. Note that this is just referring to the generate/destroy scripts themselves, not the generators that come with it.

0.2 - Integrate into CI so that it can be used from either the command line or through a web browser. I've figured out how to do this, just have to implement it.

0.3 - Allow generators to call other generators.

0.4 - Create a "hooks" system for the generators. This will allow users to pick and choose which libraries they want to be used during generator processing. For example, if you have 4 different view libraries installed, you can specify in the config file which view library is used for the controller generation (or anything else that might need to use it). Then, when the controller generator is running, it calls the appropriate hook to create that "show view" command (or "add data to view" command, or whatever.) Still working out the best way to handle this.

0.5 - Integrate the Spyc YAML parser into the system so that we can use YAML for the generator instructions, instead of the nested arrays that it currently uses.

That's all that's on the map for now. The rest will be creating usable generators that are complete enough to be used, but flexible enough that they're not a pain to modify.

If anyone has ideas they'd like to see incorporated speak up. While I'm doing this primarily for my own use, I want to make it flexible enough that it can benefit everyone.
#10

[eluser]kilishan[/eluser]
Version 0.2 of the CI Tools library has been released. You can grab it at: http://ci-tools.googlecode.com.

This release integrates the scripts with CodeIgniter itself, so that all of the CodeIgniter libraries can be used within your own generators. It also allows direct access to all of the apps config settings (like database settings, etc). Actually, it uses a new frontend script that runs your app just like it would from a browser, except by using the command line, so you could use the cli.php script to run cron jobs off of.

This also means that it is theoretically possible to run the scripts through the browser now, though that functionality has not been fully tested yet.

Note that the generators themselves have not been updated, except as needed to function within the new environment. I've got one or two items I want to get into the library before working on the generators themselves. (See the roadmap above).

I've also started putting up documentation at the googlecode pages.

I've noticed a fair number of downloads but very little comments here. I hope that doesn't mean it wasn't working for you. If that's the case, hopefully the new release fixes that (as a number of small bugs were squashed) and the new docs should help everyone out.

If you're having other problems with it, though, please let me know.




Theme © iAndrew 2016 - Forum software by © MyBB