Browse Source

Add SettingsCodeGeneratorTool.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2024 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
d4c511815b
  1. 4
      src/AddIns/DisplayBindings/ResourceEditor/Project/Src/ResourceCodeGeneratorTool.cs
  2. 15
      src/AddIns/DisplayBindings/SettingsEditor/Project/ISettingsEntryHost.cs
  3. 100
      src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsCodeGeneratorTool.cs
  4. 6
      src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEditor.addin
  5. 1
      src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEditor.csproj
  6. 1
      src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEntry.cs
  7. 14
      src/Main/Base/Project/Src/Project/CustomTool.cs
  8. 5
      src/Main/Base/Project/Src/Util/Linq.cs
  9. 1
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj
  10. 287
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/EasyCodeDom.cs

4
src/AddIns/DisplayBindings/ResourceEditor/Project/Src/ResourceCodeGeneratorTool.cs

@ -25,7 +25,7 @@ namespace ResourceEditor @@ -25,7 +25,7 @@ namespace ResourceEditor
string inputFilePath = item.FileName;
IResourceReader reader;
if (Path.GetExtension(inputFilePath) == ".resx") {
if (string.Equals(Path.GetExtension(inputFilePath), ".resx", StringComparison.OrdinalIgnoreCase)) {
reader = new ResXResourceReader(inputFilePath);
} else {
reader = new ResourceReader(inputFilePath);
@ -43,7 +43,7 @@ namespace ResourceEditor @@ -43,7 +43,7 @@ namespace ResourceEditor
context.GetOutputFileName(item, ".Designer"),
StronglyTypedResourceBuilder.Create(
resources, // resourceList
"Resources", // baseName
Path.GetFileNameWithoutExtension(inputFilePath), // baseName
context.OutputNamespace, // generatedCodeNamespace
context.OutputNamespace, // resourcesNamespace
context.Project.LanguageProperties.CodeDomProvider, // codeProvider

15
src/AddIns/DisplayBindings/SettingsEditor/Project/ISettingsEntryHost.cs

@ -14,4 +14,19 @@ namespace ICSharpCode.SettingsEditor @@ -14,4 +14,19 @@ namespace ICSharpCode.SettingsEditor
string GetDisplayNameForType(Type type);
Type GetTypeByDisplayName(string displayName);
}
sealed class DummySettingsEntryHost : ISettingsEntryHost
{
public readonly static DummySettingsEntryHost Instance = new DummySettingsEntryHost();
public string GetDisplayNameForType(Type type)
{
return type.AssemblyQualifiedName;
}
public Type GetTypeByDisplayName(string displayName)
{
return Type.GetType(displayName);
}
}
}

100
src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsCodeGeneratorTool.cs

@ -0,0 +1,100 @@ @@ -0,0 +1,100 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision$</version>
// </file>
using System;
using System.CodeDom;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Xml;
using ICSharpCode.EasyCodeDom;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SettingsEditor
{
public class SettingsCodeGeneratorTool : ICustomTool
{
public void GenerateCode(FileProjectItem item, CustomToolContext context)
{
XmlDocument doc = new XmlDocument();
doc.Load(item.FileName);
SettingsDocument setDoc = new SettingsDocument(doc.DocumentElement, DummySettingsEntryHost.Instance);
if (!string.IsNullOrEmpty(item.CustomToolNamespace)) {
setDoc.GeneratedClassNamespace = item.CustomToolNamespace;
}
EasyCompileUnit ccu = new EasyCompileUnit();
ccu.AddNamespace(setDoc.GeneratedClassNamespace).Types.Add(CreateClass(setDoc));
context.WriteCodeDomToFile(item, context.GetOutputFileName(item, ".Designer"), ccu);
}
public static CodeTypeDeclaration CreateClass(SettingsDocument setDoc)
{
EasyTypeDeclaration c = new EasyTypeDeclaration(setDoc.GeneratedClassName);
c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
c.AddAttribute(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute),
Easy.Prim(typeof(SettingsCodeGeneratorTool).FullName),
Easy.Prim(typeof(SettingsCodeGeneratorTool).Assembly.GetName().Version.ToString()));
c.TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed;
c.IsPartial = true;
c.BaseTypes.Add(Easy.TypeRef(typeof(ApplicationSettingsBase)));
EasyField f = c.AddField(Easy.TypeRef(c), "defaultInstance");
f.Attributes = MemberAttributes.Private | MemberAttributes.Static;
f.InitExpression = Easy.Type(typeof(ApplicationSettingsBase))
.InvokeMethod("Synchronized", Easy.New(Easy.TypeRef(c)))
.CastTo(Easy.TypeRef(c));
c.AddProperty(f, "Default");
foreach (SettingsEntry entry in setDoc.Entries) {
Type entryType = entry.Type ?? typeof(string);
SpecialSetting? specialSetting = null;
foreach (SpecialTypeDescriptor desc in SpecialTypeDescriptor.Descriptors) {
if (desc.type == entryType) {
entryType = typeof(string);
specialSetting = desc.specialSetting;
break;
}
}
EasyProperty p = c.AddProperty(entryType, entry.Name);
if (entry.Scope == SettingScope.User) {
p.AddAttribute(typeof(UserScopedSettingAttribute));
} else {
p.AddAttribute(typeof(ApplicationScopedSettingAttribute));
}
if (!string.IsNullOrEmpty(entry.Provider)) {
p.AddAttribute(typeof(SettingsProviderAttribute),
Easy.TypeOf(new CodeTypeReference(entry.Provider)));
}
if (!string.IsNullOrEmpty(entry.Description)) {
p.AddAttribute(typeof(SettingsDescriptionAttribute), Easy.Prim(entry.Description));
Easy.AddSummary(p, entry.Description);
}
p.AddAttribute(typeof(DebuggerNonUserCodeAttribute));
if (specialSetting != null) {
p.AddAttribute(typeof(SpecialSettingAttribute),
Easy.Prim(specialSetting.Value));
}
if (entry.GenerateDefaultValueInCode) {
p.AddAttribute(typeof(DefaultSettingValueAttribute), Easy.Prim(entry.SerializedValue));
}
if (entry.Scope == SettingScope.User && entry.Roaming) {
p.AddAttribute(typeof(SettingsManageabilityAttribute),
Easy.Prim(SettingsManageability.Roaming));
}
p.Getter.Return(Easy.This.Index(Easy.Prim(entry.Name)).CastTo(entryType));
if (entry.Scope == SettingScope.User) {
p.Setter.Assign(Easy.This.Index(Easy.Prim(entry.Name)), Easy.Value);
}
}
return c;
}
}
}

