From 5df8800198140a279084a690c5dd41f991d77c42 Mon Sep 17 00:00:00 2001 From: Brenton Reeder Date: Fri, 1 Jul 2016 15:24:26 -0400 Subject: [PATCH 1/4] Adding in temporary file path configuration Working on issue #386 to add in the temp file path configuration option. --- .../CSharp/CSharpRoslynCompilerService.cs | 5 ++-- .../RoslynCompilerServiceBase.cs | 7 +++--- .../RoslynCompilerServiceFactory.cs | 7 +++--- .../CSharp/CSharpDirectCompilerService.cs | 8 +++--- .../Compilation/CompilerServiceBase.cs | 25 +++++++++++++------ .../Compilation/CompilerServiceBuilder.cs | 8 +++--- .../DefaultCompilerServiceFactory.cs | 10 +++++--- .../Compilation/DirectCompilerServiceBase.cs | 10 +++++--- .../Compilation/ICompilerServiceFactory.cs | 4 ++- .../VisualBasic/VBDirectCompilerService.cs | 8 +++--- .../FluentTemplateServiceConfiguration.cs | 6 +++++ .../ITemplateServiceConfiguration.cs | 5 ++++ .../RazorEngineConfigurationSection.cs | 9 +++++++ .../ReadOnlyTemplateServiceConfiguration.cs | 9 +++++++ .../TemplateServiceConfiguration.cs | 6 +++++ .../Xml/XmlTemplateServiceConfiguration.cs | 6 +++++ .../Templating/RazorEngineCore.cs | 2 +- 17 files changed, 100 insertions(+), 35 deletions(-) diff --git a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs index 0d2aeed7..fa8e7f6b 100644 --- a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs +++ b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs @@ -20,6 +20,7 @@ using RazorEngine.Compilation.CSharp; using RazorEngine.Compilation.ReferenceResolver; using Microsoft.CodeAnalysis; +using RazorEngine.Configuration; namespace RazorEngine.Roslyn.CSharp { @@ -40,10 +41,10 @@ public class CSharpRoslynCompilerService : RoslynCompilerServiceBase /// /// /// - public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null) + public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null, ITemplateServiceConfiguration config = null) : base( new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), - markupParserFactory) { + markupParserFactory, config) { #if !RAZOR4 _codeDomProvider = new Microsoft.CSharp.CSharpCodeProvider(); #endif diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs index 5dc2c313..a7ac87dd 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs @@ -19,6 +19,7 @@ using System.Security; using System.Security.Permissions; using Microsoft.CodeAnalysis.Emit; +using RazorEngine.Configuration; namespace RazorEngine.Roslyn.CSharp @@ -139,8 +140,8 @@ public override string ResolveReference(string path, string baseFilePath) /// /// /// - public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func markupParserFactory) - : base(codeLanguage, new ParserBaseCreator(markupParserFactory)) + public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func markupParserFactory, ITemplateServiceConfiguration config) + : base(codeLanguage, new ParserBaseCreator(markupParserFactory), config) { } @@ -187,7 +188,7 @@ public override Tuple CompileType(TypeContext context) var assemblyName = GetAssemblyName(context); (new PermissionSet(PermissionState.Unrestricted)).Assert(); - var tempDir = GetTemporaryDirectory(); + var tempDir = GetTemporaryDirectory(_config); var sourceCodeFile = Path.Combine(tempDir, String.Format("{0}.{1}", assemblyName, SourceFileExtension)); File.WriteAllText(sourceCodeFile, sourceCode); diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs index 6dc07911..26ad71c4 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs @@ -7,6 +7,7 @@ using System.Security; using System.Text; using System.Threading.Tasks; +using RazorEngine.Configuration; namespace RazorEngine.Roslyn { @@ -24,18 +25,18 @@ public class RoslynCompilerServiceFactory : ICompilerServiceFactory /// An instance of . [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] [SecuritySafeCritical] - public ICompilerService CreateCompilerService(Language language) + public ICompilerService CreateCompilerService(Language language, ITemplateServiceConfiguration config) { switch (language) { case Language.CSharp: - return new CSharpRoslynCompilerService(); + return new CSharpRoslynCompilerService(config: config); case Language.VisualBasic: //#if RAZOR4 throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); //#else -// return new VBRoslynCompilerService(); +// return new VBRoslynCompilerService(config: config); //#endif default: diff --git a/src/source/RazorEngine.Core/Compilation/CSharp/CSharpDirectCompilerService.cs b/src/source/RazorEngine.Core/Compilation/CSharp/CSharpDirectCompilerService.cs index f6235d8a..70c3e87a 100644 --- a/src/source/RazorEngine.Core/Compilation/CSharp/CSharpDirectCompilerService.cs +++ b/src/source/RazorEngine.Core/Compilation/CSharp/CSharpDirectCompilerService.cs @@ -1,4 +1,6 @@ -namespace RazorEngine.Compilation.CSharp +using RazorEngine.Configuration; + +namespace RazorEngine.Compilation.CSharp { using System; using System.Collections.Generic; @@ -29,11 +31,11 @@ public class CSharpDirectCompilerService : DirectCompilerServiceBase /// The markup parser factory to use. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed"), SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed in base class: DirectCompilerServiceBase")] [SecurityCritical] - public CSharpDirectCompilerService(bool strictMode = true, Func markupParserFactory = null) + public CSharpDirectCompilerService(bool strictMode = true, Func markupParserFactory = null, ITemplateServiceConfiguration config = null) : base( new CSharpRazorCodeLanguage(strictMode), new CSharpCodeProvider(), - markupParserFactory) { } + markupParserFactory, config) { } #endregion /// diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs index b6e1f9b2..42d4e33a 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs @@ -1,4 +1,6 @@ -namespace RazorEngine.Compilation +using RazorEngine.Configuration; + +namespace RazorEngine.Compilation { using System; using System.CodeDom; @@ -41,6 +43,8 @@ public abstract class CompilerServiceBase : ICompilerService /// protected internal const string ClassNamePrefix = "RazorEngine_"; + protected internal ITemplateServiceConfiguration _config; + /// /// This class only exists because we cannot use Func<ParserBase> in non security-critical class. /// @@ -76,10 +80,10 @@ public ParserBase Create() /// The code language. /// The markup parser factory. [SecurityCritical] - protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory) + protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory, ITemplateServiceConfiguration config) { Contract.Requires(codeLanguage != null); - + _config = config; CodeLanguage = codeLanguage; MarkupParserFactory = markupParserFactory ?? new ParserBaseCreator(null); ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); @@ -136,7 +140,7 @@ protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator /// Tries to create and return a unique temporary directory. /// /// the (already created) temporary directory - protected static string GetDefaultTemporaryDirectory() + protected static string GetDefaultTemporaryDirectory(ITemplateServiceConfiguration config) { var created = false; var tried = 0; @@ -146,7 +150,14 @@ protected static string GetDefaultTemporaryDirectory() tried++; try { - tempDirectory = Path.Combine(Path.GetTempPath(), "RazorEngine_" + Path.GetRandomFileName()); + string tempDirectoryPath = Path.GetTempPath(); + + if (!string.IsNullOrEmpty(config?.TemporaryDirectory)) + { + tempDirectoryPath = config.TemporaryDirectory; + } + + tempDirectory = Path.Combine(tempDirectoryPath, "RazorEngine_" + Path.GetRandomFileName()); if (!Directory.Exists(tempDirectory)) { Directory.CreateDirectory(tempDirectory); @@ -173,9 +184,9 @@ protected static string GetDefaultTemporaryDirectory() /// This can be overwritten in subclases to change the created directories. /// /// - protected virtual string GetTemporaryDirectory() + protected virtual string GetTemporaryDirectory(ITemplateServiceConfiguration config) { - return GetDefaultTemporaryDirectory(); + return GetDefaultTemporaryDirectory(config); } /// diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs index fce173f6..b8d56e54 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs @@ -34,11 +34,11 @@ public static void SetCompilerServiceFactory(ICompilerServiceFactory factory) /// /// The code language. /// The compiler service instance. - public static ICompilerService GetCompilerService(Language language) + public static ICompilerService GetCompilerService(Language language, ITemplateServiceConfiguration config) { lock (sync) { - return _factory.CreateCompilerService(language); + return _factory.CreateCompilerService(language, config); } } @@ -51,9 +51,9 @@ public static ICompilerService GetDefaultCompilerService() { var config = RazorEngineConfigurationSection.GetConfiguration(); if (config == null) - return GetCompilerService(Language.CSharp); + return GetCompilerService(Language.CSharp, null); - return GetCompilerService(config.DefaultLanguage); + return GetCompilerService(config.DefaultLanguage, null); } #endregion } diff --git a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs index 33b44b51..edd144f5 100644 --- a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs @@ -1,4 +1,6 @@ -namespace RazorEngine.Compilation +using RazorEngine.Configuration; + +namespace RazorEngine.Compilation { using System; using System.Diagnostics.CodeAnalysis; @@ -21,18 +23,18 @@ public class DefaultCompilerServiceFactory : ICompilerServiceFactory /// An instance of . [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] [SecuritySafeCritical] - public ICompilerService CreateCompilerService(Language language) + public ICompilerService CreateCompilerService(Language language, ITemplateServiceConfiguration config) { switch (language) { case Language.CSharp: - return new CSharpDirectCompilerService(); + return new CSharpDirectCompilerService(config: config); case Language.VisualBasic: #if RAZOR4 throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); #else - return new VBDirectCompilerService(); + return new VBDirectCompilerService(config: config); #endif default: diff --git a/src/source/RazorEngine.Core/Compilation/DirectCompilerServiceBase.cs b/src/source/RazorEngine.Core/Compilation/DirectCompilerServiceBase.cs index 90e6a36d..24b1490e 100644 --- a/src/source/RazorEngine.Core/Compilation/DirectCompilerServiceBase.cs +++ b/src/source/RazorEngine.Core/Compilation/DirectCompilerServiceBase.cs @@ -1,4 +1,6 @@ -namespace RazorEngine.Compilation +using RazorEngine.Configuration; + +namespace RazorEngine.Compilation { using System; using System.CodeDom; @@ -41,8 +43,8 @@ public abstract class DirectCompilerServiceBase : CompilerServiceBase /// The code dom provider used to generate code. /// The markup parser factory. [SecurityCritical] - protected DirectCompilerServiceBase(RazorCodeLanguage codeLanguage, CodeDomProvider codeDomProvider, Func markupParserFactory) - : base(codeLanguage, new ParserBaseCreator(markupParserFactory)) + protected DirectCompilerServiceBase(RazorCodeLanguage codeLanguage, CodeDomProvider codeDomProvider, Func markupParserFactory, ITemplateServiceConfiguration config) + : base(codeLanguage, new ParserBaseCreator(markupParserFactory), config) { _codeDomProvider = codeDomProvider; } @@ -91,7 +93,7 @@ private Tuple Compile(TypeContext context) GenerateExecutable = false, IncludeDebugInformation = Debug, TreatWarningsAsErrors = false, - TempFiles = new TempFileCollection(GetTemporaryDirectory(), true), + TempFiles = new TempFileCollection(GetTemporaryDirectory(_config), true), CompilerOptions = string.Format("/target:library /optimize /define:RAZORENGINE {0}", haveMscorlib ? "/nostdlib" : "") diff --git a/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs b/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs index 45b84a94..72aaef5e 100644 --- a/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs @@ -1,4 +1,6 @@ using System.Security; +using RazorEngine.Configuration; + namespace RazorEngine.Compilation { /// @@ -12,7 +14,7 @@ public interface ICompilerServiceFactory /// /// The . /// An instance of . - ICompilerService CreateCompilerService(Language language); + ICompilerService CreateCompilerService(Language language, ITemplateServiceConfiguration config); #endregion } } diff --git a/src/source/RazorEngine.Core/Compilation/VisualBasic/VBDirectCompilerService.cs b/src/source/RazorEngine.Core/Compilation/VisualBasic/VBDirectCompilerService.cs index ad58b478..7da47f95 100644 --- a/src/source/RazorEngine.Core/Compilation/VisualBasic/VBDirectCompilerService.cs +++ b/src/source/RazorEngine.Core/Compilation/VisualBasic/VBDirectCompilerService.cs @@ -1,4 +1,6 @@ -namespace RazorEngine.Compilation.VisualBasic +using RazorEngine.Configuration; + +namespace RazorEngine.Compilation.VisualBasic { #if !RAZOR4 // no support for VB.net in Razor4? using System; @@ -26,11 +28,11 @@ public class VBDirectCompilerService : DirectCompilerServiceBase /// The markup parser to use. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed in base class: DirectCompilerServiceBase")] [SecurityCritical] - public VBDirectCompilerService(bool strictMode = true, Func markupParserFactory = null) + public VBDirectCompilerService(bool strictMode = true, Func markupParserFactory = null, ITemplateServiceConfiguration config = null) : base( new VBRazorCodeLanguage(strictMode), new VBCodeProvider(), - markupParserFactory) { } + markupParserFactory, config) { } #endregion /// diff --git a/src/source/RazorEngine.Core/Configuration/Fluent/FluentTemplateServiceConfiguration.cs b/src/source/RazorEngine.Core/Configuration/Fluent/FluentTemplateServiceConfiguration.cs index 1f403383..520ddc21 100644 --- a/src/source/RazorEngine.Core/Configuration/Fluent/FluentTemplateServiceConfiguration.cs +++ b/src/source/RazorEngine.Core/Configuration/Fluent/FluentTemplateServiceConfiguration.cs @@ -156,6 +156,12 @@ public ITemplateManager TemplateManager { get { return _innerConfig.TemplateManager; } } + + /// + /// Gets the Temporary Directory. + /// + public string TemporaryDirectory { get { return _innerConfig.TemporaryDirectory; } } + #endregion } } \ No newline at end of file diff --git a/src/source/RazorEngine.Core/Configuration/ITemplateServiceConfiguration.cs b/src/source/RazorEngine.Core/Configuration/ITemplateServiceConfiguration.cs index 96763332..49eaa8ed 100644 --- a/src/source/RazorEngine.Core/Configuration/ITemplateServiceConfiguration.cs +++ b/src/source/RazorEngine.Core/Configuration/ITemplateServiceConfiguration.cs @@ -95,6 +95,11 @@ public interface ITemplateServiceConfiguration /// Gets the template resolver. /// ITemplateManager TemplateManager { get; } + + /// + /// Sets the Temporary Directory where temp files will be written. + /// + string TemporaryDirectory { get; } #endregion } diff --git a/src/source/RazorEngine.Core/Configuration/RazorEngineConfigurationSection.cs b/src/source/RazorEngine.Core/Configuration/RazorEngineConfigurationSection.cs index 9b00a175..3f1e0266 100644 --- a/src/source/RazorEngine.Core/Configuration/RazorEngineConfigurationSection.cs +++ b/src/source/RazorEngine.Core/Configuration/RazorEngineConfigurationSection.cs @@ -22,6 +22,7 @@ public class RazorEngineConfigurationSection : ConfigurationSection private const string CachingProviderAttribute = "cachingProviderType"; private const string ReferenceResolverAttribute = "referenceResolverType"; private const string TemplateServicesElement = "templateServices"; + private const string TemporaryDirectoryAttribute = "temporaryDirectory"; #endregion #region Properties @@ -115,6 +116,14 @@ public TemplateServiceConfigurationElementCollection TemplateServices { get { return (TemplateServiceConfigurationElementCollection)this[TemplateServicesElement]; } } + /// + /// Sets the location of the temporary directory where temp files will be written. + /// + [ConfigurationProperty(TemporaryDirectoryAttribute, IsRequired = false)] + public string TemporaryDirectory + { + get { return (string) this[TemporaryDirectoryAttribute]; } + } #endregion #region Methods diff --git a/src/source/RazorEngine.Core/Configuration/ReadOnlyTemplateServiceConfiguration.cs b/src/source/RazorEngine.Core/Configuration/ReadOnlyTemplateServiceConfiguration.cs index 121db54f..b9135d5d 100644 --- a/src/source/RazorEngine.Core/Configuration/ReadOnlyTemplateServiceConfiguration.cs +++ b/src/source/RazorEngine.Core/Configuration/ReadOnlyTemplateServiceConfiguration.cs @@ -34,6 +34,7 @@ public class ReadOnlyTemplateServiceConfiguration : ITemplateServiceConfiguratio [Obsolete("Use TemplateManager instead")] private readonly ITemplateResolver _resolver; private readonly ITemplateManager _templateManager; + private readonly string _temporaryDirectory; /// /// Create a new readonly view (and copy) of the given configuration. @@ -122,6 +123,7 @@ public ReadOnlyTemplateServiceConfiguration(ITemplateServiceConfiguration config } #pragma warning restore 0618 // Backwards Compat. + _temporaryDirectory = config.TemporaryDirectory; } /// @@ -288,5 +290,12 @@ public ITemplateManager TemplateManager return _templateManager; } } + /// + /// Gets the temporary directory + /// + public string TemporaryDirectory + { + get { return _temporaryDirectory; } + } } } diff --git a/src/source/RazorEngine.Core/Configuration/TemplateServiceConfiguration.cs b/src/source/RazorEngine.Core/Configuration/TemplateServiceConfiguration.cs index 93a202c9..f683d119 100644 --- a/src/source/RazorEngine.Core/Configuration/TemplateServiceConfiguration.cs +++ b/src/source/RazorEngine.Core/Configuration/TemplateServiceConfiguration.cs @@ -157,6 +157,12 @@ public ITemplateResolver Resolver { /// Gets or sets the template resolver. /// public ITemplateManager TemplateManager { get; set; } + + /// + /// Gets or sets the temporary directory. + /// + public string TemporaryDirectory { get; set; } + #endregion } } \ No newline at end of file diff --git a/src/source/RazorEngine.Core/Configuration/Xml/XmlTemplateServiceConfiguration.cs b/src/source/RazorEngine.Core/Configuration/Xml/XmlTemplateServiceConfiguration.cs index b853e58b..9ddda29c 100644 --- a/src/source/RazorEngine.Core/Configuration/Xml/XmlTemplateServiceConfiguration.cs +++ b/src/source/RazorEngine.Core/Configuration/Xml/XmlTemplateServiceConfiguration.cs @@ -113,6 +113,9 @@ public XmlTemplateServiceConfiguration(string name) /// Gets the template resolver. /// public ITemplateManager TemplateManager { get; private set; } + + public string TemporaryDirectory { get; private set; } + #endregion #region Methods @@ -196,6 +199,9 @@ private void InitialiseConfiguration(RazorEngineConfigurationSection config, Tem // Set whether we load templates with Assembly.Load(byte[]). DisableTempFileLocking = config.DisableTempFileLocking; + // Sets the tempoaryDirectory + TemporaryDirectory = config.TemporaryDirectory; + // Add the global namespaces. AddNamespaces(config.Namespaces); diff --git a/src/source/RazorEngine.Core/Templating/RazorEngineCore.cs b/src/source/RazorEngine.Core/Templating/RazorEngineCore.cs index 5356e709..ec3d2c55 100644 --- a/src/source/RazorEngine.Core/Templating/RazorEngineCore.cs +++ b/src/source/RazorEngine.Core/Templating/RazorEngineCore.cs @@ -121,7 +121,7 @@ public virtual Tuple CreateTemplateType(ITemplateSource r foreach (string ns in _config.Namespaces) context.Namespaces.Add(ns); - using (var service = _config.CompilerServiceFactory.CreateCompilerService(_config.Language)) + using (var service = _config.CompilerServiceFactory.CreateCompilerService(_config.Language, _config)) { service.Debug = _config.Debug; service.DisableTempFileLocking = _config.DisableTempFileLocking; From 1dee986eedd7a59270f16b2b470478f7c7eefff9 Mon Sep 17 00:00:00 2001 From: Brenton Reeder Date: Mon, 28 Nov 2016 11:42:20 -0500 Subject: [PATCH 2/4] Re-creating original overloads To prevent non-breaking changes --- .../CSharp/CSharpRoslynCompilerService.cs | 19 +++++- .../RoslynCompilerServiceBase.cs | 11 ++++ .../RoslynCompilerServiceFactory.cs | 26 ++++++++ .../Compilation/CompilerServiceBase.cs | 62 ++++++++++++++++++- .../Compilation/CompilerServiceBuilder.cs | 13 ++++ .../DefaultCompilerServiceFactory.cs | 26 ++++++++ .../Compilation/ICompilerServiceFactory.cs | 7 +++ 7 files changed, 160 insertions(+), 4 deletions(-) diff --git a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs index 8080f2c0..c9b9b20b 100644 --- a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs +++ b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs @@ -34,14 +34,29 @@ public class CSharpRoslynCompilerService : RoslynCompilerServiceBase /// We need a CodeDom instance as pre Razor4 uses CodeDom /// internally and we need to generate the source code file... /// - private Microsoft.CSharp.CSharpCodeProvider _codeDomProvider; + private Microsoft.CSharp.CSharpCodeProvider _codeDomProvider; #endif /// /// Creates a new CSharpRoslynCompilerService instance. /// /// /// - public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null, ITemplateServiceConfiguration config = null) + public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null) + : base( + new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), + markupParserFactory) + { +#if !RAZOR4 + _codeDomProvider = new Microsoft.CSharp.CSharpCodeProvider(); +#endif + } + + /// + /// Creates a new CSharpRoslynCompilerService instance. + /// + /// + /// + public CSharpRoslynCompilerService(ITemplateServiceConfiguration config, bool strictMode = true, Func markupParserFactory = null) : base( new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), markupParserFactory, config) { diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs index 3db6eef0..ebdaad52 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs @@ -135,6 +135,17 @@ public override string ResolveReference(string path, string baseFilePath) } } + /// + /// Creates a new instance of the class. + /// + /// + /// + public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func markupParserFactory) + : base(codeLanguage, new ParserBaseCreator(markupParserFactory)) + { + + } + /// /// Creates a new instance of the class. /// diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs index 26ad71c4..b5e4187c 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs @@ -18,6 +18,32 @@ namespace RazorEngine.Roslyn public class RoslynCompilerServiceFactory : ICompilerServiceFactory { #region Methods + /// + /// Creates a that supports the specified language. + /// + /// The . + /// An instance of . + [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] + [SecuritySafeCritical] + public ICompilerService CreateCompilerService(Language language) + { + switch (language) + { + case Language.CSharp: + return new CSharpRoslynCompilerService(); + + case Language.VisualBasic: + //#if RAZOR4 + throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); + //#else + // return new VBRoslynCompilerService(config: config); + //#endif + + default: + throw new ArgumentException("Unsupported language: " + language); + } + } + /// /// Creates a that supports the specified language. /// diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs index f2cbc15a..58cb1eb8 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs @@ -76,14 +76,25 @@ public ParserBase Create() /// The code language. /// The markup parser factory. [SecurityCritical] - protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory, ITemplateServiceConfiguration config) + protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory) { Contract.Requires(codeLanguage != null); - _config = config; CodeLanguage = codeLanguage; MarkupParserFactory = markupParserFactory ?? new ParserBaseCreator(null); ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); } + + /// + /// Initialises a new instance of + /// + /// The code language. + /// The markup parser factory. + [SecurityCritical] + protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory, ITemplateServiceConfiguration config) : this(codeLanguage, markupParserFactory) + { + Contract.Requires(codeLanguage != null); + _config = config; + } #endregion #region Properties @@ -132,6 +143,43 @@ protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator #region Methods + /// + /// Tries to create and return a unique temporary directory. + /// + /// the (already created) temporary directory + protected static string GetDefaultTemporaryDirectory() + { + var created = false; + var tried = 0; + string tempDirectory = ""; + while (!created && tried < 10) + { + tried++; + try + { + tempDirectory = Path.Combine(Path.GetTempPath(), "RazorEngine_" + Path.GetRandomFileName()); + + if (!Directory.Exists(tempDirectory)) + { + Directory.CreateDirectory(tempDirectory); + created = Directory.Exists(tempDirectory); + } + } + catch (IOException) + { + if (tried > 8) + { + throw; + } + } + } + if (!created) + { + throw new Exception("Could not create a temporary directory! Maybe all names are already used?"); + } + return tempDirectory; + } + /// /// Tries to create and return a unique temporary directory. /// @@ -175,6 +223,16 @@ protected static string GetDefaultTemporaryDirectory(ITemplateServiceConfigurati return tempDirectory; } + /// + /// Returns a new temporary directory ready to be used. + /// This can be overwritten in subclases to change the created directories. + /// + /// + protected virtual string GetTemporaryDirectory() + { + return GetDefaultTemporaryDirectory(); + } + /// /// Returns a new temporary directory ready to be used. /// This can be overwritten in subclases to change the created directories. diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs index b8d56e54..12d7e98b 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs @@ -29,6 +29,19 @@ public static void SetCompilerServiceFactory(ICompilerServiceFactory factory) } } + /// + /// Gets the for the specfied language. + /// + /// The code language. + /// The compiler service instance. + public static ICompilerService GetCompilerService(Language language) + { + lock (sync) + { + return _factory.CreateCompilerService(language); + } + } + /// /// Gets the for the specfied language. /// diff --git a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs index edd144f5..b1790a20 100644 --- a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs @@ -16,6 +16,32 @@ namespace RazorEngine.Compilation public class DefaultCompilerServiceFactory : ICompilerServiceFactory { #region Methods + /// + /// Creates a that supports the specified language. + /// + /// The . + /// An instance of . + [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] + [SecuritySafeCritical] + public ICompilerService CreateCompilerService(Language language) + { + switch (language) + { + case Language.CSharp: + return new CSharpDirectCompilerService(); + + case Language.VisualBasic: +#if RAZOR4 + throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); +#else + return new VBDirectCompilerService(); +#endif + + default: + throw new ArgumentException("Unsupported language: " + language); + } + } + /// /// Creates a that supports the specified language. /// diff --git a/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs b/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs index 72aaef5e..8117dba9 100644 --- a/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs @@ -9,6 +9,13 @@ namespace RazorEngine.Compilation public interface ICompilerServiceFactory { #region Methods + /// + /// Creates a that supports the specified language. + /// + /// The . + /// An instance of . + ICompilerService CreateCompilerService(Language language); + /// /// Creates a that supports the specified language. /// From d7bb5fef36c60e9fde6ead55b27fbc57deb10dfe Mon Sep 17 00:00:00 2001 From: Brenton Reeder Date: Mon, 28 Nov 2016 12:03:53 -0500 Subject: [PATCH 3/4] Cleanup of code --- .../CSharp/CSharpRoslynCompilerService.cs | 15 +++--------- .../RoslynCompilerServiceBase.cs | 2 +- .../RoslynCompilerServiceFactory.cs | 24 ++++--------------- .../Compilation/CompilerServiceBuilder.cs | 5 +--- .../DefaultCompilerServiceFactory.cs | 16 +------------ 5 files changed, 11 insertions(+), 51 deletions(-) diff --git a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs index c9b9b20b..de245641 100644 --- a/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs +++ b/src/source/RazorEngine.Core.Roslyn/CSharp/CSharpRoslynCompilerService.cs @@ -41,15 +41,7 @@ public class CSharpRoslynCompilerService : RoslynCompilerServiceBase /// /// /// - public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null) - : base( - new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), - markupParserFactory) - { -#if !RAZOR4 - _codeDomProvider = new Microsoft.CSharp.CSharpCodeProvider(); -#endif - } + public CSharpRoslynCompilerService(bool strictMode = true, Func markupParserFactory = null) : this(null, strictMode, markupParserFactory) { } /// /// Creates a new CSharpRoslynCompilerService instance. @@ -57,9 +49,8 @@ public CSharpRoslynCompilerService(bool strictMode = true, Func mark /// /// public CSharpRoslynCompilerService(ITemplateServiceConfiguration config, bool strictMode = true, Func markupParserFactory = null) - : base( - new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), - markupParserFactory, config) { + : base(new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), markupParserFactory, config) + { #if !RAZOR4 _codeDomProvider = new Microsoft.CSharp.CSharpCodeProvider(); #endif diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs index ebdaad52..ab68a510 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceBase.cs @@ -141,7 +141,7 @@ public override string ResolveReference(string path, string baseFilePath) /// /// public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func markupParserFactory) - : base(codeLanguage, new ParserBaseCreator(markupParserFactory)) + : this(codeLanguage, markupParserFactory, null) { } diff --git a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs index b5e4187c..14f36340 100644 --- a/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core.Roslyn/RoslynCompilerServiceFactory.cs @@ -27,21 +27,7 @@ public class RoslynCompilerServiceFactory : ICompilerServiceFactory [SecuritySafeCritical] public ICompilerService CreateCompilerService(Language language) { - switch (language) - { - case Language.CSharp: - return new CSharpRoslynCompilerService(); - - case Language.VisualBasic: - //#if RAZOR4 - throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); - //#else - // return new VBRoslynCompilerService(config: config); - //#endif - - default: - throw new ArgumentException("Unsupported language: " + language); - } + return CreateCompilerService(language, null); } /// @@ -59,11 +45,11 @@ public ICompilerService CreateCompilerService(Language language, ITemplateServic return new CSharpRoslynCompilerService(config: config); case Language.VisualBasic: -//#if RAZOR4 + //#if RAZOR4 throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); -//#else -// return new VBRoslynCompilerService(config: config); -//#endif + //#else + // return new VBRoslynCompilerService(config: config); + //#endif default: throw new ArgumentException("Unsupported language: " + language); diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs index 12d7e98b..34aa606d 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs @@ -36,10 +36,7 @@ public static void SetCompilerServiceFactory(ICompilerServiceFactory factory) /// The compiler service instance. public static ICompilerService GetCompilerService(Language language) { - lock (sync) - { - return _factory.CreateCompilerService(language); - } + return GetCompilerService(language, null); } /// diff --git a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs index b1790a20..97f29c6d 100644 --- a/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs +++ b/src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs @@ -25,21 +25,7 @@ public class DefaultCompilerServiceFactory : ICompilerServiceFactory [SecuritySafeCritical] public ICompilerService CreateCompilerService(Language language) { - switch (language) - { - case Language.CSharp: - return new CSharpDirectCompilerService(); - - case Language.VisualBasic: -#if RAZOR4 - throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); -#else - return new VBDirectCompilerService(); -#endif - - default: - throw new ArgumentException("Unsupported language: " + language); - } + return CreateCompilerService(language, null); } /// From 74a3fd4b276dc4937605407498daec6eaf8dc942 Mon Sep 17 00:00:00 2001 From: Anthony Leiper Date: Thu, 9 Jan 2020 08:50:43 +1000 Subject: [PATCH 4/4] Parameterless GetDefaultTemporaryDirectory to use GetDefaultTemporaryDirectory (ITemplateServiceConfiguration) --- .../Compilation/CompilerServiceBase.cs | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs index 0dcc9ebc..89f6b006 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs @@ -147,38 +147,8 @@ protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator /// Tries to create and return a unique temporary directory. /// /// the (already created) temporary directory - protected static string GetDefaultTemporaryDirectory() - { - var created = false; - var tried = 0; - string tempDirectory = ""; - while (!created && tried < 10) - { - tried++; - try - { - tempDirectory = Path.Combine(Path.GetTempPath(), "RazorEngine_" + Path.GetRandomFileName()); - - if (!Directory.Exists(tempDirectory)) - { - Directory.CreateDirectory(tempDirectory); - created = Directory.Exists(tempDirectory); - } - } - catch (IOException) - { - if (tried > 8) - { - throw; - } - } - } - if (!created) - { - throw new Exception("Could not create a temporary directory! Maybe all names are already used?"); - } - return tempDirectory; - } + ///protected static string GetDefaultTemporaryDirectory() => GetDefaultTemporaryDirectory(null); + protected static string GetDefaultTemporaryDirectory() => GetDefaultTemporaryDirectory(null); /// /// Tries to create and return a unique temporary directory.