Skip to content

Commit 347ccbe

Browse files
committed
Refactor
1 parent 841c97f commit 347ccbe

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

src/InEngine.Core/Plugin.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34

45
namespace InEngine.Core
@@ -12,5 +13,13 @@ public Plugin(Assembly assembly)
1213
{
1314
Assembly = assembly;
1415
}
16+
17+
public IOptions MakeOptions()
18+
{
19+
var optionType = Assembly.GetTypes().FirstOrDefault(x => x.IsClass && typeof(IOptions).IsAssignableFrom(x));
20+
if (optionType == null)
21+
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
22+
return Assembly.CreateInstance(optionType.FullName) as IOptions;
23+
}
1524
}
1625
}

src/InEngine.Net.userprefs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<Properties StartupConfiguration="{DF1A45FD-4887-4D65-8BAD-EE17B70FE79D}|Default">
22
<MonoDevelop.Ide.ItemProperties.InEngineScheduler PreferredExecutionTarget="MonoDevelop.Default" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="InEngineCli/ArgumentInterpreter.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="InEngine.Commands/Options.cs">
44
<Files>
5-
<File FileName="InEngineCli/ArgumentInterpreter.cs" Line="25" Column="1" />
6-
<File FileName="InEngine.Commands/Options.cs" Line="19" Column="25" />
7-
<File FileName="InEngineCli/Options.cs" Line="20" Column="1" />
8-
<File FileName="InEngine.Core/Plugin.cs" Line="17" Column="1" />
5+
<File FileName="InEngineCli/ArgumentInterpreter.cs" Line="46" Column="39" />
6+
<File FileName="InEngine.Core/Plugin.cs" Line="15" Column="10" />
7+
<File FileName="InEngine.Commands/Options.cs" Line="10" Column="22" />
98
</Files>
109
<Pads>
1110
<Pad Id="ProjectPad">
1211
<State name="__root__">
1312
<Node name="InEngine.Net" expanded="True">
14-
<Node name="InEngine.Commands" expanded="True" />
15-
<Node name="InEngine.Core" expanded="True" />
16-
<Node name="InEngineCli" expanded="True">
17-
<Node name="ArgumentInterpreter.cs" selected="True" />
13+
<Node name="InEngine.Commands" expanded="True">
14+
<Node name="Options.cs" selected="True" />
1815
</Node>
16+
<Node name="InEngine.Core" expanded="True" />
17+
<Node name="InEngineCli" expanded="True" />
1918
<Node name="InEngineScheduler" expanded="True" />
2019
</Node>
2120
</State>

src/InEngineCli/ArgumentInterpreter.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ public ArgumentInterpreter()
2020

2121
public void Interpret(string[] args)
2222
{
23+
var plugins = FindPlugins();
2324
if (!args.Any())
2425
{
2526
Console.WriteLine("Available plugins... ");
26-
FindPlugins().ForEach(x => Console.WriteLine(x.Name));
27+
plugins.ForEach(x => Console.WriteLine(x.Name));
2728
ExitWithSuccess();
2829
}
2930

@@ -34,23 +35,11 @@ public void Interpret(string[] args)
3435
{
3536
if (options == null)
3637
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
37-
38-
// Get plugins.
39-
// If no arg is specified, list plugins
40-
41-
// Get possible types from plugin assembly.
42-
var targetAssembly = Assembly.LoadFrom(options.PlugInName + ".dll");
43-
var types = targetAssembly.GetTypes();
44-
var optionType = types.FirstOrDefault(x => x.IsClass && typeof(IOptions).IsAssignableFrom(x));
45-
if (optionType == null)
46-
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
47-
48-
// Create an instance of the plugin's options class.
49-
var pluginOptions = targetAssembly.CreateInstance(optionType.FullName) as IOptions;
50-
51-
// If the plugin's args are empty, print the plugin's help screen and exit.
38+
39+
var pluginOptions = plugins.FirstOrDefault(x => x.Name == options.PlugInName).MakeOptions();
5240
var pluginArgs = args.Skip(1).ToArray();
5341
if (!pluginArgs.ToList().Any()) {
42+
// If the plugin's args are empty, print the plugin's help screen and exit.
5443
CommandLine.Parser.Default.ParseArguments(pluginArgs, pluginOptions);
5544
Console.WriteLine(pluginOptions.GetUsage(""));
5645
ExitWithSuccess();

0 commit comments

Comments
 (0)