6
src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEditor.addin

@ -14,4 +14,10 @@ @@ -14,4 +14,10 @@
fileNamePattern = "\.settings$"
languagePattern = "^SettingsFiles$"/>
</Path>
<Path name = "/SharpDevelop/CustomTools">
<CustomTool id = "SettingsSingleFileGenerator"
class = "ICSharpCode.SettingsEditor.SettingsCodeGeneratorTool"
fileNamePattern = "\.res(x|ources)$"/>
</Path>
</AddIn>

1
src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEditor.csproj

@ -64,6 +64,7 @@ @@ -64,6 +64,7 @@
<Compile Include="ISettingsEntryHost.cs" />
<Compile Include="SpecialTypes.cs" />
<Compile Include="SettingsDocument.cs" />
<Compile Include="SettingsCodeGeneratorTool.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj">

1
src/AddIns/DisplayBindings/SettingsEditor/Project/SettingsEntry.cs

@ -126,6 +126,7 @@ namespace ICSharpCode.SettingsEditor @@ -126,6 +126,7 @@ namespace ICSharpCode.SettingsEditor
return typeof(object).Assembly.GetType(typeName, false)
?? typeof(Uri).Assembly.GetType(typeName, false)
?? typeof(System.Drawing.Font).Assembly.GetType(typeName, false)
?? typeof(System.Data.DataRow).Assembly.GetType(typeName, false);
}

14
src/Main/Base/Project/Src/Project/CustomTool.cs

@ -145,6 +145,7 @@ namespace ICSharpCode.SharpDevelop.Project @@ -145,6 +145,7 @@ namespace ICSharpCode.SharpDevelop.Project
outputItem.FileName = outputFileName;
outputItem.DependentUpon = Path.GetFileName(baseItem.FileName);
ProjectService.AddProjectItem(project, outputItem);
project.Save();
ProjectBrowserPad.Instance.ProjectBrowserControl.RefreshView();
}
return outputItem;
@ -156,17 +157,22 @@ namespace ICSharpCode.SharpDevelop.Project @@ -156,17 +157,22 @@ namespace ICSharpCode.SharpDevelop.Project
CodeDomProvider provider = project.LanguageProperties.CodeDomProvider;
CodeGeneratorOptions options = new CodeDOMGeneratorUtility().CreateCodeGeneratorOptions;
NamedFileOperationDelegate method = delegate(string fileName) {
using (StreamWriter writer = new StreamWriter(fileName, false, System.Text.Encoding.UTF8)) {
string codeOutput;
using (StringWriter writer = new StringWriter()) {
if (provider == null) {
writer.WriteLine("No CodeDom provider was found for this language.");
} else {
provider.GenerateCodeFromCompileUnit(ccu, writer, options);
}
codeOutput = writer.ToString();
}
};
FileUtility.ObservedSave(method, outputFileName, FileErrorPolicy.Inform);
FileUtility.ObservedSave(delegate(string fileName) {
File.WriteAllText(fileName, codeOutput, Encoding.UTF8);
},
outputFileName, FileErrorPolicy.Inform);
EnsureOutputFileIsInProject(baseItem, outputFileName);
ParserService.EnqueueForParsing(outputFileName, codeOutput);
}
public void GenerateCodeDomAsync(FileProjectItem baseItem, string outputFileName, Func<CodeCompileUnit> func)

