-
Notifications
You must be signed in to change notification settings - Fork 0
/
PocoToTypescriptGenerator.cs
164 lines (137 loc) · 5.73 KB
/
PocoToTypescriptGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
namespace Pocoyo
{
public static class PocoToTypescriptGenerator
{
private static string TempExtension => $".temp.{Process.GetCurrentProcess().Id}";
public static Options Options { get; private set; }
public static int Main(string[] args)
{
try
{
Console.WriteLine(nameof(PocoToTypescriptGenerator) + ".exe " + string.Join(" ", args));
Options = Options.ParseArgs(args);
if (Options == null)
return -1;
Log.SilentMode = Options.Silent;
Log.VerbosMode = Options.Verbose; // || Debugger.IsAttached;
if (Log.VerbosMode)
Log.Info($"Options: {Options}");
Pocoyo.DefaultNamespace = Options.Namespace;
if (Options.Excluded != null)
Pocoyo.Excluded = Options.Excluded.ToList();
if (Options.ExcludedAttributes != null)
Pocoyo.ExcludedAttributes = Options.ExcludedAttributes.ToList();
if (Options.KnownTypes != null)
Pocoyo.KnownTypes = Options.KnownTypes.ToList();
Log.Verbose($"{Utility.AssemblyName} {string.Join(" ", args)}");
// Preprocess all files
if (Options.PreProcess)
{
foreach (var inputFile in Options.InputFiles)
{
Pocoyo.PreProcess(inputFile);
}
}
// Generate typescript definition files
var outputFiles = new List<string>();
foreach (var inputFile in Options.InputFiles)
{
var outputFilePath = GetTempOutputFilePath(inputFile);
var realOutputFilePath = outputFilePath.Replace(TempExtension, "");
if (!outputFiles.Contains(outputFilePath))
{
outputFiles.Add(outputFilePath);
Utility.TryDeleteFile(outputFilePath);
}
Pocoyo.Process(inputFile, outputFilePath, !Options.PreProcess);
if (!string.Equals(Options.OutputFilePath, realOutputFilePath))
Log.Info($"Generated: {realOutputFilePath}");
}
if (Options.TypescriptFiles != null)
{
foreach (var inputFile in Options.TypescriptFiles)
{
if (File.Exists(inputFile))
{
var outputFilePath = GetTempOutputFilePath(inputFile);
if (!outputFiles.Contains(outputFilePath))
{
outputFiles.Add(outputFilePath);
Utility.TryDeleteFile(outputFilePath);
}
var contents = SharedFile.ReadAllText(inputFile);
Execute(outputFilePath, () =>
{
SharedFile.AppendAllText(outputFilePath, contents);
});
}
}
}
foreach (var outputFile in outputFiles)
{
var realOutputFilePath = outputFile.Replace(TempExtension, "");
Execute(realOutputFilePath, () =>
{
Utility.TryMoveFile(outputFile, realOutputFilePath);
Utility.TryDeleteFile(outputFile);
});
}
// Log combined output file if any
if (!string.IsNullOrEmpty(Options.OutputFilePath))
{
Log.Info($"Generated {Options.OutputFilePath}");
var pocoyoDataFilePath = Path.ChangeExtension(Options.OutputFilePath, ".Pocoyo.json");
Execute(pocoyoDataFilePath, () =>
{
var data = new PocoyoData();
data.SaveToData(pocoyoDataFilePath);
});
}
return 0;
}
catch (Exception ex)
{
Log.Error($"{nameof(Main)} Exception: {ex}");
return -2;
}
}
/// <summary>
/// Lock output operations for multi target case
/// </summary>
private static void Execute(string name, Action action)
{
try
{
var semaphore = new Semaphore(1, 1, name.Replace(":", ".").Replace("\\", "."));
using (new SemaphoreLock(semaphore))
{
action();
}
}
catch (Exception ex)
{
Log.Warn($"{name} {ex.Message}");
}
}
/// <summary>
/// Returns OutputFile if specified or output file from OutputFolder based on input file
/// </summary>
private static string GetTempOutputFilePath(string inputFile)
{
if (!string.IsNullOrEmpty(Options.OutputFolder))
{
var outputFilePath = Path.Combine(Options.OutputFolder, Path.GetFileNameWithoutExtension(inputFile) + ".d.ts");
if (File.Exists(outputFilePath))
File.Delete(outputFilePath);
return outputFilePath + TempExtension;
}
return Options.OutputFilePath + TempExtension;
}
}
}