Skip to content

Commit

Permalink
Enums and modules represented as const enums; removed runtime file; r…
Browse files Browse the repository at this point in the history
…emoved nested namespaces;
  • Loading branch information
zspitz committed Jun 22, 2017
1 parent 1c7d254 commit f97a380
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 90 deletions.
3 changes: 0 additions & 3 deletions core/Classes/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static bool OperatorEquals(T x, T y) {
}

public class TSEnumDescription {
public TSSimpleType Typename { get; set; }
public Dictionary<string, string> Members { get; } = new Dictionary<string, string>(); //values -> string representation of value
public List<KeyValuePair<string, string>> JsDoc { get; } = new List<KeyValuePair<string, string>>();
}
Expand Down Expand Up @@ -88,7 +87,6 @@ public class TSNamespace {
public string Description { get; set; }
public Dictionary<string, TSEnumDescription> Enums { get; } = new Dictionary<string, TSEnumDescription>();
public Dictionary<string, TSInterfaceDescription> Interfaces { get; } = new Dictionary<string, TSInterfaceDescription>();
public Dictionary<string, TSNamespaceDescription> Namespaces { get; } = new Dictionary<string, TSNamespaceDescription>();
public Dictionary<string, TSSimpleType> Aliases { get; } = new Dictionary<string, TSSimpleType>();
public HashSet<string> Dependencies { get; } = new HashSet<string>();
public Dictionary<string, TSInterfaceDescription> GlobalInterfaces { get; } = new Dictionary<string, TSInterfaceDescription>();
Expand Down Expand Up @@ -142,7 +140,6 @@ public class NamespaceOutput {
public string Description { get; set; }
public string MainFile { get; set; }
public HashSet<string> Dependencies { get; set; }
public string RuntimeFile { get; set; }
public string TestsFile { get; set; }
}

Expand Down
58 changes: 5 additions & 53 deletions core/Classes/TSBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,11 @@ private void writeEnumDeclaration(KeyValuePair<string, TSEnumDescription> x) {

writeJsDoc(@enum.JsDoc, 2);

//https://github.com/zspitz/ts-activex-gen/issues/25
if (@enum.Typename.FullName == "number") {
$"const enum {name} {{".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"{memberName} = {value}", 2, ",");
"}".AppendWithNewSection(sb, 1);
} else {
$"type {name} = ".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"\"{value}\" //{memberName}", 2, null, "| ");
sb.AppendLine();
}
}

private void writeNamespace(string name, Dictionary<string, string> members) {
name = NameOnly(name);
var orderedMembers = members.OrderBy(y => y.Key);

$"export namespace {name} {{".AppendLineTo(sb, 1);
orderedMembers.AppendLinesTo(sb, (memberName, value) => $"export const {memberName} = {value};", 2);
$"const enum {name} {{".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"{memberName} = {value}", 2, ",");
"}".AppendWithNewSection(sb, 1);
}

private void writeEnumValues(KeyValuePair<string, TSEnumDescription> x) => writeNamespace(x.Key, x.Value.Members);

private void writeNamespace(KeyValuePair<string, TSNamespaceDescription> x) => writeNamespace(x.Key, x.Value.Members);

