From 411decc5669c1fc5fa122180d976faff15dadf6d Mon Sep 17 00:00:00 2001 From: Andrei Timar Date: Sat, 4 Apr 2026 18:07:13 +0300 Subject: [PATCH 1/2] feat(backend): relocated sample code generator --- .../ProjectStructure}/SampleCodeFile.cs | 2 +- .../ProjectStructure}/SampleCodeGenerator.cs | 166 +++++++++++------- 2 files changed, 104 insertions(+), 64 deletions(-) rename {ScriptBee/Scripts/ScriptSampleGenerators => ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure}/SampleCodeFile.cs (66%) rename {ScriptBee/Scripts/ScriptSampleGenerators => ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure}/SampleCodeGenerator.cs (52%) diff --git a/ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeFile.cs b/ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeFile.cs similarity index 66% rename from ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeFile.cs rename to ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeFile.cs index be2eef3a..0936ab0a 100644 --- a/ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeFile.cs +++ b/ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeFile.cs @@ -1,4 +1,4 @@ -namespace ScriptBee.Scripts.ScriptSampleGenerators; +namespace ScriptBee.Service.Project.ProjectStructure; public record SampleCodeFile { diff --git a/ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeGenerator.cs b/ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeGenerator.cs similarity index 52% rename from ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeGenerator.cs rename to ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeGenerator.cs index fe2ab08c..0276fc7c 100644 --- a/ScriptBee/Scripts/ScriptSampleGenerators/SampleCodeGenerator.cs +++ b/ScriptBeeWebApp/src/Gateway/Application/Service.Project/ProjectStructure/SampleCodeGenerator.cs @@ -1,22 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Reflection; +using System.Reflection; using System.Text; -using System.Threading; -using System.Threading.Tasks; using DxWorks.ScriptBee.Plugin.Api; -using ScriptBee.ProjectContext; -namespace ScriptBee.Scripts.ScriptSampleGenerators; +namespace ScriptBee.Service.Project.ProjectStructure; -public class SampleCodeGenerator +public class SampleCodeGenerator( + IScriptGeneratorStrategy scriptGeneratorStrategy, + ISet acceptedModules +) { - private const BindingFlags BindingFlags = System.Reflection.BindingFlags.DeclaredOnly | - System.Reflection.BindingFlags.Instance | - System.Reflection.BindingFlags.Public; - private readonly ISet _acceptedModules; + private const BindingFlags BindingFlags = + System.Reflection.BindingFlags.DeclaredOnly + | System.Reflection.BindingFlags.Instance + | System.Reflection.BindingFlags.Public; + private readonly HashSet _generatedClassNames = new(); - private readonly IScriptGeneratorStrategy _scriptGeneratorStrategy; private const string ClassName = "ScriptContent"; private const string MethodName = "ExecuteScript"; @@ -26,14 +24,10 @@ public class SampleCodeGenerator private const string MethodNameMarker = "$METHOD_NAME$"; private const string ModelTypeMarker = "$MODEL_TYPE$"; - public SampleCodeGenerator(IScriptGeneratorStrategy scriptGeneratorStrategy, ISet acceptedModules) - { - _scriptGeneratorStrategy = scriptGeneratorStrategy; - _acceptedModules = acceptedModules; - } - - public async Task> GetSampleCode(IEnumerable objects, - CancellationToken cancellationToken = default) + public async Task> GetSampleCode( + IEnumerable objects, + CancellationToken cancellationToken = default + ) { var generatedClasses = new List(); @@ -44,11 +38,7 @@ public async Task> GetSampleCode(IEnumerable objec } var generateSampleCode = await GenerateSampleCode(cancellationToken); - generatedClasses.Add(new SampleCodeFile - { - Name = "script", - Content = generateSampleCode - }); + generatedClasses.Add(new SampleCodeFile { Name = "script", Content = generateSampleCode }); return generatedClasses; } @@ -56,7 +46,7 @@ public async Task> GetSampleCode(IEnumerable objec public async Task GenerateSampleCode(CancellationToken cancellationToken = default) { var stringBuilder = new StringBuilder(); - var imports = await _scriptGeneratorStrategy.GenerateImports(); + var imports = await scriptGeneratorStrategy.GenerateImports(); if (!string.IsNullOrEmpty(imports)) { @@ -64,7 +54,7 @@ public async Task GenerateSampleCode(CancellationToken cancellationToken stringBuilder.AppendLine(); } - var modelDeclaration = _scriptGeneratorStrategy.GenerateModelDeclaration(nameof(Project)); + var modelDeclaration = scriptGeneratorStrategy.GenerateModelDeclaration(nameof(Project)); if (!string.IsNullOrEmpty(modelDeclaration)) { stringBuilder.AppendLine(modelDeclaration); @@ -72,7 +62,7 @@ public async Task GenerateSampleCode(CancellationToken cancellationToken stringBuilder.AppendLine(); } - var generateSampleCode = await _scriptGeneratorStrategy.GenerateSampleCode(); + var generateSampleCode = await scriptGeneratorStrategy.GenerateSampleCode(); var sampleCode = ReplaceSampleCodeTemplates(nameof(Project), generateSampleCode); stringBuilder.Append(sampleCode); @@ -81,7 +71,11 @@ public async Task GenerateSampleCode(CancellationToken cancellationToken private IList GenerateClasses(Type type) { - if (_generatedClassNames.Contains(type.Name) || IsPrimitive(type) || !IsAcceptedModule(type.Module)) + if ( + _generatedClassNames.Contains(type.Name) + || IsPrimitive(type) + || !IsAcceptedModule(type.Module) + ) return new List(); _generatedClassNames.Add(type.Name); @@ -97,27 +91,42 @@ private IList GenerateClasses(Type type) if (baseType != null && IsAcceptedModule(baseType.Module)) { stringBuilder.AppendLine( - _scriptGeneratorStrategy.GenerateClassName(type, baseType, out var baseClassGenericTypes)); - - foreach (var genericType in baseClassGenericTypes) genericTypes.Add(genericType); - - if (!_generatedClassNames.Contains(baseType.Name)) sampleCodeFiles.AddRange(GenerateClasses(baseType)); + scriptGeneratorStrategy.GenerateClassName( + type, + baseType, + out var baseClassGenericTypes + ) + ); + + foreach (var genericType in baseClassGenericTypes) + genericTypes.Add(genericType); + + if (!_generatedClassNames.Contains(baseType.Name)) + sampleCodeFiles.AddRange(GenerateClasses(baseType)); } else { - stringBuilder.AppendLine(_scriptGeneratorStrategy.GenerateClassName(type)); + stringBuilder.AppendLine(scriptGeneratorStrategy.GenerateClassName(type)); } - var classStart = _scriptGeneratorStrategy.GenerateClassStart(); - if (!string.IsNullOrEmpty(classStart)) stringBuilder.AppendLine(classStart); + var classStart = scriptGeneratorStrategy.GenerateClassStart(); + if (!string.IsNullOrEmpty(classStart)) + stringBuilder.AppendLine(classStart); foreach (var fieldInfo in type.GetFields(BindingFlags)) { var modifier = GetFieldModifier(fieldInfo); - stringBuilder.AppendLine(_scriptGeneratorStrategy.GenerateField(modifier, fieldInfo.FieldType, - fieldInfo.Name, out var receivedGenericTypes)); + stringBuilder.AppendLine( + scriptGeneratorStrategy.GenerateField( + modifier, + fieldInfo.FieldType, + fieldInfo.Name, + out var receivedGenericTypes + ) + ); - foreach (var genericType in receivedGenericTypes) genericTypes.Add(genericType); + foreach (var genericType in receivedGenericTypes) + genericTypes.Add(genericType); if (!_generatedClassNames.Contains(fieldInfo.FieldType.Name)) sampleCodeFiles.AddRange(GenerateClasses(fieldInfo.FieldType)); @@ -126,10 +135,17 @@ private IList GenerateClasses(Type type) foreach (var propertyInfo in type.GetProperties(BindingFlags)) { const string modifier = "public"; - stringBuilder.AppendLine(_scriptGeneratorStrategy.GenerateProperty(modifier, propertyInfo.PropertyType, - propertyInfo.Name, out var receivedGenericTypes)); + stringBuilder.AppendLine( + scriptGeneratorStrategy.GenerateProperty( + modifier, + propertyInfo.PropertyType, + propertyInfo.Name, + out var receivedGenericTypes + ) + ); - foreach (var genericType in receivedGenericTypes) genericTypes.Add(genericType); + foreach (var genericType in receivedGenericTypes) + genericTypes.Add(genericType); if (!_generatedClassNames.Contains(propertyInfo.PropertyType.Name)) sampleCodeFiles.AddRange(GenerateClasses(propertyInfo.PropertyType)); @@ -137,7 +153,8 @@ private IList GenerateClasses(Type type) foreach (var methodInfo in type.GetMethods(BindingFlags)) { - if (methodInfo.IsSpecialName) continue; + if (methodInfo.IsSpecialName) + continue; var modifier = GetMethodModifier(methodInfo); var methodParameters = methodInfo.GetParameters(); @@ -145,32 +162,47 @@ private IList GenerateClasses(Type type) var parameters = new List>(); foreach (var param in methodParameters) + { parameters.Add(new Tuple(param.ParameterType, param.Name)); + } stringBuilder.AppendLine(); - stringBuilder.Append(_scriptGeneratorStrategy.GenerateMethod(modifier, methodInfo.ReturnType, - methodInfo.Name, parameters, out var receivedGenericTypes)); - - foreach (var genericType in receivedGenericTypes) genericTypes.Add(genericType); + stringBuilder.Append( + scriptGeneratorStrategy.GenerateMethod( + modifier, + methodInfo.ReturnType, + methodInfo.Name, + parameters, + out var receivedGenericTypes + ) + ); + + foreach (var genericType in receivedGenericTypes) + genericTypes.Add(genericType); } foreach (var genericType in genericTypes) + { if (!_generatedClassNames.Contains(genericType.Name)) sampleCodeFiles.AddRange(GenerateClasses(genericType)); + } // 4 = default object methods - if (type.GetProperties().Length == 0 && type.GetFields().Length == 0 && type.GetMethods().Length <= 4) - stringBuilder.AppendLine(_scriptGeneratorStrategy.GenerateEmptyClass()); + if ( + type.GetProperties().Length == 0 + && type.GetFields().Length == 0 + && type.GetMethods().Length <= 4 + ) + stringBuilder.AppendLine(scriptGeneratorStrategy.GenerateEmptyClass()); - var classEnd = _scriptGeneratorStrategy.GenerateClassEnd(); - if (!string.IsNullOrEmpty(classEnd)) stringBuilder.AppendLine(classEnd); + var classEnd = scriptGeneratorStrategy.GenerateClassEnd(); + if (!string.IsNullOrEmpty(classEnd)) + stringBuilder.AppendLine(classEnd); - sampleCodeFiles.Add(new SampleCodeFile - { - Name = type.Name, - Content = stringBuilder.ToString() - }); + sampleCodeFiles.Add( + new SampleCodeFile { Name = type.Name, Content = stringBuilder.ToString() } + ); return sampleCodeFiles; } @@ -178,8 +210,14 @@ private IList GenerateClasses(Type type) // todo extract hardcoded strings as constants private string ReplaceSampleCodeTemplates(string modelName, string sampleCode) { - var finalSampleCode = sampleCode.Replace(StartCommentMarker, _scriptGeneratorStrategy.GetStartComment()); - finalSampleCode = finalSampleCode.Replace(EndCommentMarker, _scriptGeneratorStrategy.GetEndComment()); + var finalSampleCode = sampleCode.Replace( + StartCommentMarker, + scriptGeneratorStrategy.GetStartComment() + ); + finalSampleCode = finalSampleCode.Replace( + EndCommentMarker, + scriptGeneratorStrategy.GetEndComment() + ); finalSampleCode = finalSampleCode.Replace(ClassNameMarker, ClassName); finalSampleCode = finalSampleCode.Replace(MethodNameMarker, MethodName); finalSampleCode = finalSampleCode.Replace(ModelTypeMarker, modelName); @@ -198,7 +236,8 @@ private string GetFieldModifier(FieldInfo fieldInfo) var modifier = "public"; if (fieldInfo.IsPrivate) modifier = "private"; - else if (fieldInfo.IsFamily) modifier = "protected"; + else if (fieldInfo.IsFamily) + modifier = "protected"; return modifier; } @@ -209,13 +248,14 @@ private string GetMethodModifier(MethodInfo methodInfo) var modifier = "public"; if (methodInfo.IsPrivate) modifier = "private"; - else if (methodInfo.IsFamily) modifier = "protected"; + else if (methodInfo.IsFamily) + modifier = "protected"; return modifier; } private bool IsAcceptedModule(Module module) { - return _acceptedModules.Contains(module.Name); + return acceptedModules.Contains(module.Name); } } From 8a3d9e3abaa3a2725a0d0764cfeb4a5c17220453 Mon Sep 17 00:00:00 2001 From: Andrei Timar Date: Sat, 4 Apr 2026 18:08:36 +0300 Subject: [PATCH 2/2] feat(backend): upgrade default bundle to dotnet 10 with dependencies and link it to the main solution file --- Directory.Packages.props | 15 +- .../ConsoleHelperFunctions.cs | 8 +- .../CsvHelperFunctions.cs | 21 +- ...tBee.Plugin.HelperFunctions.Default.csproj | 17 +- .../FileHelperFunctions.cs | 41 ++- .../JsonHelperFunctions.cs | 22 +- Plugins/ScriptBeeDefaultPlugins.sln | 64 ---- .../CSharpScriptRunnerTests.cs | 154 ++++---- .../DummyHelperFunctions.cs | 4 +- ...ee.Plugin.ScriptRunner.CSharp.Tests.csproj | 42 +-- .../HelperFunctionWithCollections.cs | 11 +- ...tionWithoutGenericMethodThatReturnsVoid.cs | 4 +- .../HelperFunctionsContainer.cs | 2 - .../HelperFunctionsGeneratorTests.cs | 76 ++-- ...unctionsWithGenericMethodWithConstrains.cs | 12 +- .../ISomething.cs | 4 +- .../ScriptGeneratorStrategyTests.cs | 348 +++++++++++++----- .../ScriptParametersGeneratorTest.cs | 82 +++-- .../CSharpScriptRunner.cs | 111 ++++-- ...criptBee.Plugin.ScriptRunner.CSharp.csproj | 42 +-- .../Exceptions/CompilationErrorException.cs | 15 +- .../InvalidParameterTypeException.cs | 16 +- .../HelperFunctionsGenerator.cs | 214 +++++++---- .../RelativeFileContentProvider.cs | 9 +- .../ScriptGeneratorStrategy.cs | 42 ++- .../ScriptParametersGenerator.cs | 55 ++- ...lugin.ScriptRunner.Javascript.Tests.csproj | 51 +-- .../ScriptGeneratorTests.cs | 303 ++++++++++----- .../ValidScriptExtractorTest.cs | 58 +-- ...tBee.Plugin.ScriptRunner.Javascript.csproj | 32 +- .../JavascriptScriptRunner.cs | 28 +- .../RelativeFileContentProvider.cs | 9 +- .../ScriptGeneratorStrategy.cs | 86 +++-- .../ValidScriptExtractor.cs | 8 +- ...ee.Plugin.ScriptRunner.Python.Tests.csproj | 48 +-- .../ScriptGeneratorStrategyTests.cs | 312 +++++++++++----- .../ValidScriptExtractorTest.cs | 68 ++-- ...criptBee.Plugin.ScriptRunner.Python.csproj | 31 +- .../PythonScriptRunner.cs | 27 +- .../RelativeFileContentProvider.cs | 9 +- .../ScriptGeneratorStrategy.cs | 73 +++- .../ValidScriptExtractor.cs | 8 +- .../DummyModelInheritor.cs | 1 - ...Bee.Plugin.ScriptRunner.TestsCommon.csproj | 25 +- .../EmptyModel.cs | 4 +- .../GenericModel.cs | 10 +- .../NestedGenericModel.cs | 6 +- .../TestModelLoader.cs | 10 +- ScriptBee.slnx | 10 + 49 files changed, 1630 insertions(+), 1018 deletions(-) delete mode 100644 Plugins/ScriptBeeDefaultPlugins.sln diff --git a/Directory.Packages.props b/Directory.Packages.props index 3a43ca62..346a0e25 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -15,10 +15,7 @@ - + @@ -29,10 +26,14 @@ - + - + + + + + @@ -48,4 +49,4 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + \ No newline at end of file diff --git a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/ConsoleHelperFunctions.cs b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/ConsoleHelperFunctions.cs index e84caed7..d1f639fe 100644 --- a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/ConsoleHelperFunctions.cs +++ b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/ConsoleHelperFunctions.cs @@ -19,8 +19,12 @@ public async Task OnUnloadAsync(CancellationToken cancellationToken = default) { var consoleOutput = _consoleStringBuilder.ToString(); - await _helperFunctionsResultService.UploadResultAsync("ConsoleOutput", RunResultDefaultTypes.ConsoleType, - consoleOutput, cancellationToken); + await _helperFunctionsResultService.UploadResultAsync( + "ConsoleOutput", + RunResultDefaultTypes.ConsoleType, + consoleOutput, + cancellationToken + ); } public void ConsoleWrite(object message) diff --git a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/CsvHelperFunctions.cs b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/CsvHelperFunctions.cs index 7bb0b6c6..9a2b2f54 100644 --- a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/CsvHelperFunctions.cs +++ b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/CsvHelperFunctions.cs @@ -14,8 +14,11 @@ public CsvHelperFunctions(IHelperFunctionsResultService helperFunctionsResultSer _helperFunctionsResultService = helperFunctionsResultService; } - public async Task ExportCsvAsync(string fileName, IEnumerable records, - CancellationToken cancellationToken = default) + public async Task ExportCsvAsync( + string fileName, + IEnumerable records, + CancellationToken cancellationToken = default + ) { await using var stream = new MemoryStream(); @@ -24,8 +27,12 @@ public async Task ExportCsvAsync(string fileName, IEnumerable records, await csv.WriteRecordsAsync(records, cancellationToken); - await _helperFunctionsResultService.UploadResultAsync(fileName, RunResultDefaultTypes.FileType, stream, - cancellationToken); + await _helperFunctionsResultService.UploadResultAsync( + fileName, + RunResultDefaultTypes.FileType, + stream, + cancellationToken + ); } public void ExportCsv(string fileName, List records) @@ -37,6 +44,10 @@ public void ExportCsv(string fileName, List records) csv.WriteRecords(records); - _helperFunctionsResultService.UploadResult(fileName, RunResultDefaultTypes.FileType, stream); + _helperFunctionsResultService.UploadResult( + fileName, + RunResultDefaultTypes.FileType, + stream + ); } } diff --git a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/DxWorks.ScriptBee.Plugin.HelperFunctions.Default.csproj b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/DxWorks.ScriptBee.Plugin.HelperFunctions.Default.csproj index 157405f6..ea9e941e 100644 --- a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/DxWorks.ScriptBee.Plugin.HelperFunctions.Default.csproj +++ b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/DxWorks.ScriptBee.Plugin.HelperFunctions.Default.csproj @@ -1,14 +1,9 @@ + + + - - net6.0 - enable - enable - - - - - - - + + + diff --git a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/FileHelperFunctions.cs b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/FileHelperFunctions.cs index e3abbf91..89ce57ae 100644 --- a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/FileHelperFunctions.cs +++ b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/FileHelperFunctions.cs @@ -14,24 +14,47 @@ public FileHelperFunctions(IHelperFunctionsResultService helperFunctionsResultSe public void FileWrite(string fileName, string fileContent) { - _helperFunctionsResultService.UploadResult(fileName, RunResultDefaultTypes.FileType, fileContent); + _helperFunctionsResultService.UploadResult( + fileName, + RunResultDefaultTypes.FileType, + fileContent + ); } - public async Task FileWriteAsync(string fileName, string fileContent, CancellationToken cancellationToken = default) + public async Task FileWriteAsync( + string fileName, + string fileContent, + CancellationToken cancellationToken = default + ) { - await _helperFunctionsResultService.UploadResultAsync(fileName, RunResultDefaultTypes.FileType, fileContent, - cancellationToken); + await _helperFunctionsResultService.UploadResultAsync( + fileName, + RunResultDefaultTypes.FileType, + fileContent, + cancellationToken + ); } - public async Task FileWriteStreamAsync(string fileName, Stream stream, - CancellationToken cancellationToken = default) + public async Task FileWriteStreamAsync( + string fileName, + Stream stream, + CancellationToken cancellationToken = default + ) { - await _helperFunctionsResultService.UploadResultAsync(fileName, RunResultDefaultTypes.FileType, stream, - cancellationToken); + await _helperFunctionsResultService.UploadResultAsync( + fileName, + RunResultDefaultTypes.FileType, + stream, + cancellationToken + ); } public void FileWriteStream(string fileName, Stream stream) { - _helperFunctionsResultService.UploadResult(fileName, RunResultDefaultTypes.FileType, stream); + _helperFunctionsResultService.UploadResult( + fileName, + RunResultDefaultTypes.FileType, + stream + ); } } diff --git a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/JsonHelperFunctions.cs b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/JsonHelperFunctions.cs index 3b365613..4d83eed6 100644 --- a/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/JsonHelperFunctions.cs +++ b/Plugins/HelperFunctions/DxWorks.ScriptBee.Plugin.HelperFunctions.Default/JsonHelperFunctions.cs @@ -13,8 +13,12 @@ public JsonHelperFunctions(IHelperFunctionsResultService helperFunctionsResultSe _helperFunctionsResultService = helperFunctionsResultService; } - public async Task ExportJsonAsync(string fileName, T obj, JsonSerializerSettings? settings = default, - CancellationToken cancellationToken = default) + public async Task ExportJsonAsync( + string fileName, + T obj, + JsonSerializerSettings? settings = default, + CancellationToken cancellationToken = default + ) { var jsonSerializer = JsonSerializer.Create(settings); await using var stream = new MemoryStream(); @@ -25,8 +29,12 @@ public async Task ExportJsonAsync(string fileName, T obj, JsonSerializerSetti await jsonWriter.FlushAsync(cancellationToken); stream.Position = 0; - await _helperFunctionsResultService.UploadResultAsync(fileName, RunResultDefaultTypes.FileType, stream, - cancellationToken); + await _helperFunctionsResultService.UploadResultAsync( + fileName, + RunResultDefaultTypes.FileType, + stream, + cancellationToken + ); } public void ExportJson(string fileName, object obj) @@ -40,7 +48,11 @@ public void ExportJson(string fileName, object obj) jsonWriter.Flush(); stream.Position = 0; - _helperFunctionsResultService.UploadResult(fileName, RunResultDefaultTypes.FileType, stream); + _helperFunctionsResultService.UploadResult( + fileName, + RunResultDefaultTypes.FileType, + stream + ); } public string ConvertJson(object obj) diff --git a/Plugins/ScriptBeeDefaultPlugins.sln b/Plugins/ScriptBeeDefaultPlugins.sln deleted file mode 100644 index 0ac45c24..00000000 --- a/Plugins/ScriptBeeDefaultPlugins.sln +++ /dev/null @@ -1,64 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.HelperFunctions.Default", "HelperFunctions\DxWorks.ScriptBee.Plugin.HelperFunctions.Default\DxWorks.ScriptBee.Plugin.HelperFunctions.Default.csproj", "{2E8D70B7-769A-40AF-84AD-557E15464688}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp\DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.csproj", "{DD819150-B37E-4FAC-8F5C-97F15E925B8A}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests\DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.csproj", "{17833311-0A94-4A3E-B8D0-82455A61B5F5}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript\DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript.csproj", "{5C5B89E8-727C-43E7-BFCC-D04E2A8E50BA}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.Python", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.Python\DxWorks.ScriptBee.Plugin.ScriptRunner.Python.csproj", "{C4DBA0BF-F578-4EDC-85AF-1592004E3F2D}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.Api", "..\DxWorks.ScriptBee.Plugin.Api\DxWorks.ScriptBee.Plugin.Api.csproj", "{C3F5C587-B2DD-4D09-BA43-5EF7FFA49548}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript.Tests", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript.Tests\DxWorks.ScriptBee.Plugin.ScriptRunner.Javascript.Tests.csproj", "{DC00D135-DF9C-4089-A998-0A128CC22549}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.Python.Tests", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.Python.Tests\DxWorks.ScriptBee.Plugin.ScriptRunner.Python.Tests.csproj", "{97DBC61C-6280-4D68-B226-D8ED284FABD0}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DxWorks.ScriptBee.Plugin.ScriptRunner.TestsCommon", "ScriptRunner\DxWorks.ScriptBee.Plugin.ScriptRunner.TestsCommon\DxWorks.ScriptBee.Plugin.ScriptRunner.TestsCommon.csproj", "{5CEBB83D-1429-4461-BB6B-062C037DA4FF}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2E8D70B7-769A-40AF-84AD-557E15464688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2E8D70B7-769A-40AF-84AD-557E15464688}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2E8D70B7-769A-40AF-84AD-557E15464688}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2E8D70B7-769A-40AF-84AD-557E15464688}.Release|Any CPU.Build.0 = Release|Any CPU - {DD819150-B37E-4FAC-8F5C-97F15E925B8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DD819150-B37E-4FAC-8F5C-97F15E925B8A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DD819150-B37E-4FAC-8F5C-97F15E925B8A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DD819150-B37E-4FAC-8F5C-97F15E925B8A}.Release|Any CPU.Build.0 = Release|Any CPU - {17833311-0A94-4A3E-B8D0-82455A61B5F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {17833311-0A94-4A3E-B8D0-82455A61B5F5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {17833311-0A94-4A3E-B8D0-82455A61B5F5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {17833311-0A94-4A3E-B8D0-82455A61B5F5}.Release|Any CPU.Build.0 = Release|Any CPU - {5C5B89E8-727C-43E7-BFCC-D04E2A8E50BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5C5B89E8-727C-43E7-BFCC-D04E2A8E50BA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5C5B89E8-727C-43E7-BFCC-D04E2A8E50BA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5C5B89E8-727C-43E7-BFCC-D04E2A8E50BA}.Release|Any CPU.Build.0 = Release|Any CPU - {C4DBA0BF-F578-4EDC-85AF-1592004E3F2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C4DBA0BF-F578-4EDC-85AF-1592004E3F2D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C4DBA0BF-F578-4EDC-85AF-1592004E3F2D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C4DBA0BF-F578-4EDC-85AF-1592004E3F2D}.Release|Any CPU.Build.0 = Release|Any CPU - {C3F5C587-B2DD-4D09-BA43-5EF7FFA49548}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C3F5C587-B2DD-4D09-BA43-5EF7FFA49548}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C3F5C587-B2DD-4D09-BA43-5EF7FFA49548}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C3F5C587-B2DD-4D09-BA43-5EF7FFA49548}.Release|Any CPU.Build.0 = Release|Any CPU - {DC00D135-DF9C-4089-A998-0A128CC22549}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DC00D135-DF9C-4089-A998-0A128CC22549}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DC00D135-DF9C-4089-A998-0A128CC22549}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DC00D135-DF9C-4089-A998-0A128CC22549}.Release|Any CPU.Build.0 = Release|Any CPU - {97DBC61C-6280-4D68-B226-D8ED284FABD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {97DBC61C-6280-4D68-B226-D8ED284FABD0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {97DBC61C-6280-4D68-B226-D8ED284FABD0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {97DBC61C-6280-4D68-B226-D8ED284FABD0}.Release|Any CPU.Build.0 = Release|Any CPU - {5CEBB83D-1429-4461-BB6B-062C037DA4FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5CEBB83D-1429-4461-BB6B-062C037DA4FF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5CEBB83D-1429-4461-BB6B-062C037DA4FF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5CEBB83D-1429-4461-BB6B-062C037DA4FF}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/CSharpScriptRunnerTests.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/CSharpScriptRunnerTests.cs index 28edb1fc..cb43cb26 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/CSharpScriptRunnerTests.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/CSharpScriptRunnerTests.cs @@ -1,108 +1,116 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using DxWorks.ScriptBee.Plugin.Api; +using DxWorks.ScriptBee.Plugin.Api; using DxWorks.ScriptBee.Plugin.Api.Model; using DxWorks.ScriptBee.Plugin.Api.Services; -using Moq; -using Xunit; +using NSubstitute; namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; public class CSharpScriptRunnerTests { - private readonly CSharpScriptRunner _scriptRunner; - - public CSharpScriptRunnerTests() - { - _scriptRunner = new CSharpScriptRunner(); - } + private readonly CSharpScriptRunner _scriptRunner = new(); [Fact] public async Task GivenEmptyFile_WhenRunAsync_FileIsCompiledWithoutErrors() { - var project = new Mock().Object; - var helperFunctionsContainer = new Mock().Object; - var scriptContent = @" -using System; -using System.Text; -using System.Linq; -using DxWorks.ScriptBee.Plugin.Api; -using DxWorks.ScriptBee.Plugin.Api.Model; + var project = Substitute.For(); + var helperFunctionsContainer = Substitute.For(); + var scriptContent = """ -public class ScriptContent -{ - public void ExecuteScript(IProject project) - { - } -}".Replace(Environment.NewLine, "\r\n"); + using System; + using System.Text; + using System.Linq; + using DxWorks.ScriptBee.Plugin.Api; + using DxWorks.ScriptBee.Plugin.Api.Model; + + public class ScriptContent + { + public void ExecuteScript(IProject project) + { + } + } + """.Replace(Environment.NewLine, "\r\n"); - await _scriptRunner.RunAsync(project, helperFunctionsContainer, new List(), scriptContent, - It.IsAny()); + await _scriptRunner.RunAsync( + project, + helperFunctionsContainer, + new List(), + scriptContent, + Arg.Any() + ); } [Fact] public async Task GivenEmptyFileWithDummyHelperFunction_WhenRunAsync_FileIsCompiledWithoutErrors() { - var project = new Mock().Object; - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new DummyHelperFunctions() - }); - - var scriptContent = @" -using System; -using System.Text; -using System.Linq; -using DxWorks.ScriptBee.Plugin.Api; -using DxWorks.ScriptBee.Plugin.Api.Model; + var project = Substitute.For(); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new DummyHelperFunctions() } + ); -public class ScriptContent -{ - public void ExecuteScript(IProject project) - { - } -}".Replace(Environment.NewLine, "\r\n"); + var scriptContent = """ + + using System; + using System.Text; + using System.Linq; + using DxWorks.ScriptBee.Plugin.Api; + using DxWorks.ScriptBee.Plugin.Api.Model; - await _scriptRunner.RunAsync(project, helperFunctionsContainer, new List(), scriptContent, - It.IsAny()); + public class ScriptContent + { + public void ExecuteScript(IProject project) + { + } + } + """.Replace(Environment.NewLine, "\r\n"); + + await _scriptRunner.RunAsync( + project, + helperFunctionsContainer, + new List(), + scriptContent, + Arg.Any() + ); } - - + [Fact] public async Task GivenEmptyFileWithDummyHelperFunctionWithScriptParameters_WhenRunAsync_FileIsCompiledWithoutErrors() { - var project = new Mock().Object; - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new DummyHelperFunctions() - }); + var project = Substitute.For(); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new DummyHelperFunctions() } + ); var scriptParameters = new List { new() { Name = "a", Type = "string", - Value = "a" - } + Value = "a", + }, }; - - var scriptContent = @" -using System; -using System.Text; -using System.Linq; -using DxWorks.ScriptBee.Plugin.Api; -using DxWorks.ScriptBee.Plugin.Api.Model; -public class ScriptContent -{ - public void ExecuteScript(IProject project, ScriptParameters scriptParameters) - { - } -}".Replace(Environment.NewLine, "\r\n"); + var scriptContent = """ + + using System; + using System.Text; + using System.Linq; + using DxWorks.ScriptBee.Plugin.Api; + using DxWorks.ScriptBee.Plugin.Api.Model; + + public class ScriptContent + { + public void ExecuteScript(IProject project, ScriptParameters scriptParameters) + { + } + } + """.Replace(Environment.NewLine, "\r\n"); - await _scriptRunner.RunAsync(project, helperFunctionsContainer, scriptParameters, scriptContent, - It.IsAny()); + await _scriptRunner.RunAsync( + project, + helperFunctionsContainer, + scriptParameters, + scriptContent, + Arg.Any() + ); } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DummyHelperFunctions.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DummyHelperFunctions.cs index 5cf2b337..bc160599 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DummyHelperFunctions.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DummyHelperFunctions.cs @@ -4,7 +4,5 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; public class DummyHelperFunctions : IHelperFunctions { - public void Print(string value) - { - } + public void Print(string value) { } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.csproj b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.csproj index 736497b6..143be3cd 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.csproj +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.csproj @@ -1,34 +1,12 @@ - - - net6.0 - enable - - false - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - - - PreserveNewest - - + + + + + + + + PreserveNewest + + diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithCollections.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithCollections.cs index 96141210..a80acd2e 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithCollections.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithCollections.cs @@ -1,20 +1,15 @@ -using System.Collections.Generic; -using DxWorks.ScriptBee.Plugin.Api; +using DxWorks.ScriptBee.Plugin.Api; namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; internal class HelperFunctionWithCollections : IHelperFunctions { - public void Method(List? list) - { - } + public void Method(List? list) { } public IDictionary Method() { return new Dictionary(); } - public void Method(List> values) - { - } + public void Method(List> values) { } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithoutGenericMethodThatReturnsVoid.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithoutGenericMethodThatReturnsVoid.cs index 8371afd1..9e99847f 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithoutGenericMethodThatReturnsVoid.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionWithoutGenericMethodThatReturnsVoid.cs @@ -4,7 +4,5 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; internal class HelperFunctionWithoutGenericMethodThatReturnsVoid : IHelperFunctions { - public void Method1(string a, string b) - { - } + public void Method1(string a, string b) { } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsContainer.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsContainer.cs index 0987ab34..eb4e5ccd 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsContainer.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsContainer.cs @@ -1,5 +1,3 @@ -using System; -using System.Collections.Generic; using DxWorks.ScriptBee.Plugin.Api; using DxWorks.ScriptBee.Plugin.Api.Services; diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsGeneratorTests.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsGeneratorTests.cs index ff51d94c..513329b2 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsGeneratorTests.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/HelperFunctionsGeneratorTests.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; using DxWorks.ScriptBee.Plugin.Api; -using Xunit; namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; @@ -10,14 +7,14 @@ public class HelperFunctionsGeneratorTests [Fact] public void GivenNonGenericVoidMethod_WhenCreateSyntaxTree_ThenCorrectSyntaxTreeIsGenerated() { - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new HelperFunctionWithoutGenericMethodThatReturnsVoid() - }); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new HelperFunctionWithoutGenericMethodThatReturnsVoid() } + ); var syntaxTree = HelperFunctionsGenerator.CreateSyntaxTree(helperFunctionsContainer); - Assert.Equal(@"using System; + Assert.Equal( + @"using System; namespace DxWorks.ScriptBee.Plugin.Api { @@ -29,20 +26,25 @@ public static void Method1(string a, string b) HelperFunctionWithoutGenericMethodThatReturnsVoid.Method1(a, b); } } -}".Replace(Environment.NewLine, "\r\n"), syntaxTree.ToString()); +}".Replace(Environment.NewLine, "\r\n"), + syntaxTree.ToString() + ); } [Fact] public void GivenNonGenericMethodThatReturnsSomething_WhenCreateSyntaxTree_ThenCorrectSyntaxTreeIsGenerated() { - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new HelperFunctionWithoutGenericMethodsThatReturnSomething() - }); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List + { + new HelperFunctionWithoutGenericMethodsThatReturnSomething(), + } + ); var syntaxTree = HelperFunctionsGenerator.CreateSyntaxTree(helperFunctionsContainer); - Assert.Equal(@"using System; + Assert.Equal( + @"using System; namespace DxWorks.ScriptBee.Plugin.Api { @@ -59,20 +61,22 @@ public static DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.Something GetSo return HelperFunctionWithoutGenericMethodsThatReturnSomething.GetSomething(a, b, c); } } -}".Replace(Environment.NewLine, "\r\n"), syntaxTree.ToString()); +}".Replace(Environment.NewLine, "\r\n"), + syntaxTree.ToString() + ); } [Fact] public void GivenGenericMethods_WhenCreateSyntaxTree_ThenCorrectSyntaxTreeIsGenerated() { - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new HelperFunctionsWithGenericMethods() - }); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new HelperFunctionsWithGenericMethods() } + ); var syntaxTree = HelperFunctionsGenerator.CreateSyntaxTree(helperFunctionsContainer); - Assert.Equal(@"using System; + Assert.Equal( + @"using System; namespace DxWorks.ScriptBee.Plugin.Api { @@ -89,20 +93,22 @@ public static DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests.Something Metho return HelperFunctionsWithGenericMethods.Method(arg1, arg2, arg3); } } -}".Replace(Environment.NewLine, "\r\n"), syntaxTree.ToString()); +}".Replace(Environment.NewLine, "\r\n"), + syntaxTree.ToString() + ); } [Fact] public void GivenGenericMethodWithConstrains_WhenCreateSyntaxTree_ThenCorrectSyntaxTreeIsGenerated() { - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new HelperFunctionsWithGenericMethodWithConstrains() - }); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new HelperFunctionsWithGenericMethodWithConstrains() } + ); var syntaxTree = HelperFunctionsGenerator.CreateSyntaxTree(helperFunctionsContainer); - Assert.Equal(@"using System; + Assert.Equal( + @"using System; namespace DxWorks.ScriptBee.Plugin.Api { @@ -127,20 +133,22 @@ public static void Method() HelperFunctionsWithGenericMethodWithConstrains.Method(); } } -}".Replace(Environment.NewLine, "\r\n"), syntaxTree.ToString()); +}".Replace(Environment.NewLine, "\r\n"), + syntaxTree.ToString() + ); } [Fact] public void GivenMethodWithCollections_WhenCreateSyntaxTree_ThenCorrectSyntaxTreeIsGenerated() { - var helperFunctionsContainer = new HelperFunctionsContainer(new List - { - new HelperFunctionWithCollections() - }); + var helperFunctionsContainer = new HelperFunctionsContainer( + new List { new HelperFunctionWithCollections() } + ); var syntaxTree = HelperFunctionsGenerator.CreateSyntaxTree(helperFunctionsContainer); - Assert.Equal(@"using System; + Assert.Equal( + @"using System; namespace DxWorks.ScriptBee.Plugin.Api { @@ -162,6 +170,8 @@ public static void Method(System.Collections.Generic.List() where T1 : class, new() where T2 : struct - where T3 : Something - { - } + where T3 : Something { } public void Method() where T1 : class? where T2 : notnull, T1 - where T3 : unmanaged - { - } + where T3 : unmanaged { } public void Method() where T : Something? - where TR : Something, ISomething - { - } + where TR : Something, ISomething { } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ISomething.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ISomething.cs index 76bc4116..aa53199f 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ISomething.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ISomething.cs @@ -1,5 +1,3 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; -public interface ISomething -{ -} +public interface ISomething { } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptGeneratorStrategyTests.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptGeneratorStrategyTests.cs index 0ca533bc..65c2d340 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptGeneratorStrategyTests.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptGeneratorStrategyTests.cs @@ -1,33 +1,37 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using DxWorks.ScriptBee.Plugin.ScriptRunner.TestsCommon; -using ScriptBee.Scripts.ScriptSampleGenerators; -using Xunit; +using DxWorks.ScriptBee.Plugin.ScriptRunner.TestsCommon; +using ScriptBee.Service.Project.ProjectStructure; namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; public class ScriptGeneratorStrategyTests { - private readonly SampleCodeGenerator _sampleCodeGenerator; - - public ScriptGeneratorStrategyTests() - { - _sampleCodeGenerator = new SampleCodeGenerator(new ScriptGeneratorStrategy(), new HashSet - { - new TestModelLoader().GetType().Module.Name - }); - } + private readonly SampleCodeGenerator _sampleCodeGenerator = new( + new ScriptGeneratorStrategy(), + new HashSet { new TestModelLoader().GetType().Module.Name } + ); [Theory] [InlineData( "ScriptSampleTestStrings/CSharpDummyModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] - public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDummyModel(string pathToModel, - string pathToSampleCode) + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] + public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDummyModel( + string pathToModel, + string pathToSampleCode + ) { - var modelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new DummyModel() }); + var modelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new DummyModel() }, + TestContext.Current.CancellationToken + ); Assert.Equal(2, sampleCode.Count); @@ -42,15 +46,31 @@ public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDummyMod [InlineData( "ScriptSampleTestStrings/CSharpDummyModel.txt", "ScriptSampleTestStrings/RecursiveModel/CSharpRecursiveModel_RecursiveModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnRecursiveModel( - string pathToDummyModel, string pathToMainModel, string pathToSampleCode) + string pathToDummyModel, + string pathToMainModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToMainModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new RecursiveModel() }); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToMainModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new RecursiveModel() }, + TestContext.Current.CancellationToken + ); Assert.Equal(3, sampleCode.Count); @@ -67,15 +87,31 @@ public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnRecursiv [InlineData( "ScriptSampleTestStrings/CSharpDummyModel.txt", "ScriptSampleTestStrings/DummyModelInheritor/CSharpDummyModelInheritor_DummyModelInheritor.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDummyModelInheritor( - string pathToDummyModel, string pathToMainModel, string pathToSampleCode) + string pathToDummyModel, + string pathToMainModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToMainModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new DummyModelInheritor() }); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToMainModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new DummyModelInheritor() }, + TestContext.Current.CancellationToken + ); Assert.Equal(3, sampleCode.Count); @@ -92,21 +128,33 @@ public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDummyMod [InlineData( "ScriptSampleTestStrings/CSharpDummyModel.txt", "ScriptSampleTestStrings/RecursiveModel/CSharpRecursiveModel_RecursiveModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnRecursiveModel( - string pathToDummyModel, string pathToMainModel, string pathToSampleCode) + string pathToDummyModel, + string pathToMainModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToMainModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var models = new List - { - new DummyModel(), - new RecursiveModel() - }; - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(models); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var mainModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToMainModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var models = new List { new DummyModel(), new RecursiveModel() }; + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + models, + TestContext.Current.CancellationToken + ); Assert.Equal(3, sampleCode.Count); @@ -126,19 +174,46 @@ public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnRecu "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_RecursiveModel.txt", "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_RecursiveModel2.txt", "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_DeepModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] - public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDeepModel(string pathToDummyModel, - string pathToEmptyModel, string pathToRecursiveModel, string pathToRecursiveModel2, string pathToMainModel, - string pathToSampleCode) + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] + public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDeepModel( + string pathToDummyModel, + string pathToEmptyModel, + string pathToRecursiveModel, + string pathToRecursiveModel2, + string pathToMainModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var emptyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToEmptyModel); - var recursiveModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToRecursiveModel); - var recursiveModel2Content = await RelativeFileContentProvider.GetFileContentAsync(pathToRecursiveModel2); - var deepModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToMainModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new DeepModel() }); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var emptyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToEmptyModel, + TestContext.Current.CancellationToken + ); + var recursiveModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToRecursiveModel, + TestContext.Current.CancellationToken + ); + var recursiveModel2Content = await RelativeFileContentProvider.GetFileContentAsync( + pathToRecursiveModel2, + TestContext.Current.CancellationToken + ); + var deepModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToMainModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new DeepModel() }, + TestContext.Current.CancellationToken + ); Assert.Equal(6, sampleCode.Count); @@ -164,17 +239,41 @@ public async Task GenerateSampleCode_MainModelGivenAsObject_ShouldReturnDeepMode "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_RecursiveModel.txt", "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_RecursiveModel2.txt", "ScriptSampleTestStrings/DeepModel/CSharpDeepModel_DeepModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] - public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnDeepModel(string pathToDummyModel, - string pathToEmptyModel, string pathToRecursiveModel, string pathToRecursiveModel2, string pathToMainModel, - string pathToSampleCode) + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] + public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnDeepModel( + string pathToDummyModel, + string pathToEmptyModel, + string pathToRecursiveModel, + string pathToRecursiveModel2, + string pathToMainModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var emptyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToEmptyModel); - var recursiveModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToRecursiveModel); - var recursiveModel2Content = await RelativeFileContentProvider.GetFileContentAsync(pathToRecursiveModel2); - var deepModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToMainModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var emptyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToEmptyModel, + TestContext.Current.CancellationToken + ); + var recursiveModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToRecursiveModel, + TestContext.Current.CancellationToken + ); + var recursiveModel2Content = await RelativeFileContentProvider.GetFileContentAsync( + pathToRecursiveModel2, + TestContext.Current.CancellationToken + ); + var deepModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToMainModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); var models = new List { @@ -182,10 +281,13 @@ public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnDeep new RecursiveModel(), new RecursiveModel2(), new EmptyModel(), - new DeepModel() + new DeepModel(), }; - var sampleCode = await _sampleCodeGenerator.GetSampleCode(models); + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + models, + TestContext.Current.CancellationToken + ); Assert.Equal(6, sampleCode.Count); @@ -207,14 +309,26 @@ public async Task GenerateSampleCode_ModelsGivenAsListOfObjects_ShouldReturnDeep [Theory] [InlineData( "ScriptSampleTestStrings/DummyModelWithMethods/CSharpDummyModel_WithMethods.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] - public async Task GenerateSampleCode_DummyModelWithMethods_ShouldReturnDummyModelWithMethods(string pathToModel, - string pathToSampleCode) + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] + public async Task GenerateSampleCode_DummyModelWithMethods_ShouldReturnDummyModelWithMethods( + string pathToModel, + string pathToSampleCode + ) { - var modelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new DummyModelWithMethods() }); + var modelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new DummyModelWithMethods() }, + TestContext.Current.CancellationToken + ); Assert.Equal(2, sampleCode.Count); @@ -228,16 +342,26 @@ public async Task GenerateSampleCode_DummyModelWithMethods_ShouldReturnDummyMode [Theory] [InlineData( "ScriptSampleTestStrings/DummyModelWithMethods/CSharpDummyModel_WithMethods_Expando.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] - public async Task - GenerateSampleCode_WithCSharpStrategy_DummyModelWithMethodsExpando_ShouldReturnCSharpSimpleModelWithMethods( - string pathToModel, string pathToSampleCode) + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] + public async Task GenerateSampleCode_WithCSharpStrategy_DummyModelWithMethodsExpando_ShouldReturnCSharpSimpleModelWithMethods( + string pathToModel, + string pathToSampleCode + ) { - var modelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - var sampleCode = - await _sampleCodeGenerator.GetSampleCode(new List { new DummyModelWithMethodsExpando() }); + var modelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new DummyModelWithMethodsExpando() }, + TestContext.Current.CancellationToken + ); Assert.Equal(2, sampleCode.Count); @@ -255,22 +379,46 @@ public async Task "ScriptSampleTestStrings/GenericModel/CSharpGenericModel_GenericModel.txt", "ScriptSampleTestStrings/GenericModel/CSharpGenericModel_GenericModel2.txt", "ScriptSampleTestStrings/GenericModel/CSharpGenericModel_NestedGenericModel.txt", - "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt")] + "ScriptSampleTestStrings/SampleCode/CSharp_SampleCode.txt" + )] public async Task GenerateSampleCode_NestedGenericModel_ModelGivenAsObject_ShouldReturnGenericModel_CSharp( - string pathToDummyModel, string pathToDummyModelWithMethods, string pathToGenericModel, + string pathToDummyModel, + string pathToDummyModelWithMethods, + string pathToGenericModel, string pathToGenericModel2, - string pathToNestedGenericModel, string pathToSampleCode) + string pathToNestedGenericModel, + string pathToSampleCode + ) { - var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModel); - var dummyModelWithMethodsContent = - await RelativeFileContentProvider.GetFileContentAsync(pathToDummyModelWithMethods); - var genericModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToGenericModel); - var genericModel2Content = await RelativeFileContentProvider.GetFileContentAsync(pathToGenericModel2); - var nestedGenericModelContent = await RelativeFileContentProvider.GetFileContentAsync(pathToNestedGenericModel); - var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync(pathToSampleCode); - - - var sampleCode = await _sampleCodeGenerator.GetSampleCode(new List { new NestedGenericModel() }); + var dummyModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModel, + TestContext.Current.CancellationToken + ); + var dummyModelWithMethodsContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToDummyModelWithMethods, + TestContext.Current.CancellationToken + ); + var genericModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToGenericModel, + TestContext.Current.CancellationToken + ); + var genericModel2Content = await RelativeFileContentProvider.GetFileContentAsync( + pathToGenericModel2, + TestContext.Current.CancellationToken + ); + var nestedGenericModelContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToNestedGenericModel, + TestContext.Current.CancellationToken + ); + var sampleCodeContent = await RelativeFileContentProvider.GetFileContentAsync( + pathToSampleCode, + TestContext.Current.CancellationToken + ); + + var sampleCode = await _sampleCodeGenerator.GetSampleCode( + new List { new NestedGenericModel() }, + TestContext.Current.CancellationToken + ); Assert.Equal(6, sampleCode.Count); diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptParametersGeneratorTest.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptParametersGeneratorTest.cs index b6bbeafd..89ef66ab 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptParametersGeneratorTest.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests/ScriptParametersGeneratorTest.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; using DxWorks.ScriptBee.Plugin.Api.Model; using DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Exceptions; -using Xunit; namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Tests; @@ -13,9 +10,14 @@ public void GivenEmptyParameters_WhenGenerateScriptParameters_ThenEmptyClassIsGe { var scriptParametersCode = GetScriptParametersCode(new List()); - Assert.Equal(@"public class ScriptParameters -{ -}".Replace(Environment.NewLine, "\r\n"), scriptParametersCode); + Assert.Equal( + """ + public class ScriptParameters + { + } + """.Replace(Environment.NewLine, "\r\n"), + scriptParametersCode + ); } [Fact] @@ -27,36 +29,41 @@ public void GivenParameters_WhenGenerateScriptParameters_ThenClassWithProperties { Name = "a", Type = "string", - Value = "value" + Value = "value", }, new() { Name = "b", Type = "integer", - Value = "5" + Value = 5, }, new() { Name = "c", Type = "boolean", - Value = "true" + Value = true, }, new() { Name = "d", Type = "float", - Value = "6.5" - } + Value = 6.5, + }, }; var scriptParametersCode = GetScriptParametersCode(parameters); - Assert.Equal(@"public class ScriptParameters -{ - public string a { get; set; } = ""value""; - public integer b { get; set; } = 5; - public boolean c { get; set; } = true; - public float d { get; set; } = 6.5F; -}".Replace(Environment.NewLine, "\r\n"), scriptParametersCode); + Assert.Equal( + """ + public class ScriptParameters + { + public string a { get; set; } = "value"; + public integer b { get; set; } = 5; + public boolean c { get; set; } = true; + public float d { get; set; } = 6.5; + } + """.Replace(Environment.NewLine, "\r\n"), + scriptParametersCode + ); } [Fact] @@ -68,39 +75,41 @@ public void GivenParametersWithNullValue_WhenGenerateScriptParameters_ThenClassW { Name = "a", Type = "string", - Value = null + Value = null, }, new() { Name = "b", Type = "integer", - Value = null + Value = null, }, new() { Name = "c", Type = "boolean", - Value = null + Value = null, }, new() { Name = "d", Type = "float", - Value = null - } + Value = null, + }, }; var scriptParametersCode = GetScriptParametersCode(parameters); - Assert.Equal(@"public class ScriptParameters -{ - public string a { get; set; } - - public integer b { get; set; } - - public boolean c { get; set; } - - public float d { get; set; } -}".Replace(Environment.NewLine, "\r\n"), scriptParametersCode); + Assert.Equal( + """ + public class ScriptParameters + { + public string a { get; set; } + public integer b { get; set; } + public boolean c { get; set; } + public float d { get; set; } + } + """.Replace(Environment.NewLine, "\r\n"), + scriptParametersCode + ); } [Fact] @@ -112,8 +121,8 @@ public void GivenInvalidParameterType_WhenGenerateScriptParameters_ThenException { Name = "a", Type = "invalid", - Value = "123" - } + Value = "123", + }, }; Assert.Throws(() => GetScriptParametersCode(parameters)); @@ -121,7 +130,6 @@ public void GivenInvalidParameterType_WhenGenerateScriptParameters_ThenException private static string GetScriptParametersCode(IEnumerable parameters) { - return ScriptParametersGenerator.GenerateScriptParameters(parameters) - .ToFullString(); + return ScriptParametersGenerator.GenerateScriptParameters(parameters).ToFullString(); } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/CSharpScriptRunner.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/CSharpScriptRunner.cs index b98464ed..033b4917 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/CSharpScriptRunner.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/CSharpScriptRunner.cs @@ -12,21 +12,33 @@ public class CSharpScriptRunner : IScriptRunner { public string Language => "csharp"; - public async Task RunAsync(IProject project, IHelperFunctionsContainer helperFunctionsContainer, - IEnumerable parameters, string scriptContent, CancellationToken cancellationToken = default) + public async Task RunAsync( + IProject project, + IHelperFunctionsContainer helperFunctionsContainer, + IEnumerable parameters, + string scriptContent, + CancellationToken cancellationToken = default + ) { var validScript = new ScriptGeneratorStrategy().ExtractValidScript(scriptContent); - var compiledScript = - await Task.Run(() => CompileScript(validScript, parameters, helperFunctionsContainer, cancellationToken), - cancellationToken); + var compiledScript = await Task.Run( + () => + CompileScript(validScript, parameters, helperFunctionsContainer, cancellationToken), + cancellationToken + ); - await Task.Run(() => ExecuteScript(project, compiledScript, helperFunctionsContainer), - cancellationToken); + await Task.Run( + () => ExecuteScript(project, compiledScript, helperFunctionsContainer), + cancellationToken + ); } - private static void ExecuteScript(IProject project, Assembly compiledScriptAssembly, - IHelperFunctionsContainer helperFunctionsContainer) + private static void ExecuteScript( + IProject project, + Assembly compiledScriptAssembly, + IHelperFunctionsContainer helperFunctionsContainer + ) { PopulateHelperFunctionFields(compiledScriptAssembly, helperFunctionsContainer); @@ -47,26 +59,24 @@ private static void ExecuteScript(IProject project, Assembly compiledScriptAssem { case 1 when IsProjectType(methodParameters[0].ParameterType): { - var scriptContentObject = compiledScriptAssembly.CreateInstance(type.Name); + var scriptContentObject = compiledScriptAssembly.CreateInstance( + type.Name + ); - method.Invoke(scriptContentObject, new object[] - { - project - }); + method.Invoke(scriptContentObject, [project]); return; } - case 2 when IsProjectType(methodParameters[0].ParameterType) && - methodParameters[1].ParameterType.Name == "ScriptParameters": + case 2 + when IsProjectType(methodParameters[0].ParameterType) + && methodParameters[1].ParameterType.Name == "ScriptParameters": { - var scriptContentObject = compiledScriptAssembly.CreateInstance(type.Name); + var scriptContentObject = compiledScriptAssembly.CreateInstance( + type.Name + ); var scriptParameters = CreateScriptParameters(compiledScriptAssembly); - method.Invoke(scriptContentObject, new[] - { - project, - scriptParameters - }); + method.Invoke(scriptContentObject, [project, scriptParameters]); return; } @@ -78,7 +88,9 @@ private static void ExecuteScript(IProject project, Assembly compiledScriptAssem private static object? CreateScriptParameters(Assembly compiledScriptAssembly) { - var scriptParameters = compiledScriptAssembly.GetTypes().FirstOrDefault(t => t.Name == "ScriptParameters"); + var scriptParameters = compiledScriptAssembly + .GetTypes() + .FirstOrDefault(t => t.Name == "ScriptParameters"); return scriptParameters is null ? null : Activator.CreateInstance(scriptParameters); } @@ -89,10 +101,13 @@ private static bool IsProjectType(MemberInfo type) } // todo add tests - private static void PopulateHelperFunctionFields(Assembly compiledScriptAssembly, - IHelperFunctionsContainer helperFunctionsContainer) + private static void PopulateHelperFunctionFields( + Assembly compiledScriptAssembly, + IHelperFunctionsContainer helperFunctionsContainer + ) { - var helperFunctionClass = compiledScriptAssembly.GetTypes() + var helperFunctionClass = compiledScriptAssembly + .GetTypes() .FirstOrDefault(t => t.FullName == "ScriptContent"); if (helperFunctionClass is null) @@ -102,32 +117,44 @@ private static void PopulateHelperFunctionFields(Assembly compiledScriptAssembly foreach (var fieldInfo in helperFunctionClass.GetFields()) { - var helperFunction = helperFunctionsContainer.GetFunctions() + var helperFunction = helperFunctionsContainer + .GetFunctions() .FirstOrDefault(h => h.GetType().FullName == fieldInfo.FieldType.FullName); fieldInfo.SetValue(null, helperFunction); } } - private static Assembly CompileScript(string script, IEnumerable parameters, - IHelperFunctionsContainer helperFunctionsContainer, CancellationToken cancellationToken) + private static Assembly CompileScript( + string script, + IEnumerable parameters, + IHelperFunctionsContainer helperFunctionsContainer, + CancellationToken cancellationToken + ) { - var members = HelperFunctionsGenerator.GetMemberDeclarationSyntaxList(helperFunctionsContainer); + var members = HelperFunctionsGenerator.GetMemberDeclarationSyntaxList( + helperFunctionsContainer + ); var scriptParameters = ScriptParametersGenerator.GenerateScriptParameters(parameters); - script = script.Replace("public void ExecuteScript", - $"{scriptParameters}{members}public void ExecuteScript"); + script = script.Replace( + "public void ExecuteScript", + $"{scriptParameters}{members}public void ExecuteScript" + ); - // todo when writing script instead of class with execute method + // todo when writing script instead of class with execute method // https://stackoverflow.com/questions/13601412/compilation-errors-when-dealing-with-c-sharp-script-using-roslyn var syntaxTree = CSharpSyntaxTree.ParseText(script, cancellationToken: cancellationToken); CheckErrors(syntaxTree, cancellationToken); - var compilation = CSharpCompilation.Create("Compilation") - .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) - .WithOverflowChecks(true) - .WithOptimizationLevel(OptimizationLevel.Release)) + var compilation = CSharpCompilation + .Create("Compilation") + .WithOptions( + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) + .WithOverflowChecks(true) + .WithOptimizationLevel(OptimizationLevel.Release) + ) .AddReferences(FindReferences()) .AddSyntaxTrees(syntaxTree); @@ -136,7 +163,8 @@ private static Assembly CompileScript(string script, IEnumerable d.GetMessage()) + var errorMessage = emitResult + .Diagnostics.Select(d => d.GetMessage()) .Aggregate("", (current, error) => current + error + "\n"); throw new CompilationErrorException(errorMessage); } @@ -146,7 +174,9 @@ private static Assembly CompileScript(string script, IEnumerable FindReferences() { - return AppDomain.CurrentDomain.GetAssemblies() + return AppDomain + .CurrentDomain.GetAssemblies() .Where(a => !a.IsDynamic) .Where(a => !string.IsNullOrEmpty(a.Location)) .Select(assembly => MetadataReference.CreateFromFile(assembly.Location)); diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.csproj b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.csproj index 211a2326..0632261d 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.csproj +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.csproj @@ -1,27 +1,23 @@ + + + + + - - net6.0 - enable - enable - + + + - - - - - - - - - - PreserveNewest - - - PreserveNewest - - - - - + + + PreserveNewest + + + PreserveNewest + + + + + diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/CompilationErrorException.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/CompilationErrorException.cs index 6792052b..342ec662 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/CompilationErrorException.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/CompilationErrorException.cs @@ -1,15 +1,4 @@ -using System.Runtime.Serialization; - -namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Exceptions; +namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Exceptions; [Serializable] -public class CompilationErrorException : Exception - { - public CompilationErrorException(string message) : base(message) - { - } - - protected CompilationErrorException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } -} +public class CompilationErrorException(string message) : Exception(message); diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/InvalidParameterTypeException.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/InvalidParameterTypeException.cs index 941e7d57..7f52d281 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/InvalidParameterTypeException.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/Exceptions/InvalidParameterTypeException.cs @@ -1,15 +1,5 @@ -using System.Runtime.Serialization; - -namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Exceptions; +namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp.Exceptions; [Serializable] -public class InvalidParameterTypeException : Exception -{ - public InvalidParameterTypeException(string parameterType) : base($"Invalid parameter type: {parameterType}") - { - } - - protected InvalidParameterTypeException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } -} +public class InvalidParameterTypeException(string parameterType) + : Exception($"Invalid parameter type: {parameterType}"); diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/HelperFunctionsGenerator.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/HelperFunctionsGenerator.cs index 012ff7fb..d51eb616 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/HelperFunctionsGenerator.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/HelperFunctionsGenerator.cs @@ -13,26 +13,37 @@ public static class HelperFunctionsGenerator // https://roslynquoter.azurewebsites.net/ public static SyntaxList GetMemberDeclarationSyntaxList( - IHelperFunctionsContainer helperFunctionsContainer) + IHelperFunctionsContainer helperFunctionsContainer + ) { var syntaxTree = CreateSyntaxTree(helperFunctionsContainer); - return (((syntaxTree.GetRoot() as CompilationUnitSyntax)! - .Members.First() as NamespaceDeclarationSyntax)! - .Members.First() as ClassDeclarationSyntax)!.Members; + return ( + ( + (syntaxTree.GetRoot() as CompilationUnitSyntax)!.Members.First() + as NamespaceDeclarationSyntax + )!.Members.First() as ClassDeclarationSyntax + )!.Members; } public static SyntaxTree CreateSyntaxTree(IHelperFunctionsContainer helperFunctionsContainer) { - var helperFunctionTypes = helperFunctionsContainer.GetFunctions() + var helperFunctionTypes = helperFunctionsContainer + .GetFunctions() .Select(f => f.GetType()) .ToList(); - var fieldDeclarationSyntaxes = helperFunctionTypes - .Select(t => FieldDeclaration( + var fieldDeclarationSyntaxes = helperFunctionTypes.Select(t => + FieldDeclaration( VariableDeclaration(IdentifierName(t.FullName ?? t.Name)) - .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(t.Name))))) - .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))); + .WithVariables( + SingletonSeparatedList(VariableDeclarator(Identifier(t.Name))) + ) + ) + .WithModifiers( + TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) + ) + ); var methodDeclarationSyntaxes = helperFunctionTypes .SelectMany(t => t.GetMethods().Where(m => m.DeclaringType == t)) @@ -48,12 +59,17 @@ public static SyntaxTree CreateSyntaxTree(IHelperFunctionsContainer helperFuncti var classDeclaration = ClassDeclaration(nameof(HelperFunctions)) .WithMembers(List(fieldDeclarationSyntaxes.Concat(methodDeclarationSyntaxes))) - .WithModifiers(TokenList(Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.PartialKeyword))); + .WithModifiers( + TokenList(Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.PartialKeyword)) + ); var compilationUnit = CompilationUnit() - .WithMembers(SingletonList( - NamespaceDeclaration(IdentifierName(typeof(HelperFunctions).Namespace)) - .WithMembers(SingletonList(classDeclaration)))) + .WithMembers( + SingletonList( + NamespaceDeclaration(IdentifierName(typeof(HelperFunctions).Namespace)) + .WithMembers(SingletonList(classDeclaration)) + ) + ) .WithUsings(GenerateUsings()) .NormalizeWhitespace(); @@ -62,50 +78,85 @@ public static SyntaxTree CreateSyntaxTree(IHelperFunctionsContainer helperFuncti private static SyntaxList GenerateUsings() { - return List(new List - { - UsingDirective(IdentifierName("System")) - }); + return List(new List { UsingDirective(IdentifierName("System")) }); } - private static MemberDeclarationSyntax GenerateMethodDeclarationWithReturnValue(MethodInfo methodInfo) + private static MemberDeclarationSyntax GenerateMethodDeclarationWithReturnValue( + MethodInfo methodInfo + ) { return GenerateMethodSignature(methodInfo) - .WithBody(Block(SingletonList(ReturnStatement( - InvocationExpression( - GenerateHelperFunctionMethodCall(methodInfo)) - .WithArgumentList( - ArgumentList( - SeparatedList( - methodInfo.GetParameters().Select(parameterInfo => - Argument(IdentifierName(parameterInfo.Name)))))))))); + .WithBody( + Block( + SingletonList( + ReturnStatement( + InvocationExpression(GenerateHelperFunctionMethodCall(methodInfo)) + .WithArgumentList( + ArgumentList( + SeparatedList( + methodInfo + .GetParameters() + .Select(parameterInfo => + Argument(IdentifierName(parameterInfo.Name)) + ) + ) + ) + ) + ) + ) + ) + ); } private static MethodDeclarationSyntax GenerateVoidMethodDeclaration(MethodInfo methodInfo) { return GenerateMethodSignature(methodInfo) - .WithBody(Block(SingletonList(ExpressionStatement( - InvocationExpression( - GenerateHelperFunctionMethodCall(methodInfo)) - .WithArgumentList( - ArgumentList( - SeparatedList( - methodInfo.GetParameters().Select(parameterInfo => - Argument(IdentifierName(parameterInfo.Name)))))))))); + .WithBody( + Block( + SingletonList( + ExpressionStatement( + InvocationExpression(GenerateHelperFunctionMethodCall(methodInfo)) + .WithArgumentList( + ArgumentList( + SeparatedList( + methodInfo + .GetParameters() + .Select(parameterInfo => + Argument(IdentifierName(parameterInfo.Name)) + ) + ) + ) + ) + ) + ) + ) + ); } private static MethodDeclarationSyntax GenerateMethodSignature(MethodInfo methodInfo) { - var methodDeclarationSyntax = - MethodDeclaration(GenerateReturnValue(methodInfo.ReturnParameter), methodInfo.Name) - .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) - .WithParameterList(ParameterList(SeparatedList( - methodInfo.GetParameters().Select(parameterInfo => Parameter(Identifier(parameterInfo.Name)) - .WithType(GenerateParameter(parameterInfo))) + var methodDeclarationSyntax = MethodDeclaration( + GenerateReturnValue(methodInfo.ReturnParameter), + methodInfo.Name + ) + .WithModifiers( + TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) + ) + .WithParameterList( + ParameterList( + SeparatedList( + methodInfo + .GetParameters() + .Select(parameterInfo => + Parameter(Identifier(parameterInfo.Name)) + .WithType(GenerateParameter(parameterInfo)) + ) ) - )); + ) + ); - var typeParameterSyntaxes = methodInfo.GetGenericArguments() + var typeParameterSyntaxes = methodInfo + .GetGenericArguments() .Select(type => TypeParameter(Identifier(type.Name))) .ToList(); @@ -114,19 +165,21 @@ private static MethodDeclarationSyntax GenerateMethodSignature(MethodInfo method return methodDeclarationSyntax; } - var hasGenericConstrains = methodInfo.GetGenericArguments() + var hasGenericConstrains = methodInfo + .GetGenericArguments() .Any(x => x.GetGenericParameterConstraints().Any()); - var methodDeclarationWithGenericParameters = methodDeclarationSyntax - .WithTypeParameterList(TypeParameterList(SeparatedList(typeParameterSyntaxes))); - + var methodDeclarationWithGenericParameters = methodDeclarationSyntax.WithTypeParameterList( + TypeParameterList(SeparatedList(typeParameterSyntaxes)) + ); if (!hasGenericConstrains) { return methodDeclarationWithGenericParameters; } - var genericConstraintsSyntaxes = methodInfo.GetGenericArguments() + var genericConstraintsSyntaxes = methodInfo + .GetGenericArguments() .Select(type => (type, GenerateGenericConstrains(type).ToList())) .Where(tuple => tuple.Item2.Any()) .Select(tuple => @@ -135,12 +188,15 @@ private static MethodDeclarationSyntax GenerateMethodSignature(MethodInfo method return TypeParameterConstraintClause(type.Name) .WithConstraints( SeparatedList( - genericConstrains.Select(x => TypeConstraint(IdentifierName(x))))); + genericConstrains.Select(x => TypeConstraint(IdentifierName(x))) + ) + ); }) .ToList(); - return methodDeclarationWithGenericParameters - .WithConstraintClauses(List(genericConstraintsSyntaxes)); + return methodDeclarationWithGenericParameters.WithConstraintClauses( + List(genericConstraintsSyntaxes) + ); } private static IEnumerable GenerateGenericConstrains(Type type) @@ -148,7 +204,8 @@ private static IEnumerable GenerateGenericConstrains(Type type) var genericParameterConstraints = type.GetGenericParameterConstraints(); foreach (var genericParameterConstraint in genericParameterConstraints) { - var constraintName = genericParameterConstraint.FullName ?? genericParameterConstraint.Name; + var constraintName = + genericParameterConstraint.FullName ?? genericParameterConstraint.Name; if (constraintName == typeof(ValueType).FullName) { yield return "struct"; @@ -164,35 +221,57 @@ private static IEnumerable GenerateGenericConstrains(Type type) yield break; } - if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) + if ( + type.GenericParameterAttributes.HasFlag( + GenericParameterAttributes.ReferenceTypeConstraint + ) + ) { yield return "class"; } - if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) + if ( + type.GenericParameterAttributes.HasFlag( + GenericParameterAttributes.DefaultConstructorConstraint + ) + ) { yield return "new()"; } - if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) + if ( + type.GenericParameterAttributes.HasFlag( + GenericParameterAttributes.NotNullableValueTypeConstraint + ) + ) { yield return "struct"; } } - private static MemberAccessExpressionSyntax GenerateHelperFunctionMethodCall(MethodBase methodInfo) + private static MemberAccessExpressionSyntax GenerateHelperFunctionMethodCall( + MethodBase methodInfo + ) { SimpleNameSyntax nameSyntax = !methodInfo.IsGenericMethod ? IdentifierName(methodInfo.Name) : GenericName(methodInfo.Name) - .WithTypeArgumentList(TypeArgumentList(SeparatedList( - methodInfo.GetGenericArguments().Select(t => IdentifierName(t.Name)).OfType() - ))); + .WithTypeArgumentList( + TypeArgumentList( + SeparatedList( + methodInfo + .GetGenericArguments() + .Select(t => IdentifierName(t.Name)) + .OfType() + ) + ) + ); return MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName(methodInfo.DeclaringType.Name), - nameSyntax); + nameSyntax + ); } private static TypeSyntax GenerateParameter(ParameterInfo parameterInfo) @@ -221,7 +300,6 @@ private static TypeSyntax GenerateReturnValue(ParameterInfo returnTypeParameterI return returnTypeSyntax; } - private static NameSyntax GetParameterSyntax(Type parameterType) { var parameterTypeFullName = parameterType.FullName ?? parameterType.Name; @@ -234,9 +312,13 @@ private static NameSyntax GetParameterSyntax(Type parameterType) parameterTypeFullName = parameterTypeFullName[..index]; parameterSyntax = GenericName(parameterTypeFullName) - .WithTypeArgumentList(TypeArgumentList(SeparatedList( - parameterType.GetGenericArguments().Select(GetParameterSyntax) - ))); + .WithTypeArgumentList( + TypeArgumentList( + SeparatedList( + parameterType.GetGenericArguments().Select(GetParameterSyntax) + ) + ) + ); } else { @@ -252,8 +334,8 @@ private static bool IsParameterNullable(ParameterInfo parameterInfo) { var nullabilityInfo = new NullabilityInfoContext().Create(parameterInfo); - return nullabilityInfo.ReadState == NullabilityState.Nullable || - nullabilityInfo.WriteState == NullabilityState.Nullable; + return nullabilityInfo.ReadState == NullabilityState.Nullable + || nullabilityInfo.WriteState == NullabilityState.Nullable; } private static string ConvertSystemTypeToPrimitiveType(string type) @@ -277,7 +359,7 @@ private static string ConvertSystemTypeToPrimitiveType(string type) "System.Decimal" => "decimal", "System.DateTime" => "DateTime", "System.Void" => "void", - _ => type + _ => type, }; } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/RelativeFileContentProvider.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/RelativeFileContentProvider.cs index 56a79fba..4157467b 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/RelativeFileContentProvider.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/RelativeFileContentProvider.cs @@ -4,9 +4,14 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp; internal static class RelativeFileContentProvider { - public static Task GetFileContentAsync(string path, CancellationToken cancellationToken = default) + public static Task GetFileContentAsync( + string path, + CancellationToken cancellationToken = default + ) { return File.ReadAllTextAsync( - Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, path), cancellationToken); + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, path), + cancellationToken + ); } } diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptGeneratorStrategy.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptGeneratorStrategy.cs index 5cd86f06..ff150de9 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptGeneratorStrategy.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptGeneratorStrategy.cs @@ -5,7 +5,8 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp; public class ScriptGeneratorStrategy : IScriptGeneratorStrategy { - private const string StartComment = "// Only the code written in the ExecuteScript method will be executed"; + private const string StartComment = + "// Only the code written in the ExecuteScript method will be executed"; public string Language => "csharp"; public string Extension => ".cs"; @@ -21,7 +22,11 @@ public string GenerateClassName(Type classType) return $"public class {className}"; } - public string GenerateClassName(Type classType, Type baseClassType, out HashSet baseClassGenericTypes) + public string GenerateClassName( + Type classType, + Type baseClassType, + out HashSet baseClassGenericTypes + ) { var baseTypeName = GetTypeName(baseClassType, out baseClassGenericTypes); var className = GetTypeName(classType); @@ -38,22 +43,35 @@ public string GenerateClassEnd() return "}"; } - public string GenerateField(string fieldModifier, Type fieldType, string fieldName, - out HashSet genericTypes) + public string GenerateField( + string fieldModifier, + Type fieldType, + string fieldName, + out HashSet genericTypes + ) { var fieldTypeName = GetTypeName(fieldType, out genericTypes); return $" {fieldModifier} {fieldTypeName} {fieldName};"; } - public string GenerateProperty(string propertyModifier, Type propertyType, string propertyName, - out HashSet genericTypes) + public string GenerateProperty( + string propertyModifier, + Type propertyType, + string propertyName, + out HashSet genericTypes + ) { var propertyTypeName = GetTypeName(propertyType, out genericTypes); return $" {propertyModifier} {propertyTypeName} {propertyName} {{ get; set; }}"; } - public string GenerateMethod(string methodModifier, Type methodType, string methodName, - List> methodParams, out HashSet genericTypes) + public string GenerateMethod( + string methodModifier, + Type methodType, + string methodName, + List> methodParams, + out HashSet genericTypes + ) { var stringBuilder = new StringBuilder(); @@ -97,7 +115,9 @@ public string GenerateModelDeclaration(string modelType) public async Task GenerateSampleCode() { - return await RelativeFileContentProvider.GetFileContentAsync("SampleCodes/CSharpSampleCode.txt"); + return await RelativeFileContentProvider.GetFileContentAsync( + "SampleCodes/CSharpSampleCode.txt" + ); } public string GenerateEmptyClass() @@ -107,7 +127,9 @@ public string GenerateEmptyClass() public async Task GenerateImports() { - return await RelativeFileContentProvider.GetFileContentAsync("SampleCodes/CSharpImports.txt"); + return await RelativeFileContentProvider.GetFileContentAsync( + "SampleCodes/CSharpImports.txt" + ); } public string GetStartComment() diff --git a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptParametersGenerator.cs b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptParametersGenerator.cs index 7b608431..107485fa 100644 --- a/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptParametersGenerator.cs +++ b/Plugins/ScriptRunner/DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp/ScriptParametersGenerator.cs @@ -7,9 +7,11 @@ namespace DxWorks.ScriptBee.Plugin.ScriptRunner.CSharp; -public static class ScriptParametersGenerator +public static class ScriptParametersGenerator { - public static ClassDeclarationSyntax GenerateScriptParameters(IEnumerable parameters) + public static ClassDeclarationSyntax GenerateScriptParameters( + IEnumerable parameters + ) { return ClassDeclaration("ScriptParameters") .AddModifiers(Token(SyntaxKind.PublicKeyword)) @@ -17,7 +19,9 @@ public static ClassDeclarationSyntax GenerateScriptParameters(IEnumerable