Use named parameters for format strings
This commit is contained in:
parent
95d5324bcf
commit
31ba4e8430
|
|
@ -15,12 +15,10 @@
|
||||||
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
|
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
|
||||||
<#@ import namespace="EnvDTE" #>
|
<#@ import namespace="EnvDTE" #>
|
||||||
<#@ import namespace="EnvDTE80" #>
|
<#@ import namespace="EnvDTE80" #>
|
||||||
|
|
||||||
<#
|
<#
|
||||||
var hostServiceProvider = (IServiceProvider)Host;
|
var hostServiceProvider = (IServiceProvider)Host;
|
||||||
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
|
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
|
||||||
var templateProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
|
var templateProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
|
||||||
|
|
||||||
var projectDirectory = Path.GetDirectoryName(templateProjectItem.ContainingProject.FullName);
|
var projectDirectory = Path.GetDirectoryName(templateProjectItem.ContainingProject.FullName);
|
||||||
var ttDirectory = Path.Combine(projectDirectory, "Properties");
|
var ttDirectory = Path.Combine(projectDirectory, "Properties");
|
||||||
var projectName = Path.GetFileName(projectDirectory.TrimEnd('/'));
|
var projectName = Path.GetFileName(projectDirectory.TrimEnd('/'));
|
||||||
|
|
@ -28,7 +26,7 @@
|
||||||
foreach (var resxFile in Directory.EnumerateFiles(projectDirectory, "*.resx", SearchOption.AllDirectories))
|
foreach (var resxFile in Directory.EnumerateFiles(projectDirectory, "*.resx", SearchOption.AllDirectories))
|
||||||
{
|
{
|
||||||
var fileName = Path.GetFileNameWithoutExtension(resxFile);
|
var fileName = Path.GetFileNameWithoutExtension(resxFile);
|
||||||
var parameterMatcher = new Regex(@"\{(\d)\}");
|
var parameterMatcher = new Regex(@"\{([a-z]\w+)\}");
|
||||||
var resourceStrings = new List<ResourceData>();
|
var resourceStrings = new List<ResourceData>();
|
||||||
|
|
||||||
using (var resxReader = new ResXResourceReader(resxFile))
|
using (var resxReader = new ResXResourceReader(resxFile))
|
||||||
|
|
@ -39,23 +37,27 @@
|
||||||
{
|
{
|
||||||
var node = (ResXDataNode)entry.Value;
|
var node = (ResXDataNode)entry.Value;
|
||||||
var value = (string)node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
|
var value = (string)node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
|
||||||
|
|
||||||
var matchedArgs
|
var arguments
|
||||||
= parameterMatcher.Matches(value)
|
= parameterMatcher
|
||||||
|
.Matches(value)
|
||||||
.Cast<Match>()
|
.Cast<Match>()
|
||||||
.Select(m => Convert.ToInt32(m.Groups[1].Value))
|
.Select(m => m.Groups[1].Value)
|
||||||
.ToArray();
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
|
||||||
resourceStrings.Add(new ResourceData {
|
resourceStrings.Add(
|
||||||
Name = node.Name,
|
new ResourceData
|
||||||
Value = value,
|
{
|
||||||
ArgsCount = matchedArgs.Any() ? matchedArgs.Max() + 1 : 0
|
Name = node.Name,
|
||||||
});
|
Value = value,
|
||||||
|
Arguments = arguments
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GenerationEnvironment.AppendFormat(
|
GenerationEnvironment.AppendFormat(
|
||||||
@"// <auto-generated />
|
@"// <auto-generated />
|
||||||
|
|
||||||
namespace {0}
|
namespace {0}
|
||||||
{{
|
{{
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
|
@ -67,75 +69,88 @@ namespace {0}
|
||||||
private static readonly ResourceManager _resourceManager
|
private static readonly ResourceManager _resourceManager
|
||||||
= new ResourceManager(""{0}.{1}"", typeof({1}).GetTypeInfo().Assembly);
|
= new ResourceManager(""{0}.{1}"", typeof({1}).GetTypeInfo().Assembly);
|
||||||
", projectName, fileName);
|
", projectName, fileName);
|
||||||
foreach (var resourceString in resourceStrings)
|
|
||||||
{
|
foreach (var resourceString in resourceStrings)
|
||||||
GenerationEnvironment.AppendLine();
|
{
|
||||||
GenerationEnvironment.AppendFormat(@" /// <summary>").AppendLine();
|
GenerationEnvironment.AppendLine();
|
||||||
|
GenerationEnvironment.AppendFormat(@" /// <summary>").AppendLine();
|
||||||
foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
|
foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
|
||||||
{
|
{
|
||||||
GenerationEnvironment.AppendFormat(" /// {0}", line.Replace("<", "<").Replace(">", ">"))
|
GenerationEnvironment.AppendFormat(" /// {0}", line.Replace("<", "<").Replace(">", ">"))
|
||||||
.AppendLine();
|
.AppendLine();
|
||||||
}
|
}
|
||||||
GenerationEnvironment.AppendFormat(
|
|
||||||
|
GenerationEnvironment.AppendFormat(
|
||||||
@" /// </summary>
|
@" /// </summary>
|
||||||
internal static string {0}{1}
|
internal static string {0}{1}
|
||||||
{{
|
{{
|
||||||
", resourceString.Name, resourceString.ArgsCount > 0 ? resourceString.ParamsArray : string.Empty);
|
", resourceString.Name, resourceString.Arguments.Count > 0 ? resourceString.Parameters : string.Empty);
|
||||||
if (resourceString.ArgsCount == 0)
|
|
||||||
{
|
if (resourceString.Arguments.Count == 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);
|
GenerationEnvironment.AppendFormat(
|
||||||
|
@" get {{ return GetString(""{0}""); }}", resourceString.Name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GenerationEnvironment.AppendFormat(
|
||||||
|
@" return string.Format(CultureInfo.CurrentCulture, GetString(""{0}"", {2}), {1});",
|
||||||
|
resourceString.Name, resourceString.FormatArguments, resourceString.ArgumentNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
GenerationEnvironment.AppendLine().Append(
|
||||||
|
@" }").AppendLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
GenerationEnvironment.Append(@"
|
||||||
|
private static string GetString(string name, params string[] argumentNames)
|
||||||
|
{
|
||||||
|
var value = _resourceManager.GetString(name);
|
||||||
|
|
||||||
System.Diagnostics.Debug.Assert(value != null);
|
System.Diagnostics.Debug.Assert(value != null);
|
||||||
|
|
||||||
|
for (var i = 0; i < argumentNames.Length; i++)
|
||||||
|
{
|
||||||
|
value = value.Replace(""{"" + argumentNames[i] + ""}"", ""{"" + i + ""}"");
|
||||||
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
");
|
");
|
||||||
#>
|
|
||||||
<#
|
var outputPath = Path.Combine(ttDirectory, fileName + ".Designer.cs");
|
||||||
string outputPath = Path.Combine(ttDirectory, fileName + ".Designer.cs");
|
|
||||||
bool fileExists = File.Exists(outputPath);
|
File.WriteAllText(outputPath, GenerationEnvironment.ToString());
|
||||||
File.WriteAllText(outputPath, GenerationEnvironment.ToString());
|
GenerationEnvironment.Length = 0;
|
||||||
GenerationEnvironment.Length = 0;
|
|
||||||
if (!fileExists) {
|
var resxProjectItem = dte.Solution.FindProjectItem(resxFile);
|
||||||
|
resxProjectItem.ProjectItems.AddFromFile(outputPath);
|
||||||
templateProjectItem.ProjectItems.AddFromFile(outputPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#>
|
#>
|
||||||
<#+
|
<#+
|
||||||
private class ResourceData
|
|
||||||
{
|
private class ResourceData
|
||||||
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Value { get; set; }
|
public string Value { get; set; }
|
||||||
public int ArgsCount { get; set; }
|
public List<string> Arguments { get; set; }
|
||||||
|
|
||||||
public string ArgsArray
|
public string FormatArguments
|
||||||
{
|
{
|
||||||
get { return GenerateArgs("p"); }
|
get { return string.Join(", ", Arguments); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ParamsArray
|
public string ArgumentNames
|
||||||
{
|
{
|
||||||
get { return "(" + GenerateArgs("object p") + ")"; }
|
get { return string.Join(", ", Arguments.Select(a => "\"" + a + "\"")); }
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GenerateArgs(string template)
|
public string Parameters
|
||||||
{
|
{
|
||||||
return string.Join(", ", Enumerable.Range(0, ArgsCount).Select(i => template + i));
|
get { return "(" + string.Join(", ", Arguments.Select(a => "object " + a)) + ")"; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#>
|
#>
|
||||||
|
|
@ -207,9 +207,7 @@ functions
|
||||||
// Templates
|
// Templates
|
||||||
const string csTemplate = @"<Compile Include=""{0}"" />";
|
const string csTemplate = @"<Compile Include=""{0}"" />";
|
||||||
const string resxDesignerTemplate = @"<Compile Include=""{0}"">
|
const string resxDesignerTemplate = @"<Compile Include=""{0}"">
|
||||||
<DependentUpon>Resources.tt</DependentUpon>
|
<DependentUpon>{1}.resx</DependentUpon>
|
||||||
<AutoGen>True</AutoGen>
|
|
||||||
<DesignTime>True</DesignTime>
|
|
||||||
</Compile>";
|
</Compile>";
|
||||||
const string resxTemplate = @"<EmbeddedResource Include=""{0}"">
|
const string resxTemplate = @"<EmbeddedResource Include=""{0}"">
|
||||||
<LogicalName>{1}.{2}.resources</LogicalName>
|
<LogicalName>{1}.{2}.resources</LogicalName>
|
||||||
|
|
@ -220,15 +218,17 @@ functions
|
||||||
var resxDesignerFiles = new HashSet<string>(resxFileNames.Select(f => Path.ChangeExtension(f, "Designer.cs")),
|
var resxDesignerFiles = new HashSet<string>(resxFileNames.Select(f => Path.ChangeExtension(f, "Designer.cs")),
|
||||||
StringComparer.OrdinalIgnoreCase);
|
StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var resxFiles = String.Join(Environment.NewLine,
|
var resxFiles
|
||||||
resxFileNames.Select(p => String.Format(resxTemplate, p, projectName, Path.GetFileNameWithoutExtension(p))));
|
= String.Join(Environment.NewLine,
|
||||||
|
resxFileNames.Select(p => String.Format(resxTemplate, p, projectName, Path.GetFileNameWithoutExtension(p))));
|
||||||
|
|
||||||
// Build the list of cs files
|
// Build the list of cs files
|
||||||
var csFiles
|
var csFiles
|
||||||
= String.Join(Environment.NewLine,
|
= String.Join(Environment.NewLine,
|
||||||
FindFilesOfType(projectDir, "*.cs")
|
FindFilesOfType(projectDir, "*.cs")
|
||||||
.Select(p => resxDesignerFiles.Contains(p) ? String.Format(resxDesignerTemplate, p) :
|
.Select(p => resxDesignerFiles.Contains(p)
|
||||||
String.Format(csTemplate, p)));
|
? String.Format(resxDesignerTemplate, p, Path.GetFileNameWithoutExtension(p).Replace(".Designer", ""))
|
||||||
|
: String.Format(csTemplate, p)));
|
||||||
|
|
||||||
bool isSample = Path.GetDirectoryName(projectDir)
|
bool isSample = Path.GetDirectoryName(projectDir)
|
||||||
.TrimEnd(Path.DirectorySeparatorChar)
|
.TrimEnd(Path.DirectorySeparatorChar)
|
||||||
|
|
@ -253,7 +253,6 @@ functions
|
||||||
string ttFileTemplate = @"<None Include=""..\..\packages\KoreBuild\Build\Resources.tt"">
|
string ttFileTemplate = @"<None Include=""..\..\packages\KoreBuild\Build\Resources.tt"">
|
||||||
<Link>Properties\Resources.tt</Link>
|
<Link>Properties\Resources.tt</Link>
|
||||||
<Generator>TextTemplatingFileGenerator</Generator>
|
<Generator>TextTemplatingFileGenerator</Generator>
|
||||||
<LastGenOutput>_Resources.cs</LastGenOutput>
|
|
||||||
<CustomToolNamespace>{0}</CustomToolNamespace>
|
<CustomToolNamespace>{0}</CustomToolNamespace>
|
||||||
</None>";
|
</None>";
|
||||||
// Link the tt file from packages dir
|
// Link the tt file from packages dir
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue