Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Nov 25, 2023
1 parent 3b815f2 commit a593ae4
Show file tree
Hide file tree
Showing 36 changed files with 3,142 additions and 768 deletions.
1,796 changes: 1,796 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
rm -rf *.nupkg
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs.Logging
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs.Timings
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs.Legacy
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs.StringNormalisation
dotnet pack --nologo --version-suffix preview$GITHUB_RUN_ID.$(git rev-parse --short HEAD) -o . ArcaneLibs.Blazor.Components
dotnet nuget push $(ls *.nupkg) -k ${{ secrets.NUGET_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
Expand Down
2 changes: 1 addition & 1 deletion ArcaneLibs.Blazor.Components/ExampleJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public async ValueTask DisposeAsync() {
await module.DisposeAsync();
}
}
}
}
9 changes: 9 additions & 0 deletions ArcaneLibs.Legacy/ArcaneLibs.Legacy.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
12 changes: 6 additions & 6 deletions ArcaneLibs/CsvTools.cs → ArcaneLibs.Legacy/CsvTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public static string ToCsv<T>(string separator, IEnumerable<T> objectlist) {
}

public static string ToCsvFields(string separator, PropertyInfo[] fields, object o) {
var linie = new StringBuilder();
var line = new StringBuilder();

foreach (var f in fields) {
if (linie.Length > 0)
linie.Append(separator);
if (line.Length > 0)
line.Append(separator);

var x = f.GetValue(o);

if (x != null)
linie.Append(x.ToString());
line.Append(x.ToString());
}

return linie.ToString();
return line.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ internal void StartDownload() {
if (File.Exists(TargetFile)) File.Delete(TargetFile);
client.DownloadFileAsync(new Uri(Url), TargetFile);
}
}
}
2 changes: 1 addition & 1 deletion ArcaneLibs.Logging/LogEndpoints/BaseEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ namespace ArcaneLibs.Logging.LogEndpoints;
public class BaseEndpoint {
public virtual void Write(string text) =>
Console.WriteLine("ArcaneLibs.Logging: invalid endpoint type: Write not implemented");
}
}
2 changes: 1 addition & 1 deletion ArcaneLibs.Logging/LogEndpoints/ConsoleEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ namespace ArcaneLibs.Logging.LogEndpoints;

public class ConsoleEndpoint : BaseEndpoint {
public override void Write(string text) => Console.WriteLine(text);
}
}
2 changes: 1 addition & 1 deletion ArcaneLibs.Logging/LogEndpoints/DebugEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ namespace ArcaneLibs.Logging.LogEndpoints;

public class DebugEndpoint : BaseEndpoint {
public override void Write(string text) => Debug.WriteLine(text);
}
}
2 changes: 1 addition & 1 deletion ArcaneLibs.Logging/LogEndpoints/FileEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public FileEndpoint(string path, bool append) {
}

public override void Write(string text) => _stream.WriteLine(text);
}
}
19 changes: 6 additions & 13 deletions ArcaneLibs.Logging/LogManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using ArcaneLibs.Logging.LogEndpoints;

