Skip to content

Commit

Permalink
Use new RazorEngine features to fix fsprojects#273 . There are some d…
Browse files Browse the repository at this point in the history
…ownsides when F# Formatting is used as a library in a long running application, but it is an improvement for now.
  • Loading branch information
matthid committed Mar 27, 2015
1 parent a1ca24e commit 37fd208
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Common/Razor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,36 @@ type RazorRender(layoutRoots, namespaces, templateName:string, ?modeltype:System
if templateName.EndsWith(".cshtml") then
templateName.Substring(0, templateName.Length - 7)
else templateName
let templateCache = new ConcurrentDictionary<string[] * string, ITemplateSource>()
let templateCache = new ConcurrentDictionary<string, ITemplateSource>()
// Create resolver & set it to the global static field
let templateManager =
{ new ITemplateManager with
member x.GetKey (name, resolveType, context) = new NameOnlyTemplateKey(name, resolveType, context) :> ITemplateKey
member x.AddDynamic (key, source) = failwith "dynamic templates are not supported!"
member x.Resolve templateKey =
let name = templateKey.Name
let key = Array.ofSeq layoutRoots, name
templateCache.GetOrAdd (key, fun (layoutRoots, name) ->
templateCache.GetOrAdd (name, fun name ->
let file = RazorRender.Resolve(layoutRoots, name + ".cshtml")
new LoadedTemplateSource(File.ReadAllText(file), file) :> ITemplateSource) }

// Configure templating engine
let config = new TemplateServiceConfiguration()
do config.EncodedStringFactory <- new RawStringFactory()
do config.TemplateManager <- templateManager
do
config.EncodedStringFactory <- new RawStringFactory()
config.TemplateManager <- templateManager
// NOTE: this is good in the context of running F# Formatting via F# scripts,
// however when using F# Formatting as library this hides a memory leak.
// We cannot really know which case this is, so applications using this library
// should make sure they are not in the default AppDomain.
// And F# scripts on the other hand will not explode the temp directory.
config.DisableTempFileLocking <- System.AppDomain.CurrentDomain.IsDefaultAppDomain()
config.Debug <- not config.DisableTempFileLocking
config.CachingProvider <-
new DefaultCachingProvider(
if config.DisableTempFileLocking then
new System.Action<string>(fun s -> ())
else null) :> ICachingProvider

match references with
| Some r ->
config.ReferenceResolver <-
Expand All @@ -71,9 +83,9 @@ type RazorRender(layoutRoots, namespaces, templateName:string, ?modeltype:System
r |> List.toSeq |> Seq.map (CompilerReference.From) }
| None -> ()

do namespaces |> Seq.iter (config.Namespaces.Add >> ignore)
do config.BaseTemplateType <- typedefof<DocPageTemplateBase<_>>
do config.Debug <- true
namespaces |> Seq.iter (config.Namespaces.Add >> ignore)
config.BaseTemplateType <- typedefof<DocPageTemplateBase<_>>

let razorEngine = RazorEngineService.Create(config)

let handleCompile source f =
Expand Down

0 comments on commit 37fd208

Please sign in to comment.