forked from paulirwin/JavaToCSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (100 loc) · 4.26 KB
/
Copy pathProgram.cs
File metadata and controls
110 lines (100 loc) · 4.26 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
using System;
using System.IO;
using System.Threading;
using JavaToCSharp;
using Microsoft.Extensions.Logging;
namespace JavaToCSharpCli
{
public class Program
{
private static readonly ILogger _logger;
static Program()
{
var loggerFactory =
LoggerFactory.Create(builder => builder.AddSimpleConsole().SetMinimumLevel(LogLevel.Information));
_logger = loggerFactory.CreateLogger<Program>();
}
public static void Main(string[] args)
{
try
{
if (args == null || args.Length < 3)
ShowHelp();
else
switch (args[0])
{
case "-f":
ConvertToCSharpFile(args[1], args[2]);
break;
case "-d":
ConvertToCSharpDir(args[1], args[2]);
break;
default:
ShowHelp();
break;
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message, ex);
}
// allow logger background thread to flush
Thread.Sleep(TimeSpan.FromSeconds(1));
}
private static void ConvertToCSharpDir(string folderPath, string outputFolderPath)
{
if (Directory.Exists(folderPath))
{
var input = new DirectoryInfo(folderPath);
foreach (var f in input.GetFiles("*.java", SearchOption.AllDirectories))
{
var newFolderPath =
f.DirectoryName.Replace(folderPath, outputFolderPath, StringComparison.OrdinalIgnoreCase);
if (!Directory.Exists(newFolderPath))
{
Directory.CreateDirectory(newFolderPath);
}
ConvertToCSharpFile(f.FullName,
Path.Combine(newFolderPath, Path.ChangeExtension(f.Name, ".cs")),
false);
}
}
else
_logger.LogError("Java input folder {path} doesn't exist!", folderPath);
}
private static void ConvertToCSharpFile(string filePath, string outputFilePath, bool overwrite = true)
{
if (!overwrite && File.Exists(outputFilePath))
_logger.LogInformation("{outputFilePath} exists, skip to next.", outputFilePath);
else if (File.Exists(filePath))
{
try
{
var javaText = File.ReadAllText(filePath);
var options = new JavaConversionOptions();
options.WarningEncountered += (sender, eventArgs) =>
{
var message = $"Line {eventArgs.JavaLineNumber}: {eventArgs.Message}";
_logger.LogWarning(message);
File.AppendAllText(Path.ChangeExtension(outputFilePath, ".warning"), message + Environment.NewLine);
};
var parsed = JavaToCSharpConverter.ConvertText(javaText, options);
File.WriteAllText(outputFilePath, parsed);
_logger.LogInformation("{filePath} converted!", filePath);
}
catch (Exception ex)
{
_logger.LogError("{filePath} failed! {type}: {message}", filePath, ex.GetType().Name, ex);
File.WriteAllText(Path.ChangeExtension(outputFilePath, ".error"), ex.ToString());
}
}
else
_logger.LogError("Java input file {filePath} doesn't exist!", filePath);
}
private static void ShowHelp()
{
Console.WriteLine("Usage:\r\n\tJavaToCSharpCli.exe -f [pathToJavaFile] [pathToCsOutputFile]");
Console.WriteLine("\tJavaToCSharpCli.exe -d [pathToJavaFolder] [pathToCsOutputFolder]");
}
}
}