Expand All @@ -18,12 +18,9 @@ public class LogManager {
/// <param name="message">Text to send</param>
/// <param name="file">Filename (auto)</param>
/// <param name="line">Line number (auto)</param>
public void Log(string message,
[CallerFilePath] string file = null,
[CallerLineNumber] int line = 0) {
public void Log(string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0) {

Check warning on line 21 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 21 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
foreach (var endpoint in _endpoints)
endpoint.Write((LogTime ? $"[{DateTime.Now:hh:mm:ss}]" : "") +
$"{Prefix}{Path.GetFileName(file)}:{line} {MessagePrefix}{message}");
endpoint.Write((LogTime ? $"[{DateTime.Now:hh:mm:ss}]" : "") + $"{Prefix}{Path.GetFileName(file)}:{line} {MessagePrefix}{message}");
}

/// <summary>
Expand All @@ -33,12 +30,8 @@ public void Log(string message,
/// <param name="file">Filename (auto)</param>
/// <param name="line">Line number (auto)</param>
[Conditional("DEBUG")]
public void LogDebug(string message,
[CallerFilePath] string file = null,
[CallerLineNumber] int line = 0) =>
Log(message, file, line);
public void LogDebug(string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0) => Log(message, file, line);

Check warning on line 33 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 33 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

public void LogSplit(string separator, [CallerFilePath] string file = null,
[CallerLineNumber] int line = 0, params object[] parts) =>
public void LogSplit(string separator, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0, params object[] parts) =>

Check warning on line 35 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 35 in ArcaneLibs.Logging/LogManager.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
Log(string.Join(separator, parts), file, line);
}
}
4 changes: 2 additions & 2 deletions ArcaneLibs.StringNormalisation/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Unidecode.NET;
Expand All @@ -24,4 +24,4 @@ public static string RemoveAnsi(this string str) {
var ansiRegex = new Regex(@"\x1B\[[0-?]*[ -/]*[@-~]");
return ansiRegex.Replace(str, "");
}
}
}
9 changes: 9 additions & 0 deletions ArcaneLibs.Timings/ArcaneLibs.Timings.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions ArcaneLibs/Timing.cs → ArcaneLibs.Timings/Timing.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace ArcaneLibs;
Expand All @@ -19,8 +19,8 @@ public Stopwatch StartTiming(string name, int timeoutMs = 60000) {
new Thread(() => {
if (Timings.TryGetValue(name, out var timing))
while (timing.IsRunning) {
if (timing.ElapsedMilliseconds >= timeoutMs) Fail(name);
if (timing.ElapsedMilliseconds >= timeoutMs)
Fail(name);
Thread.Sleep(250);
}
else
Expand Down Expand Up @@ -79,4 +79,4 @@ public Stopwatch Fail(string name) {
}

public ConcurrentDictionary<string, Stopwatch> Timings = new();
}
}
5 changes: 0 additions & 5 deletions ArcaneLibs.UsageTest/Config.cs

This file was deleted.

3 changes: 0 additions & 3 deletions ArcaneLibs.UsageTest/Config.json

This file was deleted.

23 changes: 6 additions & 17 deletions ArcaneLibs.UsageTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// See https://aka.ms/new-console-template for more information
// See https://aka.ms/new-console-template for more information

using System.Drawing;
using ArcaneLibs;
using ArcaneLibs.Collections;
using ArcaneLibs.Extensions;
using ArcaneLibs.Logging;
using ArcaneLibs.Logging.LogEndpoints;
using ArcaneLibs.UsageTest;
using ArcaneLibs.Extensions;

Console.WriteLine("Hello, World!");
//create logs
Expand All @@ -25,15 +25,6 @@
log3.Log("file log");
log3.LogDebug("file dbg log");

for (var i = 0; i < 5; i++) {
var myConfig = Config.Read();
Console.WriteLine(myConfig.SomeNumber);
myConfig.SomeNumber = i;
myConfig.Save();
myConfig = Config.Read();
Console.WriteLine(myConfig.SomeNumber);
}

/*
Console.Write("Expected: ");
for (int i = 0; i < 5; i++)
Expand All @@ -47,31 +38,29 @@
}
*/
var autodict = new AutoPopulatingDictionary<int, string>();
var autodict2 = new AutoPopulatingDictionary<int, Config>();
var autodict3 = new AutoPopulatingDictionary<int, Point>();

var a = autodict[3];
var b = autodict2[5];
var c = autodict3[6];

//return;

Console.WriteLine(Util.GetCommandOutputSync("bash", "-c asdf"));
Console.WriteLine(Util.GetCommandOutputSync("bash", "-c ls"));

RandomClass arc = new RandomClass(){
RandomClass arc = new RandomClass() {
Id = 1,
Name = "asdf",
DateCreated = DateTime.Now,
SubClassInst = new(){
SubClassInst = new() {
Description = "asdf"
}
};
RandomClass brc = new RandomClass(){
RandomClass brc = new RandomClass() {
Id = 2,
Name = "asdf",
DateCreated = DateTime.Now,
SubClassInst = new(){
SubClassInst = new() {
Description = "asdf"
}
};
Expand Down
6 changes: 2 additions & 4 deletions ArcaneLibs.UsageTest/RandomClass.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
namespace ArcaneLibs.UsageTest;

public class RandomClass
{
public class RandomClass {
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public SubClass SubClassInst { get; set; }

public class SubClass
{
public class SubClass {
public string Description { get; set; }
}
}
2 changes: 0 additions & 2 deletions ArcaneLibs.UsageTest/out.txt

This file was deleted.

12 changes: 12 additions & 0 deletions ArcaneLibs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.StringNormalisat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Blazor.Components", "ArcaneLibs.Blazor.Components\ArcaneLibs.Blazor.Components.csproj", "{3CB4C1FB-B63C-4ED4-B3D2-716E4140CF32}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Timings", "ArcaneLibs.Timings\ArcaneLibs.Timings.csproj", "{01E2960C-1482-445B-A37D-C182A6DB3C34}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Legacy", "ArcaneLibs.Legacy\ArcaneLibs.Legacy.csproj", "{9A77704F-5099-401E-99ED-5B756214461B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -42,5 +46,13 @@ Global
{3CB4C1FB-B63C-4ED4-B3D2-716E4140CF32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CB4C1FB-B63C-4ED4-B3D2-716E4140CF32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CB4C1FB-B63C-4ED4-B3D2-716E4140CF32}.Release|Any CPU.Build.0 = Release|Any CPU
{01E2960C-1482-445B-A37D-C182A6DB3C34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01E2960C-1482-445B-A37D-C182A6DB3C34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01E2960C-1482-445B-A37D-C182A6DB3C34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01E2960C-1482-445B-A37D-C182A6DB3C34}.Release|Any CPU.Build.0 = Release|Any CPU
{9A77704F-5099-401E-99ED-5B756214461B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A77704F-5099-401E-99ED-5B756214461B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A77704F-5099-401E-99ED-5B756214461B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A77704F-5099-401E-99ED-5B756214461B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 4 additions & 9 deletions ArcaneLibs/ClassCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ namespace ArcaneLibs;
public class ClassCollector<T> where T : class {
static ClassCollector() {
if (!typeof(T).IsInterface && !typeof(T).IsAbstract)
throw new ArgumentException(
$"ClassCollector<T> must be used with an interface or abstract type. Passed type: {typeof(T).Name}");
throw new ArgumentException($"ClassCollector<T> must be used with an interface or abstract type. Passed type: {typeof(T).Name}");
}

public List<Type> ResolveFromAllAccessibleAssemblies() =>
AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList();
public List<Type> ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList();

public List<Type> ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType());

public List<Type> ResolveFromTypeReference(Type t) =>
Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ??
new List<Type>();
public List<Type> ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List<Type>();

public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) =>
ResolveFromAssembly(Assembly.Load(assemblyName));
public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName));

public List<Type> ResolveFromAssembly(Assembly assembly) => assembly.GetTypes()
.Where(x => x is { IsClass: true, IsAbstract: false } && x.IsAssignableTo(typeof(T))).ToList();
Expand Down
12 changes: 4 additions & 8 deletions ArcaneLibs/Collections/AutoPopulatingDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ public class AutoPopulatingDictionary<T1, T2> : Dictionary<T1, T2> {
public T2 this[T1 key] {

Check warning on line 9 in ArcaneLibs/Collections/AutoPopulatingDictionary.cs

View workflow job for this annotation

GitHub Actions / build

'AutoPopulatingDictionary<T1, T2>.this[T1]' hides inherited member 'Dictionary<T1, T2>.this[T1]'. Use the new keyword if hiding was intended.

Check warning on line 9 in ArcaneLibs/Collections/AutoPopulatingDictionary.cs

View workflow job for this annotation

GitHub Actions / build

'AutoPopulatingDictionary<T1, T2>.this[T1]' hides inherited member 'Dictionary<T1, T2>.this[T1]'. Use the new keyword if hiding was intended.

Check warning on line 9 in ArcaneLibs/Collections/AutoPopulatingDictionary.cs

View workflow job for this annotation

GitHub Actions / build

'AutoPopulatingDictionary<T1, T2>.this[T1]' hides inherited member 'Dictionary<T1, T2>.this[T1]'. Use the new keyword if hiding was intended.
get {
var a = typeof(T2).GetConstructors();
if (!_backingStore.ContainsKey(key)) _backingStore.Add(key, GetNewInstance.Invoke());
return _backingStore[key];
}
set {
if (!_backingStore.ContainsKey(key)) _backingStore.Add(key, value);
else _backingStore[key] = value;
}
set => _backingStore[key] = value;
}

private Func<T2> GetNewInstance = () => {
public Func<T1, T2> GetNewInstance = (T1 key) => {
if (typeof(T2) == typeof(string)) return (T2)(object)"";
return Activator.CreateInstance<T2>();
};
}
}
2 changes: 1 addition & 1 deletion ArcaneLibs/Extensions/BooleanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public static class BooleanExtensions {
public static (bool, string) ToggleAlt(this bool @bool) => (@bool ^= true, @bool ? "enabled" : "disabled");

public static string ToEnglish(this bool @bool) => @bool ? "enabled" : "disabled";
}
}
12 changes: 6 additions & 6 deletions ArcaneLibs/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public static async Task<Y> GetOrCreateAsync<X, Y>(this IDictionary<X, Y> dict,
if (semaphore is not null) await semaphore.WaitAsync();
Y value;
// lock (dict) {
if (dict.TryGetValue(key, out value)) {
if (semaphore is not null) semaphore.Release();
return value;
}
if (dict.TryGetValue(key, out value)) {

Check warning on line 38 in ArcaneLibs/Extensions/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 38 in ArcaneLibs/Extensions/DictionaryExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
if (semaphore is not null) semaphore.Release();
return value;
}

value = await valueFactory(key);
dict.TryAdd(key, value);
value = await valueFactory(key);
dict.TryAdd(key, value);
// }

if (semaphore is not null) semaphore.Release();
Expand Down
Loading

0 comments on commit a593ae4

Please sign in to comment.