From 8a630d81e52be6f8169211332a863640ef8febab Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Sat, 27 Jan 2024 11:57:40 -0300 Subject: [PATCH] Clear CodeDom and add `ICodeDomService` --- CHANGELOG.md | 3 +++ .../Revit/Commands/CommandTest.cs | 2 +- .../Revit/Commands/CommandTestGist.cs | 2 +- .../Revit/Commands/CommandTestGistFiles.cs | 3 ++- .../RevitAddin.CommandLoader.csproj | 2 +- .../Services/{ => CodeDom}/CodeDomService.cs | 25 +++++++++++-------- .../{ => CodeDom}/CodeProviderService.cs | 2 +- .../Services/CodeDom/ICodeDomService.cs | 11 ++++++++ .../Services/CodeDomFactory.cs | 17 +++++++++++++ .../ViewModels/CompileViewModel.cs | 23 ++++++++++------- 10 files changed, 66 insertions(+), 24 deletions(-) rename RevitAddin.CommandLoader/Services/{ => CodeDom}/CodeDomService.cs (73%) rename RevitAddin.CommandLoader/Services/{ => CodeDom}/CodeProviderService.cs (97%) create mode 100644 RevitAddin.CommandLoader/Services/CodeDom/ICodeDomService.cs create mode 100644 RevitAddin.CommandLoader/Services/CodeDomFactory.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 885396b..9e7af6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [1.0.6] / 2024-01-27 ### Features - Using `ricaun.Revit.UI.Tasks` +### Updated +- Add `ICodeDomService` interface +- Add `CodeDomFactory` class ### Remove - Remove `Revit.Async` diff --git a/RevitAddin.CommandLoader/Revit/Commands/CommandTest.cs b/RevitAddin.CommandLoader/Revit/Commands/CommandTest.cs index 30ce3a2..f67666a 100644 --- a/RevitAddin.CommandLoader/Revit/Commands/CommandTest.cs +++ b/RevitAddin.CommandLoader/Revit/Commands/CommandTest.cs @@ -18,7 +18,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme try { - CodeDomService codeDomService = new CodeDomService(); + var codeDomService = CodeDomFactory.Instance; var assembly = codeDomService.GenerateCode( CodeSamples.CommandVersion, CodeSamples.CommandTask, diff --git a/RevitAddin.CommandLoader/Revit/Commands/CommandTestGist.cs b/RevitAddin.CommandLoader/Revit/Commands/CommandTestGist.cs index 7188f96..97f2f8e 100644 --- a/RevitAddin.CommandLoader/Revit/Commands/CommandTestGist.cs +++ b/RevitAddin.CommandLoader/Revit/Commands/CommandTestGist.cs @@ -21,7 +21,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme try { - CodeDomService codeDomService = new CodeDomService() { UseLegacyCodeDom = true }; + var codeDomService = CodeDomFactory.Instance; var assembly = codeDomService.GenerateCode(gistContent); App.CreateCommands(assembly); diff --git a/RevitAddin.CommandLoader/Revit/Commands/CommandTestGistFiles.cs b/RevitAddin.CommandLoader/Revit/Commands/CommandTestGistFiles.cs index 1868606..c1367d2 100644 --- a/RevitAddin.CommandLoader/Revit/Commands/CommandTestGistFiles.cs +++ b/RevitAddin.CommandLoader/Revit/Commands/CommandTestGistFiles.cs @@ -20,7 +20,8 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme try { - CodeDomService codeDomService = new CodeDomService(); + System.Console.WriteLine(gistFilesContent.Length); + var codeDomService = CodeDomFactory.Instance; var assembly = codeDomService.GenerateCode(gistFilesContent); App.CreateCommands(assembly); diff --git a/RevitAddin.CommandLoader/RevitAddin.CommandLoader.csproj b/RevitAddin.CommandLoader/RevitAddin.CommandLoader.csproj index b2856c2..d54f34b 100644 --- a/RevitAddin.CommandLoader/RevitAddin.CommandLoader.csproj +++ b/RevitAddin.CommandLoader/RevitAddin.CommandLoader.csproj @@ -89,7 +89,7 @@ RevitAddin.CommandLoader - 1.0.5 + 1.0.6 {82070359-36DA-4441-A59D-9018B6A8B348} diff --git a/RevitAddin.CommandLoader/Services/CodeDomService.cs b/RevitAddin.CommandLoader/Services/CodeDom/CodeDomService.cs similarity index 73% rename from RevitAddin.CommandLoader/Services/CodeDomService.cs rename to RevitAddin.CommandLoader/Services/CodeDom/CodeDomService.cs index 5cca921..8437963 100644 --- a/RevitAddin.CommandLoader/Services/CodeDomService.cs +++ b/RevitAddin.CommandLoader/Services/CodeDom/CodeDomService.cs @@ -2,24 +2,29 @@ using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Reflection; -namespace RevitAddin.CommandLoader.Services +namespace RevitAddin.CommandLoader.Services.CodeDom { - public class CodeDomService + public class CodeDomService : ICodeDomService { - public bool UseLegacyCodeDom { get; set; } - public string CompilerOptions { get; set; } - public CodeDomService SetDefines(params string[] defines) + private CodeDomProvider provider; + + public CodeDomService(CodeDomProvider provider) + { + this.provider = provider; + } + private string CompilerOptions { get; set; } + public ICodeDomService SetDefines(params string[] defines) { - CompilerOptions += $" /define:{string.Join(";", defines).Replace(" ", "")}"; + CompilerOptions = $" /define:{string.Join(";", defines).Replace(" ", "")}"; return this; } - public Assembly GenerateCode(params string[] sources) + + public Assembly GenerateCode(params string[] sourceCode) { - var compilationUnits = sources + var compilationUnits = sourceCode .Select(s => new CodeSnippetCompileUnit(s)) .ToArray(); @@ -28,7 +33,6 @@ public Assembly GenerateCode(params string[] sources) public Assembly GenerateCode(params CodeCompileUnit[] compilationUnits) { - CodeDomProvider provider = CodeProviderService.GetCSharpCodeProvider(UseLegacyCodeDom); CompilerParameters compilerParametes = new CompilerParameters(); compilerParametes.GenerateExecutable = false; @@ -51,6 +55,7 @@ public Assembly GenerateCode(params CodeCompileUnit[] compilationUnits) { compilerParametes.ReferencedAssemblies.Add(keyAssembly.Value.Location); } + #endregion CompilerResults results = provider.CompileAssemblyFromDom(compilerParametes, compilationUnits); diff --git a/RevitAddin.CommandLoader/Services/CodeProviderService.cs b/RevitAddin.CommandLoader/Services/CodeDom/CodeProviderService.cs similarity index 97% rename from RevitAddin.CommandLoader/Services/CodeProviderService.cs rename to RevitAddin.CommandLoader/Services/CodeDom/CodeProviderService.cs index 5105b4b..2b2d9a7 100644 --- a/RevitAddin.CommandLoader/Services/CodeProviderService.cs +++ b/RevitAddin.CommandLoader/Services/CodeDom/CodeProviderService.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.IO; -namespace RevitAddin.CommandLoader.Services +namespace RevitAddin.CommandLoader.Services.CodeDom { public class CodeProviderService { diff --git a/RevitAddin.CommandLoader/Services/CodeDom/ICodeDomService.cs b/RevitAddin.CommandLoader/Services/CodeDom/ICodeDomService.cs new file mode 100644 index 0000000..51f3913 --- /dev/null +++ b/RevitAddin.CommandLoader/Services/CodeDom/ICodeDomService.cs @@ -0,0 +1,11 @@ +using System.Reflection; + +namespace RevitAddin.CommandLoader.Services.CodeDom +{ + public interface ICodeDomService + { + Assembly GenerateCode(params string[] sourceCode); + public ICodeDomService SetDefines(params string[] defines); + + } +} \ No newline at end of file diff --git a/RevitAddin.CommandLoader/Services/CodeDomFactory.cs b/RevitAddin.CommandLoader/Services/CodeDomFactory.cs new file mode 100644 index 0000000..484d765 --- /dev/null +++ b/RevitAddin.CommandLoader/Services/CodeDomFactory.cs @@ -0,0 +1,17 @@ +using RevitAddin.CommandLoader.Services.CodeDom; + +namespace RevitAddin.CommandLoader.Services +{ + public class CodeDomFactory + { + public static ICodeDomService Instance { get; private set; } = CreateCodeDomService(); + + private static ICodeDomService CreateCodeDomService() + { + var provider = CodeProviderService.GetCSharpCodeProvider(); + + return new CodeDomService(provider); + } + + } +} \ No newline at end of file diff --git a/RevitAddin.CommandLoader/ViewModels/CompileViewModel.cs b/RevitAddin.CommandLoader/ViewModels/CompileViewModel.cs index e8641d4..c0d31cb 100644 --- a/RevitAddin.CommandLoader/ViewModels/CompileViewModel.cs +++ b/RevitAddin.CommandLoader/ViewModels/CompileViewModel.cs @@ -25,7 +25,6 @@ public class CompileViewModel : ObservableObject #else CodeSamples.Command; #endif - public bool UseLegacyCodeDom { get; set; } = false; public bool EnableText { get; set; } = true; public IAsyncRelayCommand Command => new AsyncRelayCommand(CompileText); #endregion @@ -79,16 +78,22 @@ await App.RevitTask.Run((uiapp) => var version = uiapp.Application.VersionNumber; try { - var codeDomService = new CodeDomService() - { - UseLegacyCodeDom = UseLegacyCodeDom - }; + var codeDomService = CodeDomFactory.Instance; - var assembly = codeDomService + var defines = new[] { + $"REVIT{version}", + $"Revit{version}", #if DEBUG - .SetDefines("DEBUG") + "DEBUG", #endif - .SetDefines($"REVIT{version}", $"Revit{version}") + }; + + var assembly = codeDomService + //#if DEBUG + // .SetDefines("DEBUG") + //#endif + // .SetDefines($"REVIT{version}", $"Revit{version}") + .SetDefines(defines) .GenerateCode(sources); App.CreateCommands(assembly); @@ -109,7 +114,7 @@ private void InitializeCompile() { Task.Run(() => { - new CodeDomService().GenerateCode(CodeSamples.Command); + CodeDomFactory.Instance.GenerateCode(CodeSamples.Command); }); } #endregion