Example #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);
            }
        }
Example #2
0
        /// <summary>
        /// Encapsulated method - taking 3 input params
        /// Does the file generation.
        /// First based on inputs decides the final file name for the file to be generated.
        /// </summary>
        /// <param name="scenarioid"></param>
        /// <param name="variationstring"></param>
        private void GenerateFile(string scenarioid, string variationstring)
        {
            // If no scenarioid is defined set to all.
            if (scenarioid == null)
            {
                scenarioid = "_all";
            }

            // If newfiledata is null make a bunch of checks to come up with final name and fileextension.
            FileData tempfiledata = definedfiledata;

            if (String.IsNullOrEmpty(tempfiledata.FileName))
            {
                // Use default filename (defined in Attribute or element in TemplateData). Refer GetTemplateData method.
                tempfiledata.FileName = defaultfiledata.FileName;
            }

            if (String.IsNullOrEmpty(tempfiledata.FileExtension))
            {
                // Use default fileextension (defined in Attribute or element in TemplateData). Refer GetTemplateData method.
                tempfiledata.FileExtension = defaultfiledata.FileExtension;
            }

            // Generate XmlDocument with defaultdata document innerxml.
            if (base.canvasdoc == null)
            {
                UtilsLogger.LogError = "Variation document is null";
                return;
            }

            if (base.canvasdoc.GetElementsByTagName(Constants.TemplateDataElement) == null)
            {
                UtilsLogger.LogError = "Variation document did not contain TemplateData element";
                return;
            }

            if (base.canvasdoc.GetElementsByTagName(Constants.TemplateDataElement).Count > 1)
            {
                UtilsLogger.LogError = "Variation document contained more than one TemplateData element";
                return;
            }

            // Generated file name.
            if (_retainfilename == false)
            {
                if (variationstring != null)
                {
                    generatedFile = tempfiledata.FileName + "_Sc" + scenarioid + "_Var" + variationstring + tempfiledata.FileExtension;
                }
                else
                {
                    generatedFile = tempfiledata.FileName + "_Sc" + scenarioid + "_Var" + "all" + tempfiledata.FileExtension;
                }
            }
            else
            {
                generatedFile = tempfiledata.FileName + tempfiledata.FileExtension;
            }

            if (String.IsNullOrEmpty(_fileoutputdirectory) == false)
            {
                if (DirectorySW.Exists(_fileoutputdirectory))
                {
                    generatedFile = _fileoutputdirectory + PathSW.DirectorySeparatorChar + generatedFile;
                }
            }

            if (generatedFile.Contains(PathSW.DirectorySeparatorChar.ToString()))
            {
                string directoryname = PathSW.GetDirectoryName(generatedFile);
                if (string.IsNullOrEmpty(directoryname) == false)
                {
                    if (DirectorySW.Exists(directoryname) == false)
                    {
                        if (DirectorySW.Exists(directoryname) == false)
                        {
                            DirectorySW.CreateDirectory(directoryname);
                        }
                    }
                }
            }

            if (_isxmldocument)
            {
                XmlDocumentSW tempdoc = new XmlDocumentSW();
                tempdoc.LoadXml(base.canvasdoc.DocumentElement.InnerXml);

                GenerateFile(ref tempdoc);

                tempdoc = null;
            }
            else
            {
                GenerateFile(base.canvasdoc.DocumentElement.InnerText);
            }
        }