From 7c2e4796cf3899ff06a209b03f121e03ce2fd212 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Wed, 11 Dec 2024 00:11:15 +0200 Subject: [PATCH] Nope --- .github/workflows/ci.yml | 14 +- .../PrismSharp.RegExProvider.csproj | 58 +++++++ .../Program.cs | 158 +++++++----------- .../LanguageDeclarationGenerator.cs | 1 - .../PrismSharp.SourceGenerator.csproj | 15 +- 5 files changed, 128 insertions(+), 118 deletions(-) create mode 100644 PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj rename PrismSharp.SourceGenerator/RegexCompilerGenerator.cs => PrismSharp.RegExProvider/Program.cs (86%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e453e99..422c64f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,10 +17,22 @@ jobs: - name: Pack run: >- + dotnet build PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj --configuration Release; + + mkdir regex; + mkdir -p regex/netstandard2.0; + mkdir -p regex/net45; + mkdir -p regex/net8.0; + mkdir -p regex/net9.0; + dotnet run --project PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj --configuration Release -- "$PWD/regex/netstandard2.0" netstandard2.0; + dotnet run --project PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj --configuration Release -- "$PWD/regex/net45" net45; + dotnet run --project PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj --configuration Release -- "$PWD/regex/net8.0" net8.0; + dotnet run --project PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj --configuration Release -- "$PWD/regex/net9.0" net9.0; + dotnet build PrismSharp.RegExCompiler/PrismSharp.RegExCompiler.csproj --configuration Release; dotnet build PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj --configuration Release; dotnet build PrismSharp.Core/PrismSharp.Core.csproj --configuration Release; - + dotnet pack PrismSharp.Core/PrismSharp.Core.csproj --configuration Release -o "./packages"; shell: pwsh diff --git a/PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj b/PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj new file mode 100644 index 0000000..413552c --- /dev/null +++ b/PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj @@ -0,0 +1,58 @@ + + + + net9.0 + disable + enable + Exe + true + + + + + + + + + + + + + + PrismSharp.RegExCompiler.exe + PrismSharp.RegExCompiler.exe + + + + + SimpleJSON.cs + + + Flatted.cs + + + GrammarTokenParser.cs + + + LanguageDeclarationGenerator.cs + + + + prismjs\%(FileName)%(Extension) + prismjs\%(FileName)%(Extension) + + + prismjs\%(FileName)%(Extension) + prismjs\%(FileName)%(Extension) + + + prismjs\%(FileName)%(Extension) + prismjs\%(FileName)%(Extension) + + + prismjs\%(FileName)%(Extension) + prismjs\%(FileName)%(Extension) + + + + diff --git a/PrismSharp.SourceGenerator/RegexCompilerGenerator.cs b/PrismSharp.RegExProvider/Program.cs similarity index 86% rename from PrismSharp.SourceGenerator/RegexCompilerGenerator.cs rename to PrismSharp.RegExProvider/Program.cs index 37d1037..05ba8d2 100644 --- a/PrismSharp.SourceGenerator/RegexCompilerGenerator.cs +++ b/PrismSharp.RegExProvider/Program.cs @@ -1,19 +1,19 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection.Metadata; using System.Reflection.PortableExecutable; -using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Xml.Serialization; using Basic.Reference.Assemblies; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; +using PrismSharp.SourceGenerator; -namespace PrismSharp.SourceGenerator; +namespace PrismSharp.RegExProvider; [XmlRoot("Root")] public class Root @@ -40,23 +40,12 @@ public class PatternEntry public string Pattern { get; set; } = default!; } -[Generator] -public class RegexCompilerGenerator : ISourceGenerator +public static class Program { - public void Initialize(GeneratorInitializationContext context) { } - - public void Execute(GeneratorExecutionContext context) + public static void Main(string[] args) { - if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.outputpath", out var path)) - return; - - if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.msbuildprojectfullpath", out var csPath)) - return; - - if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.targetframework", out var targetFramework)) - return; - - var fullPath = Path.Combine(Path.GetDirectoryName(csPath)!, path); + var fullPath = args[0]; + var targetFramework = args[1]; var languageDefinitions = LanguageDeclarationGenerator.GetFlattedLanguageDefinitions().ToArray(); var languageDefinitionsHash = SHA256Hash(languageDefinitions.SelectMany(x => SHA256Hash(Encoding.UTF8.GetBytes(x.Value))).ToArray()); @@ -65,7 +54,6 @@ public void Execute(GeneratorExecutionContext context) var dllPath = Path.Combine(fullPath, "PrismSharp.RegEx.dll"); var pdbPath = Path.Combine(fullPath, "PrismSharp.RegEx.pdb"); -#pragma warning disable RS1035 if (File.Exists(dllPath) && File.Exists(pdbPath)) { try @@ -88,7 +76,6 @@ public void Execute(GeneratorExecutionContext context) } catch (Exception) { /* ignore */ } } -#pragma warning restore RS1035 if (targetFramework.StartsWith("net") && int.TryParse(targetFramework.Substring(3, 1), out var version)) { @@ -138,86 +125,11 @@ private static CustomAttribute[] GetAssemblyMetadataAttributes(MetadataReader me .OfType() .FirstOrDefault(); - private static void CompileNET90(KeyValuePair[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath) - { - var list = GenerateLanguageRegularExpressions(languageDefinitions, languageDefinitionsHashBase64).ToArray(); - - var compilation = CSharpCompilation.Create( - "PrismSharp.RegEx", - list.Select(x => CSharpSyntaxTree.ParseText(x.Value, path: $"{x.Key}.cs", encoding: Encoding.UTF8)), - Net90.References.All, - new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - //new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu).WithOptimizationLevel(OptimizationLevel.Release)); - - //var sourceGenDriver = CSharpGeneratorDriver.Create(new System.Text.RegularExpressions.Generator.RegexGenerator()); - var type = ByName("System.Text.RegularExpressions.Generator.RegexGenerator")!; - var sourceGenDriver = CSharpGeneratorDriver.Create((IIncrementalGenerator)Activator.CreateInstance(type)!); - sourceGenDriver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); - - using var dllStream = new MemoryStream(); - using var pdbStream = new MemoryStream(); - var emitResult = outputCompilation.Emit(dllStream, pdbStream); - if (!emitResult.Success) - { - // emitResult.Diagnostics - } - - dllStream.Seek(0, SeekOrigin.Begin); - using (var fileStream = new FileStream(dllPath, FileMode.Create, FileAccess.Write)) - { - dllStream.CopyTo(fileStream); - } - - pdbStream.Seek(0, SeekOrigin.Begin); - using (var fileStream = new FileStream(pdbPath, FileMode.Create, FileAccess.Write)) - { - pdbStream.CopyTo(fileStream); - } - } - - private static void CompileNET80(KeyValuePair[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath) - { - var list = GenerateLanguageRegularExpressions(languageDefinitions, languageDefinitionsHashBase64).ToArray(); - - var compilation = CSharpCompilation.Create( - "PrismSharp.RegEx", - list.Select(x => CSharpSyntaxTree.ParseText(x.Value, path: $"{x.Key}.cs", encoding: Encoding.UTF8)), - Net80.References.All, - new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - //new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu).WithOptimizationLevel(OptimizationLevel.Release)); - - //var sourceGenDriver = CSharpGeneratorDriver.Create(new System.Text.RegularExpressions.Generator.RegexGenerator()); - var type = ByName("System.Text.RegularExpressions.Generator.RegexGenerator")!; - var sourceGenDriver = CSharpGeneratorDriver.Create((IIncrementalGenerator)Activator.CreateInstance(type)!); - sourceGenDriver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); - - using var dllStream = new MemoryStream(); - using var pdbStream = new MemoryStream(); - var emitResult = outputCompilation.Emit(dllStream, pdbStream); - if (!emitResult.Success) - { - // emitResult.Diagnostics - } - - dllStream.Seek(0, SeekOrigin.Begin); - using (var fileStream = new FileStream(dllPath, FileMode.Create, FileAccess.Write)) - { - dllStream.CopyTo(fileStream); - } - - pdbStream.Seek(0, SeekOrigin.Begin); - using (var fileStream = new FileStream(pdbPath, FileMode.Create, FileAccess.Write)) - { - pdbStream.CopyTo(fileStream); - } - } - -#pragma warning disable RS1035 - private static void CompileNET45(KeyValuePair[] languageDefinitions, string outputDirectory) + private static void CompileNET45(KeyValuePair[] languageDefinitions, string outputDirectory) { // The generated Regex seem to work on any platform, but the generator itself // only works on .NET Framework, so we launch a separate process to generate the Regex - var asm = typeof(RegexCompilerGenerator).Assembly.GetManifestResourceStream("PrismSharp.RegExCompiler.exe")!; + var asm = typeof(Program).Assembly.GetManifestResourceStream("PrismSharp.RegExCompiler.exe")!; var tempPath = $"{Path.GetTempFileName()}.exe"; var fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write); try @@ -279,22 +191,64 @@ private static void CompileNET45(KeyValuePair[] languageDefiniti File.Delete(tempPath); } } -#pragma warning restore RS1035 - private static void CompileNETStandard20(KeyValuePair[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath) + + private static void CompileNET90(KeyValuePair[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath) + { + var list = GenerateLanguageRegularExpressions(languageDefinitions, languageDefinitionsHashBase64).ToArray(); + + var compilation = CSharpCompilation.Create( + "PrismSharp.RegEx", + list.Select(x => CSharpSyntaxTree.ParseText(x.Value, path: $"{x.Key}.cs", encoding: Encoding.UTF8)), + Net90.References.All, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + //new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu).WithOptimizationLevel(OptimizationLevel.Release)); + + //var sourceGenDriver = CSharpGeneratorDriver.Create(new System.Text.RegularExpressions.Generator.RegexGenerator()); + var type = ByName("System.Text.RegularExpressions.Generator.RegexGenerator")!; + var sourceGenDriver = CSharpGeneratorDriver.Create((IIncrementalGenerator)Activator.CreateInstance(type)!); + sourceGenDriver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); + + using var dllStream = new MemoryStream(); + using var pdbStream = new MemoryStream(); + var emitResult = outputCompilation.Emit(dllStream, pdbStream); + if (!emitResult.Success) + { + // emitResult.Diagnostics + } + + dllStream.Seek(0, SeekOrigin.Begin); + using (var fileStream = new FileStream(dllPath, FileMode.Create, FileAccess.Write)) + { + dllStream.CopyTo(fileStream); + } + + pdbStream.Seek(0, SeekOrigin.Begin); + using (var fileStream = new FileStream(pdbPath, FileMode.Create, FileAccess.Write)) + { + pdbStream.CopyTo(fileStream); + } + } + + private static void CompileNET80(KeyValuePair[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath) { - var list = GenerateLanguageRegularExpressionsSimple(languageDefinitions, languageDefinitionsHashBase64).ToArray(); + var list = GenerateLanguageRegularExpressions(languageDefinitions, languageDefinitionsHashBase64).ToArray(); var compilation = CSharpCompilation.Create( "PrismSharp.RegEx", list.Select(x => CSharpSyntaxTree.ParseText(x.Value, path: $"{x.Key}.cs", encoding: Encoding.UTF8)), - NetStandard20.References.All, + Net80.References.All, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); //new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu).WithOptimizationLevel(OptimizationLevel.Release)); + //var sourceGenDriver = CSharpGeneratorDriver.Create(new System.Text.RegularExpressions.Generator.RegexGenerator()); + var type = ByName("System.Text.RegularExpressions.Generator.RegexGenerator")!; + var sourceGenDriver = CSharpGeneratorDriver.Create((IIncrementalGenerator)Activator.CreateInstance(type)!); + sourceGenDriver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); + using var dllStream = new MemoryStream(); using var pdbStream = new MemoryStream(); - var emitResult = compilation.Emit(dllStream, pdbStream); + var emitResult = outputCompilation.Emit(dllStream, pdbStream); if (!emitResult.Success) { // emitResult.Diagnostics diff --git a/PrismSharp.SourceGenerator/LanguageDeclarationGenerator.cs b/PrismSharp.SourceGenerator/LanguageDeclarationGenerator.cs index cf4da57..f9d9a04 100644 --- a/PrismSharp.SourceGenerator/LanguageDeclarationGenerator.cs +++ b/PrismSharp.SourceGenerator/LanguageDeclarationGenerator.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj b/PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj index a834bf3..ac4705c 100644 --- a/PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj +++ b/PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj @@ -1,7 +1,7 @@  - netstandard2.0;net8.0;net9.0 + netstandard2.0 latest enable true @@ -10,12 +10,8 @@ - - - - @@ -26,9 +22,6 @@ - - - @@ -40,12 +33,6 @@ SimpleJSON.cs - - - PrismSharp.RegExCompiler.exe - PrismSharp.RegExCompiler.exe - - prismjs\%(FileName)%(Extension) prismjs\%(FileName)%(Extension)