|
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.
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