public bool Execute()
{
try
{
string java;
try
{
if (!string.IsNullOrEmpty(JavaExecutable))
{
java = JavaExecutable;
}
else
{
string javaHome = JavaHome;
java = Path.Combine(Path.Combine(javaHome, "bin"), "java.exe");
if (!File.Exists(java))
java = Path.Combine(Path.Combine(javaHome, "bin"), "java");
}
}
catch (NotSupportedException)
{
// Fall back to using IKVM
java = Path.Combine(Path.GetDirectoryName(ToolPath), "ikvm.exe");
}
List<string> arguments = new List<string>();
arguments.Add("-cp");
arguments.Add(ToolPath);
arguments.Add("org.antlr.v4.CSharpTool");
arguments.Add("-o");
arguments.Add(OutputPath);
if (!string.IsNullOrEmpty(Encoding))
{
arguments.Add("-encoding");
arguments.Add(Encoding);
}
if (GenerateListener)
arguments.Add("-listener");
else
arguments.Add("-no-listener");
if (GenerateVisitor)
arguments.Add("-visitor");
else
arguments.Add("-no-visitor");
if (ForceAtn)
arguments.Add("-Xforce-atn");
if (AbstractGrammar)
arguments.Add("-Dabstract=true");
if (!string.IsNullOrEmpty(TargetLanguage))
{
string framework = TargetFrameworkVersion;
if (string.IsNullOrEmpty(framework))
framework = "v2.0";
else if (new Version(framework.Substring(1)) >= new Version(4, 5))
framework = "v4.5";
string language;
if (TargetLanguage.Equals("CSharp", StringComparison.OrdinalIgnoreCase))
language = TargetLanguage + '_' + framework.Replace('.', '_');
else
language = TargetLanguage;
arguments.Add("-Dlanguage=" + language);
}
if (!string.IsNullOrEmpty(TargetNamespace))
{
arguments.Add("-package");
arguments.Add(TargetNamespace);
}
arguments.AddRange(SourceCodeFiles);
ProcessStartInfo startInfo = new ProcessStartInfo(java, JoinArguments(arguments))
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
this.BuildMessages.Add(new BuildMessage(TraceLevel.Info, "Executing command: \"" + startInfo.FileName + "\" " + startInfo.Arguments, "", 0, 0));
Process process = new Process();
process.StartInfo = startInfo;
process.ErrorDataReceived += HandleErrorDataReceived;
process.OutputDataReceived += HandleOutputDataReceived;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.StandardInput.Dispose();
process.WaitForExit();
return process.ExitCode == 0;
//using (LoggingTraceListener traceListener = new LoggingTraceListener(_buildMessages))
//{
// SetTraceListener(traceListener);
// ProcessArgs(args.ToArray());
// process();
//}
//_generatedCodeFiles.AddRange(GetGeneratedFiles().Where(file => LanguageSourceExtensions.Contains(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase)));
//int errorCount = GetNumErrors();
//return errorCount == 0;
}
catch (Exception e)
{
if (e is TargetInvocationException && e.InnerException != null)
e = e.InnerException;
_buildMessages.Add(new BuildMessage(e.Message));
throw;
}
}