-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
307 lines (265 loc) · 10.4 KB
/
Program.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Copyright (C) 2016 Cameron Angus. All Rights Reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;
namespace KantanDocGen
{
class Program
{
static string ParseArgumentValue(List<string> ArgumentList, string Prefix, string DefaultValue)
{
for (int Idx = 0; Idx < ArgumentList.Count; Idx++)
{
if (ArgumentList[Idx].StartsWith(Prefix))
{
string Value = ArgumentList[Idx].Substring(Prefix.Length);
ArgumentList.RemoveAt(Idx);
return Value;
}
}
return DefaultValue;
}
static string ParseArgumentPath(List<string> ArgumentList, string Prefix, string DefaultValue)
{
string Value = ParseArgumentValue(ArgumentList, Prefix, DefaultValue);
if (Value != null)
{
Value = Path.GetFullPath(Value);
}
return Value;
}
static string ParseArgumentDirectory(List<string> ArgumentList, string Prefix, string DefaultValue)
{
string Value = ParseArgumentPath(ArgumentList, Prefix, DefaultValue);
if (Value != null && !Directory.Exists(Value))
{
Directory.CreateDirectory(Value);
}
return Value;
}
static private void ProcessOutputReceived(Object Sender, DataReceivedEventArgs Line)
{
if (Line.Data != null && Line.Data.Length > 0)
{
Console.WriteLine(Line.Data);
}
}
public static void SafeCreateDirectory(string Path)
{
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
}
private static void CopyWholeDirectory(string SourceDir, string DestDir)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(SourceDir);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ SourceDir);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(DestDir))
{
Directory.CreateDirectory(DestDir);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(DestDir, file.Name);
file.CopyTo(temppath, true);
}
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(DestDir, subdir.Name);
CopyWholeDirectory(subdir.FullName, temppath);
}
}
// @NOTE: Currently unused, seemingly no way to use Slate for the node rendering when running commandlet.
// Instead this tool is now invoked by a plugin.
static bool RunXmlDocGenCommandlet(string EngineDir, string EditorPath, string OutputDir)
{
// Create the output directory
SafeCreateDirectory(OutputDir);
string Arguments = "-run=KantanDocs -path=" + OutputDir + " -name=BlueprintAPI -stdout -FORCELOGFLUSH -CrashForUAT -unattended -AllowStdOutLogVerbosity";
Console.WriteLine("Running: {0} {1}", EditorPath, Arguments);
using (Process NewProcess = new Process())
{
NewProcess.StartInfo.WorkingDirectory = EngineDir;
NewProcess.StartInfo.FileName = EditorPath;
NewProcess.StartInfo.Arguments = Arguments;
NewProcess.StartInfo.UseShellExecute = false;
NewProcess.StartInfo.RedirectStandardOutput = true;
NewProcess.StartInfo.RedirectStandardError = true;
NewProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputReceived);
NewProcess.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputReceived);
try
{
NewProcess.Start();
NewProcess.BeginOutputReadLine();
NewProcess.BeginErrorReadLine();
NewProcess.WaitForExit();
if (NewProcess.ExitCode != 0)
{
Console.WriteLine("Error: Xml doc generation commandlet failed, aborting.\nIs the plugin installed?");
return false;
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString() + "\n" + Ex.StackTrace);
return false;
}
}
return true;
}
static void Main(string[] args)
{
List<string> ArgumentList = new List<string>(args);
Console.WriteLine("KantanDocGen invoked with arguments:");
foreach (string Arg in ArgumentList)
{
Console.WriteLine(Arg);
}
string DocsTitle = ParseArgumentValue(ArgumentList, "-name=", null);
if(DocsTitle == null)
{
Console.WriteLine("KantanDocGen: Error: Documentation title (-name=) required. Aborting.");
return;
}
// Get the default paths
// If unspecified, assume the directory containing our binary is one level below the base directory
string DocGenBaseDir = ParseArgumentDirectory(ArgumentList, "-basedir=", Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ".."));
string OutputRoot = ParseArgumentDirectory(ArgumentList, "-outputdir=", Directory.GetCurrentDirectory());
string OutputDir = Path.Combine(OutputRoot, DocsTitle);
//string MsxslPath = ParseArgumentPath(ArgumentList, "-xslproc=", Path.Combine(EngineDir, "Binaries/ThirdParty/Msxsl/msxsl.exe"));
// Xsl transform files - if not specified explicitly, look for defaults relative to base directory
string IndexTransformPath = ParseArgumentPath(ArgumentList, "-indexxsl=", Path.Combine(DocGenBaseDir, "xslt/index_xform.xsl"));
string ClassTransformPath = ParseArgumentPath(ArgumentList, "-classxsl=", Path.Combine(DocGenBaseDir, "xslt/class_docs_xform.xsl"));
string NodeTransformPath = ParseArgumentPath(ArgumentList, "-nodexsl=", Path.Combine(DocGenBaseDir, "xslt/node_docs_xform.xsl"));
bool bFromIntermediate = ArgumentList.Contains("-fromintermediate");
string IntermediateDir;
if (bFromIntermediate)
{
// Intermediate docs already created, we need to have been passed an intermediate directory to locate them
IntermediateDir = ParseArgumentDirectory(ArgumentList, "-intermediatedir=", null);
if (IntermediateDir == null)
{
Console.WriteLine("KantanDocGen: Error: -fromintermediate requires -intermediatedir to be set. Aborting.");
return;
}
if(!Directory.Exists(IntermediateDir))
{
Console.WriteLine("KantanDocGen: Error: Specified intermediate directory not found. Aborting.");
return;
}
}
else
{
// @TODO: This doesn't work, since commandlet cannot create Slate windows!
// Can reenable this path if manage to get a Program target type to build against the engine.
Console.WriteLine("KantanDocGen: Error: Calling without -fromintermediate currently not supported. Use the KantanDocGen engine plugin to generate documentation.");
return;
/* IntermediateDir = ParseArgumentDirectory(ArgumentList, "-intermediatedir=", Path.Combine(EngineDir, "Intermediate\\KantanDocGen"));
// Need to generate intermediate docs first
// Run editor commandlet to generate XML and image files
string EditorPath = Path.Combine(EngineDir, "Binaries\\Win64\\UE4Editor-Cmd.exe");
if (!RunXmlDocGenCommandlet(EngineDir, EditorPath, IntermediateDir))
{
return;
}
*/ }
const bool bCleanOutput = true;
bool bHardClean = ArgumentList.Contains("-cleanoutput");
if (bCleanOutput)
{
// If the output directory exists, attempt to delete it (this will fail if bHardClean is false and the directory contains files/subfolders)
if (Directory.Exists(OutputDir))
{
try
{
Directory.Delete(OutputDir, bHardClean);
}
catch(Exception)
{
Console.WriteLine("KantanDocGen: Error: Output directory '{0}' exists and not empty/couldn't delete. Remove and rerun, or specify -cleanoutput (If running from plugin console, add 'clean' parameter).", OutputDir);
return;
}
}
}
//var XslXform = new MsxslXform(MsxslPath);
var IndexXform = new SaxonXform();
var ClassXform = new SaxonXform();
var NodeXform = new SaxonXform();
// Initialize the transformations
if (!IndexXform.Initialize(IndexTransformPath, ProcessOutputReceived))
{
Console.WriteLine("Error: Failed to initialize xslt processor.");
return;
}
if (!ClassXform.Initialize(ClassTransformPath, ProcessOutputReceived))
{
Console.WriteLine("Error: Failed to initialize xslt processor.");
return;
}
if (!NodeXform.Initialize(NodeTransformPath, ProcessOutputReceived))
{
Console.WriteLine("Error: Failed to initialize xslt processor.");
return;
}
// Loop over all generated xml files and apply the transformation
int Success = 0;
int Failed = 0;
// @TODO: Should iterate over index/class xml entries rather than enumerate files and directories
var SubFolders = Directory.EnumerateDirectories(IntermediateDir);
foreach (string Sub in SubFolders)
{
string ClassTitle = Path.GetFileName(Sub);
string OutputClassDir = Path.Combine(OutputDir, ClassTitle);
SafeCreateDirectory(OutputClassDir);
string NodeDir = Path.Combine(Sub, "nodes");
if (Directory.Exists(NodeDir))
{
string OutputNodesDir = Path.Combine(OutputClassDir, "nodes");
SafeCreateDirectory(OutputNodesDir);
var InputFiles = Directory.EnumerateFiles(NodeDir, "*.xml", SearchOption.TopDirectoryOnly);
foreach (string FilePath in InputFiles)
{
string FileTitle = Path.GetFileNameWithoutExtension(FilePath);
string OutputPath = Path.Combine(OutputNodesDir, FileTitle + ".html");
string InputPath = FilePath;
if (!NodeXform.TransformXml(InputPath, OutputPath))
{
Console.WriteLine("Error: Xsl transform failed for file {0} - skipping.", InputPath);
++Failed;
continue;
}
++Success;
}
string OutputClassPath = Path.Combine(OutputClassDir, ClassTitle + ".html");
ClassXform.TransformXml(Path.Combine(Sub, ClassTitle + ".xml"), OutputClassPath);
}
// Copy the images for this class to the output directory
CopyWholeDirectory(Path.Combine(Sub, "img"), Path.Combine(OutputClassDir, "img"));
}
string OutputIndexPath = Path.Combine(OutputDir, "index.html");
IndexXform.TransformXml(Path.Combine(IntermediateDir, "index.xml"), OutputIndexPath);
CopyWholeDirectory(Path.Combine(DocGenBaseDir, "css"), Path.Combine(OutputDir, "css"));
Console.WriteLine("KantanDocGen completed:");
Console.WriteLine("{0} node docs successfully transformed.", Success);
Console.WriteLine("{0} failed.", Failed);
}
}
}