forked from paulirwin/JavaToCSharp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (60 loc) · 2.26 KB
/
Copy pathProgram.cs
File metadata and controls
69 lines (60 loc) · 2.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
using JavaToCSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace JavaToCSharpCli
{
class Program
{
static void Main(string[] args)
{
if (args == null || args.Length < 2)
{
Console.WriteLine("Usage: JavaToCSharpCli.exe [pathToJavaDir] [pathToCsOutputDir]");
return;
}
//if (!File.Exists(args[0]))
//{
// Console.WriteLine("Java input file doesn't exist!");
// return;
//}
var inputDir = args[0];
var outputDir = args[1];
var inputInfo = new DirectoryInfo(inputDir);
var outputInfo = new DirectoryInfo(outputDir);
var files = inputInfo.GetFiles("*.java", SearchOption.AllDirectories);
var filePaths = files.Select(x => x.FullName).ToList();
if (filePaths.Any())
{
var options = new JavaConversionOptions();
var inOutPairs = filePaths.ToDictionary(fileIn => fileIn, fileIn => $"{fileIn.Replace(inputInfo.FullName, outputInfo.FullName)}.cs");
WriteAllFiles(inOutPairs, options);
}
Console.WriteLine("Done!");
Console.ReadKey();
}
private static void WriteAllFiles(Dictionary<string, string> files, JavaConversionOptions options)
{
foreach (var file in files)
{
var directoryName = new FileInfo(file.Value).DirectoryName;
if (directoryName != null) Directory.CreateDirectory(directoryName);
if (!File.Exists(file.Value))
{
var javaText = File.ReadAllText(file.Key);
try
{
var parsed = JavaToCSharpConverter.ConvertText(javaText, options);
File.WriteAllText(file.Value, parsed);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine($"Skipping file {file.Value} that would write to {file.Key}\n\n");
}
}
}
}
}
}