- src.zip
- Src
- PluginSample
- ConsoleApplication1
- Genuilder
- PluginSample.sln
- StronglyTypedAppSettingsPlugin
- StronglyTypedAppSettingsPlugin2.Gen
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Genuilder.Extensibility;
using System.Configuration;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Visitors;
namespace StronglyTypedAppSettingsPlugin2.Gen
{
public class StronglyTypedAppSettingsPlugin : 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(".config"))
{
var dependency = item.SourceOf("StronglyTypedSettings.cs");
dependency.ShouldUpdateTarget += new CodeDependencyHandler(dependency_ShouldUpdateTarget);
}
}
void dependency_ShouldUpdateTarget(CodeDependency sender, CodeItem target)
{
var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = ((FileCodeItem)sender.Source).File.FullName
}, ConfigurationUserLevel.None);
var targetNRefactoryExtension = target.GetExtension<Genuilder.Extensibility.NRefactory.CompilationUnitExtension>();
targetNRefactoryExtension.CompilationUnit.Children.Clear();
var typeDeclaration = new TypeDeclaration(Modifiers.Public | Modifiers.Static, null)
{
Name = "AppConfigSettings"
};
targetNRefactoryExtension.CompilationUnit.Children.Add(typeDeclaration);
foreach(KeyValueConfigurationElement setting in configuration.AppSettings.Settings)
{
var propertyDeclaration = new PropertyDeclaration(Modifiers.Public | Modifiers.Static, null, setting.Key, null)
{
TypeReference = new TypeReference(typeof(String).FullName)
};
var indexer = new IndexerExpression(
new MemberReferenceExpression(
new TypeReferenceExpression(
new TypeReference(typeof(ConfigurationManager).FullName)), "AppSettings")
, new List<Expression>()
{
new ICSharpCode.NRefactory.Ast.PrimitiveExpression(setting.Key)
});
propertyDeclaration.GetRegion = new PropertyGetRegion(new BlockStatement(), null);
propertyDeclaration.GetRegion.Block.Children.Add(new ReturnStatement(indexer));
typeDeclaration.Children.Add(propertyDeclaration);
}
targetNRefactoryExtension.Save();
target.Logger.Send("Settings generated !!", Genuilder.Extensibility.Utilities.LogType.Warning);
}
#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