Generate equivalent of resx .Designer.cs in build

This commit is contained in:
Nick Guerrera 2017-06-06 21:47:58 -07:00
parent 30a0bc3028
commit a08ebb95a0
3 changed files with 104 additions and 0 deletions

View file

@ -0,0 +1,45 @@
<Project>
<UsingTask TaskName="GenerateResxSource" AssemblyFile="$(CLIBuildDll)" />
<Target Name="GenerateResxSource"
BeforeTargets="CoreCompile"
DependsOnTargets="PrepareResourceNames;
GetEmbeddedResourcesWithSourceGeneration;
BatchGenerateResxSource">
<ItemGroup>
<GeneratedResxSource Include="@(EmbeddedResourceSG->'%(SourceOutputPath)')" />
<FileWrites Include="@(GeneratedResxSource)" />
<Compile Include="@(GeneratedResxSource)" />
</ItemGroup>
</Target>
<Target Name="CustomizeResourceNames"
BeforeTargets="PrepareResourceNames"
>
<ItemGroup>
<EmbeddedResource>
<ManifestResourceName Condition="'%(EmbeddedResource.Namespace)' != ''">%(EmbeddedResource.Namespace).%(EmbeddedResource.Filename)</ManifestResourceName>
</EmbeddedResource>
</ItemGroup>
</Target>
<Target Name="BatchGenerateResxSource"
Inputs="@(EmbeddedResourceSG)"
Outputs="%(EmbeddedResourceSG.SourceOutputPath)"
>
<GenerateResxSource ResxFile="%(EmbeddedResourceSG.FullPath)"
ResourceName="%(EmbeddedResourceSG.ManifestResourceName)"
SourceOutputPath="%(EmbeddedResourceSG.SourceOutputPath)" />
</Target>
<Target Name="GetEmbeddedResourcesWithSourceGeneration">
<ItemGroup>
<EmbeddedResourceSG Include="@(EmbeddedResource)" Condition="'%(EmbeddedResource.GenerateSource)' == 'true'" />
<EmbeddedResourceSG>
<SourceOutputPath>$(IntermediateOutputPath)%(EmbeddedResourceSG.ManifestResourceName).cs</SourceOutputPath>
</EmbeddedResourceSG>
</ItemGroup>
</Target>
</Project>

View file

@ -0,0 +1,58 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Microsoft.DotNet.Cli.Build
{
public class GenerateResxSource : Task
{
[Required]
public string ResxFile { get; set; }
[Required]
public string ResourceName { get; set; }
[Required]
public string SourceOutputPath { get; set; }
public override bool Execute()
{
var source = new StringBuilder();
void _(string line) { source.AppendLine(line); }
string @namespace = Path.GetFileNameWithoutExtension(ResourceName);
string @class = Path.GetExtension(ResourceName).TrimStart('.');
_($"using System;");
_($"using System.Globalization;");
_($"using System.Reflection;");
_($"using System.Resources;");
_($"");
_($"namespace {@namespace}");
_($"{{");
_($" internal static class {@class}");
_($" {{");
_($" internal static CultureInfo Culture {{ get; set; }}");
_($" internal static ResourceManager ResourceManager {{ get; }} = new ResourceManager(\"{ResourceName}\", typeof({@class}).GetTypeInfo().Assembly);");
foreach (var key in XDocument.Load(ResxFile)
.Descendants("data")
.Select(n => n.Attribute("name").Value))
{
_($" internal static string {key} => ResourceManager.GetString(\"{key}\", Culture);");
}
_($" }}");
_($"}}");
File.WriteAllText(SourceOutputPath, source.ToString());
return true;
}
}
}

View file

@ -41,4 +41,5 @@
<Import Project="build/AzureInfo.props" />
<Import Project="build/InstallerInfo.props" />
<Import Project="build/GenerateResxSource.targets" />
</Project>