-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuantowerIndicatorDeployUtils.cs
More file actions
330 lines (263 loc) · 10.3 KB
/
QuantowerIndicatorDeployUtils.cs
File metadata and controls
330 lines (263 loc) · 10.3 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using System.Diagnostics;
namespace JollyWizard.Quantower.CLI.IndicatorDeploy.Helpers;
public class MAGIC_VALUES
{
public const string CONFIG_FILE_NAME = "deploy.ini";
public static string[] CONFIG_FILE_PATTERNS => new string[] {
// Pattern for in executing directory.
$"*{CONFIG_FILE_NAME}"
/*@TODO Does this make first entry redundant?*/
, $"**/*{CONFIG_FILE_NAME}"
};
}
public class Utils
{
/// <summary>
/// Reads a config INI file and converts it into a plan.
/// @TODO Support multiple plans.
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public static List<CopyPlan> ProcessConfigFile(String? filepath)
{
List<CopyPlan> copyPlans = new ();
CopyPlan currentPlan = new();
if (filepath == null || !File.Exists(filepath)) return copyPlans;
foreach (String line in File.ReadLines(filepath))
{
// Ignore comments and blank lines.
if (line.StartsWith("#") || line.Trim().Length == 0) continue;
// "Key=Value" => ["Key", "Value"]
String[] keyvalue = line.Split('=');
//@TODO verify no edge cases missed.
if (keyvalue.Length != 2) continue;
string key = keyvalue[0];
string value = keyvalue[1];
/*
* Process Each Key as a command, directing how to use value.
*/
switch (key)
{
case "~":
case "INPUT_ROOT":
case "ROOT":
// Collect previous plan if has configuration.
if (currentPlan.IncludePatterns.Count > 0)
copyPlans.Add(currentPlan);
currentPlan = new ();
currentPlan.SourcesRoot = value;
break;
case "+":
case "INCLUDE":
currentPlan.IncludePatterns.Add(value);
break;
case "-":
case "EXCLUDE":
currentPlan.ExcludePatterns.Add(value);
break;
case "?":
case "SLUG":
currentPlan.OutputSlug = value;
break;
case "@":
case "TARGET":
case "OUTPUT":
case "OUTPUT_ROOT":
currentPlan.OutputRoot = value;
break;
}
}
// Don't forget the open target.
copyPlans.Add(currentPlan);
return copyPlans;
}
public static List<String> DetectQidConfigFiles()
{
// Where to search for the config.
// @TODO make sure that relative path here leads to consistent absolute results in the CopyPlan
String dirPath = ".\\";
CopyPlan plan = CopyPlans.ConfigFilePlan();
plan.SourcesRoot = dirPath;
List<String>? ConfigPaths = plan?.SourcesAbsolutePaths;
return ConfigPaths ?? new();
}
public static void Test()
{
List<CopyPlan> plans = new();
foreach (String configPath in DetectQidConfigFiles())
{
plans.AddRange(ProcessConfigFile(configPath));
}
Console.WriteLine(plans);
}
}
public class CopyPlans
{
public static CopyPlan ConfigFilePlan()
{
CopyPlan plan = new();
plan.IncludePatterns.AddRange(MAGIC_VALUES.CONFIG_FILE_PATTERNS);
return plan;
}
}
public class CopyPlan
{
#region Instance Properties
public String? SourcesRoot { get; set; } = Directory.GetCurrentDirectory();
public List<String> IncludePatterns = new();
public List<String> ExcludePatterns = new();
public String? OutputRoot { get; set; }
public String? OutputSlug { get; set; }
#endregion
public String OutputRelativeBase => (this.OutputSlug is null ? "" : this.OutputSlug );
public String? OutputRelativePath(String? relativePath)
{
return Path.Combine(this.OutputRelativeBase, relativePath ?? "");
}
public static String? ResolveAbsoluePath(String? root, String? relative, bool ResolveEnvironmentVariables = true)
{
if (root is null || relative is null) return null;
if (ResolveEnvironmentVariables)
{
root = Environment.ExpandEnvironmentVariables(root);
relative = Environment.ExpandEnvironmentVariables(relative);
}
String CombinedPath = Path.Combine(root, relative ?? "");
return Path.GetFullPath(CombinedPath);
}
public String? SourcesAbsolutePath(String? relativePath) => ResolveAbsoluePath(this.SourcesRoot, relativePath);
public String? OutputAbsolutePath(String? relativePath) => ResolveAbsoluePath(this.OutputRoot, relativePath);
public List<CopyOrder> CreateCopyOrders()
{
List<CopyOrder> copyOrders = new();
foreach (String sourceRelativePath in this.SourcesRelativePaths ?? new())
{
CopyOrder co = new()
{
SourcePath = this.SourcesAbsolutePath(sourceRelativePath)
, TargetPath = this.OutputAbsolutePath(this.OutputRelativePath(sourceRelativePath))
};
copyOrders.Add(co);
}
return copyOrders;
}
/// <summary>
/// Gets the canonical path to `SourcesRoot`.
/// </summary>
public String? SourcesRootAbsolutePath => this.SourcesRoot == null ? null : Path.GetFullPath(this.SourcesRoot);
/// <summary>
/// Checks if `SourcesRootAbsoluePath` is a valid directory.
/// </summary>
public Boolean SourcesRootExists => this.SourcesRootAbsolutePath is not null && Directory.Exists(this.SourcesRoot);
/// <summary>
/// Version of the SourceRoot that can be fed into `Matcher.Execute(DirectoryInfoWrapper)`.
/// </summary>
public DirectoryInfoWrapper? SourcesRootMatchable => this.SourcesRoot is null ? null : new(new(this.SourcesRoot));
/// <summary>
/// Gets a `Matcher` that is configured with the inclusion and exclusion patterns.
///
/// Used to match the source files when applied to the root.
/// </summary>
public Matcher? SourcesMatcher
{
get
{
if (this.SourcesRootMatchable is null) return null;
Matcher m = new();
m.AddIncludePatterns(this.IncludePatterns);
m.AddExcludePatterns(this.ExcludePatterns);
return m;
}
}
public PatternMatchingResult? SourcesMatcherResult => this.SourcesMatcher?.Execute(this.SourcesRootMatchable);
/// <summary>
/// Takes a path and resolves it relative to `SourcesRoot`.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public String? GetPathRelativeToSourcesRoot(String? path)
{
if (
!this.SourcesRootExists // Ensures dir.exists, but doesn't reassure compiler.
|| this.SourcesRootAbsolutePath is null // Assure compiler for below.
|| path is null
) return null;
return GetRelativePath(this.SourcesRootAbsolutePath, path);
}
private static String GetRelativePath(String? a, String? b)
{
Uri path1 = new Uri(a);
Uri path2 = new Uri(b);
Uri diff = path1.MakeRelativeUri(path2);
return diff.OriginalString;
}
/// <summary>
/// Get the absolute paths of all source matcher results.
/// </summary>
public List<String>? SourcesAbsolutePaths => this.SourcesMatcher?.GetResultsInFullPath(this.SourcesRoot).ToList();
/// <summary>
/// @TODO Convert the matcher results into relative paths.
/// </summary>
public List<String>? SourcesRelativePaths =>
this.SourcesAbsolutePaths?.Select(absPath => this.GetPathRelativeToSourcesRoot(absPath)).OfType<String>().ToList();
public override string ToString()
{
String[] r = new string[]
{
"~=" + this.SourcesRoot ?? ""
, "+=" + String.Join(";", this.IncludePatterns)
, "-=" + String.Join(";", this.ExcludePatterns)
, this.OutputRoot is null ? "" : "@=" + this.OutputRoot
, this.OutputSlug is null ? "" : "?=" + this.OutputSlug
};
return "[" + String.Join("|", r) + "] (" + this.SourcesRootAbsolutePath + ") {" + this.SourcesAbsolutePaths?.Count + "}";
}
}
public class CopyOrder
{
//@TODO. Figure out how to handle empty directories.
public String? SourcePath;
public String? TargetPath;
public bool Execute(bool Force = true)
{
// Can trim this more for helpful exceptions.
if (!this.IsSourceAvailable())
return false;
else if (this.TargetPath is null || this.SourcePath is null)
return false;
else if (this.IsTargetConflict() && !Force)
return false;
try
{
// Already filtered for Force, but just to be clear.
if (this.IsTargetConflict() && Force)
File.Delete(this.TargetPath);
// Short circuit of input null; should generate exception anyway if the Path function fails..
Directory.CreateDirectory(Path.GetDirectoryName(this.TargetPath) ?? "");
File.Copy(this.SourcePath, this.TargetPath);
return true;
}
catch (Exception e)
{
Console.Error.WriteLine($"<Copy-Error Message=\"{e.Message}\" />");
return false;
}
}
public bool IsSourceAvailable()
{
if (this.SourcePath is null) return false;
return File.Exists(this.SourcePath);
}
public bool IsTargetConflict()
{
if (this.TargetPath is null) return true;
return File.Exists(this.TargetPath);
}
public override string ToString()
{
return $"<CopyOrder \"{this.SourcePath}\" > \"{this.TargetPath}\" />";
}
}