Skip to content

Commit c49fe74

Browse files
committed
Tried to fix some more tests, but ended up breaking others.
Ran out of time again, deployment will have to be put off for another night.
1 parent 9ea5df4 commit c49fe74

32 files changed

Lines changed: 457 additions & 158 deletions
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

lib/ServiceStack.OrmLite.dll

3 KB
Binary file not shown.

lib/ServiceStack.OrmLite.pdb

12 KB
Binary file not shown.

src/ServiceStack.Razor/RazorFormat.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static bool IsRegistered
7575

7676
public Dictionary<string, Type> RazorExtensionBaseTypes { get; set; }
7777

78-
readonly TemplateProvider templateProvider = new TemplateProvider(DefaultTemplateName);
78+
public TemplateProvider TemplateProvider { get; set; }
7979

8080
public Type DefaultBaseType
8181
{
@@ -115,6 +115,11 @@ public RazorFormat()
115115
{"cshtml", typeof(ViewPage<>) },
116116
{"rzr", typeof(ViewPage<>) },
117117
};
118+
this.TemplateProvider = new TemplateProvider(DefaultTemplateName) {
119+
CompileInParallel = true,
120+
CompileWithNoOfThreads = Environment.ProcessorCount * 2,
121+
};
122+
this.VirtualPathProvider = EndpointHost.VirtualPathProvider;
118123
}
119124

120125
public void Register(IAppHost appHost)
@@ -134,7 +139,7 @@ public void Configure(IAppHost appHost)
134139
foreach (var ns in EndpointHostConfig.RazorNamespaces)
135140
TemplateNamespaces.Add(ns);
136141

137-
this.ReplaceTokens = new Dictionary<string, string>(appHost.Config.MarkdownReplaceTokens);
142+
this.ReplaceTokens = appHost.Config.MarkdownReplaceTokens ?? new Dictionary<string, string>();
138143
if (!appHost.Config.WebHostUrl.IsNullOrEmpty())
139144
this.ReplaceTokens["~/"] = appHost.Config.WebHostUrl.WithTrailingSlash();
140145

@@ -362,6 +367,21 @@ public ViewPageRef GetViewPage(string pageName)
362367
return razorPage;
363368
}
364369

