Skip to content

Commit

Permalink
Nope
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Dec 10, 2024
1 parent 9435f0d commit 7c2e479
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 118 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
58 changes: 58 additions & 0 deletions PrismSharp.RegExProvider/PrismSharp.RegExProvider.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />

<PackageReference Include="Jint" Version="4.1.0" />

<PackageReference Include="Basic.Reference.Assemblies.Net90" Version="1.7.9" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" />
<PackageReference Include="Basic.Reference.Assemblies.NetStandard20" Version="1.7.9" />

<ProjectReference Include="..\PrismSharp.RegExCompiler\PrismSharp.RegExCompiler.csproj" />
<EmbeddedResource Include="..\PrismSharp.RegExCompiler\bin\$(Configuration)\net45\PrismSharp.RegExCompiler.exe" >
<Link>PrismSharp.RegExCompiler.exe</Link>
<LogicalName>PrismSharp.RegExCompiler.exe</LogicalName>
</EmbeddedResource>


<Compile Include="..\SimpleJSON\SimpleJSON.cs">
<Link>SimpleJSON.cs</Link>
</Compile>
<Compile Include="..\PrismSharp.SourceGenerator\Flatted.cs">
<Link>Flatted.cs</Link>
</Compile>
<Compile Include="..\PrismSharp.SourceGenerator\GrammarTokenParser.cs">
<Link>GrammarTokenParser.cs</Link>
</Compile>
<Compile Include="..\PrismSharp.SourceGenerator\LanguageDeclarationGenerator.cs">
<Link>LanguageDeclarationGenerator.cs</Link>
</Compile>

<EmbeddedResource Include="..\prismjs\prism.js">
<Link>prismjs\%(FileName)%(Extension)</Link>
<LogicalName>prismjs\%(FileName)%(Extension)</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="..\PrismSharp.SourceGenerator\prismjs\prism-nasm.js">
<Link>prismjs\%(FileName)%(Extension)</Link>
<LogicalName>prismjs\%(FileName)%(Extension)</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="..\prismjs\components\prism-cil.js">
<Link>prismjs\%(FileName)%(Extension)</Link>
<LogicalName>prismjs\%(FileName)%(Extension)</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="..\prismjs\components\prism-csharp.js">
<Link>prismjs\%(FileName)%(Extension)</Link>
<LogicalName>prismjs\%(FileName)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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());
Expand All @@ -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
Expand All @@ -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))
{
Expand Down Expand Up @@ -138,86 +125,11 @@ private static CustomAttribute[] GetAssemblyMetadataAttributes(MetadataReader me
.OfType<Type>()
.FirstOrDefault();

private static void CompileNET90(KeyValuePair<string, string>[] 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<string, string>[] 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<string, string>[] languageDefinitions, string outputDirectory)
private static void CompileNET45(KeyValuePair<string, string>[] 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
Expand Down Expand Up @@ -279,22 +191,64 @@ private static void CompileNET45(KeyValuePair<string, string>[] languageDefiniti
File.Delete(tempPath);
}
}
#pragma warning restore RS1035

private static void CompileNETStandard20(KeyValuePair<string, string>[] languageDefinitions, string languageDefinitionsHashBase64, string dllPath, string pdbPath)

private static void CompileNET90(KeyValuePair<string, string>[] 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<string, string>[] 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
Expand Down
1 change: 0 additions & 1 deletion PrismSharp.SourceGenerator/LanguageDeclarationGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
15 changes: 1 addition & 14 deletions PrismSharp.SourceGenerator/PrismSharp.SourceGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -10,12 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net90" Version="1.7.9" GeneratePathProperty="true" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" GeneratePathProperty="true" />
<PackageReference Include="Basic.Reference.Assemblies.NetStandard20" Version="1.7.9" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />

<PackageReference Include="Jint" Version="4.1.0" GeneratePathProperty="true" />
<PackageReference Include="Acornima" Version="1.1.0" GeneratePathProperty="true" />
</ItemGroup>
Expand All @@ -26,9 +22,6 @@

<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PkgBasic_Reference_Assemblies_NetStandard20)\lib\netstandard2.0\Basic.Reference.Assemblies.NetStandard20.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PkgBasic_Reference_Assemblies_Net80)\lib\netstandard2.0\Basic.Reference.Assemblies.Net80.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PkgBasic_Reference_Assemblies_Net90)\lib\netstandard2.0\Basic.Reference.Assemblies.Net90.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PkgJint)\lib\netstandard2.0\Jint.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PkgAcornima)\lib\netstandard2.0\Acornima.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
Expand All @@ -40,12 +33,6 @@
<Link>SimpleJSON.cs</Link>
</Compile>

<ProjectReference Include="..\PrismSharp.RegExCompiler\PrismSharp.RegExCompiler.csproj" Condition="$(TargetFramework) == 'netstandard2.0'" />
<EmbeddedResource Include="..\PrismSharp.RegExCompiler\bin\$(Configuration)\net45\PrismSharp.RegExCompiler.exe" Condition="$(TargetFramework) == 'netstandard2.0'">
<Link>PrismSharp.RegExCompiler.exe</Link>
<LogicalName>PrismSharp.RegExCompiler.exe</LogicalName>
</EmbeddedResource>

<EmbeddedResource Include="..\prismjs\prism.js">
<Link>prismjs\%(FileName)%(Extension)</Link>
<LogicalName>prismjs\%(FileName)%(Extension)</LogicalName>
Expand Down

0 comments on commit 7c2e479

Please sign in to comment.