private string getParameterString(KeyValuePair<string, TSParameterDescription> x, string ns) {
var name = x.Key;
var parameterDescription = x.Value;
Expand Down Expand Up @@ -156,16 +136,9 @@ public NamespaceOutput GetTypescript(TSNamespace ns) {
ns.Aliases.OrderBy(x => x.Key).ForEach(x => WriteAlias(x, ns.Name));
}

var numericEnums = ns.Enums.Where(x => x.Value.Typename.FullName == "number");
if (numericEnums.Any()) {
"//Numeric enums".AppendLineTo(sb, 1);
numericEnums.OrderBy(x => x.Key).ForEach(writeEnumDeclaration);
}

var nonnumericEnums = ns.Enums.Where(x => x.Value.Typename.FullName != "number");
if (nonnumericEnums.Any()) {
"//Nonnumeric enums".AppendLineTo(sb, 1);
numericEnums.OrderBy(x => x.Key).ForEach(writeEnumDeclaration);
if (ns.Enums.Any()) {
"//Enums".AppendLineTo(sb, 1);
ns.Enums.OrderBy(x => x.Key).ForEach(writeEnumDeclaration);
}

if (ns.Interfaces.Any()) {
Expand All @@ -186,27 +159,6 @@ public NamespaceOutput GetTypescript(TSNamespace ns) {
Dependencies = ns.Dependencies
};


//Build the runtime constants file

sb = new StringBuilder();
if (nonnumericEnums.Any() || ns.Namespaces.Any()) {
$"namespace {ns.Name} {{".AppendWithNewSection(sb);

if (nonnumericEnums.Any()) {
"//Non-numeric enums".AppendLineTo(sb, 1);
nonnumericEnums.OrderBy(x => x.Key).ForEach(writeEnumValues);
}

if (ns.Namespaces.Any()) {
"//Modules".AppendLineTo(sb, 1);
ns.Namespaces.OrderBy(x => x.Key).ForEach(writeNamespace);
}

"}".AppendWithNewSection(sb);
}
ret.RuntimeFile = sb.ToString();

//Build the tests file
ns.GlobalInterfaces.IfContainsKey("ActiveXObject", x => {
ret.TestsFile = x.Constructors.Joined(Environment.NewLine + Environment.NewLine, (y, index) => $"let obj{index} = new ActiveXObject({GetTypeString(y.Parameters[0].Value.Type, "")})");
Expand Down
22 changes: 2 additions & 20 deletions core/Classes/TlbInf32Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,9 @@ private TSSimpleType GetTypeName(VarTypeInfo vti, object value = null) {
return ret;
}

//https://github.com/zspitz/ts-activex-gen/issues/25
private KeyValuePair<string, TSNamespaceDescription> ToTSNamespaceDescription(ConstantInfo c) {
var ret = new TSNamespaceDescription();
c.Members.Cast().Select(x => KVP(x.Name, AsString((object)x.Value))).AddRangeTo(ret.Members);
ret.JsDoc.Add("", c.HelpString);
return KVP($"{c.Parent.Name}.{c.Name}", ret);
}

private KeyValuePair<string, TSEnumDescription> ToTSEnumDescription(ConstantInfo c) {
var ret = new TSEnumDescription();
c.Members.Cast().Select(x => {
var oValue = (object)x.Value;
var typename = GetTypeName(x.ReturnType, oValue);
if (ret.Typename == null) {
ret.Typename = typename;
} else if (ret.Typename != TSSimpleType.Any && ret.Typename != typename) {
ret.Typename = TSSimpleType.Any;
}
return KVP(x.Name, AsString(oValue));
}).AddRangeTo(ret.Members);
c.Members.Cast().Select(x => KVP(x.Name, AsString((object)x.Value))).AddRangeTo(ret.Members);
ret.JsDoc.Add("", c.HelpString);
return KVP($"{c.Parent.Name}.{c.Name}", ret);
}
Expand Down Expand Up @@ -314,8 +297,7 @@ private TSNamespace ToNamespace(TypeLibInfo tli) {
var coclasses = tli.CoClasses.Cast().ToList();

var ret = new TSNamespace() { Name = tli.Name };
tli.Constants.Cast().Where(x => x.TypeKind != TKIND_MODULE).Select(ToTSEnumDescription).AddRangeTo(ret.Enums);
tli.Constants.Cast().Where(x => x.TypeKind == TKIND_MODULE).Select(ToTSNamespaceDescription).AddRangeTo(ret.Namespaces);
tli.Constants.Cast().Select(ToTSEnumDescription).AddRangeTo(ret.Enums);
coclasses.Select(ToTSInterfaceDescription).AddInterfacesTo(ret);

if (tli.Declarations.Cast().Any()) {
Expand Down
3 changes: 0 additions & 3 deletions wpf/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class OutputFileDetails {
public string DeclarationFileName { get; set; }
public bool DeclarationExists => File.Exists(FullDeclarationPath);
public string FullDeclarationPath => Path.Combine(OutputFolder, DeclarationFileName.ForceEndsWith(".d.ts"));
public string RuntimeFileName { get; set; }
public bool RuntimeExists => File.Exists(FullRuntimePath);
public string FullRuntimePath => Path.Combine(OutputFolder, RuntimeFileName.ForceEndsWith(".ts"));
public string OutputFolder { get; set; }

public bool PackageForTypings { get; set; }
Expand Down
2 changes: 0 additions & 2 deletions wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@
<my:DataGridTextColumnExt Binding="{Binding Output.Description}" IsReadOnly="True" Header="Description" TextTrimming="CharacterEllipsis" />
<DataGridTextColumn Binding="{Binding DeclarationFileName}" Header="Filename" />
<DataGridCheckBoxColumn Binding="{Binding DeclarationExists, Mode=OneWay}" IsReadOnly="True" Header="Exists" />
<DataGridTextColumn Binding="{Binding RuntimeFileName}" Header="Filename" />
<DataGridCheckBoxColumn Binding="{Binding RuntimeExists, Mode=OneWay}" IsReadOnly="True" Header="Exists" />
<DataGridCheckBoxColumn Binding="{Binding WriteOutput}" Header="Output" />
<DataGridCheckBoxColumn Binding="{Binding PackageForTypings}" Header="@types packaging" />
</DataGrid.Columns>
Expand Down
9 changes: 0 additions & 9 deletions wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ public MainWindow() {
WriteAllText(x.FullDeclarationPath, x.Output.MainFile);
}
}
if (!x.Output.RuntimeFile.IsNullOrEmpty() && createFile(x.FullRuntimePath)) {
if (x.PackageForTypings) {
//TODO export as an NPM package?
} else {
WriteAllText(x.FullRuntimePath, x.Output.RuntimeFile);
}
}
});
var firstFilePath = dtgFiles.Items<OutputFileDetails>().FirstOrDefault()?.FullDeclarationPath;
if (firstFilePath.IsNullOrEmpty()) { return; }
Expand Down Expand Up @@ -139,7 +131,6 @@ private void addFiles() {
new TSBuilder().GetTypescript(tlbGenerator.NSSet).SelectKVP((name, x) => new OutputFileDetails {
Name = name,
DeclarationFileName = $"activex-{name.ToLower()}.d.ts",
RuntimeFileName = $"activex-{name.ToLower()}-runtime.ts",
OutputFolder = txbOutputFolder.Text,
WriteOutput = true,
PackageForTypings = cbPackageForTypes.IsChecked.Value,
Expand Down

0 comments on commit f97a380

Please sign in to comment.