Generating Orchard content parts via command-line

Tags: orchard, codegen, parts, command, cli

Creating a content part in Orchard, even the most trivial one, involves creating many small objects – handler, driver, model (main part object), record, display and editor shapes to name a few. As I was tired of hand-creating/copy-pasting all those every time I needed a content part I wrote a simple Orchard module that automates the process (being a dev is about making life easier, right?)Uśmiech I called that module Code Generation Extensions, as it generally extends the default codegen features.

Module adds an Orchard command-line command “codegen part”. It’s syntax is as follows:

codegen part <module_name> <part_name> [/Properties:<comma-delimited list of name:type pairs>]

For example:

codegen part Modules.Shop ProductPart /Properties: Name:string, Price:int

Properties is an optional parameter, so if you’d like to create an empty part you can just write

codegen part Modules.Shop ProductPart

The command creates a handler, driver, model, record, display and editor shapes and updates the Placement.info file with default Content:before placement for your part shape. If you provide /Properties parameter, the model, record and editor shapes will be filled with appropriate code accordingly.

You can download the module from the Orchard Gallery. Feel free to play with it and I hope you find it useful!Uśmiech

Cheers!

12 Comments

  • springy said

    Sounds useful :) Will it modify Migrations.cs?

    BTW: Please don't use url shortening services on non-twitter content.

  • pszmyd said

    @springy: No, because it would double the existing functionality - codegen migrations, which should be used after creating all the necessary parts.

    I use the shortened links for tracking, but ok - it's unclear where they lead - changed those to real URLs.

  • technics said

    Very useful, although I think it is important to force people to write stuff manually when they are learning Orchard, as it helps to reinforce those concepts and make them 'stick'.

    On the other hand, I've written 3 modules in the last 2 or 3 weeks and each time have forgotten either the handler, the placement.info, or both, so I can fully appreciate the handiness of this tool!

  • pszmyd said

    @technics: Right, a generator which does all the stuff isn't clearly an excuse to not having to read docs. It's a tool for people who know what's going on and want just to save time.

    Had exactly the same. Every time I've been writing a module I forgot to add some elements and wondered "why the hell my part isn't displaying?!?!":)

  • Pete Hurst said

    This is great - I kept thinking about looking into codegen to try and do this.

    In all honesty I think this would be great when learning. It's a really steep curve having to understand the purpose of so many different pieces before you can see anything.

    But with this you can have a tutorial that pretty much instantly gives you a new part in the Dashboard; and then walks you through making small changes to each of the components to learn why they're there and what they do.

    It's much clearer that way, and there's none of the confusion of "Why isn't anything showing? What have I missed this time?"

    Ok, I've got a tutorial to write...

  • AlexMilo said

    Its important to note that you can't use spaces between the properties elements like in your example

  • Pete Hurst said

    I've tried it out now and it's amazing. The only thing that could make it any more amazing would be if it generated Settings as well :)

  • pszmyd said

    @AlexMilo: I'll check this out why it's happening like that and post a relevant bugfix.

    @Pete Hurst: Well, I thought about that (part type definition settings and site settings):) I'm gonna add Settings generation as an optional property as they are used only with a rather small subset of parts (I didn't want to create too much files).

  • Steve said

    thnkx much for the contribution!

    +1 to Peter for the idea that this makes for a great learning tool - makes for immediate feedback re: discovery and exploration.

  • Pete Hurst said

    Perhaps settings generation could be a second command. So you can add settings to a part after creation.

    BTW I'm having a bit of a problem where it's not finding my module. It might be because I'm using Directory Junctions (similar to soft links) to include the projects in the modules folder. The only other unusual thing is that the module has several features. Would also it be possible to have the codegen add to a specific feature?

  • mit said

    our team has tried to execute the command line but not successfully create the expected result. The command: codegen part aModule ProductPart/Properties: Name:string, Price:int

    Please give us advice. Thank you very much

  • pszmyd said

    @mit: You have to put space before each switch ("/Properties" in this case) and no spaces between each property. So should be "codegen part aModule ProductPart /Properties:Name:string,Price:int"