Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2086 from ericstj/resourceEnum
Browse files Browse the repository at this point in the history
Add support for generating resource IDs as an enum
  • Loading branch information
ericstj committed Jul 4, 2018
2 parents 94f18eb + 3bf98ea commit 70f7153
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
47 changes: 40 additions & 7 deletions src/Microsoft.DotNet.Build.Tasks/GenerateResourcesCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,18 @@ public class GenerateResourcesCode : BuildTask
/// </summary>
public bool AsConstants { get; set; }

/// <summary>
/// Emit enum of Resource IDs instead of a class with properties that retrieve values.
/// </summary>
public bool AsEnum { get; set; }

public override bool Execute()
{
if (AsConstants && AsEnum)
{
Log.LogError($"{nameof(AsConstants)} and {nameof(AsEnum)} cannot both be set to true.");
}

try
{
_resourcesName = "FxResources." + AssemblyName;
Expand Down Expand Up @@ -136,12 +146,14 @@ private void WriteResources()
var accessorNamespace = String.IsNullOrEmpty(ResourcesNamespace) ? _srNamespace : ResourcesNamespace;
var accessorClassName = String.IsNullOrEmpty(ResourcesClassName) ? _srClassName : ResourcesClassName;


if (_targetLanguage == TargetLanguage.CSharp)
{
_targetStream.WriteLine($"namespace {accessorNamespace}");
_targetStream.WriteLine("{");
_targetStream.WriteLine("");
_targetStream.WriteLine($" internal static partial class {accessorClassName}");
var accessor = AsEnum ? "enum" : "static partial class";
_targetStream.WriteLine($" internal {accessor} {accessorClassName}");
_targetStream.WriteLine(" {");
_targetStream.WriteLine("");

Expand All @@ -150,7 +162,8 @@ private void WriteResources()
{
_targetStream.WriteLine($"Namespace {accessorNamespace}");
_targetStream.WriteLine("");
_targetStream.WriteLine($" Friend Partial Class {accessorClassName}");
var accessor = AsEnum ? "Enum" : "Partial Class";
_targetStream.WriteLine($" Friend {accessor} {accessorClassName}");
_targetStream.WriteLine("");
}

Expand All @@ -161,6 +174,22 @@ private void WriteResources()
WriteResourceConstant((string)resourcePair.Key);
}
}
else if (AsEnum)
{
foreach(var resourcePair in resources)
{
_targetStream.Write($" {resourcePair.Key}");
if (_targetLanguage == TargetLanguage.CSharp)
{
_targetStream.WriteLine(",");
}
else
{
_targetStream.WriteLine();
}
}

}
else
{
_targetStream.WriteLine(_targetLanguage == TargetLanguage.CSharp ?
Expand Down Expand Up @@ -196,7 +225,8 @@ private void WriteResources()
}
else
{
_targetStream.WriteLine(" End Class");
var accessor = AsEnum ? "Enum" : "Partial Class";
_targetStream.WriteLine($" End {accessor}");
_targetStream.WriteLine("End Namespace");
}
}
Expand Down Expand Up @@ -258,22 +288,25 @@ private enum TargetLanguage
CSharp, VB
}

internal Dictionary<string, string> GetResources(string fileName)
internal List<KeyValuePair<string, string>> GetResources(string fileName)
{
Dictionary<string, string> resources = new Dictionary<string, string>();
// preserve the ordering of the resources
List<KeyValuePair<string, string>> resources = new List<KeyValuePair<string, string>>();
HashSet<string> resourceIds = new HashSet<string>();

XDocument doc = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
foreach (XElement dataElem in doc.Element("root").Elements("data"))
{
string name = dataElem.Attribute("name").Value;
string value = dataElem.Element("value").Value;
if (resources.ContainsKey(name))
if (resourceIds.Contains(name))
{
Log.LogError($"Duplicate resource id \"{name}\"");
}
else
{
resources[name] = value;
resourceIds.Add(name);
resources.Add(new KeyValuePair<string, string>(name, value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
SRClassName="$(GenerateResourcesSRClassName)"
ResourcesNamespace="$(GenerateResourcesResourcesNamespace)"
ResourcesClassName="$(GenerateResourcesResourcesClassName)"
AsConstants="$(GenerateResourcesCodeAsConstants)">
AsConstants="$(GenerateResourcesCodeAsConstants)"
AsEnum="$(GenerateResourcesCodeAsEnum)">
</GenerateResourcesCode>

<ItemGroup>
Expand Down

0 comments on commit 70f7153

Please sign in to comment.