Skip to content

Commit

Permalink
Add an option to ConstantsScraper to import constants from another na…
Browse files Browse the repository at this point in the history
…mespace (#1890)
  • Loading branch information
dpaoliello committed May 6, 2024
1 parent d7c12d8 commit 602e5ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 19 additions & 2 deletions sources/ConstantsScraper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Text;
using MetadataUtils;

namespace ConstantsScraperApp
Expand All @@ -27,7 +28,8 @@ static int Main(string[] args)
new Option<string>("--with-attribute", "Add an attribute to a constant.", ArgumentArity.OneOrMore),
new Option<string>("--memberRemap", "A field or parameter that should get remapped to a certain type.", ArgumentArity.OneOrMore),
new Option<string>("--with-type", "Use a type for a constant.", ArgumentArity.OneOrMore),
new Option<string>("--enumsJson", "A json file with enum information.", ArgumentArity.OneOrMore)
new Option<string>("--enumsJson", "A json file with enum information.", ArgumentArity.OneOrMore),
new Option<string>("--useConstantsFrom", "Namespaces to import other constants from.", ArgumentArity.OneOrMore)
};

rootCommand.Handler = CommandHandler.Create<InvocationContext>(Run);
Expand All @@ -46,6 +48,7 @@ public static int Run(InvocationContext context)
var remappedNameValuePairs = context.ParseResult.ValueForOption<string[]>("--memberRemap");
var withTypeValuePairs = context.ParseResult.ValueForOption<string[]>("--with-type");
var withAttributeValuePairs = context.ParseResult.ValueForOption<string[]>("--with-attribute");
var useConstantsFrom = context.ParseResult.ValueForOption<string[]>("--useConstantsFrom");

var exclusionNames = new HashSet<string>(excludeItems ?? (new string[0]));
Dictionary<string, string> traversedHeadersToNamespaces = ConvertValuePairsToDictionary(traversedHeaderToNamespaceValuePairs);
Expand All @@ -54,7 +57,21 @@ public static int Run(InvocationContext context)
Dictionary<string, string> withTypes = ConvertValuePairsToDictionary(withTypeValuePairs);
Dictionary<string, string> withAttributes = ConvertValuePairsToDictionary(withAttributeValuePairs);

var headerText = !string.IsNullOrEmpty(headerTextFile) ? File.ReadAllText(headerTextFile) : string.Empty;
StringBuilder headerTextBuilder = new StringBuilder();
if (!string.IsNullOrEmpty(headerTextFile))
{
headerTextBuilder.Append(File.ReadAllText(headerTextFile));
headerTextBuilder.AppendLine();
}
foreach (var name in useConstantsFrom)
{
headerTextBuilder.AppendLine($"using static {name};");
}
if (useConstantsFrom.Length > 0)
{
headerTextBuilder.AppendLine();
}
var headerText = headerTextBuilder.ToString();

// Always exclude this
exclusionNames.Add("__cplusplus");
Expand Down
1 change: 0 additions & 1 deletion sources/MetadataUtils/ConstantWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ private void EnsureStarted()
this.writer = new StreamWriter(this.outputStream);
this.writer.WriteLine(
@$"{this.headerText}
namespace {this.@namespace}
{{
public static partial class Apis
Expand Down

0 comments on commit 602e5ba

Please sign in to comment.