forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractVirtualDirectoryBase.cs
More file actions
147 lines (114 loc) · 4.98 KB
/
AbstractVirtualDirectoryBase.cs
File metadata and controls
147 lines (114 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections;
using System.Collections.Generic;
namespace ServiceStack.Razor.VirtualPath
{
public abstract class AbstractVirtualDirectoryBase : IVirtualDirectory
{
protected IVirtualPathProvider VirtualPathProvider;
protected IVirtualDirectory ParentDirectory;
public virtual string VirtualPath { get { return GetVirtualPathToRoot(); } }
public virtual string RealPath { get { return GetRealPathToRoot(); } }
public virtual bool IsDirectory { get { return true; } }
public virtual bool IsRoot { get { return ParentDirectory == null; } }
public abstract IEnumerable<IVirtualFile> Files { get; }
public abstract IEnumerable<IVirtualDirectory> Directories { get; }
public abstract string Name { get; }
protected AbstractVirtualDirectoryBase(IVirtualPathProvider owningProvider)
: this(owningProvider, null) {}
protected AbstractVirtualDirectoryBase(IVirtualPathProvider owningProvider, IVirtualDirectory parentDirectory)
{
if (owningProvider == null)
throw new ArgumentNullException("owningProvider");
this.VirtualPathProvider = owningProvider;
this.ParentDirectory = parentDirectory;
}
public virtual IVirtualFile GetFile(string virtualPath)
{
var tokens = virtualPath.TokenizeVirtualPath(VirtualPathProvider);
return GetFile(tokens);
}
public virtual IVirtualDirectory GetDirectory(string virtualPath)
{
var tokens = virtualPath.TokenizeVirtualPath(VirtualPathProvider);
return GetDirectory(tokens);
}
public virtual IVirtualFile GetFile(Stack<string> virtualPath)
{
if (virtualPath.Count == 0)
return null;
var pathToken = virtualPath.Pop();
if (virtualPath.Count == 0)
return GetFileFromBackingDirectoryOrDefault(pathToken);
var virtDir = GetDirectoryFromBackingDirectoryOrDefault(pathToken);
return virtDir != null
? virtDir.GetFile(virtualPath)
: null;
}
public virtual IVirtualDirectory GetDirectory(Stack<string> virtualPath)
{
if (virtualPath.Count == 0)
return null;
var pathToken = virtualPath.Pop();
var virtDir = GetDirectoryFromBackingDirectoryOrDefault(pathToken);
if (virtDir == null)
return null;
return virtualPath.Count == 0
? virtDir
: virtDir.GetDirectory(virtualPath);
}
public virtual IEnumerable<IVirtualFile> GetAllMatchingFiles(string globPattern, int maxDepth = Int32.MaxValue)
{
if (maxDepth == 0)
yield break;
foreach (var f in GetMatchingFilesInDir(globPattern))
yield return f;
foreach (var childDir in Directories)
{
var matchingFilesInChildDir = childDir.GetAllMatchingFiles(globPattern, maxDepth - 1);
foreach (var f in matchingFilesInChildDir)
yield return f;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
protected virtual string GetVirtualPathToRoot()
{
if (IsRoot)
return VirtualPathProvider.VirtualPathSeparator;
return GetPathToRoot(VirtualPathProvider.VirtualPathSeparator, p => p.VirtualPath);
}
protected virtual string GetRealPathToRoot()
{
return GetPathToRoot(VirtualPathProvider.RealPathSeparator, p => p.RealPath);
}
protected virtual string GetPathToRoot(string separator, Func<IVirtualDirectory, string> pathSel)
{
var parentPath = ParentDirectory != null ? pathSel(ParentDirectory) : string.Empty;
if (parentPath == separator)
parentPath = string.Empty;
return string.Concat(parentPath, separator, Name);
}
public override bool Equals(object obj)
{
var other = obj as AbstractVirtualDirectoryBase;
if (other == null)
return false;
return other.VirtualPath == this.VirtualPath;
}
public override int GetHashCode()
{
return VirtualPath.GetHashCode();
}
public override string ToString()
{
return string.Format("{0} -> {1}", RealPath, VirtualPath);
}
public abstract IEnumerator<IVirtualNode> GetEnumerator();
protected abstract IVirtualFile GetFileFromBackingDirectoryOrDefault(string fileName);
protected abstract IEnumerable<IVirtualFile> GetMatchingFilesInDir(string globPattern);
protected abstract IVirtualDirectory GetDirectoryFromBackingDirectoryOrDefault(string directoryName);
}
}