Skip to content

Commit

Permalink
Output runtime artifacts -- #25
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Feb 23, 2017
1 parent c56e95d commit 23b5d31
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
52 changes: 33 additions & 19 deletions core/Classes/TSBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TsActivexGen.Util;
Expand Down Expand Up @@ -32,7 +31,8 @@ private void WriteJsDoc(List<KeyValuePair<string, string>> JsDoc, int indentatio
}
}

private void WriteEnum(KeyValuePair<string, TSEnumDescription> x) {
//https://github.com/zspitz/ts-activex-gen/issues/25#issue-204161318
private void WriteEnumDeclaration(KeyValuePair<string, TSEnumDescription> x) {
var name = NameOnly(x.Key);
var @enum = x.Value;
var members = @enum.Members.OrderBy(y => y.Key);
Expand All @@ -51,17 +51,19 @@ private void WriteEnum(KeyValuePair<string, TSEnumDescription> x) {
}
}

private void WriteNamespace(KeyValuePair<string, TSNamespaceDescription> 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<string,string> 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<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 @@ -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()) {
Expand All @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions wpf/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down
6 changes: 4 additions & 2 deletions wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" IsReadOnly="True" Header="Name" />
<my:DataGridTextColumnExt Binding="{Binding Output.Description}" IsReadOnly="True" Header="Description" TextTrimming="CharacterEllipsis" />
<DataGridTextColumn Binding="{Binding FileName}" Header="Filename" />
<DataGridCheckBoxColumn Binding="{Binding Exists, Mode=OneWay}" IsReadOnly="True" Header="Exists" />
<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
24 changes: 18 additions & 6 deletions wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,33 @@ public MainWindow() {
}
dtgFiles.Items<OutputFileDetails>().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<OutputFileDetails>().FirstOrDefault()?.FullPath;
var firstFilePath = dtgFiles.Items<OutputFileDetails>().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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 23b5d31

Please sign in to comment.