diff --git a/src/Castle.MonoRail.Views.Spark/SparkViewFactory.cs b/src/Castle.MonoRail.Views.Spark/SparkViewFactory.cs index 2dd13be8..9565d90d 100644 --- a/src/Castle.MonoRail.Views.Spark/SparkViewFactory.cs +++ b/src/Castle.MonoRail.Views.Spark/SparkViewFactory.cs @@ -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( diff --git a/src/Spark.JsTests/Generate.ashx.cs b/src/Spark.JsTests/Generate.ashx.cs index 586f0736..5e9b5ec8 100644 --- a/src/Spark.JsTests/Generate.ashx.cs +++ b/src/Spark.JsTests/Generate.ashx.cs @@ -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, diff --git a/src/Spark.Python.Tests/PythonViewCompilerTests.cs b/src/Spark.Python.Tests/PythonViewCompilerTests.cs index 644e580a..259d984f 100644 --- a/src/Spark.Python.Tests/PythonViewCompilerTests.cs +++ b/src/Spark.Python.Tests/PythonViewCompilerTests.cs @@ -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(); diff --git a/src/Spark.Python/Compiler/PythonViewCompiler.cs b/src/Spark.Python/Compiler/PythonViewCompiler.cs index e4936fbe..f182a61f 100644 --- a/src/Spark.Python/Compiler/PythonViewCompiler.cs +++ b/src/Spark.Python/Compiler/PythonViewCompiler.cs @@ -29,7 +29,7 @@ public override void CompileView(IEnumerable> 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); } diff --git a/src/Spark.Ruby.Tests/RubyViewCompilerTests.cs b/src/Spark.Ruby.Tests/RubyViewCompilerTests.cs index b63b24d3..ad38a50a 100644 --- a/src/Spark.Ruby.Tests/RubyViewCompilerTests.cs +++ b/src/Spark.Ruby.Tests/RubyViewCompilerTests.cs @@ -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(); diff --git a/src/Spark.Ruby/Compiler/RubyViewCompiler.cs b/src/Spark.Ruby/Compiler/RubyViewCompiler.cs index 7ebee1d2..06f8501e 100644 --- a/src/Spark.Ruby/Compiler/RubyViewCompiler.cs +++ b/src/Spark.Ruby/Compiler/RubyViewCompiler.cs @@ -30,7 +30,7 @@ public override void CompileView(IEnumerable> 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); } diff --git a/src/Spark.Web.Mvc.Ruby.Tests/HtmlHelperExtensionsTester.cs b/src/Spark.Web.Mvc.Ruby.Tests/HtmlHelperExtensionsTester.cs index 60701dee..971badaa 100644 --- a/src/Spark.Web.Mvc.Ruby.Tests/HtmlHelperExtensionsTester.cs +++ b/src/Spark.Web.Mvc.Ruby.Tests/HtmlHelperExtensionsTester.cs @@ -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); diff --git a/src/Spark.Web.Mvc.Tests/Compiler/CSharpViewCompilerTester.cs b/src/Spark.Web.Mvc.Tests/Compiler/CSharpViewCompilerTester.cs index 3b7f66a1..bd38c81a 100644 --- a/src/Spark.Web.Mvc.Tests/Compiler/CSharpViewCompilerTester.cs +++ b/src/Spark.Web.Mvc.Tests/Compiler/CSharpViewCompilerTester.cs @@ -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 chunks) @@ -306,7 +306,7 @@ public void LenientSilentNullDoesNotCauseWarningCS0168() var settings = new SparkSettings { 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); @@ -327,7 +327,7 @@ public void LenientOutputNullDoesNotCauseWarningCS0168() var settings = new SparkSettings { 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[] diff --git a/src/Spark.Web.Mvc.Tests/Compiler/VisualBasicViewCompilerTester.cs b/src/Spark.Web.Mvc.Tests/Compiler/VisualBasicViewCompilerTester.cs index d44bdb72..e111632d 100644 --- a/src/Spark.Web.Mvc.Tests/Compiler/VisualBasicViewCompilerTester.cs +++ b/src/Spark.Web.Mvc.Tests/Compiler/VisualBasicViewCompilerTester.cs @@ -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 chunks) diff --git a/src/Spark.Web.Tests/Compiler/SourceMappingTester.cs b/src/Spark.Web.Tests/Compiler/SourceMappingTester.cs index 0b7eb390..ff439361 100644 --- a/src/Spark.Web.Tests/Compiler/SourceMappingTester.cs +++ b/src/Spark.Web.Tests/Compiler/SourceMappingTester.cs @@ -40,7 +40,7 @@ public void Init() _viewFolder = new InMemoryViewFolder(); - var batchCompiler = new RoslynBatchCompiler(); + var batchCompiler = new RoslynBatchCompiler(new SparkSettings()); _engine = new SparkViewEngine( settings, diff --git a/src/Spark/Compiler/BatchCompiler.cs b/src/Spark/Compiler/BatchCompiler.cs index 0cef55a4..b004cc74 100644 --- a/src/Spark/Compiler/BatchCompiler.cs +++ b/src/Spark/Compiler/BatchCompiler.cs @@ -185,16 +185,18 @@ public class CodeDomCompilerException(string message, CompilerResults results) : public class RoslynBatchCompiler : IBatchCompiler { private readonly IEnumerable links; + private readonly ISparkSettings settings; private readonly IList 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 links) + public RoslynBatchCompiler(IEnumerable links, ISparkSettings settings) { this.links = links; + this.settings = settings; this.References = new List(); } @@ -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)) @@ -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()) diff --git a/src/Spark/Compiler/CSharp/ChunkVisitors/UsingNamespaceVisitor.cs b/src/Spark/Compiler/CSharp/ChunkVisitors/UsingNamespaceVisitor.cs index 0d6252c6..19300311 100644 --- a/src/Spark/Compiler/CSharp/ChunkVisitors/UsingNamespaceVisitor.cs +++ b/src/Spark/Compiler/CSharp/ChunkVisitors/UsingNamespaceVisitor.cs @@ -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; @@ -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); } } diff --git a/src/Spark/Compiler/Roslyn/CSharpLink.cs b/src/Spark/Compiler/Roslyn/CSharpLink.cs index a86791d3..4b817a51 100644 --- a/src/Spark/Compiler/Roslyn/CSharpLink.cs +++ b/src/Spark/Compiler/Roslyn/CSharpLink.cs @@ -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); diff --git a/src/Spark/ISparkSettings.cs b/src/Spark/ISparkSettings.cs index eb553314..0f1392d3 100644 --- a/src/Spark/ISparkSettings.cs +++ b/src/Spark/ISparkSettings.cs @@ -56,6 +56,10 @@ public interface ISparkSettings : IParserSettings LanguageType DefaultLanguage { get; } IEnumerable UseNamespaces { get; } + + /// + /// A list of name, fullname or absolute paths to .dll for assemblies to load before compiling views. + /// IEnumerable UseAssemblies { get; } /// diff --git a/src/Spark/SparkSettings.cs b/src/Spark/SparkSettings.cs index 8a57c7dd..5dcc767a 100644 --- a/src/Spark/SparkSettings.cs +++ b/src/Spark/SparkSettings.cs @@ -80,6 +80,10 @@ public SparkSettings(string rootPath) public IEnumerable UseNamespaces => _useNamespaces; private readonly IList _useAssemblies; + + /// + /// A list of names, full names or absolute paths to .dll for assemblies to load before compiling views. + /// public IEnumerable UseAssemblies => _useAssemblies; private readonly IList _excludeAssemblies; @@ -166,7 +170,7 @@ public SparkSettings SetDefaultLanguage(LanguageType language) } /// - /// 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. /// /// The full name of an assembly. /// @@ -182,7 +186,7 @@ public SparkSettings AddAssembly(string assembly) /// public SparkSettings AddAssembly(Assembly assembly) { - _useAssemblies.Add(assembly.FullName); + _useAssemblies.Add(assembly.Location); return this; } diff --git a/src/Xpark/Program.cs b/src/Xpark/Program.cs index 95e32504..5d341bf1 100644 --- a/src/Xpark/Program.cs +++ b/src/Xpark/Program.cs @@ -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,