1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Switch cs2j to use NDesk.Options, much saner

This commit is contained in:
Kevin Glynn 2010-10-22 20:28:34 +02:00
parent 7f6eda7e9b
commit 0a2b669ac4

View File

@ -8,6 +8,8 @@ using Antlr.Runtime.Tree;
using Antlr.Runtime; using Antlr.Runtime;
using System.Xml.Serialization; using System.Xml.Serialization;
using NDesk.Options;
using RusticiSoftware.Translator.Utils; using RusticiSoftware.Translator.Utils;
using RusticiSoftware.Translator.AntlrUtils; using RusticiSoftware.Translator.AntlrUtils;
using RusticiSoftware.Translator.CLR; using RusticiSoftware.Translator.CLR;
@ -32,17 +34,18 @@ namespace RusticiSoftware.Translator.CSharp
internal static bool dumpJavaSyntax = false; internal static bool dumpJavaSyntax = false;
internal static bool dumpJava = false; internal static bool dumpJava = false;
internal static bool dumpXMLs = false; internal static bool dumpXmls = false;
internal static bool dumpEnums = false;
internal static string outDir = "."; internal static string outDir = ".";
internal static string cheatDir = ""; internal static string cheatDir = "";
internal static ArrayList netRoot = new ArrayList(); internal static IList<string> netRoot = new List<string>();
internal static ArrayList exNetRoot = new ArrayList(); internal static IList<string> exNetRoot = new List<string>();
internal static ArrayList appRoot = new ArrayList(); internal static IList<string> appRoot = new List<string>();
internal static ArrayList exAppRoot = new ArrayList(); internal static IList<string> exAppRoot = new List<string>();
internal static ArrayList exclude = new ArrayList(); internal static IList<string> exclude = new List<string>();
internal static DirectoryHT appEnv = new DirectoryHT(null); internal static DirectoryHT appEnv = new DirectoryHT(null);
internal static List<string> macroDefines = new List<string>(); internal static IList<string> macroDefines = new List<string>();
internal static XmlTextWriter enumXmlWriter; internal static XmlTextWriter enumXmlWriter = new XmlTextWriter(Path.Combine(Directory.GetCurrentDirectory(), "enums"), System.Text.Encoding.UTF8);
internal static string xmldumpDir = Path.Combine(".", "tmpXMLs"); internal static string xmldumpDir = Path.Combine(".", "tmpXMLs");
internal static int verbosity = 0; internal static int verbosity = 0;
@ -73,136 +76,54 @@ namespace RusticiSoftware.Translator.CSharp
Environment.Exit(0); Environment.Exit(0);
} }
private static void addDirectories(IList<string> strs, string rawStr) {
string[] argDirs = rawStr.Split(';');
for (int i = 0; i < argDirs.Length; i++)
strs.Add(Path.GetFullPath(argDirs[i]).ToLower());
}
public static void CS2JMain(string[] args) public static void CS2JMain(string[] args)
{ {
long startTime = DateTime.Now.Ticks; long startTime = DateTime.Now.Ticks;
IList<string> remArgs = new List<string>();
// Use a try/catch block for parser exceptions // Use a try/catch block for parser exceptions
try try
{ {
// if we have at least one command-line argument // if we have at least one command-line argument
if (args.Length > 0) if (args.Length > 0)
{ {
if (verbosity >= 2) Console.Error.WriteLine("Parsing...");
// for each directory/file specified on the command line if (verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments...");
for (int i = 0; i < args.Length; i++)
{ OptionSet p = new OptionSet ()
if (args[i].ToLower().Equals("-showtree")) .Add ("v", v => verbosity++)
{ .Add ("version", v => showVersion())
showTree = true; .Add ("help|h|?", v => showUsage())
} .Add ("showtree", v => showTree = true)
else if (args[i].ToLower().Equals("-showcsharp")) .Add ("showcsharp", v => showCSharp = true)
{ .Add ("showjava", v => showJava = true)
showCSharp = true; .Add ("showjavasyntax", v => showJavaSyntax = true)
} .Add ("dumpcsharp", v => dumpCSharp = true)
else if (args[i].ToLower().Equals("-showjava")) .Add ("dumpjava", v => dumpJava = true)
{ .Add ("dumpjavasyntax", v => dumpJavaSyntax = true)
showJava = true; .Add ("tokens", v => displayTokens = true)
} .Add ("D=", def => macroDefines.Add(def))
else if (args[i].ToLower().Equals("-showjavasyntax")) .Add ("dumpenums", v => dumpEnums = true)
{ .Add ("enumdir=", dir => enumXmlWriter = new XmlTextWriter(dir, System.Text.Encoding.UTF8))
showJavaSyntax = true; .Add ("dumpxmls", v => dumpXmls = true)
} .Add ("xmldir=", dir => xmldumpDir = Path.Combine(Directory.GetCurrentDirectory(), dir))
else if (args[i].ToLower().Equals("-dumpcsharp")) .Add ("odir=", dir => outDir = dir)
{ .Add ("cheatdir=", dir => cheatDir = dir)
dumpCSharp = true; .Add ("netdir=", dirs => addDirectories(netRoot, dirs))
} .Add ("exnetdir=", dirs => addDirectories(exNetRoot, dirs))
else if (args[i].ToLower().Equals("-dumpjava")) .Add ("appdir=", dirs => addDirectories(appRoot, dirs))
{ .Add ("exappdir=", dirs => addDirectories(exAppRoot, dirs))
dumpJava = true; .Add ("exclude=", dirs => addDirectories(exclude, dirs))
} ;
else if (args[i].ToLower().Equals("-dumpjavasyntax"))
{
dumpJavaSyntax = true;
}
else if (args[i].ToLower().Equals("-tokens"))
{
displayTokens = true;
}
else if (args[i].ToLower().Equals("-dumpxml"))
{
dumpXMLs = true;
}
else if (args[i].ToLower().Equals("-v"))
{
verbosity++;
}
else if (args[i].ToLower().Equals("-help"))
{
showUsage();
}
else if (args[i].ToLower().Equals("-version"))
{
showVersion();
}
else if (args[i].ToLower().Equals("-D") && i < (args.Length - 1))
{
i++;
macroDefines.Add(args[i]);
}
else if (args[i].ToLower().Equals("-odir") && i < (args.Length - 1))
{
i++;
outDir = args[i];
}
else if (args[i].ToLower().Equals("-dumpenums") && i < (args.Length - 1))
{
i++;
enumXmlWriter = new XmlTextWriter(args[i], System.Text.Encoding.UTF8);
enumXmlWriter.WriteStartElement("enums");
}
else if (args[i].ToLower().Equals("-cheatdir") && i < (args.Length - 1))
{
i++;
cheatDir = args[i];
}
else if (args[i].ToLower().Equals("-netdir") && i < (args.Length - 1))
{
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
netRoot.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-exnetdir") && i < (args.Length - 1))
{
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
exNetRoot.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-appdir") && i < (args.Length - 1))
{
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
appRoot.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-exappdir") && i < (args.Length - 1))
{
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
exAppRoot.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-exclude") && i < (args.Length - 1))
{
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
exclude.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-xmldir") && i < (args.Length - 1))
{
i++;
xmldumpDir = args[i];
}
else
{
// Final argument is translation target // Final argument is translation target
remArgs = p.Parse (args);
// Load .Net templates // Load .Net templates
foreach (string r in netRoot) foreach (string r in netRoot)
@ -211,10 +132,10 @@ namespace RusticiSoftware.Translator.CSharp
// Load Application Class Signatures (i.e. generate templates) // Load Application Class Signatures (i.e. generate templates)
if (appRoot.Count == 0) if (appRoot.Count == 0)
// By default translation target is application root // By default translation target is application root
appRoot.Add(args[i]); appRoot.Add(remArgs[0]);
foreach (string r in appRoot) foreach (string r in appRoot)
doFile(new FileInfo(r), ".cs", addAppSigTranslation, exAppRoot); // parse it doFile(new FileInfo(r), ".cs", addAppSigTranslation, exAppRoot); // parse it
if (dumpXMLs) if (dumpXmls)
{ {
// Get package name and convert to directory name // Get package name and convert to directory name
foreach (DictionaryEntry de in appEnv) foreach (DictionaryEntry de in appEnv)
@ -239,8 +160,6 @@ namespace RusticiSoftware.Translator.CSharp
enumXmlWriter.Close(); enumXmlWriter.Close();
} }
} }
}
}
else else
{ {
showUsage(); showUsage();
@ -262,7 +181,7 @@ namespace RusticiSoftware.Translator.CSharp
// Call processFile on all files below f that have the given extension // Call processFile on all files below f that have the given extension
public static void doFile(FileInfo f, string ext, FileProcessor processFile, ArrayList excludes) public static void doFile(FileInfo f, string ext, FileProcessor processFile, IList<string> excludes)
{ {
// If this is a directory, walk each file/dir in that directory // If this is a directory, walk each file/dir in that directory
if (!excludes.Contains(Path.GetFullPath(f.FullName).ToLower())) if (!excludes.Contains(Path.GetFullPath(f.FullName).ToLower()))