-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (118 loc) · 5.72 KB
/
Program.cs
File metadata and controls
144 lines (118 loc) · 5.72 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using Common;
using log4net;
using ServiceStack;
namespace PluginPackager
{
public class Program
{
private static ILog _log;
public static void Main(string[] args)
{
Environment.ExitCode = 1;
try
{
ConfigureLogging();
var context = ParseArgs(args);
new Program(context).Run();
Environment.ExitCode = 0;
}
catch (ExpectedException exception)
{
_log.Error(exception.Message);
}
catch (Exception exception)
{
_log.Error("Unhandled exception", exception);
}
}
private static void ConfigureLogging()
{
using (var stream = new MemoryStream(LoadEmbeddedResource("log4net.config")))
using (var reader = new StreamReader(stream))
{
var xml = new XmlDocument();
xml.LoadXml(reader.ReadToEnd());
log4net.Config.XmlConfigurator.Configure(xml.DocumentElement);
_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
}
}
private static byte[] LoadEmbeddedResource(string path)
{
// ReSharper disable once PossibleNullReferenceException
var resourceName = $"{MethodBase.GetCurrentMethod().DeclaringType.Namespace}.{path}";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new ExpectedException($"Can't load '{resourceName}' as embedded resource.");
return stream.ReadFully();
}
}
private static Context ParseArgs(string[] args)
{
var context = new Context();
var options = new[]
{
new CommandLineOption {Key = nameof(context.AssemblyFolder), Setter = value => context.AssemblyFolder = value, Getter = () => context.AssemblyFolder, Description = "Path to the folder containing the plugin."},
new CommandLineOption {Key = nameof(context.AssemblyPath), Setter = value => context.AssemblyPath = value, Getter = () => context.AssemblyPath, Description = "Path to the plugin assembly."},
new CommandLineOption {Key = nameof(context.OutputPath), Setter = value => context.OutputPath = value, Getter = () => context.OutputPath, Description = "Path to packaged output file."},
new CommandLineOption {Key = nameof(context.DeployedFolderName), Setter = value => context.DeployedFolderName = value, Getter = () => context.DeployedFolderName, Description = "Name of the deployed folder"},
new CommandLineOption {Key = nameof(context.Description), Setter = value => context.Description = value, Getter = () => context.Description, Description = "Description of the plugin"},
new CommandLineOption {Key = nameof(context.AssemblyQualifiedTypeName), Setter = value => context.AssemblyQualifiedTypeName = value, Getter = () => context.AssemblyQualifiedTypeName, Description = "Assembly-qualified type name of the plugin"},
new CommandLineOption {Key = nameof(context.Subfolders), Setter = value => context.Subfolders = bool.Parse(value), Getter = () => context.Subfolders.ToString(), Description = "Include all subfolders"},
new CommandLineOption {Key = nameof(context.Include), Setter = value => AddToList(value, context.Include), Getter = () => string.Join(", ", context.Include), Description = "Include file or DOS wildcard pattern"},
new CommandLineOption {Key = nameof(context.Exclude), Setter = value => AddToList(value, context.Exclude), Getter = () => string.Join(", ", context.Exclude), Description = "Exclude file or DOS wildcard pattern"},
};
var usageMessage = CommandLineUsage.ComposeUsageText(
"Package a field data plugin into a deployable .plugin file.", options);
var optionResolver = new CommandLineOptionResolver();
optionResolver.Resolve(args, options, usageMessage, arg => ResolvePositionalArgument(context, arg));
if (string.IsNullOrEmpty(context.AssemblyFolder) && string.IsNullOrEmpty(context.AssemblyPath))
throw new ExpectedException($"You must specify at least one /{nameof(context.AssemblyPath)} or /{nameof(context.AssemblyFolder)} option.");
if (string.IsNullOrEmpty(context.OutputPath))
throw new ExpectedException($"You must specify the /{nameof(context.OutputPath)} option.");
return context;
}
private static bool ResolvePositionalArgument(Context context, string arg)
{
if (Directory.Exists(arg))
{
context.AssemblyFolder = arg;
return true;
}
if (File.Exists(arg))
{
context.AssemblyPath = arg;
return true;
}
return false;
}
private static void AddToList(string value, List<string> values)
{
if (value.Equals("~"))
{
values.Clear();
}
else
{
values.Add(value);
}
}
private readonly Context _context;
public Program(Context context)
{
_context = context;
}
private void Run()
{
new Packager
{
Context = _context
}.CreatePackage();
}
}
}