Files
jube-fork/Jube.Data/SyntaxTree/SyntaxTreeHelpers.cs
richard-churchman 20caa92619 Implemented a refactor of Inline Script functionality:
* Updated documentation for the revised Inline Script approach, aligning with context, strategy, and pipeline patterns. Inline Scripts now use a transient instance model; dependencies are assumed to exist in the context, and state is not supported.
* Added a syntax tree helper parser for VB.net and C# in the data layer, enhancing queries for request XPath fields and Inline Script properties in the UI. The syntax tree helper is a Roslyn-based parser for Inline Scripts that extracts public properties without a full compile, enabling property access in the UI for rule writing and abstraction rules.
* Inline Script properties fully supported in parser, completions for coder and builder, and abstraction rule grouping keys, with search keys synchronized via the `SearchKey` attribute.
* Refactored Inline Script methdology to be a transient instance class, merging payload values from public properties via reflection, removing the need to manually add properties.
* Added migrations to include a language identifier for Inline Scripts.
* Inline Scripts now fully support C# and Visual Basic (VB.net).
* Removed `ClassName` and `MethodName` from the `EntityAnalysisInlineScript` table, now derived from the IInlineScript interface.
* Updated Activation Rule Library to fall back to Inline Script properties if Latitude and Longitude are not specified in the Request XPath.
* Moved Inline Script execution to asynchronous methods to avoid blocking IO during potential remote calls; updated reflection helpers accordingly.
* Renamed variables to better match POCO conventions, as came across them.
* Updated assembly loading for Inline Scripts to include all assemblies in the Jube.App context.
* Optimized Inline Script synchronization to reduce unnecessary reflection during invocation.
* Created Jube.Sandbox for testing and deploying Inline Scripts outside the main application.
* Updated Inline Script OTP demo code new Inline Script Interface signature.
2026-01-11 16:14:54 +02:00

141 lines
6.0 KiB
C#

/* Copyright (C) 2022-present Jube Holdings Limited.
*
* This file is part of Jube™ software.
*
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
* see <https://www.gnu.org/licenses/>.
*/
namespace Jube.Data.SyntaxTree
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using LanguageVersion=Microsoft.CodeAnalysis.CSharp.LanguageVersion;
public static class SyntaxTreeHelpers
{
public static List<string> GetPublicPropertiesForSearchKey(string code, bool cSharp = false)
{
var value = new List<string>();
if (cSharp)
{
var parseOptions = new CSharpParseOptions(LanguageVersion.Latest);
var tree = CSharpSyntaxTree.ParseText(code, parseOptions);
foreach (var @class in tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>())
{
value.AddRange(@class.Members
.OfType<PropertyDeclarationSyntax>()
.Where(prop =>
prop.Modifiers.Any(m => m.Text == "public") &&
prop.AttributeLists
.SelectMany(a => a.Attributes)
.Any(attr => attr?.Name.ToString() == "SearchKey")
)
.Select(prop => prop.Identifier.Text));
}
}
else
{
var parseOptions = new VisualBasicParseOptions(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic16);
var tree = VisualBasicSyntaxTree.ParseText(code, parseOptions);
foreach (var @class in tree.GetRoot().DescendantNodes().OfType<ClassBlockSyntax>())
{
value.AddRange(@class.Members.OfType<PropertyStatementSyntax>()
.Where(prop =>
prop.Modifiers.Any(m => m.Text == "public") &&
prop.AttributeLists
.SelectMany(a => a.Attributes)
.Any(attr => attr?.Name.ToString() == "SearchKey")
)
.Select(prop => prop.Identifier.Text));
}
}
return value;
}
public static Dictionary<string, int> GetPublicProperties(string code, bool cSharp = false)
{
var value = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
if (cSharp)
{
var parseOptions = new CSharpParseOptions(LanguageVersion.Latest);
var tree = CSharpSyntaxTree.ParseText(code, parseOptions);
foreach (var @class in tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>())
{
var results = @class.Members.OfType<PropertyDeclarationSyntax>()
.Select(prop => (prop, modifiers: String.Join(" ", prop.Modifiers.Select(m => m.Text))))
.Where(t => t.modifiers.Contains("public"))
.Select(t => (t, dataTypeId: t.prop.Type.ToString().ToLower() switch
{
"string" => 1,
"int" or "int32" => 2,
"double" => 3,
"datetime" => 4,
"bool" or "boolean" => 5,
_ => 1
}))
.Select(t => (Name: t.t.prop.Identifier.Text, TypeId: t.dataTypeId));
foreach (var item in results)
{
value[item.Name] = item.TypeId;
}
}
}
else
{
var parseOptions = new VisualBasicParseOptions(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic16);
var tree = VisualBasicSyntaxTree.ParseText(code, parseOptions);
foreach (var @class in tree.GetRoot().DescendantNodes().OfType<ClassBlockSyntax>())
{
var results = @class.Members.OfType<PropertyStatementSyntax>()
.Select(prop => new
{
prop,
modifiers = String.Join(" ", prop.Modifiers.Select(m => m.Text))
})
.Where(t => t.modifiers.Contains("Public"))
.Select(t => new
{
t,
dataTypeId = t.prop.AsClause?.Type()?.ToString().ToLower() switch
{
"string" => 1,
"integer" => 2,
"double" => 3,
"datetime" => 4,
"boolean" => 5,
_ => 1
}
})
.Select(t => (Name: t.t.prop.Identifier.Text, TypeId: t.dataTypeId));
foreach (var item in results)
{
value[item.Name] = item.TypeId;
}
}
}
return value;
}
}
}