forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentHandler.cs
More file actions
134 lines (108 loc) · 4.86 KB
/
ArgumentHandler.cs
File metadata and controls
134 lines (108 loc) · 4.86 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
using PowerArgs;
using ScriptCs.Contracts;
using ServiceStack.Text;
using System;
using System.Linq;
using System.Reflection;
namespace ScriptCs.Argument
{
public class ArgumentHandler : IArgumentHandler
{
private readonly IArgumentParser _argumentParser;
private readonly IConfigFileParser _configFileParser;
private readonly IFileSystem _fileSystem;
public ArgumentHandler(IArgumentParser argumentParser, IConfigFileParser configFileParser, IFileSystem fileSystem)
{
Guard.AgainstNullArgument("argumentParser", argumentParser);
Guard.AgainstNullArgument("configFileParser", configFileParser);
Guard.AgainstNullArgument("fileSystem", fileSystem);
_fileSystem = fileSystem;
_argumentParser = argumentParser;
_configFileParser = configFileParser;
}
public ArgumentParseResult Parse(string[] args)
{
var sr = SplitScriptArgs(args);
var commandArgs = _argumentParser.Parse(sr.CommandArguments);
var configArgs = _configFileParser.Parse(GetFileContent(commandArgs != null ? commandArgs.Config : null));
var finalArguments = ReconcileArguments(configArgs, commandArgs, sr);
return new ArgumentParseResult(args, finalArguments, sr.ScriptArguments);
}
private string GetFileContent(string fileName)
{
string filePath = _fileSystem.CurrentDirectory + '\\' + fileName;
if (_fileSystem.FileExists(filePath))
{
return _fileSystem.ReadFile(filePath);
}
return null;
}
public static SplitResult SplitScriptArgs(string[] args)
{
Guard.AgainstNullArgument("args", args);
var result = new SplitResult() { CommandArguments = args };
// Split the arguments list on "--".
// The arguments before the "--" (or all arguments if there is no "--") are
// for ScriptCs.exe, and the arguments after that are for the csx script.
int separatorLocation = Array.IndexOf(args, "--");
int scriptArgsCount = separatorLocation == -1 ? 0 : args.Length - separatorLocation - 1;
result.ScriptArguments = new string[scriptArgsCount];
Array.Copy(args, separatorLocation + 1, result.ScriptArguments, 0, scriptArgsCount);
if (separatorLocation != -1)
result.CommandArguments = args.Take(separatorLocation).ToArray();
return result;
}
private static ScriptCsArgs ReconcileArguments(ScriptCsArgs configArgs, ScriptCsArgs commandArgs, SplitResult splitResult)
{
if (configArgs == null)
return commandArgs;
if (commandArgs == null)
return configArgs;
foreach (var property in typeof(ScriptCsArgs).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
var configValue = property.GetValue(configArgs);
var commandValue = property.GetValue(commandArgs);
var defaultValue = GetPropertyDefaultValue(property);
if (!object.Equals(configValue, commandValue))
{
if (!object.Equals(commandValue, defaultValue))
{
property.SetValue(configArgs, commandValue);
}
else
{
if (IsCommandLinePresent(splitResult.CommandArguments, property))
property.SetValue(configArgs, commandValue);
}
}
}
return configArgs;
}
private static bool IsCommandLinePresent(string[] args, PropertyInfo property)
{
bool attributeFound = false;
var attribute = property.GetCustomAttribute<ArgShortcut>();
if (attribute != null)
attributeFound = args.Any(a => a.Contains((attribute as ArgShortcut).Shortcut));
var result = args.Any(a => a.Contains(property.Name)) || attributeFound;
return result;
}
private static object GetPropertyDefaultValue(PropertyInfo property)
{
var defaultAttribute = property.GetCustomAttribute<DefaultValueAttribute>();
return defaultAttribute != null
? ((PowerArgs.DefaultValueAttribute)defaultAttribute).Value
: property.PropertyType.GetDefaultValue();
}
public class SplitResult
{
public SplitResult()
{
CommandArguments = new string[0];
ScriptArguments = new string[0];
}
public string[] CommandArguments { get; set; }
public string[] ScriptArguments { get; set; }
}
}
}