using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Protobuf.CLI
{
class Program
{
static int Main(string[] args)
{
try
{
Console.Title = "Tango Protobuf Compiler";
#if DEBUG
if (args.Length == 0)
{
args = new string[]
{
"-i " + Path.GetFullPath("..\\..\\..\\..\\PMR\\Messages"),
"-o " + Path.GetFullPath("..\\..\\..\\Tango.PMR"),
"-l CSharp",
};
}
#endif
if (args.Length == 0)
{
return ExitHelp();
}
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
options.SourceFolder = options.SourceFolder.Trim();
options.OutputFolder = options.OutputFolder.Trim();
options.Language = options.Language.Trim();
if (!Directory.Exists(options.SourceFolder))
{
return ExitError("Could not locate source folder \"" + Path.GetFullPath(options.SourceFolder) + "\"");
}
if (!Directory.Exists(options.OutputFolder))
{
return ExitError("Could not locate output folder " + Path.GetFullPath(options.OutputFolder));
}
CompilerLanguage language;
if (!Enum.TryParse<CompilerLanguage>(options.Language, out language))
{
return ExitError("Invalid language: " + options.Language);
}
try
{
var compiler = CompilerFactory.CreateCompiler(language);
compiler.CompilationProgress += (x, e) =>
{
try
{
Console.SetCursorPosition(0, 0);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, 0);
Console.Write("Compiling " + Path.GetFileName(e.File) + "...");
Console.SetCursorPosition(Console.WindowWidth - 7, 0);
Console.Write("(% " + Math.Round((((double)e.Current / (double)e.Total) * 100d), 0) + ")");
}
catch { }
};
var result = compiler.CompileFolder(Path.GetFullPath(options.SourceFolder), options.Includes != null ? options.Includes.Split(',') : null);
result.Save(Path.GetFullPath(options.OutputFolder));
}
catch (Exception ex)
{
return ExitError(ex.ToString());
}
}
return ExitSuccess("Protobuf folder compiled to " + Path.GetFullPath(options.OutputFolder));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return -1;
}
}
private static int ExitSuccess(String text)
{
Console.WriteLine(text);
return 0;
}
private static int ExitError(String error)
{
Console.WriteLine(error);
return -1;
}
private static int ExitHelp()
{
Console.WriteLine(
@"
Example: proto-tc -i <source folder> -o <output folder> -l <language>
Available -l arguments:
CSharp
Java
JavaNano
CPP
C
EmbeddedC
JS
Python
PHP
Ruby
");
return -1;
}
}
}