forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (85 loc) · 3.55 KB
/
Program.cs
File metadata and controls
97 lines (85 loc) · 3.55 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RxUIViewModelGenerator
{
enum TemplateType {
GeneratedViewModel,
ViewModel,
XamlControl,
XamlCodeBehind,
};
class Program
{
static int Main(string[] args)
{
var opts = default(OptionSet);
var type = default(TemplateType);
var input = Console.In;
var template = default(string);
opts = new OptionSet() {
{ "t=|type=", "Type of template, one of ViewModel, GeneratedViewModel, XamlControl, XamlCodeBehind",
(string x) => Enum.TryParse<TemplateType>(x, out type) },
{ "i=|input=", "The input interface file, defaults to stdin",
(string x) => input = new StreamReader(File.OpenRead(x), Encoding.UTF8) },
{ "template-override=", "The input template file (ignores built-in one if specified)",
(string x) => template = File.ReadAllText(x, Encoding.UTF8) },
{ "h|help", "Displays Help",
_ => opts.WriteOptionDescriptions(Console.Out) },
};
var rest = default(List<string>);
try {
rest = opts.Parse(args);
} catch (OptionException ex) {
Console.Error.WriteLine(ex.Message);
opts.WriteOptionDescriptions(Console.Error);
}
removeExtraneousPowerShellParameter(rest);
var dict = default(Dictionary<string, object>);
try {
dict = rest.Select(x => x.Split('=')).ToDictionary(k => k[0], v => (object) v[1]);
}
catch {
Console.Error.WriteLine("Extra parameters are specified as theKey=theValue");
return -1;
}
var content = input.ReadToEnd();
var renderer = new ScaffoldRenderer();
var toWrite = default(IEnumerable<Tuple<string, string>>);
switch (type) {
case TemplateType.GeneratedViewModel:
toWrite = new[] { renderer.RenderGeneratedViewModel(content, dict, template) };
break;
case TemplateType.ViewModel:
toWrite = new[] { renderer.RenderUserViewModel(content, dict, template) };
break;
case TemplateType.XamlControl:
toWrite = renderer.RenderUserControlXaml(content, dict, template);
break;
case TemplateType.XamlCodeBehind:
toWrite = renderer.RenderUserControlCodeBehind(content, dict, template);
break;
}
foreach (var v in toWrite) {
using (var sw = new StreamWriter(File.OpenWrite(v.Item1))) {
Console.WriteLine("Writing {0}", v.Item1);
sw.Write(v.Item2);
}
}
return 0;
}
static void removeExtraneousPowerShellParameter(List<string> rest)
{
if (rest == null || !rest.Any()) return;
var currentExe = new FileInfo(Assembly.GetExecutingAssembly().Location).FullName;
if(rest.RemoveAll(r => r.ToLowerInvariant() == currentExe.ToLowerInvariant()) > 0)
Debug.WriteLine("The name of the invoked executable was pass as the first argument; " +
"Powershell's call operator (&) does this.");
}
}
}