this is my favorite style, though always a pain
reflect derived types, then instantiate. No registration, just derive :)
This commit is contained in:
parent
b64edf8f8a
commit
72d6e1e254
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
@ -15,7 +15,8 @@
|
|||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||||
"console": "internalConsole",
|
"console": "internalConsole",
|
||||||
"stopAtEntry": false
|
"stopAtEntry": false,
|
||||||
|
"requireExactSource": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": ".NET Core Attach",
|
"name": ".NET Core Attach",
|
||||||
|
@ -7,7 +7,7 @@ namespace newsletter
|
|||||||
public List<Reporter> Reporters { get; set; }
|
public List<Reporter> Reporters { get; set; }
|
||||||
public class Reporter
|
public class Reporter
|
||||||
{
|
{
|
||||||
public string Type { get; set; }
|
public string ReporterClass { get; set; }
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
48
Program.cs
48
Program.cs
@ -24,7 +24,7 @@ namespace newsletter
|
|||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
var conf = Configure();
|
var conf = Configure();
|
||||||
if(conf == null) return;
|
if (conf == null) return;
|
||||||
|
|
||||||
var reports = await CollectReports(CollectReporters(conf));
|
var reports = await CollectReports(CollectReporters(conf));
|
||||||
|
|
||||||
@ -35,14 +35,14 @@ namespace newsletter
|
|||||||
|
|
||||||
static Configuration Configure(string configurationPath = "appsettings.json")
|
static Configuration Configure(string configurationPath = "appsettings.json")
|
||||||
{
|
{
|
||||||
if(!File.Exists(configurationPath))
|
if (!File.Exists(configurationPath))
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"could not find configuration at {configurationPath}! copying sample to that spot.");
|
Console.Error.WriteLine($"could not find configuration at {configurationPath}! copying sample to that spot.");
|
||||||
File.Copy("sample-appsettings.json", configurationPath); //and you know what, if that explodes at the OS level, the OS should give you an error
|
File.Copy("sample-appsettings.json", configurationPath); //and you know what, if that explodes at the OS level, the OS should give you an error
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var fileContents = File.ReadAllText(configurationPath);
|
var fileContents = File.ReadAllText(configurationPath);
|
||||||
if(string.IsNullOrWhiteSpace(fileContents))
|
if (string.IsNullOrWhiteSpace(fileContents))
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
|
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
|
||||||
File.Copy("sample-appsettings.json", configurationPath, true);
|
File.Copy("sample-appsettings.json", configurationPath, true);
|
||||||
@ -51,7 +51,7 @@ namespace newsletter
|
|||||||
|
|
||||||
var conf = JsonConvert.DeserializeObject<Configuration>(fileContents);
|
var conf = JsonConvert.DeserializeObject<Configuration>(fileContents);
|
||||||
|
|
||||||
if(conf == null)
|
if (conf == null)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
|
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
|
||||||
File.Copy("sample-appsettings.json", configurationPath, true);
|
File.Copy("sample-appsettings.json", configurationPath, true);
|
||||||
@ -63,14 +63,47 @@ namespace newsletter
|
|||||||
|
|
||||||
static List<Task<Report>> CollectReporters(Configuration conf)
|
static List<Task<Report>> CollectReporters(Configuration conf)
|
||||||
{
|
{
|
||||||
//TODO
|
var toReturn = new List<Task<Report>>();
|
||||||
throw new NotImplementedException();
|
|
||||||
|
var reporterTypes = AppDomain.CurrentDomain.GetAssemblies()
|
||||||
|
.SelectMany(domainAssembly => domainAssembly.GetTypes())
|
||||||
|
.Where(type => type.IsSubclassOf(typeof(newsletter.Reporters.Reporter)) && !type.IsAbstract)
|
||||||
|
.ToList();
|
||||||
|
var skipPortionInName = "newsletter.Reporters.".Length;
|
||||||
|
|
||||||
|
|
||||||
|
foreach(var discoveredConfType in conf.Reporters.Select(r => r.ReporterClass))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{discoveredConfType} configuration type discovered");
|
||||||
|
}
|
||||||
|
foreach (var reporterType in reporterTypes)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{reporterType.ToString().Substring(skipPortionInName).ToLower()} reporter class discovered");
|
||||||
|
Func<Configuration.Reporter, bool> predicate = r => r.ReporterClass == reporterType.ToString().Substring(skipPortionInName).ToLower();
|
||||||
|
if (!conf.Reporters.Any(predicate))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"no configurations for it");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var configurationsFor = conf.Reporters.Where(predicate);
|
||||||
|
Console.WriteLine($"{configurationsFor.Count()} configurations for it");
|
||||||
|
|
||||||
|
foreach (var confFor in configurationsFor)
|
||||||
|
{
|
||||||
|
var instance = (newsletter.Reporters.Reporter) Activator.CreateInstance(reporterType);
|
||||||
|
toReturn.Add(instance.Report(confFor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async Task<List<Report>> CollectReports(List<Task<Report>> tasks)
|
static async Task<List<Report>> CollectReports(List<Task<Report>> tasks)
|
||||||
{
|
{
|
||||||
var results = new List<Report>();
|
var results = new List<Report>();
|
||||||
foreach(var task in tasks)
|
foreach (var task in tasks)
|
||||||
{
|
{
|
||||||
var rep = await task;
|
var rep = await task;
|
||||||
results.Add(rep);
|
results.Add(rep);
|
||||||
@ -81,6 +114,7 @@ namespace newsletter
|
|||||||
static HtmlDocument AssembleHTML(IEnumerable<HtmlNode> reportSections)
|
static HtmlDocument AssembleHTML(IEnumerable<HtmlNode> reportSections)
|
||||||
{
|
{
|
||||||
//TODO
|
//TODO
|
||||||
|
Console.WriteLine($"assembling {reportSections.Count()} sections");
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ namespace newsletter.Reporters
|
|||||||
public class Dummy : Reporter
|
public class Dummy : Reporter
|
||||||
{
|
{
|
||||||
#pragma warning disable CS1998
|
#pragma warning disable CS1998
|
||||||
public async Task<Report> Report(Configuration.Reporter config)
|
public override async Task<Report> Report(Configuration.Reporter config)
|
||||||
{
|
{
|
||||||
return new Report()
|
return new Report()
|
||||||
{
|
{
|
||||||
|
@ -2,8 +2,8 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace newsletter.Reporters
|
namespace newsletter.Reporters
|
||||||
{
|
{
|
||||||
public interface Reporter
|
public abstract class Reporter
|
||||||
{
|
{
|
||||||
Task<Report> Report(Configuration.Reporter config);
|
public abstract Task<Report> Report(Configuration.Reporter config);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user