Beispiel #1
0
        /// <summary>
        /// Deletes old app definition files, project files, and built components
        /// left over from previous runs.
        ///
        /// When we run in an automated test run, the previous test process may not have
        /// died completely, and it may still hold access
        /// to the files/directories we are trying to delete. Thus, we make multiple
        /// attempts to cleanup, sleeping for some time between attempts.
        /// </summary>
        public void CleanUpCompilation()
        {
            GlobalLog.LogStatus("Cleaning up compilation directory, if there is any....");

            // Initialization for the multi-attempt logic
            bool cleanupDone         = false;
            int  maxAttempts         = 5;
            int  timeBetweenAttempts = 1000; // sleep time, in milliseconds

            int attempt = 0;

            while (!cleanupDone && attempt < maxAttempts)
            {
                attempt++;
                try
                {
                    // Delete appdef file.
                    if (FileSW.Exists(_appDefFileName))
                    {
                        GlobalLog.LogStatus("Found appdef file " + _appDefFileName + ". Deleting...");
                        FileSW.Delete(_appDefFileName);
                    }

                    string currentFolder = DirectorySW.GetCurrentDirectory();

                    // Delete bin directory.
                    string binPath = PathSW.Combine(currentFolder, "bin");
                    if (DirectorySW.Exists(binPath))
                    {
                        GlobalLog.LogStatus("Found bin folder " + binPath + ". Deleting...");
                        DirectorySW.Delete(binPath, true);
                        GlobalLog.LogStatus("Bin folder deleted.");
                    }

                    // Delete obj directory.
                    string objPath = PathSW.Combine(currentFolder, "obj");
                    if (DirectorySW.Exists(objPath))
                    {
                        GlobalLog.LogStatus("Found obj folder " + objPath + ". Deleting...");
                        DirectorySW.Delete(objPath, true);
                        GlobalLog.LogStatus("Obj folder deleted.");
                    }

                    // Cleanup done!
                    cleanupDone = true;
                }
                catch (Exception e)
                {
                    // We catch only IOException or UnauthorizedAccessException
                    // since those are thrown if some other process is using the
                    // files and directories we are trying to delete.
                    if (e is IOException || e is UnauthorizedAccessException)
                    {
                        GlobalLog.LogStatus("Cleanup attempt #" + attempt + " failed.");
                        if ((1 == attempt) || (maxAttempts == attempt))
                        {
                            GlobalLog.LogStatus("Here are the active processes on the system.");
                            LogProcessesInfo();
                        }
                        if (maxAttempts == attempt)
                        {
                            GlobalLog.LogStatus("Maximum no. of cleanup attempts reached. Bailing out....");
                            throw;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                Thread.Sleep(timeBetweenAttempts);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generates and compiles the application.
        /// </summary>
        /// <param name="xamlFiles">The first in this array is main page.</param>
        /// <param name="hostType">Host type. Could be Application</param>
        /// <param name="uiCulture"></param>
        /// <param name="extraFiles"></param>
        /// <param name="supportingAssemblies"></param>
        /// <param name="language"></param>
        /// <param name="additionalAppMarkup"></param>
        /// <param name="resources"></param>
        /// <param name="contents"></param>
        /// <param name="debugBaml"></param>
        public void CompileApp(string[] xamlFiles, string hostType, string uiCulture, List <string> extraFiles, List <string> supportingAssemblies, Languages language, string additionalAppMarkup, List <Resource> resources, List <Content> contents, bool debugBaml)
        {
            // Cleanup temp. Xaml file, if necessary.
            string _tempXamlFile = "__XamlTestRunnerTempFile.xaml";

            if (FileSW.Exists(_tempXamlFile))
            {
                FileSW.Delete(_tempXamlFile);
            }



            // Some Xaml files (e.g. those which contain events, <x:Code>, x:Name attributes etc.)
            // need to specify an x:Class attribute on the root tag. We add that here.
            string xClassName = "MySubclass";

            // Load the original Xaml file into a DOM tree, add x:Class attribute to the root element,
            // and then save the DOM tree into a temporary Xaml file
            XmlDocumentSW doc = new XmlDocumentSW();

            doc.PreserveWhitespace = true;
            doc.Load(xamlFiles[0]);


            XmlElement rootElement = doc.DocumentElement;

            if (AddClassAttribute)
            {
                rootElement.SetAttribute("Class", "http://schemas.microsoft.com/winfx/2006/xaml", xClassName);
            }

            if (rootElement.NamespaceURI.IndexOf("schemas.microsoft.com") != -1) //&& !verifierFound)
            {
                AutoCloseWindow = true;
            }

            doc.Save(_tempXamlFile);

            xamlFiles[0] = _tempXamlFile;

            GlobalLog.LogStatus("Start compilation...");

            // Generate app definition file.
            GlobalLog.LogStatus("Generate app definition file...");
            GenerateAppdef(_tempXamlFile, hostType, "winexe", language, additionalAppMarkup);


            CompilerParams compilerParams = new CompilerParams(true);

            //
            // Generate the 'proj' file.
            //
            GlobalLog.LogStatus("Generate project file...");
            foreach (string xamlFile in xamlFiles)
            {
                compilerParams.XamlPages.Add(xamlFile);
            }

            compilerParams.OutputType            = "winexe";
            compilerParams.ApplicationDefinition = _appDefFileName;
            compilerParams.AssemblyName          = _assemblyName;
            compilerParams.RootNamespace         = "Avalon.Test.CoreUI.Parser.MyName";
            compilerParams.Language = language;

            // Put debugging info (line,position) in Baml.
            if (debugBaml)
            {
                compilerParams.XamlDebuggingInformation = true;
            }

            if (uiCulture != null)
            {
                compilerParams.UICulture = uiCulture;
            }

            // Add extraFiles.  They should be either xaml files or code files.
            for (int i = 0; extraFiles != null && i < extraFiles.Count; i++)
            {
                string fileName = extraFiles[i];

                if (PathSW.GetExtension(fileName) == "xaml")
                {
                    compilerParams.XamlPages.Add(fileName);
                }
                else
                {
                    compilerParams.CompileFiles.Add(fileName);
                }
            }

            // Add code-behind files if they exist and they weren't in the extraFiles.
            // Always assume they should be added.
            for (int i = 0; i < compilerParams.XamlPages.Count; i++)
            {
                string fileName = compilerParams.XamlPages[i] + ".cs";

                if (FileSW.Exists(fileName) && !compilerParams.CompileFiles.Contains(fileName))
                {
                    compilerParams.CompileFiles.Add(fileName);
                }
            }

            // Add supporting assemblies, if any, as references
            if (null != supportingAssemblies)
            {
                string currentDirectory = EnvironmentSW.CurrentDirectory;
                for (int i = 0; i < supportingAssemblies.Count; i++)
                {
                    string assemblyName = supportingAssemblies[i];
                    compilerParams.References.Add(new Reference(assemblyName, currentDirectory + "\\" + assemblyName + ".dll"));
                }
            }


            if (References.Count > 0)
            {
                compilerParams.References.AddRange(References);
            }

            // Add Resources, if any
            if (null != resources)
            {
                foreach (Resource r in resources)
                {
                    compilerParams.Resources.Add(r);
                }
            }

            // Add Contents, if any
            if (null != contents)
            {
                foreach (Content c in contents)
                {
                    compilerParams.Contents.Add(c);
                }
            }

            // Also add any loose files specified by TestCaseInfo.SupportFiles
            //



            //TestDefinition td = TestDefinition.Current;
            //td.
            //if (currentTestCaseInfo != null)
            //{
            //    string[] currentSupportFiles = currentTestCaseInfo.SupportFiles;
            //    if (currentSupportFiles != null)
            //    {
            //        foreach (string fileName in currentSupportFiles)
            //        {
            //            compilerParams.Contents.Add(new Content(fileName, "Always"));
            //        }
            //    }
            //}

            //
            // Compile project.
            //
            GlobalLog.LogStatus("Compiling project...");
            Compiler compiler = new Compiler(compilerParams);
            List <ErrorWarningCode> buildErrorsAndWarnings = compiler.Compile(true);

            //
            // Check if Compiling project is successful by verifying that compiled app exists.
            //
            GlobalLog.LogStatus("Check if compiling project was successful...");
            GlobalLog.LogStatus("Check if the BAML and the compiled app both exist.");
            string bamlPath        = _objPath + PathSW.DirectorySeparatorChar + PathSW.ChangeExtension(xamlFiles[0], "baml");
            bool   compilerFailure = false;

            GlobalLog.LogStatus("Baml Path: " + bamlPath);
            if (!FileSW.Exists(bamlPath))
            {
                GlobalLog.LogEvidence("Baml file did not exist");
                compilerFailure = true;
            }
            GlobalLog.LogStatus("Compiled path: " + _compiledPath);
            if (!FileSW.Exists(_compiledPath))
            {
                GlobalLog.LogEvidence("Compiled path did not exist");
                compilerFailure = true;
            }

            if (compilerFailure)
            {
                //Save files to be used for debugging
                GlobalLog.LogStatus("Saving files used in compilation...");
                GlobalLog.LogFile("__CompilerServicesSave.proj");
                foreach (string file in compilerParams.XamlPages)
                {
                    GlobalLog.LogFile(file);
                }
                foreach (string file in compilerParams.CompileFiles)
                {
                    GlobalLog.LogFile(file);
                }
                // Get compilation error message
                string compileErrors   = "Errors: \n";
                string compileWarnings = "Warnings \n";
                if ((null != buildErrorsAndWarnings) && (buildErrorsAndWarnings.Count > 0))
                {
                    // The list contains both errors and warnings.
                    // Get the first error and get its description.
                    foreach (ErrorWarningCode errAndWarn in buildErrorsAndWarnings)
                    {
                        if (errAndWarn.Type == ErrorType.Error)
                        {
                            compileErrors += errAndWarn.Description + "\n";
                            GlobalLog.LogStatus("\nBuild Error Found - " + errAndWarn.Description + "\n");
                            break;
                        }
                        else
                        {
                            compileWarnings += errAndWarn.Description + "\n";
                            GlobalLog.LogStatus("\nWarning - " + errAndWarn.Description + "\n");
                        }
                    }
                }

                TestSetupException setupException = new TestSetupException("Compilation failed: " + compileErrors + compileWarnings);
                // Add the list of build errors and warnings as custom Exception data.
                // This can be used by callers to retrive the errors and warnings list.
                setupException.Data.Add("buildErrorsAndWarnings", buildErrorsAndWarnings);
                throw setupException;
            }
        }