Repository navigation
Expand file tree
/
Copy pathStructure.cs
More file actions
359 lines (318 loc) · 11.8 KB
/
Copy pathStructure.cs
File metadata and controls
359 lines (318 loc) · 11.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.IO;
using System.Collections;
using System.Net;
using Ionic.Zip;
using Winstruct.DALTableAdapters;
namespace Winstruct
{
public class Structure
{
/// <summary>
/// index.php -> file (no \ present)
/// index.php:html -> template (no \ present and : present)
/// css\ -> folder (ends with \)
/// \style.css -> file (starts with \)
/// \style.css:reset -> template (starts with \ and : present)
/// :reset -> template (starts with :)
/// css\style.css -> file (has one or more \ [not at start or end])
/// css\reset.css:reset -> template (has one or more \ [not at start or end] and : present)
///
/// wordpress\ -> create folder named wordpress
/// :wp -> download WP installation to wordpress folder
///
/// :ci -> download CI installation to root of project folder
/// </summary>
public string basePath;
public string projectName;
public string templatePath;
private string projectDir;
private string currentDir;
private string[] Lines;
private ArrayList Errors;
private DAL.ContentTemplatesDataTable table;
/// <summary>
///
/// </summary>
/// <param name="txt"></param>
public Structure(string[] txt = null)
{
this.Lines = txt;
Errors = new ArrayList();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ArrayList Parse()
{
if (this._createProjectDir())
{
this._setDataTable();
foreach (String Line in this.Lines)
{
this._processLine(Line);
}
}
else
{
Errors.Add("Could not create project directory");
}
return Errors;
}
#region Helpers
/// <summary>
///
/// </summary>
/// <param name="Line"></param>
protected void _processLine(String Line)
{
if (string.IsNullOrEmpty(Line) != true)
{
string[] lineParts = Line.Split(':');
if (this._isFolder(Line))
{
currentDir = Line;
if (lineParts.Length > 1)
{
DAL.ContentTemplatesRow row = this._inDataTable(lineParts[1]);
this._createFolder(row.content, this.projectDir + lineParts[0]);
}
else
{
this._createFolder(this.projectDir + Line);
}
}
else // Ok, so we're dealing with a file!
{
// ...and there is a colon present
if (lineParts.Length > 1)
{
DAL.ContentTemplatesRow row = this._inDataTable(lineParts[1]);
if (row == null)
{
Errors.Add("Could not process line: " + Line);
return;
}
int Last = Line.LastIndexOf('\\');
if (row.ItemArray.Length > 0)
{
// We're dealing with a single file for current dir
if (Line.StartsWith("\\"))
{
if (row.contenttype == 1)
{
this._createFile(this.projectDir + currentDir + lineParts[0], row.content);
}
else
{
this._createFileFromUrl(this.projectDir + currentDir + lineParts[0], row.content);
}
}
else if (Last == -1) // We're dealing with a single file for project root
{
if (row.contenttype == 1)
{
this._createFile(this.projectDir + lineParts[0], row.content);
}
else
{
this._createFileFromUrl(this.projectDir + lineParts[0], row.content);
}
}
else // We're dealing with a folder/file combination
{
if (Last > 0)
{
this._createFolder(this.projectDir + Line.Substring(0, Last));
currentDir = Line.Substring(0, Last);
}
if (row.contenttype == 1)
{
this._createFile(this.projectDir + lineParts[0], row.content);
}
else
{
this._createFileFromUrl(this.projectDir + lineParts[0], row.content);
}
}
}
}
else // There is no colon
{
int Last = Line.LastIndexOf('\\');
// We're dealing with a single file for current dir
if (Line.StartsWith("\\"))
{
this._createFile(this.projectDir + currentDir + Line);
}
else if (Last == -1) // We're dealing with a single file for project root
{
this._createFile(this.projectDir + Line);
}
else // We're dealing with a folder/file combination
{
if (Last > 0)
{
this._createFolder(this.projectDir + Line.Substring(0, Last));
currentDir = Line.Substring(0, Last);
}
this._createFile(this.projectDir + Line);
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected Boolean _createProjectDir()
{
this.projectDir = this.basePath + "\\" + this.projectName + "\\";
if (this._createFolder(this.projectDir))
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="Line"></param>
/// <returns></returns>
protected Boolean _isFolder(String Line)
{
char Last = Line[Line.Length - 1];
if (Line.EndsWith("\\") || Line.StartsWith(":") || Line.IndexOf("\\:") > 0)
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="folderName"></param>
/// <returns></returns>
protected Boolean _createFolder(String folderName)
{
FileSystemInfo fsi = Directory.CreateDirectory(folderName);
if (fsi.Exists)
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="Url"></param>
/// <param name="folderName"></param>
protected void _createFolder(String Url, String folderName)
{
if (! Directory.Exists(folderName))
{
this._createFolder(folderName);
}
String zipFile = folderName + "tmp.zip";
WebClient Client = new WebClient();
Client.DownloadFile(Url, @zipFile);
String rootFolder = "";
bool replaceFiles = false;
using (ZipFile zip = ZipFile.Read(zipFile)) {
if (zip[0].IsDirectory)
{
rootFolder = zip[0].FileName;
replaceFiles = true;
}
foreach (ZipEntry entry in zip) {
if (entry.FileName.StartsWith(rootFolder) == false)
{
replaceFiles = false;
}
entry.Extract(folderName, ExtractExistingFileAction.OverwriteSilently);
}
}
File.Delete(zipFile);
if (replaceFiles)
{
DirectoryInfo source = new DirectoryInfo(folderName + "\\" + rootFolder);
DirectoryInfo target = new DirectoryInfo(folderName);
CopyFilesRecursively(source, target);
Directory.Delete(folderName + "\\" + rootFolder, true);
}
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
private void _createFile(String fileName)
{
FileInfo fi = new FileInfo(fileName);
FileStream fstr = fi.Create();
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="Content"></param>
protected void _createFile(string fileName, string Content)
{
File.WriteAllText(@fileName, Content);
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="Url"></param>
protected void _createFileFromUrl(string fileName, string Url)
{
int lastBackslash = fileName.LastIndexOf("\\");
string filePath = fileName.Substring(0, lastBackslash);
if (!Directory.Exists(filePath))
{
this._createFolder(filePath);
}
WebClient Client = new WebClient();
Client.DownloadFile(Url, @fileName);
}
/// <summary>
///
/// </summary>
private void _setDataTable()
{
ContentTemplatesTableAdapter ta = new ContentTemplatesTableAdapter();
table = ta.GetContentTemplates();
}
/// <summary>
///
/// </summary>
/// <param name="Shortcut"></param>
/// <returns></returns>
private DAL.ContentTemplatesRow _inDataTable(string Shortcut)
{
foreach (DAL.ContentTemplatesRow row in table.Rows)
{
if (row.shortcut == Shortcut)
{
return row;
}
}
return null;
}
#endregion
}
}