From e4f99c5aa7e8c660e3f732ca3b456c1475605eaa Mon Sep 17 00:00:00 2001 From: Stephan Date: Sun, 27 Oct 2013 15:35:41 +0100 Subject: [PATCH] Import custom tool, refactor --- .gitignore | 1 + .../CustomTool/ClassNameExtractor.cs | 16 ++ .../CustomTool/CodeDomCodeGenerator.cs | 206 ++++++++++++++ .../CustomTool/CodeGenerator.cs | 16 ++ .../CustomTool/CodeGeneratorFactory.cs | 14 + .../CustomTool/ICodeGenerator.cs | 9 + .../CustomTool/IResourceParser.cs | 10 + .../CustomTool/ResourceItem.cs | 9 + .../CustomTool/ResourceParser.cs | 53 ++++ .../ReswFileCSharpCodeGeneratorInternal.cs | 23 ++ .../ReswFileVisualBasicCodeGenerator.cs | 21 ++ ...eswFileVisualBasicCodeGeneratorInternal.cs | 22 ++ .../CustomTool/StringExtension.cs | 20 ++ .../CustomTool/VisualStudioVersion.cs | 8 + .../CustomTool/ZbuCSharpModelsBuilder.cs | 21 ++ .../CustomTool/ZbuModelsBuilder.cs | 68 +++++ .../GlobalSuppressions.cs | 11 + Zbu.ModelsBuilder.CustomTool/Guids.cs | 15 + .../Properties/AssemblyInfo.cs | 9 + .../Resources.Designer.cs | 63 +++++ Zbu.ModelsBuilder.CustomTool/Resources.resx | 129 +++++++++ .../Resources/Package.ico | Bin 0 -> 2998 bytes Zbu.ModelsBuilder.CustomTool/VSPackage.resx | 140 ++++++++++ .../VisualStudio2012Package.cs | 62 ++++ .../VisualStudio2013Package.cs | 62 ++++ .../Zbu.ModelsBuilder.CustomTool.csproj | 187 +++++++++++++ Zbu.ModelsBuilder.CustomTool/license.txt | 21 ++ .../source.extension.vsixmanifest | 33 +++ Zbu.ModelsBuilder.Tests/App.config | 2 + Zbu.ModelsBuilder.Tests/BuilderTests.cs | 47 ++-- Zbu.ModelsBuilder.Tests/ConfigTests.cs | 50 ++++ Zbu.ModelsBuilder.Tests/ExtensionsTests.cs | 27 ++ .../Properties/AssemblyInfo.cs | 34 +-- .../UmbracoApplicationTests.cs | 32 +++ .../Zbu.ModelsBuilder.Tests.csproj | 27 +- Zbu.ModelsBuilder.Tests/ZbuModelsBuilder.txt | 1 + Zbu.ModelsBuilder.Umbraco/Application.cs | 213 ++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++ .../Zbu.ModelsBuilder.Umbraco.csproj | 79 ++++++ Zbu.ModelsBuilder.sln | 12 + Zbu.ModelsBuilder/Builder.cs | 76 ++--- Zbu.ModelsBuilder/ConfigSystem.cs | 264 ++++++++++++++++++ Zbu.ModelsBuilder/EnumerableExtensions.cs | 22 ++ Zbu.ModelsBuilder/Properties/AssemblyInfo.cs | 34 +-- Zbu.ModelsBuilder/Properties/CommonInfo.cs | 14 + Zbu.ModelsBuilder/PropertyModel.cs | 11 + Zbu.ModelsBuilder/StringExtensions.cs | 16 ++ Zbu.ModelsBuilder/TypeModel.cs | 35 +++ Zbu.ModelsBuilder/Zbu.ModelsBuilder.csproj | 22 +- license.txt | 21 ++ readme.txt | 13 + 51 files changed, 2197 insertions(+), 140 deletions(-) create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ClassNameExtractor.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/CodeDomCodeGenerator.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGenerator.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGeneratorFactory.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ICodeGenerator.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/IResourceParser.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceItem.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceParser.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileCSharpCodeGeneratorInternal.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGenerator.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGeneratorInternal.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/StringExtension.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/VisualStudioVersion.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuCSharpModelsBuilder.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuModelsBuilder.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/GlobalSuppressions.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/Guids.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/Properties/AssemblyInfo.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/Resources.Designer.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/Resources.resx create mode 100644 Zbu.ModelsBuilder.CustomTool/Resources/Package.ico create mode 100644 Zbu.ModelsBuilder.CustomTool/VSPackage.resx create mode 100644 Zbu.ModelsBuilder.CustomTool/VisualStudio2012Package.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/VisualStudio2013Package.cs create mode 100644 Zbu.ModelsBuilder.CustomTool/Zbu.ModelsBuilder.CustomTool.csproj create mode 100644 Zbu.ModelsBuilder.CustomTool/license.txt create mode 100644 Zbu.ModelsBuilder.CustomTool/source.extension.vsixmanifest create mode 100644 Zbu.ModelsBuilder.Tests/ConfigTests.cs create mode 100644 Zbu.ModelsBuilder.Tests/ExtensionsTests.cs create mode 100644 Zbu.ModelsBuilder.Tests/UmbracoApplicationTests.cs create mode 100644 Zbu.ModelsBuilder.Tests/ZbuModelsBuilder.txt create mode 100644 Zbu.ModelsBuilder.Umbraco/Application.cs create mode 100644 Zbu.ModelsBuilder.Umbraco/Properties/AssemblyInfo.cs create mode 100644 Zbu.ModelsBuilder.Umbraco/Zbu.ModelsBuilder.Umbraco.csproj create mode 100644 Zbu.ModelsBuilder/ConfigSystem.cs create mode 100644 Zbu.ModelsBuilder/EnumerableExtensions.cs create mode 100644 Zbu.ModelsBuilder/Properties/CommonInfo.cs create mode 100644 Zbu.ModelsBuilder/PropertyModel.cs create mode 100644 Zbu.ModelsBuilder/StringExtensions.cs create mode 100644 Zbu.ModelsBuilder/TypeModel.cs create mode 100644 license.txt create mode 100644 readme.txt diff --git a/.gitignore b/.gitignore index dd8e647a..e14ec8dd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ obj *.suo *.user packages +refs diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ClassNameExtractor.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ClassNameExtractor.cs new file mode 100644 index 00000000..650bbf62 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ClassNameExtractor.cs @@ -0,0 +1,16 @@ +using System.IO; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public static class ClassNameExtractor + { + public static string GetClassName(string wszInputFilePath) + { + if (!File.Exists(wszInputFilePath)) + throw new FileNotFoundException(); + + var fileInfo = new FileInfo(wszInputFilePath); + return fileInfo.Name.Replace(fileInfo.Extension, string.Empty); + } + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeDomCodeGenerator.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeDomCodeGenerator.cs new file mode 100644 index 00000000..b1b1c1bf --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeDomCodeGenerator.cs @@ -0,0 +1,206 @@ +using System; +using System.CodeDom; +using System.CodeDom.Compiler; +using System.Globalization; +using System.IO; +using System.Reflection; +using System.Text; +using Microsoft.CSharp; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public class CodeDomCodeGenerator : CodeGenerator, IDisposable + { + private readonly TypeAttributes? classAccessibility; + private readonly VisualStudioVersion visualStudioVersion; + private readonly string className; + private readonly CodeNamespace codeNamespace; + private readonly CodeCompileUnit compileUnit; + private readonly CodeDomProvider provider; + + public CodeDomCodeGenerator(IResourceParser resourceParser, + string className, + string defaultNamespace, + CodeDomProvider codeDomProvider = null, + TypeAttributes? classAccessibility = null, + VisualStudioVersion visualStudioVersion = VisualStudioVersion.VS2012) + : base(resourceParser, defaultNamespace) + { + this.className = className; + this.classAccessibility = classAccessibility; + this.visualStudioVersion = visualStudioVersion; + compileUnit = new CodeCompileUnit(); + provider = codeDomProvider ?? new CSharpCodeProvider(); + codeNamespace = new CodeNamespace(defaultNamespace); + } + + public override string GenerateCode() + { + codeNamespace.Comments.Add( + new CodeCommentStatement("--------------------------------------------------------------------------------------------------")); + codeNamespace.Comments.Add(new CodeCommentStatement("")); + codeNamespace.Comments.Add(new CodeCommentStatement("\tThis code was generated by ResW File Code Generator (http://reswcodegen.codeplex.com)")); + codeNamespace.Comments.Add(new CodeCommentStatement("\tResW File Code Generator was written by Christian Resma Helle")); + codeNamespace.Comments.Add(new CodeCommentStatement("\tand is under GNU General Public License version 2 (GPLv2)")); + codeNamespace.Comments.Add(new CodeCommentStatement(string.Empty)); + codeNamespace.Comments.Add(new CodeCommentStatement("\tThis code contains a helper class exposing property representations")); + codeNamespace.Comments.Add(new CodeCommentStatement("\tof the string resources defined in the specified .ResW file")); + codeNamespace.Comments.Add(new CodeCommentStatement(string.Empty)); + codeNamespace.Comments.Add(new CodeCommentStatement("\tGenerated: " + DateTime.Now.ToString(CultureInfo.InvariantCulture))); + codeNamespace.Comments.Add(new CodeCommentStatement("")); + codeNamespace.Comments.Add( + new CodeCommentStatement("--------------------------------------------------------------------------------------------------")); + + codeNamespace.Imports.Add(new CodeNamespaceImport("Windows.ApplicationModel.Resources")); + + var targetClass = new CodeTypeDeclaration(className) + { + IsClass = true, + IsPartial = true, + TypeAttributes = classAccessibility.HasValue ? classAccessibility.Value : TypeAttributes.Public + }; + + const string resourceLoaderType = "ResourceLoader"; + var resourceLoaderField = new CodeMemberField(resourceLoaderType, "resourceLoader") + { + Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final + }; + targetClass.Members.Add(resourceLoaderField); + + var constructor = new CodeTypeConstructor(); + + var executingAssemblyVar = new CodeVariableDeclarationStatement(typeof (string), "executingAssemblyName"); + var executingAssemblyInit = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblyName"), + new CodeSnippetExpression("Windows.UI.Xaml.Application.Current.GetType().AssemblyQualifiedName")); + var executingAssemblySplit = new CodeVariableDeclarationStatement(typeof (string[]), "executingAssemblySplit"); + var executingAssemblyInit2 = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblySplit"), + new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("executingAssemblyName"), + "Split", + new CodePrimitiveExpression(','))); + var executingAssemblyInit3 = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblyName"), + new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("executingAssemblySplit"), + new CodePrimitiveExpression(1))); + + var currentAssemblyVar = new CodeVariableDeclarationStatement(typeof (string), "currentAssemblyName"); + var currentAssemblyInit = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblyName"), + new CodePropertyReferenceExpression(new CodeTypeOfExpression(className), "AssemblyQualifiedName")); + var currentAssemblySplit = new CodeVariableDeclarationStatement(typeof (string[]), "currentAssemblySplit"); + var currentAssemblyInit2 = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblySplit"), + new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("currentAssemblyName"), + "Split", + new CodePrimitiveExpression(','))); + var currentAssemblyInit3 = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblyName"), + new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("currentAssemblySplit"), + new CodePrimitiveExpression(1))); + + var createResourceLoader = new CodeConditionStatement( + new CodeSnippetExpression("executingAssemblyName.Equals(currentAssemblyName)"), + new CodeStatement[] // true + { + visualStudioVersion == VisualStudioVersion.VS2013 + ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"), + new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"), + "GetForCurrentView", + new CodeSnippetExpression("\"" + className + "\""))) + : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"), + new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"), + new CodeSnippetExpression("\"" + className + "\""))) + }, + new CodeStatement[] // false + { + visualStudioVersion == VisualStudioVersion.VS2013 + ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"), + new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"), + "GetForCurrentView", + new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\""))) + : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"), + new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"), + new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\""))) + }); + + constructor.Statements.Add(executingAssemblyVar); + constructor.Statements.Add(executingAssemblyInit); + constructor.Statements.Add(executingAssemblySplit); + constructor.Statements.Add(executingAssemblyInit2); + constructor.Statements.Add(executingAssemblyInit3); + constructor.Statements.Add(currentAssemblyVar); + constructor.Statements.Add(currentAssemblyInit); + constructor.Statements.Add(currentAssemblySplit); + constructor.Statements.Add(currentAssemblyInit2); + constructor.Statements.Add(currentAssemblyInit3); + constructor.Statements.Add(createResourceLoader); + + targetClass.Members.Add(constructor); + + var resources = ResourceParser.Parse(); + foreach (var item in resources) + { + if (string.IsNullOrEmpty(item.Name) || item.Name.Contains(".")) + continue; + + var property = new CodeMemberProperty + { + Name = item.Name.Trim(), + Attributes = MemberAttributes.Public | MemberAttributes.Static, + HasGet = true, + Type = new CodeTypeReference(typeof (string)) + }; + + property.Comments.Add(new CodeCommentStatement("", true)); + property.Comments.Add(new CodeCommentStatement("Localized resource similar to \"" + (item.Value ?? item.Name) + "\"", true)); + property.Comments.Add(new CodeCommentStatement("", true)); + property.GetStatements.Add( + new CodeMethodReturnStatement( + new CodeMethodInvokeExpression( + new CodeFieldReferenceExpression(null, "resourceLoader"), + "GetString", + new CodePrimitiveExpression(item.Name)))); + + targetClass.Members.Add(property); + } + + codeNamespace.Types.Add(targetClass); + compileUnit.Namespaces.Add(codeNamespace); + + return GenerateCodeFromCompileUnit(); + } + + private string GenerateCodeFromCompileUnit() + { + var options = new CodeGeneratorOptions {BracingStyle = "C"}; + + var code = new StringBuilder(); + + using (var writer = new StringWriter(code)) + provider.GenerateCodeFromCompileUnit(compileUnit, writer, options); + + return code.ToString(); + } + + #region IDisposable + + private bool disposed; + + public void Dispose() + { + Dispose(true); + } + + ~CodeDomCodeGenerator() + { + Dispose(false); + } + + protected virtual void Dispose(bool dispose) + { + if (disposed) + return; + disposed = true; + + if (dispose) + provider.Dispose(); + } + + #endregion + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGenerator.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGenerator.cs new file mode 100644 index 00000000..3edb8239 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGenerator.cs @@ -0,0 +1,16 @@ +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public abstract class CodeGenerator : ICodeGenerator + { + protected CodeGenerator(IResourceParser resourceParser, string defaultNamespace) + { + ResourceParser = resourceParser; + Namespace = defaultNamespace; + } + + public IResourceParser ResourceParser { get; set; } + public string Namespace { get; set; } + + public abstract string GenerateCode(); + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGeneratorFactory.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGeneratorFactory.cs new file mode 100644 index 00000000..bc11bc7d --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/CodeGeneratorFactory.cs @@ -0,0 +1,14 @@ +using System.CodeDom; +using System.CodeDom.Compiler; +using System.Reflection; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public class CodeGeneratorFactory + { + public ICodeGenerator Create(string className, string defaultNamespace, string inputFileContents, CodeDomProvider codeDomProvider = null, TypeAttributes? classAccessibility = null) + { + return new CodeDomCodeGenerator(new ResourceParser(inputFileContents), className, defaultNamespace, codeDomProvider, classAccessibility); + } + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ICodeGenerator.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ICodeGenerator.cs new file mode 100644 index 00000000..965b2849 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ICodeGenerator.cs @@ -0,0 +1,9 @@ +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public interface ICodeGenerator + { + IResourceParser ResourceParser { get; set; } + string Namespace { get; set; } + string GenerateCode(); + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/IResourceParser.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/IResourceParser.cs new file mode 100644 index 00000000..51629c5b --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/IResourceParser.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public interface IResourceParser + { + string ReswFileContents { get; set; } + List Parse(); + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceItem.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceItem.cs new file mode 100644 index 00000000..644ec73f --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceItem.cs @@ -0,0 +1,9 @@ +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public class ResourceItem + { + public string Name { get; set; } + public string Value { get; set; } + public string Comment { get; set; } + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceParser.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceParser.cs new file mode 100644 index 00000000..4425c92b --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ResourceParser.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public class ResourceParser : IResourceParser + { + public ResourceParser(string reswFileContents) + { + ReswFileContents = reswFileContents; + } + + public string ReswFileContents { get; set; } + + public List Parse() + { + var doc = XDocument.Parse(ReswFileContents); + + var list = new List(); + + foreach (var element in doc.Descendants("data")) + { + if (element.Attributes().All(c => c.Name != "name")) + continue; + + var item = new ResourceItem(); + + var nameAttribute = element.Attribute("name"); + if (nameAttribute != null) + item.Name = nameAttribute.Value; + + if (element.Descendants().Any(c => c.Name == "value")) + { + var valueElement = element.Descendants("value").FirstOrDefault(); + if (valueElement != null) + item.Value = valueElement.Value; + } + + if (element.Descendants().Any(c => c.Name == "comment")) + { + var commentElement = element.Descendants("comment").FirstOrDefault(); + if (commentElement != null) + item.Comment = commentElement.Value; + } + + list.Add(item); + } + + return list; + } + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileCSharpCodeGeneratorInternal.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileCSharpCodeGeneratorInternal.cs new file mode 100644 index 00000000..d16490b8 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileCSharpCodeGeneratorInternal.cs @@ -0,0 +1,23 @@ +using System.CodeDom; +using System.Reflection; +using System.Runtime.InteropServices; +using Microsoft.CSharp; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + [Guid("151F74CA-404D-4188-B994-D7683C32ACF4")] + [ComVisible(true)] + public class ReswFileCSharpCodeGeneratorInternal : ReswFileCodeGenerator + { + public ReswFileCSharpCodeGeneratorInternal() + : base(new CSharpCodeProvider(), TypeAttributes.NestedAssembly) + { + } + + public override int DefaultExtension(out string pbstrDefaultExtension) + { + pbstrDefaultExtension = ".cs"; + return 0; + } + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGenerator.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGenerator.cs new file mode 100644 index 00000000..d27a149b --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGenerator.cs @@ -0,0 +1,21 @@ +using System.Runtime.InteropServices; +using Microsoft.VisualBasic; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + [Guid("92DFB543-7138-419B-99D9-90CC77607671")] + [ComVisible(true)] + public class ReswFileVisualBasicCodeGenerator : ReswFileCodeGenerator + { + public ReswFileVisualBasicCodeGenerator() + : base(new VBCodeProvider()) + { + } + + public override int DefaultExtension(out string pbstrDefaultExtension) + { + pbstrDefaultExtension = ".vb"; + return 0; + } + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGeneratorInternal.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGeneratorInternal.cs new file mode 100644 index 00000000..393b6ccf --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ReswFileVisualBasicCodeGeneratorInternal.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using Microsoft.VisualBasic; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + [Guid("6C6AC14F-9B11-47C1-BC90-DFBFB89B1CB8")] + [ComVisible(true)] + public class ReswFileVisualBasicCodeGeneratorInternal : ReswFileCodeGenerator + { + public ReswFileVisualBasicCodeGeneratorInternal() + : base(new VBCodeProvider(), TypeAttributes.NestedAssembly) + { + } + + public override int DefaultExtension(out string pbstrDefaultExtension) + { + pbstrDefaultExtension = ".vb"; + return 0; + } + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/StringExtension.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/StringExtension.cs new file mode 100644 index 00000000..8134f073 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/StringExtension.cs @@ -0,0 +1,20 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public static class StringExtension + { + public static IntPtr ConvertToIntPtr(this string code, out uint pcbOutput) + { + var data = Encoding.Default.GetBytes(code); + + var ptr = Marshal.AllocCoTaskMem(data.Length); + Marshal.Copy(data, 0, ptr, data.Length); + + pcbOutput = (uint)data.Length; + return ptr; + } + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/VisualStudioVersion.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/VisualStudioVersion.cs new file mode 100644 index 00000000..504b43c6 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/VisualStudioVersion.cs @@ -0,0 +1,8 @@ +namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool +{ + public enum VisualStudioVersion + { + VS2012, + VS2013 + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuCSharpModelsBuilder.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuCSharpModelsBuilder.cs new file mode 100644 index 00000000..5c7da908 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuCSharpModelsBuilder.cs @@ -0,0 +1,21 @@ +using System.Runtime.InteropServices; +using Microsoft.CSharp; + +namespace Zbu.ModelsBuilder.CustomTool.CustomTool +{ + [Guid("98983F6D-BC77-46AC-BA5A-8D9E8763F0D2")] + [ComVisible(true)] + public class ZbuCSharpModelsBuilder : ZbuModelsBuilder + { + public ZbuCSharpModelsBuilder() + : base(new CSharpCodeProvider()) + { + } + + public override int DefaultExtension(out string pbstrDefaultExtension) + { + pbstrDefaultExtension = ".cs"; + return 0; + } + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuModelsBuilder.cs b/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuModelsBuilder.cs new file mode 100644 index 00000000..c8820eee --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/CustomTool/ZbuModelsBuilder.cs @@ -0,0 +1,68 @@ +using System; +using System.CodeDom.Compiler; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Text; +using System.Windows.Forms; +using Microsoft.VisualStudio.Shell.Interop; + +namespace Zbu.ModelsBuilder.CustomTool.CustomTool +{ + [ComVisible(true)] + public abstract class ZbuModelsBuilder : IVsSingleFileGenerator + { + private readonly CodeDomProvider _codeDomProvider; + private readonly TypeAttributes? _classAccessibility; + + protected ZbuModelsBuilder(CodeDomProvider codeDomProvider, TypeAttributes? classAccessibility = null) + { + this._codeDomProvider = codeDomProvider; + this._classAccessibility = classAccessibility; + } + + #region IVsSingleFileGenerator Members + + public abstract int DefaultExtension(out string pbstrDefaultExtension); + + public virtual int Generate(string wszInputFilePath, + string bstrInputFileContents, + string wszDefaultNamespace, + IntPtr[] rgbOutputFileContents, + out uint pcbOutput, + IVsGeneratorProgress pGenerateProgress) + { + try + { + IList types; + using (var umbraco = Umbraco.Application.GetApplication()) + { + types = umbraco.GetContentTypes(); + } + var builder = new Builder(); + builder.Prepare(types); + var sb = new StringBuilder(); + builder.Generate(sb, types); + var code = sb.ToString(); + + var data = Encoding.Default.GetBytes(code); + + var ptr = Marshal.AllocCoTaskMem(data.Length); + Marshal.Copy(data, 0, ptr, data.Length); + + pcbOutput = (uint)data.Length; + rgbOutputFileContents[0] = ptr; + } + catch (Exception e) + { + MessageBox.Show(e.Message, "Unable to generate code"); + throw; + } + + return 0; + } + + #endregion + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/GlobalSuppressions.cs b/Zbu.ModelsBuilder.CustomTool/GlobalSuppressions.cs new file mode 100644 index 00000000..a893f9d2 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/GlobalSuppressions.cs @@ -0,0 +1,11 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. Project-level +// suppressions either have no target or are given a specific target +// and scoped to a namespace, type, member, etc. +// +// To add a suppression to this file, right-click the message in the +// Error List, point to "Suppress Message(s)", and click "In Project +// Suppression File". You do not need to add suppressions to this +// file manually. + +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1017:MarkAssembliesWithComVisible")] diff --git a/Zbu.ModelsBuilder.CustomTool/Guids.cs b/Zbu.ModelsBuilder.CustomTool/Guids.cs new file mode 100644 index 00000000..ca602565 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/Guids.cs @@ -0,0 +1,15 @@ +// Guids.cs +// MUST match guids.h + +using System; + +namespace Zbu.ModelsBuilder.CustomTool +{ + static class GuidList + { + public const string PkgString = "6a4c1726-440f-4b2d-a2e5-711277da6099"; + public const string CmdSetString = "fb40dc0b-2f75-404c-ba4e-dc1b90c41941"; + + public static readonly Guid CmdSet = new Guid(CmdSetString); + }; +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/Properties/AssemblyInfo.cs b/Zbu.ModelsBuilder.CustomTool/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..45c57c2f --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/Properties/AssemblyInfo.cs @@ -0,0 +1,9 @@ +using System; +using System.Reflection; +using System.Resources; + +[assembly: AssemblyTitle("ZpqrtBnk Umbraco ModelsBuilder Custom Tool")] +[assembly: AssemblyDescription("ZpqrtBnk Umbraco Visual Studio Custom Tool for generating strongly typed IPublishedContent models.")] +[assembly: CLSCompliant(false)] +[assembly: NeutralResourcesLanguage("en-US")] +// no guid \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/Resources.Designer.cs b/Zbu.ModelsBuilder.CustomTool/Resources.Designer.cs new file mode 100644 index 00000000..bcaf4982 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.18408 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Zbu.ModelsBuilder.CustomTool { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Zbu.ModelsBuilder.CustomTool.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/Resources.resx b/Zbu.ModelsBuilder.CustomTool/Resources.resx new file mode 100644 index 00000000..891c592b --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/Resources.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/Resources/Package.ico b/Zbu.ModelsBuilder.CustomTool/Resources/Package.ico new file mode 100644 index 0000000000000000000000000000000000000000..449296f495af26f2b41bb1626a28de7432145472 GIT binary patch literal 2998 zcmeHJIc@?$5G;1VEan=&z(kgxVUhCya&kft2^&G^5AXoz*K%ZEh6^mKRC)SN;9Ie2I^4EsEuJm3ak4(0(K0-)vtx2mz-v5Du5+`|?E{2~nF zj-DY~i1~a@z`8H2Rm8@RN^*7j%+?3;*Is5r;m32z^?Gs%|GV@5y&h|~@doa#jn6yX zPm+(*yf56oD($=C(DJ(6=%K6Llfe5Uo;dPV7%T5PITm!!YhANqXLaS`0ufOBi8crP zkWX22ZGmf>3$1+|+x>UW$1lsro%1<*HCnUTM61<8yWK{o(?Pe}#b7YNXf(oPGQo5@ z#cVdiVzIz-xkMC2Sglrgd0u0)* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ZpqrtBnk Umbraco ModelsBuilder Custom Tool + + + ZpqrtBnk Umbraco Visual Studio Custom Tool for generating strongly typed IPublishedContent models. + + + Resources\Package.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/VisualStudio2012Package.cs b/Zbu.ModelsBuilder.CustomTool/VisualStudio2012Package.cs new file mode 100644 index 00000000..8ba0eaab --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/VisualStudio2012Package.cs @@ -0,0 +1,62 @@ +using System.Diagnostics; +using System.Globalization; +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Design.Serialization; +using Zbu.ModelsBuilder.CustomTool.CustomTool; + +namespace Zbu.ModelsBuilder.CustomTool +{ + /// + /// This is the class that implements the package exposed by this assembly. + /// + /// The minimum requirement for a class to be considered a valid package for Visual Studio + /// is to implement the IVsPackage interface and register itself with the shell. + /// This package uses the helper classes defined inside the Managed Package Framework (MPF) + /// to do it: it derives from the Package class that provides the implementation of the + /// IVsPackage interface and uses the registration attributes defined in the framework to + /// register itself and its components with the shell. + /// + // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is + // a package. + [PackageRegistration(UseManagedResourcesOnly = true)] + // This attribute is used to register the information needed to show this package + // in the Help/About dialog of Visual Studio. + [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] + [Guid(GuidList.PkgString)] + [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\11.0")] + [ProvideObject(typeof(ZbuCSharpModelsBuilder))] + [ProvideGeneratorAttribute(typeof(ZbuCSharpModelsBuilder), "ZbuModelsBuilder", "ZpqrtBnk Umbraco ModelsBuilder Custom Tool for C#", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", true)] // csharp + public sealed class VisualStudio2012Package : Package + { + /// + /// Default constructor of the package. + /// Inside this method you can place any initialization code that does not require + /// any Visual Studio service because at this point the package object is created but + /// not sited yet inside Visual Studio environment. The place to do all the other + /// initialization is the Initialize method. + /// + public VisualStudio2012Package() + { + Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString())); + } + + + + ///////////////////////////////////////////////////////////////////////////// + // Overridden Package Implementation + #region Package Members + + /// + /// Initialization of the package; this method is called right after the package is sited, so this is the place + /// where you can put all the initialization code that rely on services provided by VisualStudio. + /// + protected override void Initialize() + { + Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); + base.Initialize(); + } + #endregion + + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/VisualStudio2013Package.cs b/Zbu.ModelsBuilder.CustomTool/VisualStudio2013Package.cs new file mode 100644 index 00000000..9afbf950 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/VisualStudio2013Package.cs @@ -0,0 +1,62 @@ +using System.Diagnostics; +using System.Globalization; +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Design.Serialization; +using Zbu.ModelsBuilder.CustomTool.CustomTool; + +namespace Zbu.ModelsBuilder.CustomTool +{ + /// + /// This is the class that implements the package exposed by this assembly. + /// + /// The minimum requirement for a class to be considered a valid package for Visual Studio + /// is to implement the IVsPackage interface and register itself with the shell. + /// This package uses the helper classes defined inside the Managed Package Framework (MPF) + /// to do it: it derives from the Package class that provides the implementation of the + /// IVsPackage interface and uses the registration attributes defined in the framework to + /// register itself and its components with the shell. + /// + // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is + // a package. + [PackageRegistration(UseManagedResourcesOnly = true)] + // This attribute is used to register the information needed to show this package + // in the Help/About dialog of Visual Studio. + [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] + [Guid(GuidList.PkgString)] + [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\12.0")] + [ProvideObject(typeof(ZbuCSharpModelsBuilder))] + [ProvideGeneratorAttribute(typeof(ZbuCSharpModelsBuilder), "ZbuModelsBuilder", "ZpqrtBnk Umbraco ModelsBuilder Custom Tool for C#", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", true)] // csharp + public sealed class VisualStudio2013Package : Package + { + /// + /// Default constructor of the package. + /// Inside this method you can place any initialization code that does not require + /// any Visual Studio service because at this point the package object is created but + /// not sited yet inside Visual Studio environment. The place to do all the other + /// initialization is the Initialize method. + /// + public VisualStudio2013Package() + { + Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString())); + } + + + + ///////////////////////////////////////////////////////////////////////////// + // Overridden Package Implementation + #region Package Members + + /// + /// Initialization of the package; this method is called right after the package is sited, so this is the place + /// where you can put all the initialization code that rely on services provided by VisualStudio. + /// + protected override void Initialize() + { + Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); + base.Initialize(); + } + #endregion + + } +} diff --git a/Zbu.ModelsBuilder.CustomTool/Zbu.ModelsBuilder.CustomTool.csproj b/Zbu.ModelsBuilder.CustomTool/Zbu.ModelsBuilder.CustomTool.csproj new file mode 100644 index 00000000..42c8c7ff --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/Zbu.ModelsBuilder.CustomTool.csproj @@ -0,0 +1,187 @@ + + + + 11.0 + 11.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + Debug + AnyCPU + 2.0 + {0419A43D-78C2-46C9-95A9-2470F224D60F} + {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + Properties + Zbu.ModelsBuilder.CustomTool + Zbu.ModelsBuilder.CustomTool + false + Key.snk + v4.5 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + true + + + + + + + + + + + true + + + + + + + + + + + + + + + + {80CC9F66-E7D8-4DDD-85B6-D9E6CD0E93E2} + 8 + 0 + 0 + primary + False + False + + + {26AD1324-4B7C-44BC-84F8-B86AED45729F} + 10 + 0 + 0 + primary + False + False + + + {1A31287A-4D7D-413E-8E32-3B374931BD89} + 8 + 0 + 0 + primary + False + False + + + {2CE2370E-D744-4936-A090-3FFFE667B0E1} + 9 + 0 + 0 + primary + False + False + + + {1CBA492E-7263-47BB-87FE-639000619B15} + 8 + 0 + 0 + primary + False + False + + + {00020430-0000-0000-C000-000000000046} + 2 + 0 + 0 + primary + False + False + + + + + Properties\CommonInfo.cs + + + + + + True + True + Resources.resx + + + + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + true + VSPackage + + + + + Designer + + + + + + + + true + + + Always + true + + + + + {ba9b7672-c446-4001-bb64-2b6abe7a42f2} + Zbu.ModelsBuilder.Umbraco + + + {998fb014-303a-4146-b3e4-b927bab0210f} + Zbu.ModelsBuilder + + + + true + + + + + \ No newline at end of file diff --git a/Zbu.ModelsBuilder.CustomTool/license.txt b/Zbu.ModelsBuilder.CustomTool/license.txt new file mode 100644 index 00000000..af70d805 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/license.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Pilotine - ZpqrtBnk + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Zbu.ModelsBuilder.CustomTool/source.extension.vsixmanifest b/Zbu.ModelsBuilder.CustomTool/source.extension.vsixmanifest new file mode 100644 index 00000000..9803b213 --- /dev/null +++ b/Zbu.ModelsBuilder.CustomTool/source.extension.vsixmanifest @@ -0,0 +1,33 @@ + + + + + ZpqrtBnk Umbraco ModelsBuilder Custom Tool + ZpqrtBnk Umbraco Visual Studio Custom Tool for generating strongly typed IPublishedContent models. + license.txt + + + + + + + + + + + + diff --git a/Zbu.ModelsBuilder.Tests/App.config b/Zbu.ModelsBuilder.Tests/App.config index 8e330f98..958564ba 100644 --- a/Zbu.ModelsBuilder.Tests/App.config +++ b/Zbu.ModelsBuilder.Tests/App.config @@ -3,10 +3,12 @@ + \ No newline at end of file diff --git a/Zbu.ModelsBuilder.Tests/BuilderTests.cs b/Zbu.ModelsBuilder.Tests/BuilderTests.cs index 67286ff9..8e7275c6 100644 --- a/Zbu.ModelsBuilder.Tests/BuilderTests.cs +++ b/Zbu.ModelsBuilder.Tests/BuilderTests.cs @@ -14,36 +14,30 @@ namespace Zbu.ModelsBuilder.Tests [TestFixture] public class BuilderTests { - private class AppHandler : ApplicationEventHandler - { - protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - base.ApplicationStarting(umbracoApplication, applicationContext); - - // remove core converters that are replaced by web converted - PropertyValueConvertersResolver.Current.RemoveType(); - PropertyValueConvertersResolver.Current.RemoveType(); - PropertyValueConvertersResolver.Current.RemoveType(); - } - } - //[SetUp] [TestFixtureSetUp] public void Setup() { - var app = Umbraco.Web.Standalone.StandaloneApplication.GetApplication(Environment.CurrentDirectory) - .WithoutApplicationEventHandler() - .WithApplicationEventHandler(); - //if (app.Started == false) - app.Start(); + //var app = Umbraco.Web.Standalone.StandaloneApplication.GetApplication(Environment.CurrentDirectory) + // .WithoutApplicationEventHandler() + // .WithApplicationEventHandler(); + ////if (app.Started == false) + //app.Start(); } [Test] public void Test() { - var sb = new StringBuilder(); + IList types; + using (var umbraco = Umbraco.Application.GetApplication()) + { + types = umbraco.GetContentTypes(); + } + var builder = new Builder(); - builder.Generate(sb, builder.GetTypes()); + var sb = new StringBuilder(); + builder.Prepare(types); + builder.Generate(sb, types); Console.WriteLine(sb.ToString()); } @@ -88,10 +82,17 @@ public partial class Page public string AlternativeText { get { return this.GetPropertyValue(""alternativeText""); } } // fixme } } -"; - var sb = new StringBuilder(); +"; + + IList types; + using (var umbraco = Umbraco.Application.GetApplication()) + { + types = umbraco.GetContentTypes(); + } + var builder = new Builder(); - var types = builder.GetTypes(); + var sb = new StringBuilder(); + builder.Prepare(types); builder.Parse(code, types); builder.Generate(sb, types); Console.WriteLine(sb.ToString()); diff --git a/Zbu.ModelsBuilder.Tests/ConfigTests.cs b/Zbu.ModelsBuilder.Tests/ConfigTests.cs new file mode 100644 index 00000000..3bdcc2c6 --- /dev/null +++ b/Zbu.ModelsBuilder.Tests/ConfigTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Zbu.ModelsBuilder.Tests +{ + [TestFixture] + public class ConfigTests + { + [Test] + public void Test() + { + // not here + Assert.IsNull(ConfigurationManager.AppSettings["testKey"]); + + // throws because it is read-only + Assert.Throws(() => ConfigurationManager.AppSettings.Add("testKey", "testValue")); + + // install editable configuration manger + ConfigSystem.Install(); + + // not here + Assert.IsNull(ConfigurationManager.AppSettings["testKey"]); + + // can add, read + ConfigurationManager.AppSettings.Add("testKey", "testValue"); + Assert.AreEqual("testValue", ConfigurationManager.AppSettings["testKey"]); + + // can remove + ConfigurationManager.AppSettings.Remove("testKey"); + Assert.IsNull(ConfigurationManager.AppSettings["testKey"]); + + // can reset + ConfigurationManager.AppSettings.Add("testKey", "testValue"); + Assert.AreEqual("testValue", ConfigurationManager.AppSettings["testKey"]); + ConfigSystem.Reset(); + Assert.IsNull(ConfigurationManager.AppSettings["testKey"]); + + // can uninstall + ConfigurationManager.AppSettings.Add("testKey", "testValue"); + ConfigSystem.Uninstall(); + Assert.IsNull(ConfigurationManager.AppSettings["testKey"]); + Assert.Throws(() => ConfigurationManager.AppSettings.Add("testKey", "testValue")); + } + } +} diff --git a/Zbu.ModelsBuilder.Tests/ExtensionsTests.cs b/Zbu.ModelsBuilder.Tests/ExtensionsTests.cs new file mode 100644 index 00000000..cbc05656 --- /dev/null +++ b/Zbu.ModelsBuilder.Tests/ExtensionsTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Zbu.ModelsBuilder.Tests +{ + [TestFixture] + public class ExtensionsTests + { + [Test] + public void RemoveAll() + { + var list = new List + { + "a1", "z1", "a2", "z2", "a3", "z3" + }; + + list.RemoveAll(x => x.StartsWith("a")); + + Assert.AreEqual(3, list.Count); + Assert.IsTrue(list.TrueForAll(x => x.StartsWith("z"))); + } + } +} diff --git a/Zbu.ModelsBuilder.Tests/Properties/AssemblyInfo.cs b/Zbu.ModelsBuilder.Tests/Properties/AssemblyInfo.cs index 3dfadaab..e500ee8d 100644 --- a/Zbu.ModelsBuilder.Tests/Properties/AssemblyInfo.cs +++ b/Zbu.ModelsBuilder.Tests/Properties/AssemblyInfo.cs @@ -1,36 +1,6 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Zbu.ModelsBuilder.Tests")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("HP")] -[assembly: AssemblyProduct("Zbu.ModelsBuilder.Tests")] -[assembly: AssemblyCopyright("Copyright © HP 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: AssemblyTitle("ZpqrtBnk Umbraco ModelsBuilder Tests")] +[assembly: AssemblyDescription("Tests for ZpqrtBnk Umbraco tool for generating strongly typed IPublishedContent models.")] [assembly: Guid("5957b45a-c2c9-4913-be3f-6677ebf50294")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Zbu.ModelsBuilder.Tests/UmbracoApplicationTests.cs b/Zbu.ModelsBuilder.Tests/UmbracoApplicationTests.cs new file mode 100644 index 00000000..a8e90d46 --- /dev/null +++ b/Zbu.ModelsBuilder.Tests/UmbracoApplicationTests.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using Zbu.ModelsBuilder.Umbraco; + +namespace Zbu.ModelsBuilder.Tests +{ + [TestFixture] + public class UmbracoApplicationTests + { + [Test] + public void Test() + { + // start and terminate + using (var app = Application.GetApplication()) + { } + + // start and terminate + using (var app = Application.GetApplication()) + { } + + // start, use and terminate + using (var app = Application.GetApplication()) + { + var types = app.GetContentTypes(); + } + } + } +} diff --git a/Zbu.ModelsBuilder.Tests/Zbu.ModelsBuilder.Tests.csproj b/Zbu.ModelsBuilder.Tests/Zbu.ModelsBuilder.Tests.csproj index 2497ea8a..d5c3d699 100644 --- a/Zbu.ModelsBuilder.Tests/Zbu.ModelsBuilder.Tests.csproj +++ b/Zbu.ModelsBuilder.Tests/Zbu.ModelsBuilder.Tests.csproj @@ -32,10 +32,8 @@ 4 - - False + ..\..\..\src\src\Umbraco.Web.UI\bin\interfaces.dll - True False @@ -57,6 +55,7 @@ ..\packages\Roslyn.Services.Common.1.2.20906.2\lib\net45\Roslyn.Utilities.dll + @@ -68,31 +67,45 @@ ..\..\..\src\src\Umbraco.Web.UI\bin\umbraco.dll - True - - False + ..\..\..\src\src\Umbraco.Web.UI\bin\Umbraco.Core.dll - True + + Properties\CommonInfo.cs + + + + + + {ba9b7672-c446-4001-bb64-2b6abe7a42f2} + Zbu.ModelsBuilder.Umbraco + {998fb014-303a-4146-b3e4-b927bab0210f} Zbu.ModelsBuilder + + + Foo + ZbuModelsBuilder + CustomTool.generated.cs + + + \ No newline at end of file diff --git a/Zbu.ModelsBuilder.sln b/Zbu.ModelsBuilder.sln index 84fd67da..e5800cc2 100644 --- a/Zbu.ModelsBuilder.sln +++ b/Zbu.ModelsBuilder.sln @@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0B3729 .nuget\NuGet.targets = .nuget\NuGet.targets EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zbu.ModelsBuilder.CustomTool", "Zbu.ModelsBuilder.CustomTool\Zbu.ModelsBuilder.CustomTool.csproj", "{0419A43D-78C2-46C9-95A9-2470F224D60F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zbu.ModelsBuilder.Umbraco", "Zbu.ModelsBuilder.Umbraco\Zbu.ModelsBuilder.Umbraco.csproj", "{BA9B7672-C446-4001-BB64-2B6ABE7A42F2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -26,6 +30,14 @@ Global {CF597D44-04EA-4FD7-89C8-E4849D1355C8}.Debug|Any CPU.Build.0 = Debug|Any CPU {CF597D44-04EA-4FD7-89C8-E4849D1355C8}.Release|Any CPU.ActiveCfg = Release|Any CPU {CF597D44-04EA-4FD7-89C8-E4849D1355C8}.Release|Any CPU.Build.0 = Release|Any CPU + {0419A43D-78C2-46C9-95A9-2470F224D60F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0419A43D-78C2-46C9-95A9-2470F224D60F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0419A43D-78C2-46C9-95A9-2470F224D60F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0419A43D-78C2-46C9-95A9-2470F224D60F}.Release|Any CPU.Build.0 = Release|Any CPU + {BA9B7672-C446-4001-BB64-2B6ABE7A42F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA9B7672-C446-4001-BB64-2B6ABE7A42F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA9B7672-C446-4001-BB64-2B6ABE7A42F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA9B7672-C446-4001-BB64-2B6ABE7A42F2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Zbu.ModelsBuilder/Builder.cs b/Zbu.ModelsBuilder/Builder.cs index 140aa6ba..326e0911 100644 --- a/Zbu.ModelsBuilder/Builder.cs +++ b/Zbu.ModelsBuilder/Builder.cs @@ -2,18 +2,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; using Roslyn.Compilers.CSharp; -using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Strings; namespace Zbu.ModelsBuilder { public class Builder { - public List GetTypes() + /* + public List GetTypes() { var contentTypeService = ApplicationContext.Current.Services.ContentTypeService; var contentTypes = contentTypeService.GetAllContentTypes(); @@ -21,8 +17,9 @@ public List GetTypes() return GetTypes(contentTypes).ToList(); // fixme list?! } + */ - public void Generate(StringBuilder sb, IEnumerable genTypes) + public void Generate(StringBuilder sb, IEnumerable genTypes) { //var contentTypeService = ApplicationContext.Current.Services.ContentTypeService; //var contentTypes = contentTypeService.GetAllContentTypes(); @@ -61,7 +58,7 @@ public void Generate(StringBuilder sb, IEnumerable genTypes) #region Write - void WriteContentType(StringBuilder sb, GenType type) + void WriteContentType(StringBuilder sb, TypeModel type) { if (type.IsMixin) { @@ -119,7 +116,7 @@ void WriteContentType(StringBuilder sb, GenType type) sb.Append("\t}\n"); } - void WriteContentTypeProperties(StringBuilder sb, GenType type) + void WriteContentTypeProperties(StringBuilder sb, TypeModel type) { // write the properties foreach (var prop in type.Properties) @@ -131,7 +128,7 @@ void WriteContentTypeProperties(StringBuilder sb, GenType type) WriteProperty(sb, prop); } - void WriteProperty(StringBuilder sb, GenProperty property) + void WriteProperty(StringBuilder sb, PropertyModel property) { sb.Append("\n"); @@ -149,7 +146,7 @@ void WriteProperty(StringBuilder sb, GenProperty property) property.Alias); } - void WriteInterfaceProperty(StringBuilder sb, GenProperty property) + void WriteInterfaceProperty(StringBuilder sb, PropertyModel property) { sb.Append("\t\t"); WriteClrType(sb, property.ClrType); @@ -229,15 +226,16 @@ void WriteNonGenericClrType(StringBuilder sb, string s) #endregion + /* #region Prepare - IEnumerable GetTypes(IEnumerable types) + IEnumerable GetTypes(IEnumerable types) { - var gTypes = new List(); + var gTypes = new List(); var aTypes = types.ToArray(); foreach (var type in aTypes) { - var gType = new GenType + var gType = new TypeModel { Id = type.Id, Alias = type.Alias, @@ -251,7 +249,7 @@ IEnumerable GetTypes(IEnumerable types) foreach (var property in type.PropertyTypes) { - var gProperty = new GenProperty + var gProperty = new PropertyModel { Alias = property.Alias, Name = property.Alias.ToCleanString(CleanStringType.PascalCase) @@ -301,7 +299,7 @@ IEnumerable GetTypes(IEnumerable types) var parentTree = GetTypeTree(gType.BaseType); gType.DeclaringInterfaces.AddRange(gType.MixinTypes.Except(parentTree)); - var recursiveInterfaces = new List(); + var recursiveInterfaces = new List(); foreach (var i in gType.DeclaringInterfaces) GetTypeTree(recursiveInterfaces, i); gType.ImplementingInterfaces.AddRange(recursiveInterfaces.Except(parentTree)); @@ -310,14 +308,14 @@ IEnumerable GetTypes(IEnumerable types) return gTypes; } - List GetTypeTree(GenType type) + List GetTypeTree(TypeModel type) { - var tree = new List(); + var tree = new List(); if (type != null) GetTypeTree(tree, type); return tree; } - void GetTypeTree(List types, GenType type) + void GetTypeTree(List types, TypeModel type) { if (types.Contains(type) == false) types.Add(type); @@ -327,32 +325,34 @@ void GetTypeTree(List types, GenType type) GetTypeTree(types, mixin); } - public class GenType - { - public int Id; - public string Alias; - public string Name; - public int BaseTypeId; - public GenType BaseType; - public readonly List Properties = new List(); - public readonly List MixinTypes = new List(); - public readonly List DeclaringInterfaces = new List(); - public readonly List ImplementingInterfaces = new List(); - public bool IsMixin; - } + #endregion + */ - public class GenProperty + #region Prepare + + public void Prepare(IList typeModels) { - public string Alias; - public string Name; - public Type ClrType; + // discover interfaces that need to be declared / implemented + foreach (var typeModel in typeModels) + { + var parentTree = typeModel.BaseType == null + ? new List() + : typeModel.BaseType.GetTypeTree(); + + typeModel.DeclaringInterfaces.AddRange(typeModel.MixinTypes.Except(parentTree)); + + var recursiveInterfaces = new List(); + foreach (var i in typeModel.DeclaringInterfaces) + TypeModel.GetTypeTree(recursiveInterfaces, i); + typeModel.ImplementingInterfaces.AddRange(recursiveInterfaces.Except(parentTree)); + } } - #endregion + #endregion #region Parse - public void Parse(string code, IList genTypes) + public void Parse(string code, IList genTypes) { var tree = SyntaxTree.ParseText(code); var writer = new CodeWalker(); diff --git a/Zbu.ModelsBuilder/ConfigSystem.cs b/Zbu.ModelsBuilder/ConfigSystem.cs new file mode 100644 index 00000000..9429cfc5 --- /dev/null +++ b/Zbu.ModelsBuilder/ConfigSystem.cs @@ -0,0 +1,264 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Configuration; +using System.Configuration.Internal; +using System.Linq; +using System.Reflection; +using System.Threading; + +namespace Zbu.ModelsBuilder +{ + // see http://stackoverflow.com/questions/15653621/how-to-update-add-modify-delete-keys-in-appsettings-section-of-web-config-at-r + // see http://www.codeproject.com/Articles/69364/Override-Configuration-Manager + + public sealed class ConfigSystem : IInternalConfigSystem + { + private static readonly ReaderWriterLockSlim RwLock = new ReaderWriterLockSlim(); + private static ConfigSystem _installed; + private static IInternalConfigSystem _clientConfigSystem; + private object _appsettings; + private object _connectionStrings; + private static object _sInitStateOrig; + private static object _sConfigSystemOrig; + + public static bool Installed + { + get + { + try + { + RwLock.EnterReadLock(); + return _installed != null; + } + finally + { + RwLock.ExitReadLock(); + } + } + } + + /// + /// Re-initializes the ConfigurationManager, allowing us to merge in the settings from Core.Config + /// + public static void Install() + { + try + { + RwLock.EnterWriteLock(); + + if (_installed != null) + throw new InvalidOperationException("ConfigSystem is already installed."); + + FieldInfo[] fiStateValues = null; + var tInitState = typeof(ConfigurationManager).GetNestedType("InitState", BindingFlags.NonPublic); + + if (tInitState != null) + fiStateValues = tInitState.GetFields(); + // 0: NotStarted + // 1: Started + // 2: Usable + // 3: Completed + + var fiInit = typeof(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static); + var fiSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static); + + if (fiInit != null && fiSystem != null && fiStateValues != null) + { + _sInitStateOrig = fiInit.GetValue(null); + _sConfigSystemOrig = fiSystem.GetValue(null); + fiInit.SetValue(null, fiStateValues[1].GetValue(null)); // set to Started + fiSystem.SetValue(null, null); // clear current config system + } + + _installed = new ConfigSystem(); + + var configFactoryType = Type.GetType("System.Configuration.Internal.InternalConfigSettingsFactory, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); + var configSettingsFactory = (IInternalConfigSettingsFactory)Activator.CreateInstance(configFactoryType, true); + // just does ConfigurationManager.SetConfigurationSystem(_installed, false); + // 'false' turns initState to 2 ie usable (vs 3 ie completed) + configSettingsFactory.SetConfigurationSystem(_installed, false); + + // note: prob. don't need the factory... see how we uninstall... + + var clientConfigSystemType = Type.GetType("System.Configuration.ClientConfigurationSystem, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); + _clientConfigSystem = (IInternalConfigSystem)Activator.CreateInstance(clientConfigSystemType, true); + } + finally + { + RwLock.ExitWriteLock(); + } + } + + public static void Uninstall() + { + try + { + RwLock.EnterWriteLock(); + + if (_installed == null) + throw new InvalidOperationException("ConfigSystem is not installed."); + + FieldInfo[] fiStateValues = null; + var tInitState = typeof(ConfigurationManager).GetNestedType("InitState", BindingFlags.NonPublic); + + if (tInitState != null) + fiStateValues = tInitState.GetFields(); + + var fiInit = typeof(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static); + var fiSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static); + + if (fiInit != null && fiSystem != null && fiStateValues != null) + { + // reset - the hard way + fiInit.SetValue(null, _sInitStateOrig); + fiSystem.SetValue(null, _sConfigSystemOrig); + } + + _installed = null; + _clientConfigSystem = null; + } + finally + { + RwLock.ExitWriteLock(); + } + } + + public static void Reset() + { + try + { + RwLock.EnterWriteLock(); + + if (_installed == null) + throw new InvalidOperationException("ConfigSystem is not installed."); + + _installed._appsettings = null; + _installed._connectionStrings = null; + } + finally + { + RwLock.ExitWriteLock(); + } + } + + #region IInternalConfigSystem Members + + public object GetSection(string configKey) + { + // get the section from the default location (web.config or app.config) + var section = _clientConfigSystem.GetSection(configKey); + + try + { + RwLock.EnterReadLock(); + + switch (configKey) + { + case "appSettings": + // Return cached version if exists + if (_appsettings != null) + return _appsettings; + + // create a new collection because the underlying collection is read-only + var cfg = new NameValueCollection(); + + // If an AppSettings section exists in Web.config, read and add values from it + var nvSection = section as NameValueCollection; + if (nvSection != null) + { + var localSettings = nvSection; + foreach (string key in localSettings) + cfg.Add(key, localSettings[key]); + } + + //// -------------------------------------------------------------------- + //// Here I read and decrypt keys and add them to secureConfig dictionary + //// To test assume the following line is a key stored in secure sotrage. + ////secureConfig = SecureConfig.LoadConfig(); + //secureConfig.Add("ACriticalKey", "VeryCriticalValue"); + //// -------------------------------------------------------------------- + //foreach (KeyValuePair item in secureConfig) + //{ + // if (cfg.AllKeys.Contains(item.Key)) + // { + // cfg[item.Key] = item.Value; + // } + // else + // { + // cfg.Add(item.Key, item.Value); + // } + //} + //// -------------------------------------------------------------------- + + + // Cach the settings for future use + + _appsettings = cfg; + // return the merged version of the items from secure storage and appsettings + section = _appsettings; + break; + + case "connectionStrings": + // Return cached version if exists + if (_connectionStrings != null) + return _connectionStrings; + + // create a new collection because the underlying collection is read-only + var connectionStringsSection = new ConnectionStringsSection(); + + // copy the existing connection strings into the new collection + foreach ( + ConnectionStringSettings connectionStringSetting in + ((ConnectionStringsSection) section).ConnectionStrings) + connectionStringsSection.ConnectionStrings.Add(connectionStringSetting); + + // -------------------------------------------------------------------- + // Again Load connection strings from secure storage and merge like below + // connectionStringsSection.ConnectionStrings.Add(connectionStringSetting); + // -------------------------------------------------------------------- + + // Cach the settings for future use + _connectionStrings = connectionStringsSection; + // return the merged version of the items from secure storage and appsettings + section = _connectionStrings; + break; + } + } + finally + { + RwLock.ExitReadLock(); + } + + return section; + } + + public void RefreshConfig(string sectionName) + { + try + { + RwLock.EnterWriteLock(); + + if (sectionName == "appSettings") + { + _appsettings = null; + } + + if (sectionName == "connectionStrings") + { + _connectionStrings = null; + } + } + finally + { + RwLock.ExitWriteLock(); + } + + _clientConfigSystem.RefreshConfig(sectionName); + } + + public bool SupportsUserConfig { get { return _clientConfigSystem.SupportsUserConfig; } } + + #endregion + } +} \ No newline at end of file diff --git a/Zbu.ModelsBuilder/EnumerableExtensions.cs b/Zbu.ModelsBuilder/EnumerableExtensions.cs new file mode 100644 index 00000000..00e205b8 --- /dev/null +++ b/Zbu.ModelsBuilder/EnumerableExtensions.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Zbu.ModelsBuilder +{ + public static class EnumerableExtensions + { + public static void RemoveAll(this IList list, Func predicate) + { + for (var i = 0; i < list.Count; i++) + { + if (predicate(list[i])) + { + list.RemoveAt(i--); // i-- is important here! + } + } + } + } +} diff --git a/Zbu.ModelsBuilder/Properties/AssemblyInfo.cs b/Zbu.ModelsBuilder/Properties/AssemblyInfo.cs index 95f81df2..be6fa75d 100644 --- a/Zbu.ModelsBuilder/Properties/AssemblyInfo.cs +++ b/Zbu.ModelsBuilder/Properties/AssemblyInfo.cs @@ -1,36 +1,6 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Zbu.ModelsBuilder")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("HP")] -[assembly: AssemblyProduct("Zbu.ModelsBuilder")] -[assembly: AssemblyCopyright("Copyright © HP 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: AssemblyTitle("ZpqrtBnk Umbraco ModelsBuilder")] +[assembly: AssemblyDescription("ZpqrtBnk Umbraco tool for generating strongly typed IPublishedContent models.")] [assembly: Guid("b59ebab2-bc7c-4a89-876f-7613684510e2")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Zbu.ModelsBuilder/Properties/CommonInfo.cs b/Zbu.ModelsBuilder/Properties/CommonInfo.cs new file mode 100644 index 00000000..18400b3f --- /dev/null +++ b/Zbu.ModelsBuilder/Properties/CommonInfo.cs @@ -0,0 +1,14 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("ZpqrtBnk Umbraco ModelsBuilder")] +[assembly: AssemblyCompany("Pilotine - ZpqrtBnk")] +[assembly: AssemblyCopyright("Copyright © Pilotine - ZpqrtBnk 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: AssemblyVersion("1.4.0.0")] +[assembly: AssemblyFileVersion("1.4.0.0")] diff --git a/Zbu.ModelsBuilder/PropertyModel.cs b/Zbu.ModelsBuilder/PropertyModel.cs new file mode 100644 index 00000000..843f79f5 --- /dev/null +++ b/Zbu.ModelsBuilder/PropertyModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace Zbu.ModelsBuilder +{ + public class PropertyModel + { + public string Alias; + public string Name; + public Type ClrType; + } +} diff --git a/Zbu.ModelsBuilder/StringExtensions.cs b/Zbu.ModelsBuilder/StringExtensions.cs new file mode 100644 index 00000000..dfeafe13 --- /dev/null +++ b/Zbu.ModelsBuilder/StringExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Zbu.ModelsBuilder +{ + public static class StringExtensions + { + public static bool InvariantEquals(this string s, string other) + { + return String.Equals(s, other, StringComparison.InvariantCultureIgnoreCase); + } + } +} diff --git a/Zbu.ModelsBuilder/TypeModel.cs b/Zbu.ModelsBuilder/TypeModel.cs new file mode 100644 index 00000000..bfbe0e97 --- /dev/null +++ b/Zbu.ModelsBuilder/TypeModel.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; + +namespace Zbu.ModelsBuilder +{ + public class TypeModel + { + public int Id; + public string Alias; + public string Name; + public int BaseTypeId; + public TypeModel BaseType; + public readonly List Properties = new List(); + public readonly List MixinTypes = new List(); + public readonly List DeclaringInterfaces = new List(); + public readonly List ImplementingInterfaces = new List(); + public bool IsMixin; + + public List GetTypeTree() + { + var tree = new List(); + GetTypeTree(tree, this); + return tree; + } + + public static void GetTypeTree(ICollection types, TypeModel type) + { + if (types.Contains(type) == false) + types.Add(type); + if (type.BaseType != null) + GetTypeTree(types, type.BaseType); + foreach (var mixin in type.MixinTypes) + GetTypeTree(types, mixin); + } + } +} diff --git a/Zbu.ModelsBuilder/Zbu.ModelsBuilder.csproj b/Zbu.ModelsBuilder/Zbu.ModelsBuilder.csproj index 65de0649..590a6d26 100644 --- a/Zbu.ModelsBuilder/Zbu.ModelsBuilder.csproj +++ b/Zbu.ModelsBuilder/Zbu.ModelsBuilder.csproj @@ -31,6 +31,12 @@ prompt 4 + + false + + + Key.snk + ..\packages\Roslyn.Compilers.Common.1.2.20906.2\lib\net45\Roslyn.Compilers.dll @@ -48,29 +54,29 @@ ..\packages\Roslyn.Services.Common.1.2.20906.2\lib\net45\Roslyn.Utilities.dll + - - ..\..\..\src\src\Umbraco.Web.UI\bin\umbraco.dll - True - - - ..\..\..\src\src\Umbraco.Web.UI\bin\Umbraco.Core.dll - True - + + + + + + + diff --git a/license.txt b/license.txt new file mode 100644 index 00000000..af70d805 --- /dev/null +++ b/license.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Pilotine - ZpqrtBnk + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/readme.txt b/readme.txt new file mode 100644 index 00000000..d4a8c0a8 --- /dev/null +++ b/readme.txt @@ -0,0 +1,13 @@ +ZpqrtBnk Umbraco Models Builder + +Status +All tests are working +Visual Studio custom tool is working +Cannot sign assemblies because Umbraco itself is not signed +Is this going to be an issue? + +Todo +Generate multiple files +Clear files before generating +Use the .generated.cs extension +Configure connection string, version... \ No newline at end of file