From b9599c5fa309e62c4db43c6abdaf5d5688146427 Mon Sep 17 00:00:00 2001 From: femsci Date: Mon, 27 Nov 2023 11:55:20 +0100 Subject: [PATCH] runtime --- Minime.sln | 7 +++ .../Nyanbyte.Minime.Cli.csproj | 18 ++++++ src/Nyanbyte.Minime.Cli/Program.cs | 22 +++++++ .../Bundling/BundlingContext.cs | 7 +++ src/Nyanbyte.Minime/Bundling/IBundler.cs | 6 ++ src/Nyanbyte.Minime/Minime.cs | 11 ---- src/Nyanbyte.Minime/MinimeBuildTask.cs | 8 ++- .../Processing/CssProcessor.cs | 8 +-- src/Nyanbyte.Minime/Processing/IProcessor.cs | 8 +-- .../{FileContext.cs => ProcessingContext.cs} | 8 +-- .../Processing/ProcessingOptions.cs | 6 ++ src/Nyanbyte.Minime/RuntimeManager.cs | 57 +++++++++++++++++++ src/Nyanbyte.Minime/RuntimeOptions.cs | 13 +++++ .../Processor/CssProcessorTests.cs | 6 -- 14 files changed, 148 insertions(+), 37 deletions(-) create mode 100644 src/Nyanbyte.Minime.Cli/Nyanbyte.Minime.Cli.csproj create mode 100644 src/Nyanbyte.Minime.Cli/Program.cs create mode 100644 src/Nyanbyte.Minime/Bundling/BundlingContext.cs create mode 100644 src/Nyanbyte.Minime/Bundling/IBundler.cs delete mode 100644 src/Nyanbyte.Minime/Minime.cs rename src/Nyanbyte.Minime/Processing/{FileContext.cs => ProcessingContext.cs} (61%) create mode 100644 src/Nyanbyte.Minime/Processing/ProcessingOptions.cs create mode 100644 src/Nyanbyte.Minime/RuntimeManager.cs create mode 100644 src/Nyanbyte.Minime/RuntimeOptions.cs diff --git a/Minime.sln b/Minime.sln index fb9f792..889298e 100644 --- a/Minime.sln +++ b/Minime.sln @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{9DA99E16-F EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nyanbyte.Minime.Test", "test\Nyanbyte.Minime.Test\Nyanbyte.Minime.Test.csproj", "{1E4D89E6-A38C-4E68-9DA2-BF931190696C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nyanbyte.Minime.Cli", "src\Nyanbyte.Minime.Cli\Nyanbyte.Minime.Cli.csproj", "{79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,9 +30,14 @@ Global {1E4D89E6-A38C-4E68-9DA2-BF931190696C}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E4D89E6-A38C-4E68-9DA2-BF931190696C}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E4D89E6-A38C-4E68-9DA2-BF931190696C}.Release|Any CPU.Build.0 = Release|Any CPU + {79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {96F040B9-C9C9-4B1E-ABB5-39DA51BEB00C} = {EEBF5124-1862-4105-B6AB-A9B578B0CA3D} {1E4D89E6-A38C-4E68-9DA2-BF931190696C} = {9DA99E16-F65C-466B-9FB3-C59D9E1B31AC} + {79FC08D0-6965-4CA9-9E0E-A8E2A9B5D5F5} = {EEBF5124-1862-4105-B6AB-A9B578B0CA3D} EndGlobalSection EndGlobal diff --git a/src/Nyanbyte.Minime.Cli/Nyanbyte.Minime.Cli.csproj b/src/Nyanbyte.Minime.Cli/Nyanbyte.Minime.Cli.csproj new file mode 100644 index 0000000..66e1a72 --- /dev/null +++ b/src/Nyanbyte.Minime.Cli/Nyanbyte.Minime.Cli.csproj @@ -0,0 +1,18 @@ + + + + + + + + + + + + Exe + net8.0 + enable + enable + + + diff --git a/src/Nyanbyte.Minime.Cli/Program.cs b/src/Nyanbyte.Minime.Cli/Program.cs new file mode 100644 index 0000000..2dc138b --- /dev/null +++ b/src/Nyanbyte.Minime.Cli/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Build.Locator; + +namespace Nyanbyte.Minime.Cli; + +public class Program +{ + public static async Task Main(string[] args) + { + MSBuildLocator.RegisterDefaults(); + await Execute(); + } + + public static async Task Execute() + { + RuntimeManager runtime = new(RuntimeOptions.Default); + await runtime.ExecutePlain(); + } +} diff --git a/src/Nyanbyte.Minime/Bundling/BundlingContext.cs b/src/Nyanbyte.Minime/Bundling/BundlingContext.cs new file mode 100644 index 0000000..5a34dce --- /dev/null +++ b/src/Nyanbyte.Minime/Bundling/BundlingContext.cs @@ -0,0 +1,7 @@ +namespace Nyanbyte.Minime.Bundling; + +public class BundlingContext +{ + public StreamWriter Output { get; internal set; } = null!; + public StreamReader Input { get; internal set; } = null!; +} diff --git a/src/Nyanbyte.Minime/Bundling/IBundler.cs b/src/Nyanbyte.Minime/Bundling/IBundler.cs new file mode 100644 index 0000000..b714c35 --- /dev/null +++ b/src/Nyanbyte.Minime/Bundling/IBundler.cs @@ -0,0 +1,6 @@ +namespace Nyanbyte.Minime.Bundling; + +public interface IBundler +{ + public Task Bundle(BundlingContext ctx, CancellationToken cancellation); +} diff --git a/src/Nyanbyte.Minime/Minime.cs b/src/Nyanbyte.Minime/Minime.cs deleted file mode 100644 index 3428317..0000000 --- a/src/Nyanbyte.Minime/Minime.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Nyanbyte.Minime; - -public class Minime -{ - -} diff --git a/src/Nyanbyte.Minime/MinimeBuildTask.cs b/src/Nyanbyte.Minime/MinimeBuildTask.cs index dd10f93..b07d543 100644 --- a/src/Nyanbyte.Minime/MinimeBuildTask.cs +++ b/src/Nyanbyte.Minime/MinimeBuildTask.cs @@ -1,12 +1,14 @@ -using Microsoft.Build.Utilities; - namespace Nyanbyte.Minime; public class MinimeBuildTask : Microsoft.Build.Utilities.Task { public override bool Execute() { - Log.LogMessage("meow"); + System.Threading.Tasks.Task.Run(async () => + { + RuntimeManager runtime = new(RuntimeOptions.Default); + await runtime.Execute(this.BuildEngine); + }).Wait(); return true; } } diff --git a/src/Nyanbyte.Minime/Processing/CssProcessor.cs b/src/Nyanbyte.Minime/Processing/CssProcessor.cs index 9a2e9fd..6df1742 100644 --- a/src/Nyanbyte.Minime/Processing/CssProcessor.cs +++ b/src/Nyanbyte.Minime/Processing/CssProcessor.cs @@ -1,8 +1,4 @@ -using System; using System.Buffers; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Nyanbyte.Minime.Processing; @@ -16,7 +12,9 @@ public class CssProcessor : IProcessor L_STRING_C = '\'', L_SEMI = ';'; - public async Task Execute(FileContext ctx, CancellationToken cancellation) + public string[] AcceptedExtensions => new[] { "css" }; + + public async Task Process(ProcessingContext ctx, CancellationToken cancellation) { var write = ctx.Output; var read = ctx.Input; diff --git a/src/Nyanbyte.Minime/Processing/IProcessor.cs b/src/Nyanbyte.Minime/Processing/IProcessor.cs index cced1ef..2c91a6d 100644 --- a/src/Nyanbyte.Minime/Processing/IProcessor.cs +++ b/src/Nyanbyte.Minime/Processing/IProcessor.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace Nyanbyte.Minime.Processing; public interface IProcessor { - public Task Execute(FileContext ctx, CancellationToken cancellation); + public string[] AcceptedExtensions { get; } + public Task Process(ProcessingContext ctx, CancellationToken cancellation); } diff --git a/src/Nyanbyte.Minime/Processing/FileContext.cs b/src/Nyanbyte.Minime/Processing/ProcessingContext.cs similarity index 61% rename from src/Nyanbyte.Minime/Processing/FileContext.cs rename to src/Nyanbyte.Minime/Processing/ProcessingContext.cs index 90bf120..67e8658 100644 --- a/src/Nyanbyte.Minime/Processing/FileContext.cs +++ b/src/Nyanbyte.Minime/Processing/ProcessingContext.cs @@ -1,15 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.Build.Framework; namespace Nyanbyte.Minime.Processing; -public class FileContext +public class ProcessingContext { public required string Path { get; init; } - public required IBuildEngine BuildEngine { get; init; } + public IBuildEngine? BuildEngine { get; init; } public string? OutputPath { get; set; } public StreamWriter Output { get; internal set; } = null!; public StreamReader Input { get; internal set; } = null!; diff --git a/src/Nyanbyte.Minime/Processing/ProcessingOptions.cs b/src/Nyanbyte.Minime/Processing/ProcessingOptions.cs new file mode 100644 index 0000000..611556a --- /dev/null +++ b/src/Nyanbyte.Minime/Processing/ProcessingOptions.cs @@ -0,0 +1,6 @@ +namespace Nyanbyte.Minime.Processing; + +public class ProcessingOptions +{ + +} diff --git a/src/Nyanbyte.Minime/RuntimeManager.cs b/src/Nyanbyte.Minime/RuntimeManager.cs new file mode 100644 index 0000000..469c091 --- /dev/null +++ b/src/Nyanbyte.Minime/RuntimeManager.cs @@ -0,0 +1,57 @@ +using System.Reflection; +using Microsoft.Build.Framework; +using Nyanbyte.Minime.Processing; + +namespace Nyanbyte.Minime; + +public class RuntimeManager +{ + public RuntimeManager(RuntimeOptions options) + { + _options = options; + + Init(); + } + + private readonly RuntimeOptions _options; + + private Dictionary _processors = null!; + + private void Init() + { + _processors = Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.IsAssignableFrom(typeof(IProcessor)) && t.IsClass).Select(t => (IProcessor)Activator.CreateInstance(t)!).SelectMany(m => m.AcceptedExtensions.Select(e => KeyValuePair.Create(e, m))).ToDictionary(k => k.Key, k => k.Value); + } + + public async Task ExecutePlain(CancellationToken cancellation = default) + { + await Execute(default, cancellation); + } + + public async Task Execute(IBuildEngine? buildEngine = default, CancellationToken cancellation = default) + { + Init(); + + HashSet files = new(); + foreach (string f in _options.SearchDirectories) + { + files.UnionWith(Directory.GetFiles(f, "*", _options.DeepSearch ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); + } + + foreach (string file in files) + { + if (_processors.TryGetValue(Path.GetExtension(file), out var processor)) + { + using StreamReader reader = new(File.OpenRead(file)); + ProcessingContext ctx = new() + { + Path = file, + BuildEngine = buildEngine, + Input = reader + }; + + await processor.Process(ctx, cancellation); + } + } + } +} diff --git a/src/Nyanbyte.Minime/RuntimeOptions.cs b/src/Nyanbyte.Minime/RuntimeOptions.cs new file mode 100644 index 0000000..e097c87 --- /dev/null +++ b/src/Nyanbyte.Minime/RuntimeOptions.cs @@ -0,0 +1,13 @@ +namespace Nyanbyte.Minime; + +public record RuntimeOptions +{ + public static RuntimeOptions Default => new() + { + DeepSearch = true, + SearchDirectories = new[] { Environment.CurrentDirectory } + }; + + public bool DeepSearch { get; init; } + public string[] SearchDirectories { get; init; } = null!; +} diff --git a/test/Nyanbyte.Minime.Test/Processor/CssProcessorTests.cs b/test/Nyanbyte.Minime.Test/Processor/CssProcessorTests.cs index 380579b..8e6cafc 100644 --- a/test/Nyanbyte.Minime.Test/Processor/CssProcessorTests.cs +++ b/test/Nyanbyte.Minime.Test/Processor/CssProcessorTests.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Xunit; - namespace Nyanbyte.Minime.Test.Processor; public class CssProcessorTests