-
Notifications
You must be signed in to change notification settings - Fork 0
/
PubCode.SubApp_v3.cs
402 lines (356 loc) · 14.6 KB
/
PubCode.SubApp_v3.cs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// !! FChecksum: 379BC9F2D32B45891C4500A31CB9C0F8A9D4D9DEFE131976CDE6DFB4800FBBBDCF3E2390F6CB3383400CEA1834732FC7CF41D61D9856B6645372587C20379632
//Official Location: https://github.com/andreboe/PubCode/blob/main/PubCode.SubApp_v3.cs
//DO NOT MODIFY. THIS FILE IS FROZEN. IF NEED BE, A NEW STANDALONE VERSION WILL BE ISSUED.
//////////////////////////////////////////////////////////////////////////////////////////
//ALL USAGE OF THIS FILE IN PROJECTS SHALL BE DONE BY LINKING THE FILE. NOT COPYING IT.
//////////////////////////////////////////////////////////////////////////////////////////
#region License; This is free and unencumbered software released into the public domain.
/// This is free and unencumbered software released into the public domain.
///
/// Anyone is free to copy, modify, publish, use, compile, sell, or
/// distribute this software, either in source code form or as a compiled
/// binary, for any purpose, commercial or non-commercial, and by any
/// means.
///
/// In jurisdictions that recognize copyright laws, the author or authors
/// of this software dedicate any and all copyright interest in the
/// software to the public domain. We make this dedication for the benefit
/// of the public at large and to the detriment of our heirs and
/// successors. We intend this dedication to be an overt act of
/// relinquishment in perpetuity of all present and future rights to this
/// software under copyright law.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
/// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
/// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
/// OTHER DEALINGS IN THE SOFTWARE.
///
/// For more information, please refer to <https://unlicense.org/>
#endregion
using PubCode.AppParams_v3;
using PubCode.ConsoleMgr_v1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
namespace PubCode.SubApp_v3 //v2021.5.2.1
{
internal class SubAppAttribute : Attribute
{
internal string Cmd { get; }
internal SubAppAttribute(string cmd)
{
Cmd = cmd;
}
internal static bool isAllowedToRun(Type type, bool isDefaultSubApp, ref string[] args)
{
var myAttr_Cmd = new List<string>();
foreach (var attr in Attribute.GetCustomAttributes(type))
myAttr_Cmd.Add(((dynamic)attr).Cmd);
if (myAttr_Cmd.Count != 1) throw new NotImplementedException("myAttr_Cmd.Count != 1");
var myAttr_Cmd_Singular = myAttr_Cmd[0];
bool enableSubApp;
if (isDefaultSubApp)
enableSubApp = true;
else
{
enableSubApp = false;
using (var tmpAppParams = new AppParams(args))
{
if (tmpAppParams.ParamLess != null)
{
foreach (var paramTxt in tmpAppParams.ParamLess.ParamData)
{
if (paramTxt.Equals(myAttr_Cmd_Singular, StringComparison.OrdinalIgnoreCase))
{
var args_asList = new List<string>();
bool adjustedArgs = false;
for (var i = 0; i < args.Length; i++)
{
if (!adjustedArgs && args[i].Equals(myAttr_Cmd_Singular, StringComparison.OrdinalIgnoreCase))
adjustedArgs = true;
else
args_asList.Add(args[i]);
}
if (!adjustedArgs)
Console.Error.WriteLine("Error during parsing of parameters");
else
{
enableSubApp = true;
args = args_asList.ToArray();
}
break;
}
}
}
}
}
return enableSubApp;
}
internal static void RunAppropriateApp(string[] args, ConsoleMgr myConsole, ResourceManager myResourceManager) => RunAppropriateApp(null, args, myConsole, myResourceManager);
internal static void RunAppropriateApp(string defaultApp, string[] args, ConsoleMgr myConsole, ResourceManager myResourceManager)
{
//ResourceManager myResourceManager = new ResourceManager(typeof(AppResources).ToString(), typeof(AppResources).Assembly);
var constructors = new List<Type>();
foreach (Type type in System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(myType => myType.Name.EndsWith("SubApp", StringComparison.OrdinalIgnoreCase))
)
foreach (var prop in type.GetCustomAttributes(typeof(SubAppAttribute), inherit: false))
constructors.Add(type);
var constructors_Default = new List<Type>();
var constructors_NotDefault = new List<Type>();
foreach (var type in constructors)
{
var myAttr_Cmd = new List<string>();
foreach (var attr in Attribute.GetCustomAttributes(type))
myAttr_Cmd.Add(((dynamic)attr).Cmd);
if (myAttr_Cmd.Count != 1) throw new NotImplementedException("myAttr_Cmd.Count != 1");
var myAttr_Cmd_Singular = myAttr_Cmd[0];
if (myAttr_Cmd_Singular.Equals(defaultApp, StringComparison.OrdinalIgnoreCase))
constructors_Default.Add(type);
else
constructors_NotDefault.Add(type);
}
bool done = false;
foreach (var type in constructors_NotDefault)
if (!done)
done = (bool)type.GetMethod("Try").Invoke(null, new object[] { false, args, myConsole, myResourceManager });
foreach (var type in constructors_Default)
if (!done)
done = (bool)type.GetMethod("Try").Invoke(null, new object[] { true, args, myConsole, myResourceManager });
if (!done)
myConsole.myError.WriteLine("Unable to start application engine");
}
}
internal class SubCmdAttribute : Attribute
{
internal string AppCmd { get; }
internal SubCmdAttribute(string appCmd)
{
AppCmd = appCmd;
}
}
internal abstract class AppCmdBase : IDisposable
{
internal abstract string ParamTXTRaw { get; }
internal abstract string ParamHelpTXT { get; }
internal bool isParamTXT(AppParams myParams)
{
bool ret = false;
if (myParams.Params != null)
foreach (var param in myParams.Params)
if (isParamTXT(param.ParamKey))
ret = true;
return ret;
}
internal bool isParamTXT(string[] args)
{
bool ret = false;
foreach (var arg in args)
if (isParamTXT(arg))
ret = true;
return ret;
}
internal bool isParamTXT(string paramTXT)
{
if (string.IsNullOrWhiteSpace(paramTXT) && string.IsNullOrWhiteSpace(ParamTXTRaw))
return true;
else if (string.IsNullOrWhiteSpace(paramTXT))
return false;
else if (string.IsNullOrWhiteSpace(ParamTXTRaw))
return false;
else
return paramTXT.Equals(ParamTXT_Slash, StringComparison.OrdinalIgnoreCase) || paramTXT.Equals(ParamTXT_Dash, StringComparison.OrdinalIgnoreCase);
}
private string getRaw()
{
var txt = ParamTXTRaw;
if (txt == null)
return null;
else
{
if (txt.StartsWith("-") || txt.StartsWith("/"))
{
txt = txt.Substring(1);
}
return txt;
}
}
internal string ParamTXT_Slash { get { return "/" + getRaw(); } }
internal string ParamTXT_Dash { get { return "-" + getRaw(); } }
public void Dispose() { }
}
internal abstract class AppCmdRegBase : AppCmdBase
{
internal abstract void ExecuteAppCmd();
}
internal class AppCmd_AppParams_ConsoleMgr_Param
{
internal AppParams myParams { get; }
internal ConsoleMgr myConsole { get; }
internal AppCmd_AppParams_ConsoleMgr_Param(AppParams myParams, ConsoleMgr myConsole)
{
this.myParams = myParams;
this.myConsole = myConsole;
}
}
internal abstract class AppCmd_AppParams_ConsoleMgr_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(AppCmd_AppParams_ConsoleMgr_Param param)
{
bool ret = false;
if (param.myParams.Params != null)
{
foreach (var tparam in param.myParams.Params)
if (isParamTXT(tparam.ParamKey))
{
ExecuteAppCmd(param);
ret = true;
}
}
else
{
ExecuteAppCmd(param);
ret = true;
}
return ret;
}
internal new bool isParamTXT(AppParams myParams)
{
bool ret = false;
if (myParams.Params != null)
foreach (var param in myParams.Params)
if (isParamTXT(param.ParamKey))
ret = true;
return ret;
}
internal abstract void ExecuteAppCmd(AppCmd_AppParams_ConsoleMgr_Param param);
}
internal class AppCmd_stringAry1_Param
{
internal string[] arg_fileList_aryOfString { get; }
internal AppCmd_stringAry1_Param(string[] arg_fileList_aryOfString)
{
this.arg_fileList_aryOfString = arg_fileList_aryOfString;
}
}
internal abstract class AppCmd_stringAry1_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(string arg_command, AppCmd_stringAry1_Param param)
{
if (!isParamTXT(arg_command))
return false;
else
{
ExecuteAppCmd(param);
return true;
}
}
internal void ExecuteAppCmd(AppCmd_stringAry1_Param param)
{
if (param.arg_fileList_aryOfString != null)
foreach (var arg_row_fileName in param.arg_fileList_aryOfString)
ExecuteAppCmd(arg_row_fileName);
}
internal abstract void ExecuteAppCmd(string arg_row_fileName);
}
internal class AppCmd_stringAry2_Param
{
internal string[] arg_fileList { get; }
internal AppCmd_stringAry2_Param(string[] arg_fileList)
{
this.arg_fileList = arg_fileList;
}
}
internal abstract class AppCmd_stringAry2_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(string arg_command, AppCmd_stringAry2_Param param)
{
if (!isParamTXT(arg_command))
return false;
else
{
ExecuteAppCmd(param);
return true;
}
}
internal abstract void ExecuteAppCmd(AppCmd_stringAry2_Param param);
}
internal class AppCmd_AryOfTuplestringstring_Param
{
internal Tuple<string, string>[] arg_fileList_Tuple { get; }
internal AppCmd_AryOfTuplestringstring_Param(Tuple<string, string>[] arg_fileList_Tuple)
{
this.arg_fileList_Tuple = arg_fileList_Tuple;
}
}
internal abstract class AppCmd_AryOfTuplestringstring_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(string arg_command, AppCmd_AryOfTuplestringstring_Param param)
{
if (!isParamTXT(arg_command))
return false;
else
{
ExecuteAppCmd(param);
return true;
}
}
internal abstract void ExecuteAppCmd(AppCmd_AryOfTuplestringstring_Param param);
}
internal abstract class AppCmd05Base : AppCmdBase
{
internal abstract string ExecuteAppCmd();
internal abstract string ReturnCmd { get; }
}
internal class AppCmd_errorCrumb_Param
{
internal string errorCrumb { get; }
internal AppCmd_errorCrumb_Param(string errorCrumb)
{
this.errorCrumb = errorCrumb;
}
}
internal abstract class AppCmd_errorCrumb_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(string arg_command, AppCmd_errorCrumb_Param param)
{
if (!isParamTXT(arg_command))
return false;
else
{
ExecuteAppCmd(param);
return true;
}
}
internal abstract void ExecuteAppCmd(AppCmd_errorCrumb_Param param);
}
internal class AppCmd_ConsoleMgr_errorCrumb_Param
{
internal ConsoleMgr myConsole { get; }
internal string errorCrumb { get; }
internal AppCmd_ConsoleMgr_errorCrumb_Param(ConsoleMgr myConsole, string errorCrumb)
{
this.myConsole = myConsole;
this.errorCrumb = errorCrumb;
}
}
internal abstract class AppCmd_ConsoleMgr_errorCrumb_Base : AppCmdBase
{
internal bool isParamTXT_andExecuteIfNeeded(string arg_command, AppCmd_ConsoleMgr_errorCrumb_Param param)
{
if (!isParamTXT(arg_command))
return false;
else
{
ExecuteAppCmd(param);
return true;
}
}
internal abstract void ExecuteAppCmd(AppCmd_ConsoleMgr_errorCrumb_Param param);
}
}