Dashboard > Castle Contrib > Home > Castle.Tools.CodeGenerator
Log In   View a printable version of the current page.
Castle.Tools.CodeGenerator
Added by Lee Henson, last edited by Ken Egozi on Nov 27, 2007  (view change) show comment
Labels: 
(None)


What Is The CodeGenerator?

The code generator is a tool, to generate a typed site map for Monorail web projects.
The tool creates a .cs file that will declare a reference tree to all the areas, controllers, wizard steps, views and routes in your site.
Partial declaration for extending your controllers with builtin MyViews, MyActions and MyRoutes properties is optional.
So, using this library you can write code like:

MyActions.Edit(2).Transfer();

Site.Admin.Clients.Login().Redirect();

MyViews.List.Render();

Preparing Controllers For The CodeGenerator

First, you'd need to create a base class to all your site aware controllers:

public class SiteAwareSmartDispatcherController : SmartDispatcherController
{
	private ICodeGeneratorServices codeGeneratorServices;
	private RootAreaNode site;

	public SiteAwareSmartDispatcherController()
	{
		this.codeGeneratorServices = new DefaultCodeGeneratorServices(
				new DefaultControllerReferenceFactory(),
				new AspDotNetRedirectService(),
				new DefaultArgumentConversionService(),
				new DefaultRuntimeInformationService());
	}

	protected virtual void PerformGeneratedInitialize()
	{
		codeGeneratorServices.RailsContext = Context;
		codeGeneratorServices.Controller = this;
		PropertyBag["Site"] = Site;
	}

	protected override void Initialize()
	{
		base.Initialize();

		PerformGeneratedInitialize();
	}

	public ICodeGeneratorServices CodeGeneratorServices
	{
		get { return codeGeneratorServices; }
	}

	public RootAreaNode Site
	{
		get
		{
			if (site == null)
				site = new RootAreaNode(CodeGeneratorServices);

			return site;
		}
	}
}

And make your controllers partial 

public partial class PostsController : SiteAwareSmartDispatcherController
{
	public void List()
	{
		PropertyBag["Posts"] = GetAllPosts();
	}

	public void ListByTag(string tagName)
	{
		PropertyBag["Posts"] = GetPostsByTag(tagName);
		MyViews.List.Render();
	}
}


Running The CodeGenerator

After running it for the first time, the sitemap file would be created, and you'd need to add it to your project, so it would get compiled as part of your project's assembly.

The tool itself can be run from the command line, or as a custom MSBuild task.

Using the Command Line tool

The CodeGenerator has a command line tool, which can be used to generate your site map.
The basic usage is:

D:\dev\Lib\Castle.Tools.CodeGenerator.CmdLine.exe /P:YourProjectDirectory /ProjectFileName:TheCsprojFileName

You can set it to run as part of your build process. Just add a pre-build event:

D:\dev\Lib\Castle.Tools.CodeGenerator.CmdLine.exe /P:$(ProjectDir) /ProjectFileName:$(ProjectFileName)

To see all the optional parameters, run the tool from a console window.

 Using a Custom MSBuild Task

You'd need to copy the CodeGenerator dll, the supplied .targets file, and the dependency assembly, ICSharpCode.NRefactory.dll, to the directory: 'C:\Program Files\MSBuild\Castle.Tools.CodeGenerator'

Then, edit your .csproj file, and add the following import directive:

<Import Project="$(MSBuildExtensionsPath)\Castle.Tools.CodeGenerator\Castle.Tools.CodeGenerator.targets" />

You can edit the CodeGenerator parameters using MSBuild style parameters. For example:

<PropertyGroup>
    <SiteMapNamespace>$(RootNamespace).Code.SiteMap</SiteMapNamespace>
  </PropertyGroup>
  <ItemGroup>
    <ViewSources Include="./Views/**/*.aspx">
      <InProject>false</InProject>
    </ViewSources>
    <ControllerSources Include="./Code/Controllers/**/*.cs">
      <InProject>false</InProject>
    </ControllerSources>
    <ControllerSources Include="./Code/WizardSteps/**/*.cs">
      <InProject>false</InProject>
    </ControllerSources>
    <ViewComponentSources Include="./Code/ViewComponents/**/*.cs">
      <InProject>false</InProject>
    </ViewComponentSources>
  </ItemGroup>
Troubleshooting (Castle Contrib)

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators