Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary file location configuration #562

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RazorEngine.Compilation.CSharp
using RazorEngine.Configuration;

namespace RazorEngine.Compilation.CSharp
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,11 +31,11 @@ public class CSharpDirectCompilerService : DirectCompilerServiceBase
/// <param name="markupParserFactory">The markup parser factory to use.</param>
[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<ParserBase> markupParserFactory = null)
public CSharpDirectCompilerService(bool strictMode = true, Func<ParserBase> markupParserFactory = null, ITemplateServiceConfiguration config = null)
: base(
new CSharpRazorCodeLanguage(strictMode),
new CSharpCodeProvider(),
markupParserFactory) { }
markupParserFactory, config) { }
#endregion

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Globalization;
#endif
using RazorEngine.Compilation.ReferenceResolver;
using RazorEngine.Configuration;

namespace RazorEngine.Roslyn.CSharp
{
Expand All @@ -31,18 +32,24 @@ public class CSharpRoslynCompilerService : RoslynCompilerServiceBase
/// We need a CodeDom instance as pre Razor4 uses CodeDom
/// internally and we need to generate the source code file...
/// </summary>
private Microsoft.CSharp.CSharpCodeProvider _codeDomProvider;
private Microsoft.CSharp.CSharpCodeProvider _codeDomProvider;
#endif
/// <summary>
/// Creates a new CSharpRoslynCompilerService instance.
/// </summary>
/// <param name="strictMode"></param>
/// <param name="markupParserFactory"></param>
[SecurityCritical]
public CSharpRoslynCompilerService(bool strictMode = true, Func<ParserBase> markupParserFactory = null)
: base(
new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode),
markupParserFactory) {
public CSharpRoslynCompilerService(bool strictMode = true, Func<ParserBase> markupParserFactory = null) : this(null, strictMode, markupParserFactory) { }

/// <summary>
/// Creates a new CSharpRoslynCompilerService instance.
/// </summary>
/// <param name="strictMode"></param>
/// <param name="markupParserFactory"></param>
public CSharpRoslynCompilerService(ITemplateServiceConfiguration config, bool strictMode = true, Func<ParserBase> markupParserFactory = null)
: base(new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(strictMode), markupParserFactory, config)
{
#if !RAZOR4
_codeDomProvider = new Microsoft.CSharp.CSharpCodeProvider();
#endif
Expand Down
47 changes: 43 additions & 4 deletions src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RazorEngine.Compilation
using RazorEngine.Configuration;

namespace RazorEngine.Compilation
{
using System;
using System.CodeDom;
Expand Down Expand Up @@ -37,6 +39,8 @@ public abstract class CompilerServiceBase : ICompilerService
/// </summary>
protected internal const string ClassNamePrefix = "RazorEngine_";

protected internal ITemplateServiceConfiguration _config;

/// <summary>
/// This class only exists because we cannot use Func&lt;ParserBase&gt; in non security-critical class.
/// </summary>
Expand Down Expand Up @@ -75,11 +79,22 @@ public ParserBase Create()
protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory)
{
Contract.Requires(codeLanguage != null);

CodeLanguage = codeLanguage;
MarkupParserFactory = markupParserFactory ?? new ParserBaseCreator(null);
ReferenceResolver = new UseCurrentAssembliesReferenceResolver();
}

/// <summary>
/// Initialises a new instance of <see cref="CompilerServiceBase"/>
/// </summary>
/// <param name="codeLanguage">The code language.</param>
/// <param name="markupParserFactory">The markup parser factory.</param>
[SecurityCritical]
protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator markupParserFactory, ITemplateServiceConfiguration config) : this(codeLanguage, markupParserFactory)
{
Contract.Requires(codeLanguage != null);
_config = config;
}
#endregion

#region Properties
Expand Down Expand Up @@ -132,7 +147,14 @@ protected CompilerServiceBase(RazorCodeLanguage codeLanguage, ParserBaseCreator
/// Tries to create and return a unique temporary directory.
/// </summary>
/// <returns>the (already created) temporary directory</returns>
protected static string GetDefaultTemporaryDirectory()
///protected static string GetDefaultTemporaryDirectory() => GetDefaultTemporaryDirectory(null);
protected static string GetDefaultTemporaryDirectory() => GetDefaultTemporaryDirectory(null);

/// <summary>
/// Tries to create and return a unique temporary directory.
/// </summary>
/// <returns>the (already created) temporary directory</returns>
protected static string GetDefaultTemporaryDirectory(ITemplateServiceConfiguration config)
{
var created = false;
var tried = 0;
Expand All @@ -142,7 +164,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);
Expand Down Expand Up @@ -174,6 +203,16 @@ protected virtual string GetTemporaryDirectory()
return GetDefaultTemporaryDirectory();
}

/// <summary>
/// Returns a new temporary directory ready to be used.
/// This can be overwritten in subclases to change the created directories.
/// </summary>
/// <returns></returns>
protected virtual string GetTemporaryDirectory(ITemplateServiceConfiguration config)
{
return GetDefaultTemporaryDirectory(config);
}

/// <summary>
/// Builds a type name for the specified template type.
/// </summary>
Expand Down
16 changes: 13 additions & 3 deletions src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ public static void SetCompilerServiceFactory(ICompilerServiceFactory factory)
/// <param name="language">The code language.</param>
/// <returns>The compiler service instance.</returns>
public static ICompilerService GetCompilerService(Language language)
{
return GetCompilerService(language, null);
}

/// <summary>
/// Gets the <see cref="ICompilerService"/> for the specfied language.
/// </summary>
/// <param name="language">The code language.</param>
/// <returns>The compiler service instance.</returns>
public static ICompilerService GetCompilerService(Language language, ITemplateServiceConfiguration config)
{
lock (sync)
{
return _factory.CreateCompilerService(language);
return _factory.CreateCompilerService(language, config);
}
}

Expand All @@ -56,9 +66,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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RazorEngine.Compilation
using RazorEngine.Configuration;

namespace RazorEngine.Compilation
{
using System;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -22,17 +24,29 @@ public class DefaultCompilerServiceFactory : ICompilerServiceFactory
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
[SecuritySafeCritical]
public ICompilerService CreateCompilerService(Language language)
{
return CreateCompilerService(language, null);
}

/// <summary>
/// Creates a <see cref="ICompilerService"/> that supports the specified language.
/// </summary>
/// <param name="language">The <see cref="Language"/>.</param>
/// <returns>An instance of <see cref="ICompilerService"/>.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
[SecuritySafeCritical]
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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RazorEngine.Compilation
using RazorEngine.Configuration;

namespace RazorEngine.Compilation
{
using System;
using System.CodeDom;
Expand Down Expand Up @@ -41,8 +43,8 @@ public abstract class DirectCompilerServiceBase : CompilerServiceBase
/// <param name="codeDomProvider">The code dom provider used to generate code.</param>
/// <param name="markupParserFactory">The markup parser factory.</param>
[SecurityCritical]
protected DirectCompilerServiceBase(RazorCodeLanguage codeLanguage, CodeDomProvider codeDomProvider, Func<ParserBase> markupParserFactory)
: base(codeLanguage, new ParserBaseCreator(markupParserFactory))
protected DirectCompilerServiceBase(RazorCodeLanguage codeLanguage, CodeDomProvider codeDomProvider, Func<ParserBase> markupParserFactory, ITemplateServiceConfiguration config)
: base(codeLanguage, new ParserBaseCreator(markupParserFactory), config)
{
_codeDomProvider = codeDomProvider;
}
Expand Down Expand Up @@ -91,7 +93,7 @@ private Tuple<CompilerResults, string> 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" : "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Security;
using RazorEngine.Configuration;

namespace RazorEngine.Compilation
{
/// <summary>
Expand All @@ -13,6 +15,13 @@ public interface ICompilerServiceFactory
/// <param name="language">The <see cref="Language"/>.</param>
/// <returns>An instance of <see cref="ICompilerService"/>.</returns>
ICompilerService CreateCompilerService(Language language);

/// <summary>
/// Creates a <see cref="ICompilerService"/> that supports the specified language.
/// </summary>
/// <param name="language">The <see cref="Language"/>.</param>
/// <returns>An instance of <see cref="ICompilerService"/>.</returns>
ICompilerService CreateCompilerService(Language language, ITemplateServiceConfiguration config);
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Security;
using System.Security.Permissions;
using Microsoft.CodeAnalysis.Emit;
using RazorEngine.Configuration;


namespace RazorEngine.Roslyn.CSharp
Expand Down Expand Up @@ -153,7 +154,18 @@ public override string ResolveReference(string path, string baseFilePath)
/// <param name="markupParserFactory"></param>
[SecuritySafeCritical]
public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func<ParserBase> markupParserFactory)
: base(codeLanguage, new ParserBaseCreator(markupParserFactory))
: this(codeLanguage, markupParserFactory, null)
{

}

/// <summary>
/// Creates a new instance of the <see cref="RoslynCompilerServiceBase"/> class.
/// </summary>
/// <param name="codeLanguage"></param>
/// <param name="markupParserFactory"></param>
public RoslynCompilerServiceBase(RazorCodeLanguage codeLanguage, Func<ParserBase> markupParserFactory, ITemplateServiceConfiguration config)
: base(codeLanguage, new ParserBaseCreator(markupParserFactory), config)
{

}
Expand Down Expand Up @@ -202,7 +214,7 @@ public override Tuple<Type, CompilationData> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Security;
using System.Text;
using System.Threading.Tasks;
using RazorEngine.Configuration;

namespace RazorEngine.Roslyn
{
Expand All @@ -25,18 +26,30 @@ public class RoslynCompilerServiceFactory : ICompilerServiceFactory
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
[SecuritySafeCritical]
public ICompilerService CreateCompilerService(Language language)
{
return CreateCompilerService(language, null);
}

/// <summary>
/// Creates a <see cref="ICompilerService"/> that supports the specified language.
/// </summary>
/// <param name="language">The <see cref="Language"/>.</param>
/// <returns>An instance of <see cref="ICompilerService"/>.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
[SecuritySafeCritical]
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
//#if RAZOR4
throw new NotSupportedException("Razor4 doesn't support VB.net apparently.");
//#else
// return new VBRoslynCompilerService();
//#endif
//#else
// return new VBRoslynCompilerService(config: config);
//#endif

default:
throw new ArgumentException("Unsupported language: " + language);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -26,11 +28,11 @@ public class VBDirectCompilerService : DirectCompilerServiceBase
/// <param name="markupParserFactory">The markup parser to use.</param>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed in base class: DirectCompilerServiceBase")]
[SecurityCritical]
public VBDirectCompilerService(bool strictMode = true, Func<ParserBase> markupParserFactory = null)
public VBDirectCompilerService(bool strictMode = true, Func<ParserBase> markupParserFactory = null, ITemplateServiceConfiguration config = null)
: base(
new VBRazorCodeLanguage(strictMode),
new VBCodeProvider(),
markupParserFactory) { }
markupParserFactory, config) { }
#endregion

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ public ITemplateManager TemplateManager
{
get { return _innerConfig.TemplateManager; }
}

/// <summary>
/// Gets the Temporary Directory.
/// </summary>
public string TemporaryDirectory { get { return _innerConfig.TemporaryDirectory; } }

#endregion
}
}
Loading