Compare commits

..

3 Commits

Author SHA1 Message Date
72d6e1e254 this is my favorite style, though always a pain
reflect derived types, then instantiate. No registration, just derive :)
2023-03-22 12:21:38 -04:00
b64edf8f8a basic structure 2023-03-22 11:03:06 -04:00
0b7bca27f7 initial
for the new era. we'll have to actually design this, this time
2023-03-22 09:32:33 -04:00
11 changed files with 307 additions and 0 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
appsettings.json
# ---> VisualStudioCode # ---> VisualStudioCode
.vscode/* .vscode/*
!.vscode/settings.json !.vscode/settings.json

27
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,27 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net7.0/newsletter.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false,
"requireExactSource": true
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

41
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/newsletter.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/newsletter.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/newsletter.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

16
Configuration.cs Normal file
View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace newsletter
{
public class Configuration
{
public string ExportPath { get; set; }
public List<Reporter> Reporters { get; set; }
public class Reporter
{
public string ReporterClass { get; set; }
public string Url { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
}

122
Program.cs Normal file
View File

@ -0,0 +1,122 @@
#pragma warning disable CS8618
using System;
using System.Linq;
using System.Net.Http.Json;
using Ical.Net;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using HtmlAgilityPack;
using System.Diagnostics;
using Newtonsoft.Json;
namespace newsletter
{
class Program
{
static async Task Main(string[] args)
{
var conf = Configure();
if (conf == null) return;
var reports = await CollectReports(CollectReporters(conf));
var fullHtml = AssembleHTML(reports.Select(r => r.ReportContent));
fullHtml.Save(conf.ExportPath);
}
static Configuration Configure(string configurationPath = "appsettings.json")
{
if (!File.Exists(configurationPath))
{
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
return null;
}
var fileContents = File.ReadAllText(configurationPath);
if (string.IsNullOrWhiteSpace(fileContents))
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
}
var conf = JsonConvert.DeserializeObject<Configuration>(fileContents);
if (conf == null)
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
}
return conf;
}
static List<Task<Report>> CollectReporters(Configuration conf)
{
var toReturn = new List<Task<Report>>();
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)
{
var results = new List<Report>();
foreach (var task in tasks)
{
var rep = await task;
results.Add(rep);
}
return results;
}
static HtmlDocument AssembleHTML(IEnumerable<HtmlNode> reportSections)
{
//TODO
Console.WriteLine($"assembling {reportSections.Count()} sections");
throw new NotImplementedException();
}
}
}

17
Report.cs Normal file
View File

@ -0,0 +1,17 @@
#pragma warning disable CS8618
using HtmlAgilityPack;
namespace newsletter
{
public class Report
{
///<summary>
///gets embedded in the web page. reporter should have this be a div, and assembler will add classes and attributes or whatever
///</summary>
public HtmlNode ReportContent { get; set; }
///<summary>
///if I ever hook this up to TTS, this is the text that will be spoken
///</summary>
public string TextSummary { get; set; }
}
}

19
Reporters/Dummy.cs Normal file
View File

@ -0,0 +1,19 @@
using HtmlAgilityPack;
using System.Threading.Tasks;
namespace newsletter.Reporters
{
public class Dummy : Reporter
{
#pragma warning disable CS1998
public override async Task<Report> Report(Configuration.Reporter config)
{
return new Report()
{
ReportContent = HtmlNode.CreateNode($"<div>dummy node for testing purposes. Url: {config.Url}, Username: {config.Username}, Password: {config.Password}.</div>"),
TextSummary = "dummy text for testing purposes"
};
}
#pragma warning restore CS1998
}
}

9
Reporters/Reporter.cs Normal file
View File

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace newsletter.Reporters
{
public abstract class Reporter
{
public abstract Task<Report> Report(Configuration.Reporter config);
}
}

19
newsletter.csproj Normal file
View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.40" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="puppeteersharp" Version="5.0.0" />
<PackageReference Include="WebDav-Client" Version="1.1.2" />
<PackageReference Include="Ical.Net" Version="4.2.0" />
</ItemGroup>
</Project>

11
sample-appsettings.json Normal file
View File

@ -0,0 +1,11 @@
{
"exportPath": "./newsletter.html",
"reporters": [
{
"type":"dummy",
"url": "localhost:8080",
"username": "guy who didn't configure",
"password": "sordph1sh"
}
]
}

23
template.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{
background-color: #202020;
color:white;
}
.report-content{
display:block;
border-radius: 1em;
border: 0.125em solid white;
padding: 0.25em 0.5em;
margin-top: 1em;
}
</style>
</head>
<body>
<div class="report-content">sample report</div>
<div class="report-content">sample report</div>
</body>
</html>