Author Topic: Template Engine extension for Code::Blocks  (Read 7243 times)

Offline carra

  • Multiple posting newcomer
  • *
  • Posts: 114
Template Engine extension for Code::Blocks
« on: September 28, 2012, 12:31:12 am »
What is this?
In my projects with C::B I saw the need to automatically generate various types of text files from templates. These include not only C++ headers/source, but also CodeBlocks project and workspace files (which are XML and therefore, text).

I have been able to develop scripts that can process template files with embedded CodeBlocks script commands, much like what current C::B abbreviations do. This was initially thought just to meet my purposes, but I decided to go the extra mile to make it more general, and offer it for future CodeBlocks versions, in case you guys are interested in it ;)

How is it in practice?
For a general idea, see this small simple example:

SOURCE TEMPLATE FILE:
Quote
This file's name is "[[ Write( Output.Name + "." + Output.Extension ) ]]".
It was generated from template "[[ Write( Template.Name + "." + Template.Extension ) ]]" in folder "[[ Write( Template.Folder ) ]]".

Your greeting is: [[ Greeting <- InputString( "Enter your greeting" ); Write( Greeting ) ]]
A loud greeting: [[ Write( Greeting.Upper() ) ]]
x = [[ StartNumber <- InputInteger( "Enter a number value for X" ); Write( StartNumber ) ]]
y = x+1 = [[ StartNumber++; Write( StartNumber ) ]]
Done!
[[
    WriteTime <- function()
    {
        Write( IO.ExecuteAndGetOutput( _T("cmd /c \"time /t\"") ) );
    }
]]

By the way, today is [[ Write( CBMacro( "$TODAY" ) ) ]], and the time now is [[ WriteTime() ]]
[[
    /* clean-up */
    rawdelete( "WriteTime" );
    rawdelete( "Greeting" );
    rawdelete( "StartNumber" );
]]

RESULT OUTPUT FILE:
Quote
This file's name is "Example.txt".
It was generated from template "Example.tpl.txt" in folder "J:\Codigo\CBTemplateEngine\Examples".

Your greeting is: heyy!!
A loud greeting: HEYY!!
x = 5
y = x+1 = 6
Done!

By the way, today is 2012-09-28, and the time now is 00:19

What can it do?
You can see this system is quite powerful. Most template systems are limited to replacing variables and a few "C preprocessor" style macros. But here we can use the whole squirrel scripting language, with the same syntax we already know from C::B scripts. This gives our templates access to:

- All CodeBlocks script bindings. These include:

    - The wxWidgets predefined window messages and dialogs
    - Execution of shell commands
    - Interaction with the file system
    - Calling menus and script files

- All your CodeBlocks state and definitions

    - Built-in macros
    - Environment and project variables
    - The settings and paths for compiler, projects and targets

How to use it?
While there is a lot I should tell about this template system, I don't want to make a huge, technical post that nobody will read. So if you want to read more, download the release attached below as a Zip file. It contains the following:

- The working script files, and instructions to "install" them
- Example templates and their outputs
- Some guides on its practical use
- Documentation on how the system insides work
- Some discussion on current limitations and advantages

Why did I post this?
I made this system to meet my own needs, but I post it here because I think it is a nice addition to C::B and it can help others automate their tasks. For the price of a few hundred lines of script, we get a very flexible funcionality using a syntax and environment that we already know.

As a last word: this template engine is not actually a plug-in, so why do I post it in the plug-in forum? Mainly because I think that someone with time and interest could easily take my code and extend it into a full plug-in. The tempalte engine already works and, as a script, it can be used from C::B menus. But a full plug-in could make it much easier to use (having a better GUI), give more options, and provide better integration with CodeBlocks.

From script I can only do so much, and I don't have the time to go to the full plug-in code. I'll leave it to you ;)

PD: All license and documentation details are included as part of the release, within the attached Zip file.


[attachment deleted by admin]

Offline carra

  • Multiple posting newcomer
  • *
  • Posts: 114
Re: Template Engine extension for Code::Blocks
« Reply #1 on: October 01, 2012, 02:32:07 pm »
Version 1.1 is available (attached as ZIP file below).
There are some documental fixes, but the main change is the addition of template nesting. See this example (included in the release) with automatic section numbering and templated file header:

MAIN TEMPLATE:
Quote
[[ IncludeTemplate( "Header.tpl.txt" ); ]]

[[ SectionTitle <- "string processing"; IncludeTemplate( "Section.tpl.txt" ); ]]


Your greeting is: [[ Greeting <- InputString( "Enter your greeting" ); Write( Greeting ) ]]
A loud greeting: [[ Write( Greeting.Upper() ) ]]

[[ SectionTitle <- "math and numbers"; IncludeTemplate( "Section.tpl.txt" ); ]]

x = [[ StartNumber <- InputInteger( "Enter a number value for X" ); Write( StartNumber ) ]]
y = x+1 = [[ StartNumber++; Write( StartNumber ) ]]
Done!

[[ SectionTitle <- "date and time"; IncludeTemplate( "Section.tpl.txt" ); ]]
[[
    WriteTime <- function()
    {
        Write( IO.ExecuteAndGetOutput( _T("cmd /c \"time /t\"") ) );
    }
]]

By the way, today is [[ Write( CBMacro( "$TODAY" ) ) ]], and the time now is [[ WriteTime() ]]
[[
    /* clean-up */
    rawdelete( "WriteTime" );
    rawdelete( "Greeting" );
    rawdelete( "StartNumber" );
    rawdelete( "SectionTitle" );
    rawdelete( "SectionNumber" );
]]

NESTED TEMPLATE (HEADER):
Quote
-------------------------------------------------------------------------
    This file's name is "[[ Write( Output.Name + "." + Output.Extension ) ]]".
    It was generated from template "[[ Write( Template.Name + "." + Template.Extension ) ]]" in folder "[[ Write( Template.Folder ) ]]".
-------------------------------------------------------------------------

NESTED TEMPLATE (SECTIONS):
Quote
=================================================================
           [[
                 if( !Exists( "SectionNumber" ) )
                 SectionNumber <- 1;;
                
                 Write( SectionNumber.tostring() + " - " );
                 SectionNumber++;

                 Write( _T(SectionTitle.tostring()).Upper() );
           ]]

=================================================================

RESULTING OUTPUT FILE:
Quote
-------------------------------------------------------------------------
    This file's name is "Advanced.txt".
    It was generated from template "Advanced.tpl.txt" in folder "C:\CodeBase\CBTemplateEngine".
-------------------------------------------------------------------------


=================================================================
           1 - STRING PROCESSING
=================================================================

Your greeting is: heyy!!
A loud greeting: HEYY!!

=================================================================
           2 - MATH AND NUMBERS
=================================================================

x = 5
y = x+1 = 6
Done!

=================================================================
           3 - DATE AND TIME
=================================================================

By the way, today is 2012-10-01, and the time now is 11:35

This is all for now, I have no plans for more updates at the moment.
I hope someone finds it useful...

[attachment deleted by admin]