370+
private ViewPageRef GetTemplatePage(string pageName)
371+
{
372+
ViewPageRef razorPage;
373+
374+
var key = "Views/Shared/{0}.cshtml".Fmt(pageName);
375+
MasterPageTemplates.TryGetValue(key, out razorPage);
376+
if (razorPage != null)
377+
{
378+
razorPage.EnsureCompiled();
379+
return razorPage;
380+
}
381+
return null;
382+
}
383+
384+
365385
private void RegisterRazorPages(string razorSearchPath)
366386
{
367387
foreach (var page in FindRazorPagesFn(razorSearchPath))
@@ -371,7 +391,7 @@ private void RegisterRazorPages(string razorSearchPath)
371391

372392
try
373393
{
374-
templateProvider.CompileQueuedPages();
394+
TemplateProvider.CompileQueuedPages();
375395
}
376396
catch (Exception ex)
377397
{
@@ -404,7 +424,7 @@ public IEnumerable<ViewPageRef> FindRazorPages(string dirPath)
404424
templateService.RegisterPage(csHtmlFile.VirtualPath, pageName);
405425

406426
var templatePath = pageType == RazorPageType.ContentPage
407-
? templateProvider.GetTemplatePath(csHtmlFile.Directory)
427+
? TemplateProvider.GetTemplatePath(csHtmlFile.Directory)
408428
: null;
409429

410430
yield return new ViewPageRef(this, csHtmlFile.VirtualPath, pageName, pageContents, pageType) {
@@ -423,7 +443,7 @@ public void AddPage(ViewPageRef page)
423443
{
424444
try
425445
{
426-
templateProvider.QueuePageToCompile(page);
446+
TemplateProvider.QueuePageToCompile(page);
427447
AddViewPage(page);
428448
}
429449
catch (Exception ex)
@@ -518,7 +538,7 @@ public ViewPageRef AddTemplate(string templatePath, string templateContents)
518538
try
519539
{
520540
//template.Compile();
521-
templateProvider.QueuePageToCompile(template);
541+
TemplateProvider.QueuePageToCompile(template);
522542
return template;
523543
}
524544
catch (Exception ex)

src/ServiceStack.Razor/Templating/TemplateService.ServiceStack.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public IRazorTemplate GetTemplate(string name)
8585
if (!templateCache.TryGetValue(name, out instance))
8686
{
8787
var view = viewEngine.GetView(name);
88+
if (view == null)
89+
{
90+
if (name == RazorFormat.DefaultTemplate)
91+
return null;
92+
93+
throw new Exception("Could not find template " + name);
94+
}
8895
view.Compile(); //compiling adds to templateCache
8996

9097
templateCache.TryGetValue(name, out instance);

src/ServiceStack/Html/TemplateProvider.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace ServiceStack.Html
1111
{
1212
public class TemplateProvider
1313
{
14-
public static bool CompileInParallel = false;
15-
public static int CompileWithNoOfThreads = Environment.ProcessorCount * 2;
14+
public bool CompileInParallel { get; set; }
15+
public int CompileWithNoOfThreads { get; set; }
1616

1717
private static readonly ILog Log = LogManager.GetLogger(typeof(TemplateProvider));
1818

@@ -21,6 +21,7 @@ public class TemplateProvider
2121
public TemplateProvider(string defaultTemplateName)
2222
{
2323
this.defaultTemplateName = defaultTemplateName;
24+
this.CompileInParallel = false;
2425
}
2526

2627
readonly Dictionary<string, IVirtualFile> templatePathsFound = new Dictionary<string, IVirtualFile>(StringComparer.InvariantCultureIgnoreCase);
@@ -78,9 +79,9 @@ public void QueuePageToCompile(IViewPage pageToCompile)
7879
public void CompileQueuedPages()
7980
{
8081
Log.InfoFormat("Starting to compile {0}/{1} pages, {2}",
81-
compilePages.Count, priorityCompilePages.Count,
82+
compilePages.Count, priorityCompilePages.Count,
8283
CompileInParallel ? "In Parallel" : "Sequentially");
83-
84+
8485
if (CompileInParallel)
8586
{
8687
var threadsToRun = Math.Min(CompileWithNoOfThreads, compilePages.Count);

src/ServiceStack/ServiceStack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<Compile Include="VirtualPath\FileSystemVirtualDirectory.cs" />
183183
<Compile Include="VirtualPath\FileSystemVirtualFile.cs" />
184184
<Compile Include="VirtualPath\FileSystemVirtualPathProvider.cs" />
185+
<Compile Include="VirtualPath\InMemoryVirtualPathProvider.cs" />
185186
<Compile Include="WebHost.Endpoints\AppHostExtensions.cs" />
186187
<Compile Include="Markdown\CachedExpressionCompiler.cs" />
187188
<Compile Include="Markdown\DynamicTypeGenerator .cs" />

src/ServiceStack/VirtualPath/AbstractVirtualPathProviderBase.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@ public virtual IVirtualFile GetFile(string virtualPath)
4343
public virtual string GetFileHash(string virtualPath)
4444
{
4545
var f = GetFile(virtualPath);
46-
return GetFileHash(f); ;
47-
}
48-
49-
public virtual string GetFileHash(IVirtualFile virtualFile)
50-
{
51-
if (virtualFile == null)
52-
return String.Empty;
53-
54-
return virtualFile.GetFileHash();
46+
return GetFileHash(f);
5547
}
5648

49+
public virtual string GetFileHash(IVirtualFile virtualFile)
50+
{
51+
return virtualFile == null ? string.Empty : virtualFile.GetFileHash();
52+
}
53+
5754
public virtual IVirtualDirectory GetDirectory(string virtualPath)
5855
{
5956
return RootDirectory.GetDirectory(virtualPath);
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using ServiceStack.Text;
6+
using ServiceStack.WebHost.Endpoints;
7+
8+
namespace ServiceStack.VirtualPath
9+
{
10+
/// <summary>
11+
/// In Memory repository for files. Useful for testing.
12+
/// </summary>
13+
public class InMemoryVirtualPathProvider : AbstractVirtualPathProviderBase
14+
{
15+
public InMemoryVirtualPathProvider(IAppHost appHost)
16+
: base(appHost)
17+
{
18+
this.rootDirectory = new InMemoryVirtualDirectory(this);
19+
}
20+
21+
public InMemoryVirtualDirectory rootDirectory;
22+
23+
public override IVirtualDirectory RootDirectory
24+
{
25+
get { return rootDirectory; }
26+
}
27+
28+
public override string VirtualPathSeparator
29+
{
30+
get { return "/"; }
31+
}
32+
33+
public override string RealPathSeparator
34+
{
35+
get { return "/"; }
36+
}
37+
38+
protected override void Initialize()
39+
{
40+
}
41+
42+
public void AddFile(string filePath, string contents)
43+
{
44+
rootDirectory.AddFile(filePath, contents);
45+
}
46+
47+
public override IVirtualFile GetFile(string virtualPath)
48+
{
49+
return rootDirectory.GetFile(virtualPath)
50+
?? base.GetFile(virtualPath);
51+
}
52+
}
53+
54+
public class InMemoryVirtualDirectory : AbstractVirtualDirectoryBase
55+
{
56+
public InMemoryVirtualDirectory(IVirtualPathProvider owningProvider)
57+
: base(owningProvider)
58+
{
59+
this.files = new List<InMemoryVirtualFile>();
60+
this.dirs = new List<InMemoryVirtualDirectory>();
61+
this.DirLastModified = DateTime.MinValue;
62+
}
63+
64+
public InMemoryVirtualDirectory(IVirtualPathProvider owningProvider, IVirtualDirectory parentDirectory)
65+
: base(owningProvider, parentDirectory) {}
66+
67+
public DateTime DirLastModified { get; set; }
68+
public override DateTime LastModified
69+
{
70+
get { return DirLastModified; }
71+
}
72+
73+
public List<InMemoryVirtualFile> files;
74+
75+
public override IEnumerable<IVirtualFile> Files
76+
{
77+
get { return files.Cast<IVirtualFile>(); }
78+
}
79+
80+
public List<InMemoryVirtualDirectory> dirs;
81+
82+
public override IEnumerable<IVirtualDirectory> Directories
83+
{
84+
get { return dirs.Cast<IVirtualDirectory>(); }
85+
}
86+
87+
public string DirName { get; set; }
88+
public override string Name
89+
{
90+
get { return DirName; }
91+
}
92+
93+
public override IVirtualFile GetFile(string virtualPath)
94+
{
95+
return files.FirstOrDefault(x => x.FilePath == virtualPath);
96+
}
97+
98+
public override IEnumerator<IVirtualNode> GetEnumerator()
99+
{
100+
throw new NotImplementedException();
101+
}
102+
103+
protected override IVirtualFile GetFileFromBackingDirectoryOrDefault(string fileName)
104+
{
105+
return files.FirstOrDefault(x => x.FilePath == fileName);
106+
}
107+
108+
protected override IEnumerable<IVirtualFile> GetMatchingFilesInDir(string globPattern)
109+
{
110+
throw new NotImplementedException();
111+
}
112+
113+
protected override IVirtualDirectory GetDirectoryFromBackingDirectoryOrDefault(string directoryName)
114+
{
115+
return null;
116+
}
117+
118+
static readonly char[] DirSeps = new[] { '\\', '/' };
119+
public void AddFile(string filePath, string contents)
120+
{
121+
this.files.Add(new InMemoryVirtualFile(VirtualPathProvider, this) {
122+
FilePath = filePath,
123+
FileName = filePath.Split(DirSeps).Last(),
124+
TextContents = contents,
125+
});
126+
}
127+
}
128+
129+
public class InMemoryVirtualFile : AbstractVirtualFileBase
130+
{
131+
public InMemoryVirtualFile(IVirtualPathProvider owningProvider, IVirtualDirectory directory)
132+
: base(owningProvider, directory)
133+
{
134+
this.FileLastModified = DateTime.MinValue;
135+
}
136+
137+
public string FilePath { get; set; }
138+
139+
public string FileName { get; set; }
140+
public override string Name
141+
{
142+
get { return FileName; }
143+
}
144+
145+
public DateTime FileLastModified { get; set; }
146+
public override DateTime LastModified
147+
{
148+
get { return FileLastModified; }
149+
}
150+
151+
public string TextContents { get; set; }
152+
153+
public byte[] ByteContents { get; set; }
154+
155+
public override Stream OpenRead()
156+
{
157+
return new MemoryStream(ByteContents ?? (TextContents ?? "").ToUtf8Bytes());
158+
}
159+
}
160+
161+
162+
}

0 commit comments

Comments
 (0)