5
src/Main/Base/Project/Src/Util/Linq.cs

@ -63,5 +63,10 @@ namespace ICSharpCode.SharpDevelop @@ -63,5 +63,10 @@ namespace ICSharpCode.SharpDevelop
}
return default(T);
}
public static T[] ToArray<T>(IEnumerable<T> input)
{
return new List<T>(input).ToArray();
}
}
}

1
src/Main/ICSharpCode.SharpDevelop.Dom/Project/ICSharpCode.SharpDevelop.Dom.csproj

@ -150,6 +150,7 @@ @@ -150,6 +150,7 @@
<Compile Include="Src\CSharp\ExpressionFinder.cs" />
<Compile Include="Src\VBNet\VBNetAmbience.cs" />
<Compile Include="Src\VBNet\ExpressionFinder.cs" />
<Compile Include="Src\EasyCodeDom.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configuration" />

287
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/EasyCodeDom.cs

@ -0,0 +1,287 @@ @@ -0,0 +1,287 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision$</version>
// </file>
using System;
using System.CodeDom;
namespace ICSharpCode.EasyCodeDom
{
public static class Easy
{
public static CodeTypeReference TypeRef(Type type)
{
return new CodeTypeReference(type, CodeTypeReferenceOptions.GlobalReference);
}
public static CodeTypeReference TypeRef(CodeTypeDeclaration type)
{
return new CodeTypeReference(type.Name);
}
/// <summary>
/// Gets the EasyExpression for any primitive value that can be expressed as literal.
/// Also works for enumeration values.
/// </summary>
public static EasyExpression Prim(object literalValue)
{
if (literalValue is Enum) {
return Type(literalValue.GetType()).Field(literalValue.ToString());
} else {
return new EasyExpression(new CodePrimitiveExpression(literalValue));
}
}
public static EasyExpression Type(Type type)
{
return Type(TypeRef(type));
}
public static EasyExpression Type(CodeTypeReference type)
{
return new EasyExpression(new CodeTypeReferenceExpression(type));
}
public static EasyExpression TypeOf(Type type)
{
return TypeOf(TypeRef(type));
}
public static EasyExpression TypeOf(CodeTypeReference type)
{
return new EasyExpression(new CodeTypeOfExpression(type));
}
public static EasyExpression New(Type type, params CodeExpression[] arguments)
{
return New(TypeRef(type), arguments);
}
public static EasyExpression New(CodeTypeReference type, params CodeExpression[] arguments)
{
return new EasyExpression(new CodeObjectCreateExpression(type, arguments));
}
public static EasyExpression Var(string name)
{
return new EasyExpression(new CodeVariableReferenceExpression(name));
}
public static EasyExpression This {
get {
return new EasyExpression(new CodeThisReferenceExpression());
}
}
public static EasyExpression Value {
get {
return new EasyExpression(new CodePropertySetValueReferenceExpression());
}
}
public static void AddSummary(CodeTypeMember member, string summary)
{
member.Comments.Add(new CodeCommentStatement("<summary>", true));
member.Comments.Add(new CodeCommentStatement(summary, true));
member.Comments.Add(new CodeCommentStatement("</summary>", true));
}
internal static CodeAttributeDeclaration AddAttribute(CodeTypeMember member,
CodeTypeReference type,
CodeExpression[] arguments)
{
CodeAttributeArgument[] attributeArguments = new CodeAttributeArgument[arguments.Length];
for (int i = 0; i < arguments.Length; i++) {
attributeArguments[i] = new CodeAttributeArgument(arguments[i]);
}
CodeAttributeDeclaration cad = new CodeAttributeDeclaration(type, attributeArguments);
member.CustomAttributes.Add(cad);
return cad;
}
}
public sealed class EasyExpression
{
readonly CodeExpression expr;
public EasyExpression(CodeExpression expr)
{
this.expr = expr;
}
public static implicit operator CodeExpression(EasyExpression expr)
{
return expr.expr;
}
public EasyExpression InvokeMethod(string name, params CodeExpression[] arguments)
{
return new EasyExpression(new CodeMethodInvokeExpression(expr, name, arguments));
}
public EasyExpression CastTo(Type type)
{
return CastTo(Easy.TypeRef(type));
}
public EasyExpression CastTo(CodeTypeReference type)
{
return new EasyExpression(new CodeCastExpression(type, expr));
}
public EasyExpression Index(params CodeExpression[] indices)
{
return new EasyExpression(new CodeIndexerExpression(expr, indices));
}
public EasyExpression Field(string name)
{
return new EasyExpression(new CodeFieldReferenceExpression(expr, name));
}
public EasyExpression Property(string name)
{
return new EasyExpression(new CodePropertyReferenceExpression(expr, name));
}
}
public class EasyCompileUnit : CodeCompileUnit
{
public EasyNamespace AddNamespace(string name)
{
EasyNamespace n = new EasyNamespace(name);
this.Namespaces.Add(n);
return n;
}
}
public class EasyNamespace : CodeNamespace
{
public EasyNamespace() : base() {}
public EasyNamespace(string name) : base(name) {}
public EasyTypeDeclaration AddType(string name)
{
EasyTypeDeclaration n = new EasyTypeDeclaration(name);
this.Types.Add(n);
return n;
}
}
public class EasyTypeDeclaration : CodeTypeDeclaration
{
public EasyTypeDeclaration() : base() {}
public EasyTypeDeclaration(string name) : base(name) {}
public CodeAttributeDeclaration AddAttribute(Type type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, Easy.TypeRef(type), arguments);
}
public CodeAttributeDeclaration AddAttribute(CodeTypeReference type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, type, arguments);
}
public EasyField AddField(Type type, string name)
{
return AddField(Easy.TypeRef(type), name);
}
public EasyField AddField(CodeTypeReference type, string name)
{
EasyField f = new EasyField(type, name);
this.Members.Add(f);
return f;
}
public EasyProperty AddProperty(Type type, string name)
{
return AddProperty(Easy.TypeRef(type), name);
}
public EasyProperty AddProperty(CodeTypeReference type, string name)
{
EasyProperty p = new EasyProperty(type, name);
this.Members.Add(p);
p.Attributes = MemberAttributes.Public | MemberAttributes.Final;
return p;
}
public EasyProperty AddProperty(CodeMemberField field, string name)
{
EasyProperty p = AddProperty(field.Type, name);
p.Getter.Return(new CodeVariableReferenceExpression(field.Name));
p.Attributes |= field.Attributes & MemberAttributes.Static; // copy static flag
return p;
}
}
public class EasyField : CodeMemberField
{
public EasyField() : base() {}
public EasyField(CodeTypeReference type, string name) : base(type, name) {}
public CodeAttributeDeclaration AddAttribute(Type type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, Easy.TypeRef(type), arguments);
}
public CodeAttributeDeclaration AddAttribute(CodeTypeReference type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, type, arguments);
}
}
public class EasyProperty : CodeMemberProperty
{
EasyBlock getter, setter;
public EasyProperty()
{
getter = new EasyBlock(this.GetStatements);
setter = new EasyBlock(this.SetStatements);
}
public EasyProperty(CodeTypeReference type, string name) : this()
{
this.Type = type;
this.Name = name;
}
public CodeAttributeDeclaration AddAttribute(Type type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, Easy.TypeRef(type), arguments);
}
public CodeAttributeDeclaration AddAttribute(CodeTypeReference type, params CodeExpression[] arguments)
{
return Easy.AddAttribute(this, type, arguments);
}
public EasyBlock Getter {
get { return getter; }
}
public EasyBlock Setter {
get { return setter; }
}
}
public sealed class EasyBlock
{
readonly CodeStatementCollection csc;
public EasyBlock(CodeStatementCollection csc)
{
this.csc = csc;
}
public CodeMethodReturnStatement Return(CodeExpression expr)
{
CodeMethodReturnStatement st = new CodeMethodReturnStatement(expr);
csc.Add(st);
return st;
}
public CodeAssignStatement Assign(CodeExpression lhs, CodeExpression rhs)
{
CodeAssignStatement st = new CodeAssignStatement(lhs, rhs);
csc.Add(st);
return st;
}
}
}
Loading…
Cancel
Save