-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
119 lines (105 loc) · 4.89 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
using System.Reflection;
using System.Xml;
using HandlebarsDotNet;
using System.Linq;
class Program
{
/// <summary>
/// A simple command-line application to generate ready-to-use C# class for specified window in glade file
/// </summary>
/// <param name="file">The .glade XML input file to generate from</param>
/// <param name="window">The window id in the glade resource</param>
/// <param name="withNamespace">Namespace for generated class</param>
/// <param name="outputFolder">Output location for generated files (optional, defaults to current directory)</param>
/// <param name="uiOnly">Generates the UI definitions only (optional, defaults to true)</param>
public static void Main(string file, string window, string withNamespace, string outputFolder, bool uiOnly = true)
{
//Ensure input file and window name are specified.
if (string.IsNullOrEmpty(file) || (string.IsNullOrEmpty(window)))
{
Console.WriteLine("The --file and --window must be specified. Use -h for help");
Environment.Exit(0);
}
XmlDocument document = new XmlDocument();
try
{
document.Load(file);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to load file {file}");
Environment.Exit(1);
}
string templateUIClass = GetEmbeddedResource("GeneratedUI.txt");
string templateClass = GetEmbeddedResource("Generated.txt");
//find all object nodes.
var objectNodes = document.GetElementsByTagName("object");
for (int i = 0; i < objectNodes.Count; i++) //find window with specified window name
{
var node = objectNodes[i];
if (node.Attributes != null &&
node.Attributes["class"] != null &&
node.Attributes["class"].Value == "GtkWindow" &&
node.Attributes["id"] != null &&
node.Attributes["id"].Value == window)
{
Dictionary<string, string> members = new Dictionary<string, string>();
var specifiedWindowNodes = node.SelectNodes("descendant::object[@id]");
for (int j = 0; j < specifiedWindowNodes.Count; j++)
{
var memberNode = specifiedWindowNodes[j];
string className = memberNode.Attributes["class"].Value.Remove(0, 3);
string widgetId = memberNode.Attributes["id"].Value;
members.Add(widgetId, className);
}
var uiTemplate = Handlebars.Compile(templateUIClass);
var uiData = new
{
windowName = window,
hasNamespace = withNamespace is not null,
withNamespace = withNamespace,
filename = file,
widgets = members.Keys.Select(id => new
{
className = members[id],
widgetId = id,
})
};
var resultUIClass = uiTemplate(uiData);
string resultClass = "";
if (!uiOnly) {
var classTemplate = Handlebars.Compile(templateClass);
resultClass = classTemplate(uiData);
}
string outputUIPath = string.IsNullOrEmpty(outputFolder) ? Path.Join(Environment.CurrentDirectory, $"{window}.UI.cs") : Path.Join(outputFolder, $"{window}.UI.cs");
string outputClassPath = string.IsNullOrEmpty(outputFolder) ? Path.Join(Environment.CurrentDirectory, $"{window}.cs") : Path.Join(outputFolder, $"{window}.cs");
try
{
File.WriteAllText(outputUIPath, resultUIClass);
Console.WriteLine($"Wrote output to {outputUIPath}");
if (!uiOnly) {
File.WriteAllText(outputClassPath, resultClass);
Console.WriteLine($"Wrote output to {outputClassPath}");
}
}
catch (Exception ex) {
Console.WriteLine("Unable to save output.", ex.Message);
}
Environment.Exit(0);
}
}
//Won't reach this part unless no nodes are found.
Console.WriteLine($"No GtkWindow node with id {window}");
Environment.Exit(1);
}
public static string GetEmbeddedResource(string filename)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(filename))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return result;
}
}
}