forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceModel.cs
More file actions
185 lines (153 loc) · 6.29 KB
/
SourceModel.cs
File metadata and controls
185 lines (153 loc) · 6.29 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
namespace Ext.Net.MVC.Examples
{
public class SourceModel
{
private static string[] excludeFolders = new[] { ".svn", "_svn" };
private static string[] excludeList = new[] { "config.xml" };
private static string[] excludeExtensions = new[] { ".png", ".jpg", ".gif", ".bmp", ".psd" };
public static TabPanel BuildSourceTabs(string idSuffix, string url)
{
List<FileInfo> files = SourceModel.GetFiles(url, false);
TabPanel tabs = new TabPanel
{
ID = "tpw" + idSuffix,
Border = false,
ActiveTabIndex = 0
};
int i = 0;
foreach (FileInfo fileInfo in files)
{
Panel panel = new Panel();
panel.ID = "tptw" + idSuffix + i++;
panel.Title = fileInfo.Name;
switch (fileInfo.Extension)
{
case ".aspx":
case ".cshtml":
case ".ascx":
panel.Icon = Icon.PageWhiteCode;
break;
case ".cs":
panel.Icon = Icon.PageWhiteCsharp;
break;
case ".xml":
case ".xsl":
panel.Icon = Icon.ScriptCodeRed;
break;
case ".js":
panel.Icon = Icon.Script;
break;
case ".css":
panel.Icon = Icon.Css;
break;
}
panel.Loader = new ComponentLoader();
panel.Loader.Url = ExamplesModel.ApplicationRoot + "/Source/GetSourceFile";
panel.Loader.Mode = LoadMode.Frame;
panel.Loader.Params.Add(new Parameter("file", SourceModel.PhysicalToVirtual(fileInfo.FullName), ParameterMode.Value));
panel.Loader.LoadMask.ShowMask = true;
tabs.Items.Add(panel);
}
return tabs;
}
private static string PhysicalToVirtual(string physicalPath)
{
string pathOfWebRoot = HttpContext.Current.Server.MapPath("~/").ToLower();
int index = physicalPath.IndexOf(pathOfWebRoot, StringComparison.InvariantCultureIgnoreCase);
if (index == -1)
{
throw new Exception("Physical path can't be mapped to the current application.");
}
string relUrl = Path.DirectorySeparatorChar.ToString();
index += pathOfWebRoot.Length;
relUrl += physicalPath.Substring(index);
relUrl = relUrl.Replace("\\", "/");
return ExamplesModel.ApplicationRoot + relUrl;
}
private static Regex example_RE = new Regex("^/(\\w+)/(\\w+)/$", RegexOptions.Compiled | RegexOptions.Singleline);
public static List<FileInfo> GetFiles(string url, bool download)
{
var list = new List<FileInfo>();
var match = SourceModel.example_RE.Match(url);
if(!match.Success){
return list;
}
var area = match.Groups[1].Value;
var controller = match.Groups[2].Value;
string path = HttpContext.Current.Server.MapPath(ExamplesModel.ExamplesRoot+area);
list.Add(new FileInfo(string.Concat(path, "/Controllers/", controller, "Controller.cs")));
var model = string.Concat(path, "/Models/", controller, "Model.cs");
if(File.Exists(model))
{
list.Add(new FileInfo(model));
}
model = path + "/Models/SharedModel.cs";
if (File.Exists(model))
{
list.Add(new FileInfo(model));
}
DirectoryInfo viewFolder = new DirectoryInfo(string.Concat(path, "/Views/", controller));
FileInfo[] filesInfo = viewFolder.GetFiles();
int dIndex = 0;
int len = list.Count;
for (int ind = 0; ind < filesInfo.Length; ind++)
{
var fileInfo = filesInfo[ind];
if (Path.GetFileNameWithoutExtension(fileInfo.Name).ToLowerInvariant() == "index")
{
dIndex = ind + len;
}
if (excludeList.Contains(fileInfo.Name) || (!download && excludeExtensions.Contains(fileInfo.Extension.ToLowerInvariant())))
{
continue;
}
list.Add(fileInfo);
}
if (download)
{
foreach (DirectoryInfo folder in viewFolder.GetDirectories())
{
if (excludeFolders.Contains(folder.Name.ToLower()) || folder.Name.StartsWith("_"))
{
continue;
}
SourceModel.GetSubFiles(list, folder);
}
}
if (dIndex>0)
{
FileInfo fi = list[dIndex];
list.RemoveAt(dIndex);
list.Insert(0, fi);
}
return list;
}
private static void GetSubFiles(List<FileInfo> fileList, DirectoryInfo dir)
{
FileInfo[] filesInfo = dir.GetFiles();
foreach (FileInfo file in filesInfo)
{
if (excludeExtensions.Contains(file.Extension.ToLower()))
{
continue;
}
fileList.Add(file);
}
DirectoryInfo[] folders = dir.GetDirectories();
foreach (DirectoryInfo folder in folders)
{
if (excludeFolders.Contains(folder.Name.ToLower()) || folder.Name.StartsWith("_"))
{
continue;
}
SourceModel.GetSubFiles(fileList, folder);
}
}
}
}