Skip to content

Commit

Permalink
BatchCompiler now loads the assemblies defined in spark settings befo…
Browse files Browse the repository at this point in the history
…re compilation

- Can now use name, full name or absolute path to an assembly
  • Loading branch information
bounav committed Apr 24, 2024
1 parent d5d2f96 commit 3d8701a
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Castle.MonoRail.Views.Spark/SparkViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ISparkViewEngine Engine

var viewFolder = new ViewSourceLoaderWrapper(this);

var batchCompiler = new RoslynBatchCompiler();
var batchCompiler = new RoslynBatchCompiler(new SparkSettings());

this._engine =
new SparkViewEngine(
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.JsTests/Generate.ashx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ProcessRequest(HttpContext context)

var partialProvider = new DefaultPartialProvider();

var batchCompiler = new RoslynBatchCompiler();
var batchCompiler = new RoslynBatchCompiler(new SparkSettings());

var engine = new SparkViewEngine(
settings,
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Python.Tests/PythonViewCompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Init()

_compiler = new PythonViewCompiler(_settings);

_languageFactory = new PythonLanguageFactory(new RoslynBatchCompiler(), _settings);
_languageFactory = new PythonLanguageFactory(new RoslynBatchCompiler(this._settings), _settings);

// Load up assemblies
IronPython.Hosting.Python.CreateEngine();
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Python/Compiler/PythonViewCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void CompileView(IEnumerable<IList<Chunk>> viewTemplates, IEnume
{
GenerateSourceCode(viewTemplates, allResources);

var compiler = new RoslynBatchCompiler();
var compiler = new RoslynBatchCompiler(new SparkSettings());
var assembly = compiler.Compile(settings.Debug, "csharp", null, new[] { SourceCode }, settings.ExcludeAssemblies);
CompiledType = assembly.GetType(ViewClassFullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Ruby.Tests/RubyViewCompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Init()
Debug = true
};
_compiler = new RubyViewCompiler(this._settings);
_languageFactory = new RubyLanguageFactory(new RoslynBatchCompiler(), this._settings);
_languageFactory = new RubyLanguageFactory(new RoslynBatchCompiler(this._settings), this._settings);

//load assemblies
global::IronRuby.Ruby.CreateEngine();
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Ruby/Compiler/RubyViewCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void CompileView(IEnumerable<IList<Chunk>> viewTemplates, IEnume
{
GenerateSourceCode(viewTemplates, allResources);

var compiler = new RoslynBatchCompiler();
var compiler = new RoslynBatchCompiler(new SparkSettings());
var assembly = compiler.Compile(settings.Debug, "csharp", null, new[] { SourceCode }, settings.ExcludeAssemblies);
CompiledType = assembly.GetType(ViewClassFullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Web.Mvc.Ruby.Tests/HtmlHelperExtensionsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void BuildingScriptHeader()
{
var settings = new SparkSettings();

var batchCompiler = new RoslynBatchCompiler();
var batchCompiler = new RoslynBatchCompiler(settings);

var languageFactory = new RubyLanguageFactoryWithExtensions(batchCompiler, settings);

Expand Down
6 changes: 3 additions & 3 deletions src/Spark.Web.Mvc.Tests/Compiler/CSharpViewCompilerTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CSharpViewCompilerTester
[SetUp]
public void Init()
{
this.batchCompiler = new RoslynBatchCompiler();
this.batchCompiler = new RoslynBatchCompiler(new SparkSettings());
}

private static void DoCompileView(ViewCompiler compiler, IList<Chunk> chunks)
Expand Down Expand Up @@ -306,7 +306,7 @@ public void LenientSilentNullDoesNotCauseWarningCS0168()
var settings = new SparkSettings<SparkView>
{
NullBehaviour = NullBehaviour.Lenient
}.AddAssembly(typeof(Spark.Tests.Models.Comment).Assembly.FullName);
}.AddAssembly(typeof(Spark.Tests.Models.Comment).Assembly);

var compiler = new CSharpViewCompiler(this.batchCompiler, settings);

Expand All @@ -327,7 +327,7 @@ public void LenientOutputNullDoesNotCauseWarningCS0168()
var settings = new SparkSettings<SparkView>
{
NullBehaviour = NullBehaviour.Lenient
}.AddAssembly(typeof(Spark.Tests.Models.Comment).Assembly.FullName);
}.AddAssembly(typeof(Spark.Tests.Models.Comment).Assembly);

var compiler = new CSharpViewCompiler(this.batchCompiler, settings);
var chunks = new Chunk[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class VisualBasicViewCompilerTester
public void Init()
{
this.batchCompiler =
new RoslynBatchCompiler();
new RoslynBatchCompiler(new SparkSettings());
}

private static void DoCompileView(ViewCompiler compiler, IList<Chunk> chunks)
Expand Down
2 changes: 1 addition & 1 deletion src/Spark.Web.Tests/Compiler/SourceMappingTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Init()

_viewFolder = new InMemoryViewFolder();

var batchCompiler = new RoslynBatchCompiler();
var batchCompiler = new RoslynBatchCompiler(new SparkSettings());

_engine = new SparkViewEngine(
settings,
Expand Down
20 changes: 15 additions & 5 deletions src/Spark/Compiler/BatchCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ public class CodeDomCompilerException(string message, CompilerResults results) :
public class RoslynBatchCompiler : IBatchCompiler
{
private readonly IEnumerable<IRoslynCompilationLink> links;
private readonly ISparkSettings settings;

private readonly IList<PortableExecutableReference> References;

public RoslynBatchCompiler() : this(new IRoslynCompilationLink[] { new CSharpLink(), new VisualBasicLink() })
public RoslynBatchCompiler(ISparkSettings settings) : this(new IRoslynCompilationLink[] { new CSharpLink(), new VisualBasicLink() }, settings)
{
}

public RoslynBatchCompiler(IEnumerable<IRoslynCompilationLink> links)
public RoslynBatchCompiler(IEnumerable<IRoslynCompilationLink> links, ISparkSettings settings)
{
this.links = links;
this.settings = settings;
this.References = new List<PortableExecutableReference>();
}

Expand All @@ -217,7 +219,7 @@ public bool AddAssembly(string assemblyDll)

if (!File.Exists(file))
{
// check framework or dedicated runtime app folder
// Check framework or dedicated runtime app folder
var path = Path.GetDirectoryName(typeof(object).Assembly.Location);
file = Path.Combine(path, assemblyDll);
if (!File.Exists(file))
Expand Down Expand Up @@ -337,8 +339,16 @@ public Assembly Compile(bool debug, string languageOrExtension, string outputAss
#endif

// TODO: Is this needed?
this.AddAssembly(typeof(System.Drawing.Color));

//this.AddAssembly(typeof(System.Drawing.Color));
foreach(var assemblyLocation in settings.UseAssemblies)
{
// Assumes full path to assemblies
if (assemblyLocation.EndsWith(".dll"))
{
this.AddAssembly(assemblyLocation);
}
}

foreach (var currentAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (currentAssembly.IsDynamic())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
//
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Spark.Compiler.ChunkVisitors;
using Spark.Parser.Code;
Expand Down Expand Up @@ -75,7 +76,11 @@ public void UsingAssembly(string assemblyString)
if (_assemblyAdded.ContainsKey(assemblyString))
return;

var assembly = Assembly.Load(assemblyString);
// Works with absolute path to .dll or assembly name (including fullname)
var assembly = assemblyString.EndsWith(".dll", ignoreCase: true, CultureInfo.InvariantCulture)
? Assembly.LoadFile(assemblyString)
: Assembly.Load(assemblyString);

_assemblyAdded.Add(assemblyString, assembly);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Spark/Compiler/Roslyn/CSharpLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void ThrowIfCompilationNotSuccessful(EmitResult result)

foreach (var diagnostic in failures)
{
sb.Append(diagnostic.Id).Append(":").AppendLine(diagnostic.GetMessage());
sb.AppendLine(diagnostic.ToString());
}

throw new RoslynCompilerException(sb.ToString(), result);
Expand Down
4 changes: 4 additions & 0 deletions src/Spark/ISparkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public interface ISparkSettings : IParserSettings
LanguageType DefaultLanguage { get; }

IEnumerable<string> UseNamespaces { get; }

/// <summary>
/// A list of name, fullname or absolute paths to .dll for assemblies to load before compiling views.
/// </summary>
IEnumerable<string> UseAssemblies { get; }

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions src/Spark/SparkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public SparkSettings(string rootPath)
public IEnumerable<string> UseNamespaces => _useNamespaces;

private readonly IList<string> _useAssemblies;

/// <summary>
/// A list of names, full names or absolute paths to .dll for assemblies to load before compiling views.
/// </summary>
public IEnumerable<string> UseAssemblies => _useAssemblies;

private readonly IList<string> _excludeAssemblies;
Expand Down Expand Up @@ -166,7 +170,7 @@ public SparkSettings SetDefaultLanguage(LanguageType language)
}

/// <summary>
/// Adds the full name of an assembly for the view compiler to be aware of.
/// Adds the name, full name or absolute path of an assembly for the view compiler to be aware of.
/// </summary>
/// <param name="assembly">The full name of an assembly.</param>
/// <returns></returns>
Expand All @@ -182,7 +186,7 @@ public SparkSettings AddAssembly(string assembly)
/// </summary>
public SparkSettings AddAssembly(Assembly assembly)
{
_useAssemblies.Add(assembly.FullName);
_useAssemblies.Add(assembly.Location);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Xpark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The Model in the template is an XDocument loaded from the source.

var partialProvider = new DefaultPartialProvider();

var batchCompiler = new RoslynBatchCompiler();
var batchCompiler = new RoslynBatchCompiler(new SparkSettings());

var engine = new SparkViewEngine(
settings,
Expand Down

0 comments on commit 3d8701a

Please # to comment.