-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.cs
More file actions
63 lines (52 loc) · 2.28 KB
/
Converter.cs
File metadata and controls
63 lines (52 loc) · 2.28 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace WebformsToRazorPages {
public class Converter {
internal List<string> ConvertedFiles = new List<string>();
internal Regex WebFormsExtension = new Regex(".aspx$|.aspx.cs$");
private readonly static string RazorExtension = ".cshtml";
public Converter() {
}
/// <summary>
/// Converts all webforms aspx pages and their corresponding cs pages in a directory. Subdirectories are NOT processed.
/// </summary>
/// <param name="path">Path.</param>
public void ConvertProjectInDirectory(string path) {
foreach (var file in Directory.GetFiles(path, "*.aspx")) {
var outputFile = WebFormsExtension.Replace(file, RazorExtension);
Markup.ConvertToRazor(file, outputFile);
ConvertedFiles.Add(file);
}
foreach (var file in Directory.GetFiles(path, "*.aspx.cs")) {
var outputFile = WebFormsExtension.Replace(file, RazorExtension + ".cs");
CodeBehind.ConvertToCSharpCodeBehind(file, outputFile);
ConvertedFiles.Add(file);
}
}
/// <summary>
/// Process a single webforms aspx page and its .cs pair
/// </summary>
/// <param name="path">the full file path to the aspx page</param>
public void ConvertFile(string path) {
var match = WebFormsExtension.Match(path);
if (match.Success) {
Markup.ConvertToRazor(path, WebFormsExtension.Replace(path, RazorExtension));
ConvertedFiles.Add(path);
CodeBehind.ConvertToCSharpCodeBehind(path, WebFormsExtension.Replace(path, RazorExtension + ".cs"));
ConvertedFiles.Add(path);
}
else {
throw new ArgumentException($"{path} file isn't a WebForms view page");
}
}
public IEnumerable<string> ListConvertedFiles() {
yield return $"The following {ConvertedFiles.Count} files were converted:";
foreach (var file in ConvertedFiles.OrderBy(x=>x)) {
yield return file;
}
}
}
}