Changing resx generation to use tt file

This commit is contained in:
Pranav K 2014-02-12 05:56:28 -08:00
parent ffe5f070e0
commit 076ab987b1
2 changed files with 150 additions and 6 deletions

138
build/Resources.tt Normal file
View File

@ -0,0 +1,138 @@
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Resources" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#
var hostServiceProvider = (IServiceProvider)Host;
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
var templateProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
var projectDirectory = Path.GetDirectoryName(templateProjectItem.ContainingProject.FullName);
var ttDirectory = Path.Combine(projectDirectory, "Properties");
var projectName = Path.GetFileName(projectDirectory.TrimEnd('/'));
foreach (var resxFile in Directory.EnumerateFiles(projectDirectory, "*.resx", SearchOption.AllDirectories))
{
var fileName = Path.GetFileNameWithoutExtension(resxFile);
var parameterMatcher = new Regex(@"\{(\d)\}");
var resourceStrings = new List<ResourceData>();
using (var resxReader = new ResXResourceReader(resxFile))
{
resxReader.UseResXDataNodes = true;
foreach (DictionaryEntry entry in resxReader)
{
var node = (ResXDataNode)entry.Value;
var value = (string)node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
var matchedArgs
= parameterMatcher.Matches(value)
.Cast<Match>()
.Select(m => Convert.ToInt32(m.Groups[1].Value))
.ToArray();
resourceStrings.Add(new ResourceData {
Name = node.Name,
Value = value,
ArgsCount = matchedArgs.Any() ? matchedArgs.Max() + 1 : 0
});
}
}
string outputNamespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint") ?? projectName;
#>
// <auto-generated />
namespace <#= outputNamespace #>
{
using System.Globalization;
using System.Reflection;
using System.Resources;
internal static class <#= fileName #>
{
private static readonly ResourceManager _resourceManager
= new ResourceManager("<#= projectName + "." + fileName #>", typeof(<#= fileName #>).GetTypeInfo().Assembly);
<#
foreach (var resourceString in resourceStrings)
{
#>
/// <summary>
<# foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) { #>
/// <#= line.Replace("<", "&lt;").Replace(">", "&gt;") #>
<# } #>
/// </summary>
internal static string <#= resourceString.Name #><#= resourceString.ArgsCount > 0 ? resourceString.ParamsArray : string.Empty #>
{
<#
if (resourceString.ArgsCount == 0)
{
#>get { return GetString("<#= resourceString.Name #>"); }
<#
}
else
{
#>return string.Format(CultureInfo.CurrentCulture, GetString("<#= resourceString.Name #>"), <#= resourceString.ArgsArray #>);
<#
}#>
}
<#
}#>
private static string GetString(string name)
{
string value = _resourceManager.GetString(name);
System.Diagnostics.Debug.Assert(value != null);
return value;
}
}
}
<#
string outputPath = Path.Combine(ttDirectory, fileName + ".Designer.cs");
bool fileExists = File.Exists(outputPath);
File.WriteAllText(outputPath, GenerationEnvironment.ToString());
GenerationEnvironment.Length = 0;
if (!fileExists) {
templateProjectItem.ProjectItems.AddFromFile(outputPath);
}
}
#>
<#+
private class ResourceData
{
public string Name { get; set; }
public string Value { get; set; }
public int ArgsCount { get; set; }
public string ArgsArray
{
get { return GenerateArgs("p"); }
}
public string ParamsArray
{
get { return "(" + GenerateArgs("object p") + ")"; }
}
private string GenerateArgs(string template)
{
return string.Join(", ", Enumerable.Range(0, ArgsCount).Select(i => template + i));
}
}
#>

View File

@ -207,16 +207,13 @@ functions
// Templates
const string csTemplate = @"<Compile Include=""{0}"" />";
const string resxDesignerTemplate = @"<Compile Include=""{0}"">
<DependentUpon>{1}</DependentUpon>
<DependentUpon>Resources.tt</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>";
const string resxTemplate = @"<EmbeddedResource Include=""{0}"">
<LogicalName>{1}.{2}.resources</LogicalName>
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>{2}.Designer.cs</LastGenOutput>
<CustomToolNamespace>{1}.Resources</CustomToolNamespace>
</EmbeddedResource>";
</EmbeddedResource>";
// Build the list of resx files
var resxFileNames = FindFilesOfType(projectDir, "*.resx");
@ -230,7 +227,7 @@ functions
var csFiles
= String.Join(Environment.NewLine,
FindFilesOfType(projectDir, "*.cs")
.Select(p => resxDesignerFiles.Contains(p) ? String.Format(resxDesignerTemplate, p, Path.ChangeExtension(Path.GetFileNameWithoutExtension(p), "resx")) :
.Select(p => resxDesignerFiles.Contains(p) ? String.Format(resxDesignerTemplate, p) :
String.Format(csTemplate, p)));
bool isSample = Path.GetDirectoryName(projectDir)
@ -252,6 +249,15 @@ functions
// HACK: Assume the packages folder is 2 up from the projectDir
string packagesDir = Path.GetFullPath(Path.Combine(projectDir, "..", "..", "packages"));
string ttFileTemplate = @"<None Include=""..\..\packages\KoreBuild\Build\Resources.tt"">
<Link>Properties\Resources.tt</Link>
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>_Resources.cs</LastGenOutput>
<CustomToolNamespace>{0}</CustomToolNamespace>
</None>";
// Link the tt file from packages dir
resxFiles += Environment.NewLine + String.Format(ttFileTemplate, projectName);
foreach (var pair in configs)
{