public static bool ProcessSolution(string solutionFileName, Options options)
{
if (string.IsNullOrEmpty(solutionFileName) || !File.Exists(solutionFileName))
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentUICulture,
Resources.Program_Main_the_file_path___0___is_either_invalid_or_doesn_t_exist_, solutionFileName));
}
solutionFileName = Path.GetFullPath(solutionFileName);
Source.TraceEvent(TraceEventType.Information, 0, Resources.Program_Main_Creating_VS_instance___);
using (new MessageFilter())
{
var result = DteHelper.CreateDteInstance();
var dte = result.Item2;
var processId = result.Item1;
try
{
Source.TraceEvent(TraceEventType.Information, 0, Resources.Program_Main_Opening__0_, solutionFileName);
dte.Solution.Open(solutionFileName);
Source.TraceEvent(TraceEventType.Verbose, 0, Resources.Program_Main_Finding_and_processing___tt_templates___);
var firstError =
FindTemplates(Path.GetDirectoryName(solutionFileName))
.Select(t => Tuple.Create(t, ProcessTemplate(dte, t, options)))
.FirstOrDefault(tuple => tuple.Item2.Count > 0);
if (firstError != null)
{
Source.TraceEvent(TraceEventType.Warning, 0, Resources.Program_Main_FAILED_to_process___0__,
firstError.Item1);
foreach (var error in firstError.Item2)
{
Source.TraceEvent(TraceEventType.Error, 0, Resources.Program_Main_, error);
}
return false;
}
Source.TraceEvent(TraceEventType.Information, 0, Resources.Program_Main_Everything_worked_);
return true;
}
finally
{
Process process = null;
if (processId > 0)
{
process = Process.GetProcessById(processId);
}
dte.Quit();
// Makes no sense to wait when the process already exited, or when we have no processId to kill.
int i = 0;
while (i < 10 && process != null && !process.HasExited)
{
Thread.Sleep(1000);
i++;
}
if (process != null && !process.HasExited)
{
process.Kill();
}
}
}
}