6 Commits

Author SHA1 Message Date
Christian Resma Helle 58d95afea5 Implement generating code for either VS2012 or VS2013 2013-10-24 00:08:29 +02:00
Christian Resma Helle 5e50270b4b Removed tag v1.2 2013-10-23 23:51:34 +02:00
Christian Resma Helle e93bc8bfd2 Added tag v1.2 for changeset 41eacde49f95 2013-10-23 21:56:43 +02:00
Christian Resma Helle 4f89aeefe4 Added tag v1.1 for changeset 3a9f94503dea 2013-10-23 21:56:31 +02:00
Christian Resma Helle b2429f3add Creates the ResourceLoader instance using the static method GetForCurrentView() instead of the constructor to resolve the Visual Studio 2013 compiler warning 2013-10-23 21:56:08 +02:00
Christian Resma Helle abeb5e25ab changed target to visual studio integrated shell 2013-08-19 13:01:15 +02:00
11 changed files with 93 additions and 69 deletions
@@ -62,7 +62,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.CustomTool.Tests
[TestMethod] [TestMethod]
public void ResourceLoaderInitializedWithClassName() public void ResourceLoaderInitializedWithClassName()
{ {
Assert.IsTrue(actual.Contains("new ResourceLoader(currentAssemblyName + \"/Resources\");")); Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\");"));
} }
} }
} }
@@ -63,7 +63,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.CustomTool.Tests
[TestMethod] [TestMethod]
public void ResourceLoaderInitializedWithClassName() public void ResourceLoaderInitializedWithClassName()
{ {
Assert.IsTrue(actual.Contains("new ResourceLoader(currentAssemblyName + \"/Resources\");")); Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\");"));
} }
} }
} }
@@ -64,7 +64,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.CustomTool.Tests
[TestMethod] [TestMethod]
public void ResourceLoaderInitializedWithClassName() public void ResourceLoaderInitializedWithClassName()
{ {
Assert.IsTrue(actual.Contains("New ResourceLoader(currentAssemblyName + \"/Resources\")")); Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\")"));
} }
} }
} }
@@ -57,7 +57,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.CustomTool.Tests
[TestMethod] [TestMethod]
public void ResourceLoaderInitializedWithClassName() public void ResourceLoaderInitializedWithClassName()
{ {
Assert.IsTrue(actual.Contains("New ResourceLoader(currentAssemblyName + \"/Resources\")")); Assert.IsTrue(actual.Contains("ResourceLoader.GetForCurrentView(currentAssemblyName + \"/Resources\")"));
} }
} }
} }
@@ -10,18 +10,25 @@ using Microsoft.CSharp;
namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
{ {
public class CodeDomCodeGenerator : CodeGenerator, IDisposable public class CodeDomCodeGenerator : CodeGenerator, IDisposable
{ {
private readonly string className;
private readonly TypeAttributes? classAccessibility; private readonly TypeAttributes? classAccessibility;
private readonly VisualStudioVersion visualStudioVersion;
private readonly string className;
private readonly CodeNamespace codeNamespace; private readonly CodeNamespace codeNamespace;
private readonly CodeCompileUnit compileUnit; private readonly CodeCompileUnit compileUnit;
private readonly CodeDomProvider provider; private readonly CodeDomProvider provider;
public CodeDomCodeGenerator(IResourceParser resourceParser, string className, string defaultNamespace, CodeDomProvider codeDomProvider = null, TypeAttributes? classAccessibility = null) public CodeDomCodeGenerator(IResourceParser resourceParser,
string className,
string defaultNamespace,
CodeDomProvider codeDomProvider = null,
TypeAttributes? classAccessibility = null,
VisualStudioVersion visualStudioVersion = VisualStudioVersion.VS2012)
: base(resourceParser, defaultNamespace) : base(resourceParser, defaultNamespace)
{ {
this.className = className; this.className = className;
this.classAccessibility = classAccessibility; this.classAccessibility = classAccessibility;
this.visualStudioVersion = visualStudioVersion;
compileUnit = new CodeCompileUnit(); compileUnit = new CodeCompileUnit();
provider = codeDomProvider ?? new CSharpCodeProvider(); provider = codeDomProvider ?? new CSharpCodeProvider();
codeNamespace = new CodeNamespace(defaultNamespace); codeNamespace = new CodeNamespace(defaultNamespace);
@@ -29,7 +36,8 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
public override string GenerateCode() public override string GenerateCode()
{ {
codeNamespace.Comments.Add(new CodeCommentStatement("--------------------------------------------------------------------------------------------------")); codeNamespace.Comments.Add(
new CodeCommentStatement("--------------------------------------------------------------------------------------------------"));
codeNamespace.Comments.Add(new CodeCommentStatement("<auto-generatedInfo>")); codeNamespace.Comments.Add(new CodeCommentStatement("<auto-generatedInfo>"));
codeNamespace.Comments.Add(new CodeCommentStatement("\tThis code was generated by ResW File Code Generator (http://reswcodegen.codeplex.com)")); 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("\tResW File Code Generator was written by Christian Resma Helle"));
@@ -40,66 +48,75 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
codeNamespace.Comments.Add(new CodeCommentStatement(string.Empty)); codeNamespace.Comments.Add(new CodeCommentStatement(string.Empty));
codeNamespace.Comments.Add(new CodeCommentStatement("\tGenerated: " + DateTime.Now.ToString(CultureInfo.InvariantCulture))); codeNamespace.Comments.Add(new CodeCommentStatement("\tGenerated: " + DateTime.Now.ToString(CultureInfo.InvariantCulture)));
codeNamespace.Comments.Add(new CodeCommentStatement("</auto-generatedInfo>")); codeNamespace.Comments.Add(new CodeCommentStatement("</auto-generatedInfo>"));
codeNamespace.Comments.Add(new CodeCommentStatement("--------------------------------------------------------------------------------------------------")); codeNamespace.Comments.Add(
new CodeCommentStatement("--------------------------------------------------------------------------------------------------"));
codeNamespace.Imports.Add(new CodeNamespaceImport("Windows.ApplicationModel.Resources")); codeNamespace.Imports.Add(new CodeNamespaceImport("Windows.ApplicationModel.Resources"));
var targetClass = new CodeTypeDeclaration(className) var targetClass = new CodeTypeDeclaration(className)
{ {
IsClass = true, IsClass = true,
IsPartial = true, IsPartial = true,
TypeAttributes = classAccessibility.HasValue ? classAccessibility.Value : TypeAttributes.Public TypeAttributes = classAccessibility.HasValue ? classAccessibility.Value : TypeAttributes.Public
}; };
const string resourceLoaderType = "ResourceLoader"; const string resourceLoaderType = "ResourceLoader";
var resourceLoaderField = new CodeMemberField(resourceLoaderType, "resourceLoader") var resourceLoaderField = new CodeMemberField(resourceLoaderType, "resourceLoader")
{ {
Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final
}; };
targetClass.Members.Add(resourceLoaderField); targetClass.Members.Add(resourceLoaderField);
var constructor = new CodeTypeConstructor(); var constructor = new CodeTypeConstructor();
var executingAssemblyVar = new CodeVariableDeclarationStatement(typeof(string), "executingAssemblyName"); var executingAssemblyVar = new CodeVariableDeclarationStatement(typeof (string), "executingAssemblyName");
var executingAssemblyInit = new CodeAssignStatement( var executingAssemblyInit = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblyName"),
new CodeVariableReferenceExpression("executingAssemblyName"), new CodeSnippetExpression("Windows.UI.Xaml.Application.Current.GetType().AssemblyQualifiedName"));
new CodeSnippetExpression("Windows.UI.Xaml.Application.Current.GetType().AssemblyQualifiedName")); var executingAssemblySplit = new CodeVariableDeclarationStatement(typeof (string[]), "executingAssemblySplit");
var executingAssemblySplit = new CodeVariableDeclarationStatement(typeof(string[]), "executingAssemblySplit"); var executingAssemblyInit2 = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblySplit"),
var executingAssemblyInit2 = new CodeAssignStatement( new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("executingAssemblyName"),
new CodeVariableReferenceExpression("executingAssemblySplit"), "Split",
new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("executingAssemblyName"), "Split", new CodePrimitiveExpression(','))); new CodePrimitiveExpression(',')));
var executingAssemblyInit3 = new CodeAssignStatement( var executingAssemblyInit3 = new CodeAssignStatement(new CodeVariableReferenceExpression("executingAssemblyName"),
new CodeVariableReferenceExpression("executingAssemblyName"), new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("executingAssemblySplit"),
new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("executingAssemblySplit"), new CodePrimitiveExpression(1))); new CodePrimitiveExpression(1)));
var currentAssemblyVar = new CodeVariableDeclarationStatement(typeof(string), "currentAssemblyName"); var currentAssemblyVar = new CodeVariableDeclarationStatement(typeof (string), "currentAssemblyName");
var currentAssemblyInit = new CodeAssignStatement( var currentAssemblyInit = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblyName"),
new CodeVariableReferenceExpression("currentAssemblyName"), new CodePropertyReferenceExpression(new CodeTypeOfExpression(className), "AssemblyQualifiedName"));
new CodePropertyReferenceExpression(new CodeTypeOfExpression(className), "AssemblyQualifiedName")); var currentAssemblySplit = new CodeVariableDeclarationStatement(typeof (string[]), "currentAssemblySplit");
var currentAssemblySplit = new CodeVariableDeclarationStatement(typeof(string[]), "currentAssemblySplit"); var currentAssemblyInit2 = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblySplit"),
var currentAssemblyInit2 = new CodeAssignStatement( new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("currentAssemblyName"),
new CodeVariableReferenceExpression("currentAssemblySplit"), "Split",
new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("currentAssemblyName"), "Split", new CodePrimitiveExpression(','))); new CodePrimitiveExpression(',')));
var currentAssemblyInit3 = new CodeAssignStatement( var currentAssemblyInit3 = new CodeAssignStatement(new CodeVariableReferenceExpression("currentAssemblyName"),
new CodeVariableReferenceExpression("currentAssemblyName"), new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("currentAssemblySplit"),
new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("currentAssemblySplit"), new CodePrimitiveExpression(1))); new CodePrimitiveExpression(1)));
var createResourceLoader = new CodeConditionStatement( var createResourceLoader = new CodeConditionStatement(
new CodeSnippetExpression("executingAssemblyName.Equals(currentAssemblyName)"), new CodeSnippetExpression("executingAssemblyName.Equals(currentAssemblyName)"),
new CodeStatement[] // true new CodeStatement[] // true
{ {
new CodeAssignStatement( visualStudioVersion == VisualStudioVersion.VS2013
new CodeFieldReferenceExpression(null, "resourceLoader"), ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
new CodeSnippetExpression("\"" + className + "\""))) "GetForCurrentView",
}, new CodeSnippetExpression("\"" + className + "\"")))
: new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"),
new CodeSnippetExpression("\"" + className + "\"")))
},
new CodeStatement[] // false new CodeStatement[] // false
{ {
new CodeAssignStatement( visualStudioVersion == VisualStudioVersion.VS2013
new CodeFieldReferenceExpression(null, "resourceLoader"), ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\""))) "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(executingAssemblyVar);
constructor.Statements.Add(executingAssemblyInit); constructor.Statements.Add(executingAssemblyInit);
@@ -122,12 +139,12 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
continue; continue;
var property = new CodeMemberProperty var property = new CodeMemberProperty
{ {
Name = item.Name.Trim(), Name = item.Name.Trim(),
Attributes = MemberAttributes.Public | MemberAttributes.Static, Attributes = MemberAttributes.Public | MemberAttributes.Static,
HasGet = true, HasGet = true,
Type = new CodeTypeReference(typeof(string)) Type = new CodeTypeReference(typeof (string))
}; };
property.Comments.Add(new CodeCommentStatement("<summary>", true)); property.Comments.Add(new CodeCommentStatement("<summary>", true));
property.Comments.Add(new CodeCommentStatement("Localized resource similar to \"" + (item.Value ?? item.Name) + "\"", true)); property.Comments.Add(new CodeCommentStatement("Localized resource similar to \"" + (item.Value ?? item.Name) + "\"", true));
@@ -135,7 +152,8 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
property.GetStatements.Add( property.GetStatements.Add(
new CodeMethodReturnStatement( new CodeMethodReturnStatement(
new CodeMethodInvokeExpression( new CodeMethodInvokeExpression(
new CodeFieldReferenceExpression(null, "resourceLoader"), "GetString", new CodeFieldReferenceExpression(null, "resourceLoader"),
"GetString",
new CodePrimitiveExpression(item.Name)))); new CodePrimitiveExpression(item.Name))));
targetClass.Members.Add(property); targetClass.Members.Add(property);
@@ -149,7 +167,7 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
private string GenerateCodeFromCompileUnit() private string GenerateCodeFromCompileUnit()
{ {
var options = new CodeGeneratorOptions { BracingStyle = "C" }; var options = new CodeGeneratorOptions {BracingStyle = "C"};
var code = new StringBuilder(); var code = new StringBuilder();
@@ -163,16 +181,16 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
private bool disposed; private bool disposed;
~CodeDomCodeGenerator()
{
Dispose(false);
}
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
} }
~CodeDomCodeGenerator()
{
Dispose(false);
}
protected virtual void Dispose(bool dispose) protected virtual void Dispose(bool dispose)
{ {
if (disposed) if (disposed)
@@ -0,0 +1,8 @@
namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool
{
public enum VisualStudioVersion
{
VS2012,
VS2013
}
}
@@ -29,7 +29,7 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.1.0.*")] [assembly: AssemblyVersion("1.2.0.*")]
[assembly: InternalsVisibleTo("VSPackage_IntegrationTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010045bd05a5b24c74b83f069df52e138682c2fb8b1ee65cb9d351fd7e8d622308aeb588b2a2975d75da4fe8a392510528baa26a3317809ec064d3cc852f0df94752ad2228d9f2a048ee2858c1e9d505b05f7fb4ede02f34154f75ea50445741c84f5ab1c814358f5fd6a6f08bb3d94284cff0524d5ca9d888c8648bcbc7a1e0a3c8")] [assembly: InternalsVisibleTo("VSPackage_IntegrationTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010045bd05a5b24c74b83f069df52e138682c2fb8b1ee65cb9d351fd7e8d622308aeb588b2a2975d75da4fe8a392510528baa26a3317809ec064d3cc852f0df94752ad2228d9f2a048ee2858c1e9d505b05f7fb4ede02f34154f75ea50445741c84f5ab1c814358f5fd6a6f08bb3d94284cff0524d5ca9d888c8648bcbc7a1e0a3c8")]
[assembly: InternalsVisibleTo("VSPackage_UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010045bd05a5b24c74b83f069df52e138682c2fb8b1ee65cb9d351fd7e8d622308aeb588b2a2975d75da4fe8a392510528baa26a3317809ec064d3cc852f0df94752ad2228d9f2a048ee2858c1e9d505b05f7fb4ede02f34154f75ea50445741c84f5ab1c814358f5fd6a6f08bb3d94284cff0524d5ca9d888c8648bcbc7a1e0a3c8")] [assembly: InternalsVisibleTo("VSPackage_UnitTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010045bd05a5b24c74b83f069df52e138682c2fb8b1ee65cb9d351fd7e8d622308aeb588b2a2975d75da4fe8a392510528baa26a3317809ec064d3cc852f0df94752ad2228d9f2a048ee2858c1e9d505b05f7fb4ede02f34154f75ea50445741c84f5ab1c814358f5fd6a6f08bb3d94284cff0524d5ca9d888c8648bcbc7a1e0a3c8")]
@@ -119,6 +119,7 @@
</COMReference> </COMReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CustomTool\VisualStudioVersion.cs" />
<Compile Include="VisualStudio2013Package.cs" /> <Compile Include="VisualStudio2013Package.cs" />
<Compile Include="CustomTool\ClassNameExtractor.cs" /> <Compile Include="CustomTool\ClassNameExtractor.cs" />
<Compile Include="CustomTool\CodeDomCodeGenerator.cs" /> <Compile Include="CustomTool\CodeDomCodeGenerator.cs" />
@@ -63,7 +63,6 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage
{ {
Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize(); base.Initialize();
} }
#endregion #endregion
@@ -63,7 +63,6 @@ namespace ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage
{ {
Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize(); base.Initialize();
} }
#endregion #endregion
@@ -13,8 +13,7 @@
<License>License.txt</License> <License>License.txt</License>
</Metadata> </Metadata>
<Installation InstalledByMsi="false"> <Installation InstalledByMsi="false">
<InstallationTarget Version="[11.0,12.0]" <InstallationTarget Version="[11.0,12.0]" Id="Microsoft.VisualStudio.IntegratedShell" />
Id="Microsoft.VisualStudio.Premium" />
</Installation> </Installation>
<Dependencies> <Dependencies>
<Dependency Id="Microsoft.Framework.NDP" <Dependency Id="Microsoft.Framework.NDP"