Skip to content

Commit

Permalink
getting rid of metaprogramming package
Browse files Browse the repository at this point in the history
  • Loading branch information
m0sa committed Apr 5, 2018
1 parent fdb62ad commit 6e74e32
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Meta-programming
----------------

- Create a new project
- `Install-Package StackExchange.Precompilation.Metaprogramming -Pre`
- `Install-Package StackExchange.Precompilation -Pre`
- Implement the ICompileModule interface
- `Install-Package StackExchange.Precompilation.Build -Pre` in the target project
- [Configure your new module](https://github.com/StackExchange/StackExchange.Precompilation/blob/fd536b764983e2674a4549b7be6f26e971190c1e/Test.ConsoleApp/App.config#L8) in the target project's web.config or app.config
Expand Down
41 changes: 11 additions & 30 deletions StackExchange.Precompilation.Build/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ public Compilation(PrecompilationCommandLineArgs precompilationCommandLineArgs)
CscArgs = CSharpCommandLineParser.Default.Parse(_precompilationCommandLineArgs.Arguments, _precompilationCommandLineArgs.BaseDirectory, sdkDirectory);
Diagnostics = new List<Diagnostic>(CscArgs.Errors);

// load those before anything else hooks into our AssemlbyResolve...
var compilationModules = LoadModules().ToList();
// load those before anything else hooks into our AssemlbyResolve.
var loader = new PrecompilationModuleLoader(PrecompilerSection.Current);
loader.ModuleInitializationFailed += (module, ex) =>
{
Diagnostics.Add(Diagnostic.Create(
FailedToCreateModule,
Location.Create(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, new TextSpan(), new LinePositionSpan()),
module.Type,
ex.Message));
};
var compilationModules = loader.LoadedModules;

if (Diagnostics.Any())
{
Expand Down Expand Up @@ -319,34 +328,6 @@ private Project CreateProject(AdhocWorkspace workspace, RazorParser razorParser)
return workspace.AddProject(projectInfo);
}

private IEnumerable<ICompileModule> LoadModules()
{
var compilationSection = PrecompilerSection.Current;
if (compilationSection == null) yield break;

foreach(var module in compilationSection.CompileModules.Cast<CompileModuleElement>())
{
ICompileModule compileModule = null;
try
{
var type = Type.GetType(module.Type, true);
compileModule = Activator.CreateInstance(type, true) as ICompileModule;
}
catch(Exception ex)
{
Diagnostics.Add(Diagnostic.Create(
FailedToCreateModule,
Location.Create(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, new TextSpan(), new LinePositionSpan()),
module.Type,
ex.Message));
}
if (compileModule != null)
{
yield return compileModule;
}
}
}

private RazorParser CreateRazorParser(Workspace workspace, CancellationToken cancellationToken)
{
var dir = PrecompilerSection.Current?.RazorCache?.Directory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Web" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.7.0" IncludeAssets="compile" ExcludeAssets="analyzers" />
<ProjectReference Include="..\StackExchange.Precompilation.Metaprogramming\StackExchange.Precompilation.Metaprogramming.csproj">
<Project>{E42F58D0-658F-4EB8-A031-45DE998F769D}</Project>
<Name>StackExchange.Precompilation.Metaprogramming</Name>
<ProjectReference Include="..\StackExchange.Precompilation\StackExchange.Precompilation.csproj">
<Project>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Project>
<Name>StackExchange.Precompilation</Name>
</ProjectReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.7.0" IncludeAssets="compile" ExcludeAssets="analyzers" />
<None Include="*.targets" Pack="true" PackagePath="build" />
<None Include="App.config" Pack="true" PackagePath="tools\$(AssemblyName).exe.config" />
</ItemGroup>
Expand Down

This file was deleted.

59 changes: 0 additions & 59 deletions StackExchange.Precompilation.Metaprogramming/app.config

This file was deleted.

6 changes: 0 additions & 6 deletions StackExchange.Precompilation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.Precompilatio
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.Precompilation", "StackExchange.Precompilation\StackExchange.Precompilation.csproj", "{3C0A90F1-B19E-4305-AB71-3CD31C7D0F4D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.Precompilation.Metaprogramming", "StackExchange.Precompilation.Metaprogramming\StackExchange.Precompilation.Metaprogramming.csproj", "{E42F58D0-658F-4EB8-A031-45DE998F769D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{5958EFE5-C8D6-4759-9A8E-8C64558314FD}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
Expand Down Expand Up @@ -56,10 +54,6 @@ Global
{3C0A90F1-B19E-4305-AB71-3CD31C7D0F4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C0A90F1-B19E-4305-AB71-3CD31C7D0F4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C0A90F1-B19E-4305-AB71-3CD31C7D0F4D}.Release|Any CPU.Build.0 = Release|Any CPU
{E42F58D0-658F-4EB8-A031-45DE998F769D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E42F58D0-658F-4EB8-A031-45DE998F769D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E42F58D0-658F-4EB8-A031-45DE998F769D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E42F58D0-658F-4EB8-A031-45DE998F769D}.Release|Any CPU.Build.0 = Release|Any CPU
{5FCAECC3-787B-473F-A372-783D0C235190}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FCAECC3-787B-473F-A372-783D0C235190}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FCAECC3-787B-473F-A372-783D0C235190}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StackExchange.Precompilation
/// <code language="xml"><![CDATA[
/// <configuration>
/// <configSections>
/// <section name="stackExchange.precompiler" type="StackExchange.Precompilation.PrecompilerSection, StackExchange.Precompilation.Metaprogramming" />
/// <section name="stackExchange.precompiler" type="StackExchange.Precompilation.PrecompilerSection, StackExchange.Precompilation" />
/// </configSections>
/// <stackExchange.precompiler>
/// <modules>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace StackExchange.Precompilation
{
internal class PrecompilationModuleLoader
{
/// <summary>Fires when a <see cref="CompileModuleElement" /> cannot be resolved to an actual <see cref="System.Type" />.</summary>
/// <remarks>Register the handlers before touching <see cref="LoadedModules" />.</remarks>
public event Action<CompileModuleElement, Exception> ModuleInitializationFailed;

/// <summary>Gets a cached collection of loaded modules.</summary>
public ICollection<ICompileModule> LoadedModules => _loadedModules.Value;

private readonly Lazy<ICollection<ICompileModule>> _loadedModules;
private readonly PrecompilerSection _configuration;

public PrecompilationModuleLoader(PrecompilerSection configuration)
{
_configuration = configuration;
_loadedModules = new Lazy<ICollection<ICompileModule>>(() =>
{
var result = new List<ICompileModule>();
if (_configuration == null || _configuration.CompileModules == null)
{
return result;
}

foreach(var module in _configuration.CompileModules.Cast<CompileModuleElement>())
{
try
{
var type = Type.GetType(module.Type, true);
if (Activator.CreateInstance(type, true) is ICompileModule cm)
{
result.Add(cm);
}
else
{
throw new TypeLoadException($"{module.Type} is not an {nameof(ICompileModule)}.");
}
}
catch(Exception ex)
{
ModuleInitializationFailed?.Invoke(module, ex);
}
}
return result;
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public class PrecompilerSection : ConfigurationSection

[ConfigurationProperty("razorCache", IsRequired = false)]
public RazorCacheElement RazorCache => (RazorCacheElement)base["razorCache"];

}
}
2 changes: 1 addition & 1 deletion Test.ConsoleApp/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="stackExchange.precompiler" type="StackExchange.Precompilation.PrecompilerSection, StackExchange.Precompilation.Metaprogramming" />
<section name="stackExchange.precompiler" type="StackExchange.Precompilation.PrecompilerSection, StackExchange.Precompilation" />
</configSections>
<stackExchange.precompiler>
<modules>
Expand Down
6 changes: 3 additions & 3 deletions Test.Module/Test.Module.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\StackExchange.Precompilation.Metaprogramming\StackExchange.Precompilation.Metaprogramming.csproj">
<Project>{E42F58D0-658F-4EB8-A031-45DE998F769D}</Project>
<Name>StackExchange.Precompilation.Metaprogramming</Name>
<ProjectReference Include="..\StackExchange.Precompilation\StackExchange.Precompilation.csproj">
<Project>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Project>
<Name>StackExchange.Precompilation</Name>
</ProjectReference>
</ItemGroup>
</Project>

0 comments on commit 6e74e32

Please sign in to comment.