forked from jo-neves/CSharpToTypescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (75 loc) · 2.91 KB
/
Copy pathProgram.cs
File metadata and controls
92 lines (75 loc) · 2.91 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
using CSharpToTypescript;
using System;
using System.IO;
namespace ConvertCodeConsole
{
class Program
{
private static ConvertorEngine convertor;
static void Main(string[] args)
{
Console.WriteLine(@"Please select one of the options:
1. Enter the folder path for conversion.
2. Open the window app to convert a C# text.");
string option = Console.ReadLine();
if (option == "2")
{
ConvertCode.Program.Main();
}
else if (option == "1")
{
Console.WriteLine(@"Please enter the path to parse:");
string path = Console.ReadLine();
if (!Directory.Exists(path))
return;
// the string SHOULD NOT END WITH '\'
path = path.EndsWith("\\") ? path.Substring(0, path.Length - 1) : path;
ParseFolder(path);
}
else
return;
}
private static ConvertorEngine Convertor
{
get
{
if (convertor == null)
convertor = new ConvertorEngine();
return convertor;
}
}
private static void ParseFolder(string folderPath)
{
// get all files with cs extension
string[] allFiles = Directory.GetFiles(folderPath, "*.cs");
if (allFiles.Length > 0)
{
// create a folder with -ts in the name at the same level
string currentDirName = Path.GetDirectoryName(folderPath + "\\");
string newDirName = string.Format("{0}-ts", currentDirName);
Directory.CreateDirectory(newDirName);
for (int i = 0; i < allFiles.Length; i++)
{
// get file content
string fileContent = File.ReadAllText(allFiles[i]);
if (string.IsNullOrWhiteSpace(fileContent))
continue;
string fileName = Path.GetFileNameWithoutExtension(allFiles[i]);
string tsFile = Convertor.Convert(fileContent, fileName);
if (tsFile.StartsWith("/* ERROR:"))
fileName = fileName + "-error"; // append so you can spot them faster !
string newFilePath = Path.Combine(newDirName, fileName + ".ts");
File.WriteAllText(newFilePath, tsFile);
}
}
string[] allDirectories = Directory.GetDirectories(folderPath);
if (allDirectories.Length > 0)
{
for (int i = 0; i < allDirectories.Length; i++)
{
ParseFolder(allDirectories[i]);
}
}
}
}
}