Skip to content

Commit

Permalink
Clear CodeDom and add ICodeDomService
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Jan 27, 2024
1 parent f0893a8 commit 8a630d8
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion RevitAddin.CommandLoader/Revit/Commands/CommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion RevitAddin.CommandLoader/Revit/Commands/CommandTestGist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion RevitAddin.CommandLoader/RevitAddin.CommandLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

<PropertyGroup>
<PackageId>RevitAddin.CommandLoader</PackageId>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<ProjectGuid>{82070359-36DA-4441-A59D-9018B6A8B348}</ProjectGuid>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -51,6 +55,7 @@ public Assembly GenerateCode(params CodeCompileUnit[] compilationUnits)
{
compilerParametes.ReferencedAssemblies.Add(keyAssembly.Value.Location);
}

#endregion

CompilerResults results = provider.CompileAssemblyFromDom(compilerParametes, compilationUnits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.IO;

namespace RevitAddin.CommandLoader.Services
namespace RevitAddin.CommandLoader.Services.CodeDom
{
public class CodeProviderService
{
Expand Down
11 changes: 11 additions & 0 deletions RevitAddin.CommandLoader/Services/CodeDom/ICodeDomService.cs
Original file line number Diff line number Diff line change
@@ -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);

}
}
17 changes: 17 additions & 0 deletions RevitAddin.CommandLoader/Services/CodeDomFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
23 changes: 14 additions & 9 deletions RevitAddin.CommandLoader/ViewModels/CompileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -109,7 +114,7 @@ private void InitializeCompile()
{
Task.Run(() =>
{
new CodeDomService().GenerateCode(CodeSamples.Command);
CodeDomFactory.Instance.GenerateCode(CodeSamples.Command);
});
}
#endregion
Expand Down

0 comments on commit 8a630d8

Please sign in to comment.