Generate equivalent of resx .Designer.cs in build
This commit is contained in:
parent
30a0bc3028
commit
a08ebb95a0
3 changed files with 104 additions and 0 deletions
45
build/GenerateResxSource.targets
Normal file
45
build/GenerateResxSource.targets
Normal 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>
|
58
build_projects/dotnet-cli-build/GenerateResxSource.cs
Normal file
58
build_projects/dotnet-cli-build/GenerateResxSource.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,4 +41,5 @@
|
|||
|
||||
<Import Project="build/AzureInfo.props" />
|
||||
<Import Project="build/InstallerInfo.props" />
|
||||
<Import Project="build/GenerateResxSource.targets" />
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue