<#@ 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(); 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() .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 }); } } GenerationEnvironment.AppendFormat( @"// namespace {0} {{ using System.Globalization; using System.Reflection; using System.Resources; internal static class {1} {{ private static readonly ResourceManager _resourceManager = new ResourceManager(""{0}.{1}"", typeof({1}).GetTypeInfo().Assembly); ", projectName, fileName); foreach (var resourceString in resourceStrings) { GenerationEnvironment.AppendLine(); GenerationEnvironment.AppendFormat(@" /// ").AppendLine(); foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) { GenerationEnvironment.AppendFormat(" /// {0}", line.Replace("<", "<").Replace(">", ">")) .AppendLine(); } GenerationEnvironment.AppendFormat( @" /// internal static string {0}{1} {{ ", resourceString.Name, resourceString.ArgsCount > 0 ? resourceString.ParamsArray : string.Empty); if (resourceString.ArgsCount == 0) { GenerationEnvironment.AppendFormat( @" get {{ return GetString(""{0}""); }}", resourceString.Name); } else { GenerationEnvironment.AppendFormat( @" return string.Format(CultureInfo.CurrentCulture, GetString(""{0}""), {1});", resourceString.Name, resourceString.ArgsArray); } GenerationEnvironment.AppendLine().Append( @" }").AppendLine(); } GenerationEnvironment.Append(@" 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)); } } #>