Click here to Skip to main content
15,896,269 members
Articles / .NET

Genuilder Extensibility

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
13 Nov 2011Ms-PL4 min read 33.9K   93   13  
Generate your own code during compilation without MSBuild knowledge
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Genuilder.Extensibility;
using Genuilder.Extensibility.NRefactory;
using ICSharpCode.NRefactory.Ast;


namespace InterfaceExtractor.Gen
{
	public class DependencyHelloWorldPlugin2 : IPlugin
	{
		#region IPlugin Members

		public void Initialize(ICodeRepository repository)
		{
			repository.CodeItemCreated += new CodeItemCreatedHandler(repository_CodeItemCreated);
		}

		void repository_CodeItemCreated(ICodeRepository sender, CodeItem item)
		{
			if(!item.Name.EndsWith("generated2.cs") && HasAClassWithAttribute<GenerateAttribute>(item))
			{

				var dependency = item.SourceOf(item.Name + "generated2.cs");
				dependency.ShouldUpdateTarget += new CodeDependencyHandler(dependency_ShouldUpdateTarget);
			}
		}


		private bool HasAClassWithAttribute<T>(CodeItem item)
		{
			var nRefactoryExtension = item.GetExtension<CompilationUnitExtension>();
			return HasAttribute<T>(nRefactoryExtension.CompilationUnit);
		}

		private bool HasAttribute<T>(INode node)
		{
			if(node is TypeDeclaration)
			{
				TypeDeclaration declaration = (TypeDeclaration)node;
				var hasAttribute = declaration.Attributes
					.SelectMany(attributes => attributes.Attributes)
					.Any(attribute => attribute.Name.EndsWith(typeof(GenerateAttribute).Name));
				if(hasAttribute)
					return true;
			}

			foreach(var child in node.Children)
			{
				var hasAttribute = HasAttribute<T>(child);
				if(hasAttribute)
					return true;
			}
			return false;
		}

		void dependency_ShouldUpdateTarget(CodeDependency sender, CodeItem target)
		{
			var nRefactoryExtension = target.GetExtension<CompilationUnitExtension>();
			nRefactoryExtension.CompilationUnit.Children.Clear();
			nRefactoryExtension.CompilationUnit.Children.Add(
			new TypeDeclaration(Modifiers.Public, new List<AttributeSection>())
			{
				Name = ToClassName(sender.Source.Name)
			});
			nRefactoryExtension.Save();
		}

		private string ToClassName(string fileName)
		{
			return fileName
				.Replace('.', '_')
				.Replace('/', '_')
				.Replace('\\', '_') + "2";
		}

		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions