From 23b5d31fa9a0970c1d0f1d6c64115eafcaf52feb Mon Sep 17 00:00:00 2001 From: Zev Spitz Date: Thu, 23 Feb 2017 15:49:33 +0200 Subject: [PATCH] Output runtime artifacts -- #25 --- core/Classes/TSBuilder.cs | 52 +++++++++++++++++++++++++-------------- wpf/Classes.cs | 11 ++++++--- wpf/MainWindow.xaml | 6 +++-- wpf/MainWindow.xaml.cs | 24 +++++++++++++----- 4 files changed, 63 insertions(+), 30 deletions(-) diff --git a/core/Classes/TSBuilder.cs b/core/Classes/TSBuilder.cs index 439ac4f..5308715 100644 --- a/core/Classes/TSBuilder.cs +++ b/core/Classes/TSBuilder.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Text; using TsActivexGen.Util; @@ -32,7 +31,8 @@ private void WriteJsDoc(List> JsDoc, int indentatio } } - private void WriteEnum(KeyValuePair x) { + //https://github.com/zspitz/ts-activex-gen/issues/25#issue-204161318 + private void WriteEnumDeclaration(KeyValuePair x) { var name = NameOnly(x.Key); var @enum = x.Value; var members = @enum.Members.OrderBy(y => y.Key); @@ -51,17 +51,19 @@ private void WriteEnum(KeyValuePair x) { } } - private void WriteNamespace(KeyValuePair x) { - var name = NameOnly(x.Key); - var members = x.Value.Members.OrderBy(y => y.Key); - - WriteJsDoc(x.Value.JsDoc, 2); + private void WriteNamespace(string name, Dictionary members) { + name = NameOnly(name); + var orderedMembers = members.OrderBy(y => y.Key); - $"namespace {name} {{".AppendLineTo(sb, 1); - members.AppendLinesTo(sb, (memberName, value) => $"var {memberName}: {value};", 2); + $"export namespace {name} {{".AppendLineTo(sb, 1); + orderedMembers.AppendLinesTo(sb, (memberName, value) => $"export const {memberName} = {value};", 2); "}".AppendWithNewSection(sb, 1); } + private void WriteEnumValues(KeyValuePair x) => WriteNamespace(x.Key, x.Value.Members); + + private void WriteNamespace(KeyValuePair x) => WriteNamespace(x.Key, x.Value.Members); + private string GetParameterString(KeyValuePair x, string ns) { var name = x.Key; var parameterDescription = x.Value; @@ -152,20 +154,13 @@ public NamespaceOutput GetTypescript(TSNamespace ns) { 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(WriteEnum); + 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(WriteEnum); - - //TODO add values to runtime file https://github.com/zspitz/ts-activex-gen/issues/25 - } - - if (ns.Namespaces.Any() && Debugger.IsAttached) { - //TODO add these to runtime file, not .d.ts -- https://github.com/zspitz/ts-activex-gen/issues/25 - //use the WriteNamespace method + numericEnums.OrderBy(x => x.Key).ForEach(WriteEnumDeclaration); } if (ns.Interfaces.Any()) { @@ -185,7 +180,26 @@ public NamespaceOutput GetTypescript(TSNamespace ns) { Description = ns.Description }; - //TODO build constants file here + + //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(); return ret; } diff --git a/wpf/Classes.cs b/wpf/Classes.cs index 0ac3125..d628888 100644 --- a/wpf/Classes.cs +++ b/wpf/Classes.cs @@ -9,13 +9,18 @@ namespace TsActivexGen.Wpf { public class OutputFileDetails { public string Name { get; set; } public string Description { get; set; } - public string FileName { get; set; } + 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 Exists => File.Exists(FullPath); + public bool PackageForTypings { get; set; } public bool WriteOutput { get; set; } public NamespaceOutput Output { get; set; } - public string FullPath => Path.Combine(OutputFolder, FileName.ForceEndsWith(".d.ts")); + public bool EmitModuleConstants { get; set; } } diff --git a/wpf/MainWindow.xaml b/wpf/MainWindow.xaml index 8aff3af..43fd570 100644 --- a/wpf/MainWindow.xaml +++ b/wpf/MainWindow.xaml @@ -74,8 +74,10 @@ - - + + + + diff --git a/wpf/MainWindow.xaml.cs b/wpf/MainWindow.xaml.cs index f987c69..45a5fe2 100644 --- a/wpf/MainWindow.xaml.cs +++ b/wpf/MainWindow.xaml.cs @@ -58,22 +58,33 @@ public MainWindow() { } dtgFiles.Items().ForEach(x => { if (!x.WriteOutput) { return; } - if (x.FileName.IsNullOrEmpty()) { return; } - if (createFile(x.FullPath)) { + if (x.DeclarationFileName.IsNullOrEmpty()) { return; } + if (createFile(x.FullDeclarationPath)) { if (x.PackageForTypings) { throw new NotImplementedException(); //TODO } else { - WriteAllText(x.FullPath, x.Output.MainFile); + WriteAllText(x.FullDeclarationPath, x.Output.MainFile); + } + } + + if (!x.Output.RuntimeFile.IsNullOrEmpty() && createFile(x.FullRuntimePath)) { + if (x.PackageForTypings) { + throw new NotImplementedException(); //TODO + } else { + WriteAllText(x.FullRuntimePath, x.Output.RuntimeFile); } } }); - var firstFilePath = dtgFiles.Items().FirstOrDefault()?.FullPath; + var firstFilePath = dtgFiles.Items().FirstOrDefault()?.FullDeclarationPath; if (firstFilePath.IsNullOrEmpty()) { return; } var psi = new ProcessStartInfo("explorer.exe", "/n /e,/select,\"" + firstFilePath + "\""); Process.Start(psi); }; - btnClearFiles.Click += (s, e) => fileList.Clear(); + btnClearFiles.Click += (s, e) => { + tlbGenerator = new TlbInf32Generator(); + fileList.Clear(); + }; } private bool createFile(string path) { @@ -104,7 +115,8 @@ private void addFiles() { fileList.Clear(); new TSBuilder().GetTypescript(tlbGenerator.NSSet).SelectKVP((name, x) => new OutputFileDetails { Name = name, - FileName = $"activex-{name.ToLower()}.d.ts", + DeclarationFileName = $"activex-{name.ToLower()}.d.ts", + RuntimeFileName = $"activex-{name.ToLower()}-runtime.ts", OutputFolder = txbOutputFolder.Text, WriteOutput = true, PackageForTypings = cbPackageForTypes.IsChecked.Value,