/// <summary> /// Helper method to check if file exists. /// </summary> /// <param name="filename"></param> /// <returns></returns> public static string VerifyFileExists(string filename) { if (filename == null) { return(null); } string filepath = PathSW.GetFullPath(filename.ToLowerInvariant()); if (FileSW.Exists(filepath)) { return(filepath); } if (PathSW.IsPathRooted(filename.ToLowerInvariant()) == true) { string rootpath = PathSW.GetPathRoot(filename); filepath = rootpath + PathSW.DirectorySeparatorChar + PathSW.GetFileName(filename); return(filepath); } // Use additional search paths provided by user. for (int i = 0; _searchpaths != null && i < _searchpaths.Length; i++) { if (String.IsNullOrEmpty(_searchpaths[i]) == false) { filepath = _searchpaths[i] + PathSW.DirectorySeparatorChar + filename; if (FileSW.Exists(filepath)) { return(PathSW.GetFullPath(filepath)); } } } Console.WriteLine("{0} could not be found", filename); return(null); }
/// <summary> /// Log to Build warning file and to Build Log file. /// </summary> /// <param name="e"></param> private void LogToBuildWarning(BuildWarningEventArgs e) { if (e != null) { StringBuilder sb = new StringBuilder(); StringBuilder tab = new StringBuilder(); StringBuilder warningstring = new StringBuilder(); //if (String.IsNullOrEmpty(buildwaringfile) == false) //{ // buildwarning = new StreamWriter(buildwaringfile, true); //} for (int i = 0; i < tabcount; i++) { tab.Append("\t"); } //sb.AppendFormat("{0} - ",e.Timestamp.ToString()); string currentprojfile = null; if (projectfilestack.Count > 0) { currentprojfile = projectfilestack.Peek().ToString(); } if (e.File != null && e.File.ToLowerInvariant() != currentprojfile) { sb.Append(e.File.ToString() + " "); //if (buildwarning != null) //{ // buildwarning.Write(PathSW.GetFullPath(e.File.ToString()) + " "); //buildwarning.Write(currentprojfile + " : " + e.File.ToString() + " "); warningstring.Append(currentprojfile + " : " + e.File.ToString() + " "); //} if (e.LineNumber >= 0 && e.ColumnNumber >= 0) { sb.Append("(" + e.LineNumber + "," + e.ColumnNumber + ") " + " :"); //if (buildwarning != null) //{ // buildwarning.Write("(" + e.LineNumber + "," + e.ColumnNumber + ") " + " :"); //} warningstring.Append("(" + e.LineNumber + "," + e.ColumnNumber + ") " + " :"); } } else { sb.AppendFormat("{0} - :", PathSW.GetFileName(currentprojfile)); //if (buildwarning != null) //{ // buildwarning.Write("{0} - :", currentprojfile); //} warningstring.AppendFormat("{0} - :", currentprojfile); } if (e.Code != null) { sb.Append(" warning " + e.Code + ": "); //if (buildwarning != null) //{ // buildwarning.Write(" warning " + e.Code + ": "); //} warningstring.Append(" warning " + e.Code + ": "); } sb.Append(e.Message); //if (buildwarning != null) //{ // buildwarning.Write(e.Message); // buildwarning.WriteLine(); //} warningstring.Append(e.Message + "\n"); //if (buildwarning != null) //{ // //buildwarning.WriteLine(sb.ToString()); // buildwarning.Close(); // buildwarning = null; //} if (String.IsNullOrEmpty(buildwaringfile) == false) { MSBuildEngineCommonHelper.SaveToMemory(warningstring.ToString(), ref warningmemorystream, buildwaringfile); MSBuildEngineCommonHelper.WritetoFilefromMemory(ref warningmemorystream, buildwaringfile); } MSBuildEngineCommonHelper.LogWarning = sb.ToString(); LogToBuildLog(tab.ToString() + sb.ToString()); sb = null; tab = null; } }
/// <summary> /// Overloaded Build log method, logs to buildlogfile. /// </summary> /// <param name="e"></param> /// <param name="binsertnewline"></param> private void LogToBuildLog(BuildEventArgs e, bool binsertnewline) { BuildWarningEventArgs warningeventargs = null; BuildErrorEventArgs erroreventargs = e as BuildErrorEventArgs; string currentprojectfile = null; StringBuilder sb = new StringBuilder(); string filename = null, errorid = null; int linenumber = -1, columnnumber = -1; if (erroreventargs == null) { warningeventargs = e as BuildWarningEventArgs; if (warningeventargs == null) { goto NonWarningErrorEvent; } filename = currentprojectfile; if (warningeventargs.LineNumber >= 0 && warningeventargs.ColumnNumber >= 0) { linenumber = warningeventargs.LineNumber; columnnumber = warningeventargs.ColumnNumber; } if (String.IsNullOrEmpty(warningeventargs.Code) == false) { errorid = warningeventargs.Code; } } else { filename = erroreventargs.File; if (erroreventargs.LineNumber >= 0 && erroreventargs.ColumnNumber >= 0) { linenumber = erroreventargs.LineNumber; columnnumber = erroreventargs.ColumnNumber; } if (String.IsNullOrEmpty(erroreventargs.Code) == false) { errorid = erroreventargs.Code; } } if (projectfilestack.Count == 0) { if (String.IsNullOrEmpty(globalcurrentprojfile) == false) { currentprojectfile = globalcurrentprojfile; } } else { currentprojectfile = projectfilestack.Peek().ToString(); } if (filename != null && String.IsNullOrEmpty(currentprojectfile) == false && filename.ToLowerInvariant() != PathSW.GetFileName(currentprojectfile).ToLowerInvariant()) { sb.Append(filename.ToString() + " "); if (linenumber >= 0 && columnnumber >= 0) { sb.Append("(" + linenumber + "," + columnnumber + ") " + " :"); } } if (errorid != null) { sb.Append(" warning " + errorid + ": "); } NonWarningErrorEvent: //if (_btimestamp && baddtimestamp) //{ // //sb.AppendFormat("{0} ms: {1} - ", e.Timestamp.ToString(), e.Timestamp.Millisecond.ToString()); ; // sb.AppendFormat("{0}:{1}:{2}:{3} - ", e.Timestamp.Hour.ToString(), e.Timestamp.Minute.ToString(), e.Timestamp.Second.ToString(), e.Timestamp.Millisecond.ToString()); //} sb.Append(e.Message); StringBuilder tab = new StringBuilder(); for (int i = 0; i < tabcount; i++) { tab.Append("\t"); } LogToBuildLog(tab.ToString() + sb.ToString()); if (binsertnewline) { LogToBuildLog(""); } sb = null; tab = null; }
/// <summary> /// Generate the application definition file. /// </summary> /// <param name="xamlFile"></param> /// <param name="hostType"></param> /// <param name="targetType"></param> /// <param name="language"></param> /// <param name="additionalAppMarkup"></param> private void GenerateAppdef(string xamlFile, string hostType, string targetType, Languages language, string additionalAppMarkup) { TextWriterSW appdefFile = new StreamWriterSW(_appDefFileName); const string sysXmlns = "clr-namespace:System;assembly=mscorlib"; switch (hostType) { case "Application": if (targetType != "Container") { if (!String.IsNullOrEmpty(additionalAppMarkup)) { appdefFile.WriteLine("<Application x:Class=\"Application__\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" StartupUri=\"" + PathSW.GetFileName(xamlFile) + "\" DispatcherUnhandledException=\"HandleException\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"" + sysXmlns + "\">"); } else { appdefFile.WriteLine("<Application x:Class=\"Application__\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" StartupUri=\"" + PathSW.GetFileName(xamlFile) + "\" DispatcherUnhandledException=\"HandleException\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">"); } } else if (!String.IsNullOrEmpty(additionalAppMarkup)) { appdefFile.WriteLine("<Application x:Class=\"Application__\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" DispatcherUnhandledException=\"HandleException\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"" + sysXmlns + "\">"); } else { appdefFile.WriteLine("<Application x:Class=\"Application__\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" DispatcherUnhandledException=\"HandleException\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">"); } break; default: throw new TestSetupException("Parameter hostType has invalid value of " + hostType + ". The only valid value is Application"); } if (!String.IsNullOrEmpty(additionalAppMarkup)) { appdefFile.WriteLine(additionalAppMarkup); } appdefFile.WriteLine("<x:Code>"); appdefFile.WriteLine(" <![CDATA["); if (language == Languages.CSharp) { if (AutoCloseWindow) { appdefFile.WriteLine(" protected override void OnStartup(System.Windows.StartupEventArgs e)"); appdefFile.WriteLine(" {"); appdefFile.WriteLine(" Microsoft.Test.Logging.GlobalLog.LogStatus(\"In TestParserApp.OnStartup()...\");"); appdefFile.WriteLine(" Microsoft.Test.Logging.GlobalLog.LogStatus(\"Current directory: \" + Microsoft.Test.Security.Wrappers.EnvironmentSW.CurrentDirectory);"); appdefFile.WriteLine(" System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();"); appdefFile.WriteLine(" timer.Interval = TimeSpan.FromSeconds(10);"); appdefFile.WriteLine(" timer.Tick += delegate (object o, EventArgs args) {((System.Windows.Threading.DispatcherTimer)o).Stop(); System.Windows.Application.Current.Shutdown();};"); appdefFile.WriteLine(" timer.Start();"); appdefFile.WriteLine(" }"); } appdefFile.WriteLine(" public void HandleException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)"); appdefFile.WriteLine(" {"); appdefFile.WriteLine(" if(null != e.Exception)"); appdefFile.WriteLine(" {"); appdefFile.WriteLine(" e.Handled = true;"); appdefFile.WriteLine(" Microsoft.Test.Serialization.SerializationHelper.StoreException(e.Exception);"); appdefFile.WriteLine(" System.Windows.Application.Current.Shutdown();"); appdefFile.WriteLine(" }"); appdefFile.WriteLine(" }"); } else // Languages.VisualBasic { if (AutoCloseWindow) { appdefFile.WriteLine(" Protected Overrides Sub OnStartup(ByVal e As System.Windows.StartupEventArgs)"); appdefFile.WriteLine(" Dim timer As New System.Windows.Threading.DispatcherTimer()"); appdefFile.WriteLine(" timer.Interval = TimeSpan.FromSeconds(5)"); appdefFile.WriteLine(" AddHandler timer.Tick, AddressOf TimerHandler"); appdefFile.WriteLine(" timer.Start()"); appdefFile.WriteLine(" End Sub"); appdefFile.WriteLine(" Private Sub TimerHandler(ByVal o As Object, ByVal args As EventArgs)"); appdefFile.WriteLine(" Dim timer As System.Windows.Threading.DispatcherTimer"); appdefFile.WriteLine(" timer = o"); appdefFile.WriteLine(" timer.Stop()"); appdefFile.WriteLine(" System.Windows.Application.Current.Shutdown()"); appdefFile.WriteLine(" End Sub"); } appdefFile.WriteLine(" Sub HandleException(ByVal sender As Object, ByVal e As System.Windows.Threading.DispatcherUnhandledExceptionEventArgs)"); appdefFile.WriteLine(" If Not (e.Exception is Nothing)"); appdefFile.WriteLine(" e.Handled = True"); appdefFile.WriteLine(" Microsoft.Test.Serialization.SerializationHelper.StoreException(e.Exception)"); appdefFile.WriteLine(" System.Windows.Application.Current.Shutdown()"); appdefFile.WriteLine(" End If"); appdefFile.WriteLine(" End Sub"); } appdefFile.WriteLine(" ]]>"); appdefFile.WriteLine("</x:Code>"); // Write end tags. switch (hostType) { case "Application": appdefFile.Write("</Application>"); break; } appdefFile.Close(); }