// Microsoft.CSharp.Compiler
//
// Author(s):
// Jackson Harper (Jackson@LatitudeGeo.com)
//
// (C) 2002 Jackson Harper, All rights reserved.
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Microsoft.CSharp {
[System.Obsolete]
public class Compiler {
private Compiler()
{
}
[MonoTODO("Have not implemented bugreports")]
public static CompilerError[] Compile(string[] sourceTexts,
string[] sourceTextNames, string target, string[] imports,
IDictionary options)
{
VerifyArgs (sourceTexts, sourceTextNames, target);
string[] temp_cs_files;
CompilerError[] errors;
string bugreport_path = null;
StreamWriter bug_report = null;
temp_cs_files = CreateCsFiles (sourceTexts, sourceTextNames);
if (options != null)
bugreport_path = (string)options["bugreport"];
if (bugreport_path != null) {
bug_report = CreateBugReport (sourceTexts, sourceTextNames, bugreport_path);
}
try {
errors = CompileFiles (temp_cs_files, target, imports, options, bug_report);
} catch {
throw;
} finally {
foreach (string temp_file in temp_cs_files) {
FileInfo file = new FileInfo (temp_file);
file.Delete ();
}
if (bug_report != null)
bug_report.Close ();
}
return errors;
}
//
// Private Methods
//
private static CompilerError[] CompileFiles (string[] cs_files,
string target, string[] imports, IDictionary options, StreamWriter bug_report)
{
ArrayList error_list = new ArrayList ();
Process mcs = new Process ();
string mcs_output;
string[] mcs_output_lines;
mcs.StartInfo.FileName = "mcs";
mcs.StartInfo.Arguments = BuildArgs (cs_files,
target, imports, options);
mcs.StartInfo.CreateNoWindow = true;
mcs.StartInfo.UseShellExecute = false;
mcs.StartInfo.RedirectStandardOutput = true;
mcs.StartInfo.RedirectStandardError = true;
try {
mcs.Start ();
mcs_output = mcs.StandardError.ReadToEnd ();
mcs.StandardOutput.ReadToEnd ();
mcs.WaitForExit ();
} finally {
mcs.Close ();
}
mcs_output_lines = mcs_output.Split (
System.Environment.NewLine.ToCharArray ());
foreach (string error_line in mcs_output_lines) {
CompilerError error = CreateErrorFromString (error_line);
if (null != error)
error_list.Add (error);
}
if (bug_report != null) {
bug_report.WriteLine ("### Compiler Output");
bug_report.Write (mcs_output);
}
return (CompilerError[])error_list.ToArray (typeof(CompilerError));
}
///
/// Converts an error string into a CompilerError object
/// Return null if the line was not an error string
///
private static CompilerError CreateErrorFromString(string error_string)
{
CompilerError error = new CompilerError();
Regex reg = new Regex (@"^((?.*)\((?\d*)(,(?\d*))?\)\s){0,}(?\w+)\sCS(?\d*):\s(?.*)",
RegexOptions.Compiled | RegexOptions.ExplicitCapture);
Match match = reg.Match (error_string);
if (!match.Success)
return null;
if (String.Empty != match.Result ("${file}"))
error.SourceFile = match.Result ("${file}");
if (String.Empty != match.Result ("${line}"))
error.SourceLine = Int32.Parse (match.Result ("${line}"));
if (String.Empty != match.Result ("${column}"))
error.SourceColumn = Int32.Parse (match.Result ("${column}"));
error.ErrorLevel = (ErrorLevel)Enum.Parse (typeof(ErrorLevel),
match.Result ("${level}"), true);
error.ErrorNumber = Int32.Parse (match.Result ("${number}"));
error.ErrorMessage = match.Result ("${message}");
return error;
}
private static string[] CreateCsFiles (string[] source_text, string[] source_name)
{
ArrayList temp_file_list = new ArrayList ();
for (int i=0; i