Skip to content

Commit de5fe6a

Browse files
committed
Add plugin option
1 parent 5a6f16b commit de5fe6a

File tree

13 files changed

+115
-67
lines changed

13 files changed

+115
-67
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using CommandLine;
2+
using InEngine.Commands.Sample;
3+
using InEngine.Core;
4+
5+
namespace InEngine.Commands
6+
{
7+
public class Options : IOptions
8+
{
9+
[VerbOption("sample:minimal")]
10+
public Minimal Minimal { get; set; }
11+
12+
[VerbOption("sample:show-progress")]
13+
public ShowProgress ShowProgress { get; set; }
14+
15+
[VerbOption("sample:say-hello")]
16+
public SayHello SayHello { get; set; }
17+
}
18+
}

src/InEngine.Commands/InEngine.Commands.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
</PropertyGroup>
2929
<ItemGroup>
3030
<Reference Include="System" />
31-
<Reference Include="System.ComponentModel.DataAnnotations" />
31+
<Reference Include="CommandLine">
32+
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
33+
</Reference>
3234
</ItemGroup>
3335
<ItemGroup>
3436
<Compile Include="Properties\AssemblyInfo.cs" />
3537
<Compile Include="Sample\Minimal.cs" />
3638
<Compile Include="Sample\ShowProgress.cs" />
3739
<Compile Include="Sample\SayHello.cs" />
40+
<Compile Include="CommandOptions.cs" />
3841
</ItemGroup>
3942
<ItemGroup>
4043
<Folder Include="Sample\" />
@@ -45,5 +48,8 @@
4548
<Name>InEngine.Core</Name>
4649
</ProjectReference>
4750
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="packages.config" />
53+
</ItemGroup>
4854
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4955
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="CommandLineParser" version="1.9.71" targetFramework="net462" />
4+
</packages>

src/InEngine.Core/AbstractCommand.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ public virtual CommandResult Run()
3030
throw new System.NotImplementedException();
3131
}
3232

33-
public void SetProgressBarMaxTicks(int maxTicks) => ProgressBar = new ProgressBar(maxTicks);
34-
public void UpdateProgress(int tick) => ProgressBar.Refresh(tick, Name);
33+
public void SetProgressBarMaxTicks(int maxTicks)
34+
{
35+
ProgressBar = new ProgressBar(maxTicks);
36+
}
37+
38+
public void UpdateProgress(int tick)
39+
{
40+
ProgressBar.Refresh(tick, Name);
41+
}
3542

