Click here to Skip to main content
15,891,770 members
Articles / Web Development / ASP.NET

Template-Based Code Generation with SmartCode

Rate me:
Please Sign up or sign in to vote.
4.82/5 (35 votes)
25 Dec 20067 min read 101K   3.5K   121  
SmartCode is a template based code generator.This tutorial describes the process of building a templates to SmartCode
/*
 * Copyright � 2005-2006 Danilo Mendez <danilo.mendez@kontac.net>
 * Adolfo Socorro <ajs@esolutionspr.com>
 * www.kontac.net 
 * All rights reserved.
 * Released under both BSD license and Lesser GPL library license.
 * Whenever there is any discrepancy between the two licenses,
 * the BSD license will take precedence.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartCode.Studio.Templates;
using SmartCode.Model;
using System.Threading;

namespace SmartCode.Studio.Engine
{
    public partial class CodeGenerationDlg : Form
    {
        delegate void CodeGenerationDelegate(GenerationArgs args);

        private SmartCodeEngine codeEngine;
        private ICodeOutput codeOutput;

        internal  CodeGenerationDlg()
        {
            this.InitializeComponent();
        }

        internal CodeGenerationDlg(ICodeOutput codeOutput)
            : this()
        {
            this.codeOutput = codeOutput;
            this.ConfigureProgressBar();
            CodeGenerationDelegate showProgress = new CodeGenerationDelegate(EngineProgress);
            codeEngine = new SmartCodeEngine(this, showProgress, SmartCode.Studio.SmartStudio.MainForm.CurrentProject);
        }

        private void CodeGenerationDlg_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(codeEngine.RunProcess));
            t.IsBackground = true;
            t.Start();
        }

        private void EngineProgress(GenerationArgs args)
        {
            if (args.StatusDone)
            {
                MessageBox.Show("The Code engine was successful executed", "Message", MessageBoxButtons.OK);
            }
            else
            {
                codeOutput.WriteToOutput(args.Output, args.Template );
                UpdateInfo(args.Output, args.Message, args.Template, args.Success );
            }
        }
 

        public void AddMessageToListView(bool success, string message, string fullFileName)
        {
            ListViewItem li;
            if (success)
            {
                li = new ListViewItem(new string[] { string.Empty, fullFileName, message }, 0);
                li.ImageIndex = 0;
            }
            else
            {
                li = new ListViewItem(new string[] { string.Empty, message, fullFileName }, 1);
                li.ImageIndex = 1;
            }
            this.uiLiResume.Items.Add(li);
        }
    
        private void uiLiResume_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                ListViewItem li = ((ListView)sender).SelectedItems[0];
                MessageBox.Show(li.Text.ToString() + "\n" + li.SubItems[1].Text.ToString() + "\n" + li.SubItems[2].Text.ToString(), "Code Generation Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch
            {
            }
        }

        public void UpdateInfo(OutputInfo output, string message, TemplateInfo template, bool success)
        {
            this.uiProgressBar.PerformStep();
            if (success)
            {
                if (output.CreateFile)
                {
                    this.AddMessageToListView(true, "Created successfully ", output.Folder + @"\" + output.FileName);
                }
                else
                {
                    this.AddMessageToListView(true, "Template successfully executed", template.Name);
                }
            }
            else
            {
                this.AddMessageToListView(false, "Error in Template '" + template.Name, message);
            }
        }

        internal void ConfigureProgressBar()
        {
            int total = 0;
            foreach (KeyValuePair<string, LibraryInfo> pair in SmartCode.Studio.SmartStudio.MainForm.CurrentProject.Libraries)
            {
                foreach (TemplateInfo template in pair.Value.Templates)
                {
                    total += template.AssignedObjects.Count;
                    if (template.Run)
                    {
                        total++;
                    }
                }
            }
            this.uiProgressBar.Value = 0;
            this.uiProgressBar.Minimum = 0;
            this.uiProgressBar.Maximum = total;
            this.uiProgressBar.Step = 1;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

Comments and Discussions