void BuildSatelliteAssembly (string cultureName, List <string> files)
{
string assemblyPath = BuildAssemblyPath (cultureName);
var info = new ProcessStartInfo ();
var al = new Process ();
string arguments = SetAlPath (info);
var sb = new StringBuilder (arguments);
sb.Append ("/c:\"" + cultureName + "\" ");
sb.Append ("/t:lib ");
sb.Append ("/out:\"" + assemblyPath + "\" ");
if (mainAssembly != null)
sb.Append ("/template:\"" + mainAssembly.Location + "\" ");
string responseFilePath = assemblyPath + ".response";
using (FileStream fs = File.OpenWrite (responseFilePath)) {
using (StreamWriter sw = new StreamWriter (fs)) {
foreach (string f in files)
sw.WriteLine ("/embed:\"" + f + "\" ");
}
}
sb.Append ("@\"" + responseFilePath + "\"");
info.Arguments = sb.ToString ();
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
al.StartInfo = info;
var alOutput = new StringCollection ();
var alMutex = new Mutex ();
DataReceivedEventHandler outputHandler = (object sender, DataReceivedEventArgs args) => {
if (args.Data != null) {
alMutex.WaitOne ();
alOutput.Add (args.Data);
alMutex.ReleaseMutex ();
}
};
al.ErrorDataReceived += outputHandler;
al.OutputDataReceived += outputHandler;
// TODO: consider using asynchronous processes
try {
al.Start ();
} catch (Exception ex) {
throw new HttpException (String.Format ("Error running {0}", al.StartInfo.FileName), ex);
}
Exception alException = null;
int exitCode = 0;
try {
al.BeginOutputReadLine ();
al.BeginErrorReadLine ();
al.WaitForExit ();
exitCode = al.ExitCode;
} catch (Exception ex) {
alException = ex;
} finally {
al.CancelErrorRead ();
al.CancelOutputRead ();
al.Close ();
}
if (exitCode != 0 || alException != null) {
// TODO: consider adding a new type of compilation exception,
// tailored for al
CompilerErrorCollection errors = null;
if (alOutput.Count != 0) {
foreach (string line in alOutput) {
if (!line.StartsWith ("ALINK: error ", StringComparison.Ordinal))
continue;
if (errors == null)
errors = new CompilerErrorCollection ();
int colon = line.IndexOf (':', 13);
string errorNumber = colon != -1 ? line.Substring (13, colon - 13) : "Unknown";
string errorText = colon != -1 ? line.Substring (colon + 1) : line.Substring (13);
errors.Add (new CompilerError (Path.GetFileName (assemblyPath), 0, 0, errorNumber, errorText));
}
}
throw new CompilationException (Path.GetFileName (assemblyPath), errors, null);
}
}