3643
public void Execute(IJobExecutionContext context)
3744
{

src/InEngine.Core/Exceptions/CommandFailedException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace InEngine.Core.Exceptions
44
{
55
public class CommandFailedException : Exception
66
{
7-
public CommandFailedException(string message) : base(message)
7+
public CommandFailedException(string message = "") : base(message)
88
{
99
}
1010

src/InEngine.Core/IOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace InEngine.Core
2+
{
3+
public interface IOptions
4+
{}
5+
}

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Queue\IBrokerConfiguration.cs" />
8888
<Compile Include="Config.cs" />
8989
<Compile Include="Queue\AbstractBrokerCommand.cs" />
90+
<Compile Include="IOptions.cs" />
9091
</ItemGroup>
9192
<ItemGroup>
9293
<None Include="packages.config" />

src/InEngine.Net.userprefs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
<Properties StartupConfiguration="{DF1A45FD-4887-4D65-8BAD-EE17B70FE79D}|Default">
22
<MonoDevelop.Ide.ItemProperties.InEngineScheduler PreferredExecutionTarget="MonoDevelop.Default" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="InEngine.Commands/Properties/AssemblyInfo.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="InEngineCli/ArgumentInterpreter.cs">
44
<Files>
5-
<File FileName="InEngine.Commands/Sample/Minimal.cs" Line="1" Column="16" />
6-
<File FileName="InEngineScheduler/Jobs.cs" Line="2" Column="19" />
7-
<File FileName="InEngineCli/Options.cs" Line="5" Column="1" />
8-
<File FileName="InEngine.Commands/Sample/ShowProgress.cs" Line="1" Column="1" />
9-
<File FileName="InEngineScheduler/InEngineServerInstaller.cs" Line="1" Column="1" />
10-
<File FileName="InEngineScheduler/Program.cs" Line="22" Column="14" />
11-
<File FileName="InEngine.Core/Properties/AssemblyInfo.cs" Line="12" Column="37" />
12-
<File FileName="InEngine.Commands/Properties/AssemblyInfo.cs" Line="1" Column="1" />
5+
<File FileName="InEngineScheduler/Jobs.cs" Line="15" Column="1" />
6+
<File FileName="InEngineCli/ArgumentInterpreter.cs" Line="72" Column="28" />
7+
<File FileName="InEngine.Core/AbstractCommand.cs" Line="40" Column="1" />
8+
<File FileName="InEngineCli/packages.config" Line="8" Column="12" />
9+
<File FileName="InEngine.Core/IOptions.cs" Line="3" Column="22" />
10+
<File FileName="InEngineCli/Program.cs" Line="7" Column="1" />
1311
</Files>
1412
<Pads>
1513
<Pad Id="ProjectPad">
1614
<State name="__root__">
1715
<Node name="InEngine.Net" expanded="True">
1816
<Node name="InEngine.Commands" expanded="True">
19-
<Node name="Properties" expanded="True">
20-
<Node name="AssemblyInfo.cs" selected="True" />
21-
</Node>
17+
<Node name="References" expanded="True" />
2218
</Node>
2319
<Node name="InEngine.Core" expanded="True">
24-
<Node name="Properties" expanded="True" />
20+
<Node name="Exceptions" expanded="True" />
21+
</Node>
22+
<Node name="InEngineCli" expanded="True">
23+
<Node name="ArgumentInterpreter.cs" selected="True" />
2524
</Node>
2625
<Node name="InEngineScheduler" expanded="True" />
2726
</Node>
@@ -36,4 +35,9 @@
3635
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
3736
<MultiItemStartupConfigurations />
3837
<MonoDevelop.Ide.ItemProperties.InEngineCli PreferredExecutionTarget="MonoDevelop.Default" />
39-
</Properties>
38+
<DisabledProjects>
39+
<String>DataIntegration.Core.Tests/DataIntegration.Core.Tests.csproj</String>
40+
<String>DataIntegration.Commands/InEngine.Commands.csproj</String>
41+
<String>DataIntegration.Core/InEngine.Core.csproj</String>
42+
</DisabledProjects>
43+
</Properties>

src/InEngineCli/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
</configuration>

src/InEngineCli/ArgumentInterpreter.cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Linq;
3+
using System.Reflection;
24
using InEngine.Core;
35
using InEngine.Core.Exceptions;
46
using NLog;
@@ -16,46 +18,63 @@ public ArgumentInterpreter()
1618

1719
public void Interpret(string[] args)
1820
{
21+
var parser = new CommandLine.Parser(with => with.IgnoreUnknownArguments = true);
1922
var options = new Options();
20-
var isSuccessful = CommandLine.Parser.Default.ParseArguments(args, options, (verb, subOptions) =>
23+
24+
if (parser.ParseArguments(args, options))
2125
{
22-
try
26+
if (options == null)
27+
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
28+
var targetAssembly = Assembly.LoadFrom(options.PlugInName + ".dll");
29+
var types = targetAssembly.GetTypes();
30+
var optionType = types.FirstOrDefault(x => typeof(IOptions).IsAssignableFrom(x));
31+
if (optionType == null)
32+
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
33+
var pluginOptions = targetAssembly.CreateInstance(optionType.FullName) as IOptions;
34+
var isSuccessful = CommandLine
35+
.Parser
36+
.Default
37+
.ParseArguments(args.Skip(1).ToArray(), pluginOptions, (verb, subOptions) =>
2338
{
24-
if (subOptions == null)
25-
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
39+
try
40+
{
41+
if (subOptions == null)
42+
ExitWithFailure(new CommandFailedException("Could not parse plugin options"));
2643

27-
var command = subOptions as ICommand;
44+
var command = subOptions as ICommand;
2845

29-
if (command is AbstractCommand)
30-
(command as AbstractCommand).Name = verb.Normalize();
46+
if (command is AbstractCommand)
47+
(command as AbstractCommand).Name = verb.Normalize();
3148

32-
var commandResult = command.Run();
49+
var commandResult = command.Run();
3350

34-
if (commandResult.IsSuccessful)
35-
ExitWithSuccess(commandResult.Message);
36-
else
37-
ExitWithFailure(new CommandFailedException(commandResult.Message));
38-
}
39-
catch (Exception exception)
40-
{
41-
ExitWithFailure(exception);
42-
}
43-
});
51+
if (commandResult.IsSuccessful)
52+
ExitWithSuccess(commandResult.Message);
53+
else
54+
ExitWithFailure(new CommandFailedException(commandResult.Message));
55+
}
56+
catch (Exception exception)
57+
{
58+
ExitWithFailure(exception);
59+
}
60+
});
4461

45-
if (!isSuccessful)
46-
ExitWithFailure();
62+
if (!isSuccessful)
63+
ExitWithFailure();
64+
}
4765
}
4866

4967
public void ExitWithSuccess(string message)
5068
{
51-
Logger.Debug("Command successful: " + message);
69+
if (string.IsNullOrWhiteSpace(message))
70+
message = "success";
71+
Logger.Debug($"✔ {message}");
5272
Environment.Exit(0);
5373
}
5474

5575
public void ExitWithFailure(Exception exception = null)
5676
{
57-
if (exception != null)
58-
Logger.Error(exception, "Command failed");
77+
Logger.Error(exception != null ? exception : new CommandFailedException(), "✘ fail");
5978
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
6079
}
6180
}

0 commit comments

Comments
 (0)