/// <summary>
 /// This function was added as a defensive measure where we saw a couple of times
 /// a file was created for what should have been a directory. This caused the
 /// build scripts to fail in a very non obvious way.
 ///
 /// </summary>
 /// <param name="path"></param>
 private static void DeleteIntermediateDirectory(string path)
 {
     if (File.Exists(path))
     {
         GeneralFileOperations.DeleteFile(path);
     }
     if (Directory.Exists(path))
     {
         GeneralFileOperations.DeleteDirectory(path);
     }
 }
        /// <summary>
        /// Creates the build script and execute.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="results">The results.</param>
        public static void CreateBuildScriptAndExecute(
            BuildControlParameters configuration,
            DailyBuildFullResults results)
        {
            string heroAppSource = FindHeroAppSourceFolder(configuration);

            if (String.IsNullOrEmpty(heroAppSource))
            {
                Console.WriteLine("Could not find Application Parent Folder. Aborting ...");
                ProgramExecutionLog.AddEntry("Could not find Application Parent Folder. Aborting ...");
                return;
            }

            Console.Write("Starting Build/Sign/License Process... ");
            GeneralFileOperations.WriteProgramStatusFile(ExecutionStatus.Running);
            if (configuration.BuildType == BuildTypeEnum.Daily &&
                configuration.UpdateDailyBuildVersion)
            {
                bool allCompletedWithSuccess = ModifyBuildVersionFiles(configuration, results);
                Console.WriteLine("Completed Modifying Revision Files. Overall status was " +
                                  allCompletedWithSuccess.ToString());
            }
            else
            {
                foreach (BuildTargetResults t in results.BuildTargets)
                {
                    t.ModifyVersion.RequestedToRun = false;
                }
            }
            Console.WriteLine("Completed!");
            Console.Write("Beginning to create batch file for build/sign/license operations ... ");
            string commandFile = BuildBatchFile(configuration, results);

            Console.WriteLine("Completed. Running batch file ....");
            ProcessOperations.RunProcess(commandFile, App.StartupPath, "", true);
            Console.WriteLine("Batch File completed. Status not reported here");
            ValidateBuildResults(configuration, results);
            PublishBuildOutput(configuration, results);
            if (configuration.BuildType == BuildTypeEnum.Daily &&
                configuration.UpdateDailyBuildVersion)
            {
                RevertBuildVersionFiles(configuration, results);
            }

            if (configuration.EmailResults)
            {
                SendResultViaEmail(configuration, results);
            }
        }
 /// <summary>
 /// Syncs the enlistment.
 /// </summary>
 /// <param name="configuration">The configuration.</param>
 /// <param name="results">The results.</param>
 public static void SyncEnlistment(
     BuildControlParameters configuration,
     DailyBuildFullResults results)
 {
     Console.Write("Syncing Enlistment ... ");
     GeneralFileOperations.WriteProgramStatusFile(ExecutionStatus.SynEnlistment);
     try
     {
         ProcessInformation process = ProcessOperations.SDSync(configuration);
         CheckForErrorFromSDOperation(process, results, results.EnlistmentSync);
         Console.WriteLine("Success!");
     }
     catch (Exception e)
     {
         Console.WriteLine("Fail!");
         results.EnlistmentSync.SetResults(false, e.Message);
         ProgramExecutionLog.AddEntry(
             "Failed to sync enlistment " +
             " Error was " + e.Message);
     }
     Console.WriteLine("Enlistment Sync Status = " + results.EnlistmentSync.ToString());
 }
        /// <summary>
        /// Publishes the build output.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="results">The results.</param>
        private static void PublishBuildOutput(
            BuildControlParameters configuration,
            DailyBuildFullResults results)
        {
            Console.WriteLine("Publishing results to release share ...");
            string dateFolder = CreateDateTimeFormatString(results.BuildStartTime);

            results.PublishToReleaseShare.Success = true;
            try
            {
                string publishShare       = Path.Combine(configuration.ReleaseShareRoot, dateFolder);
                string publishLogShare    = Path.Combine(publishShare, LogsFolder);
                string publishSourceShare = Path.Combine(publishShare, SourceArchiveFolder);
                string signedXapFolder    = Path.Combine(publishShare, SignedXapFolder);
                string unsignedXapFolder  = Path.Combine(publishShare, UnsignedXapFolder);
                results.PublishShare       = publishShare;
                results.PublishLogShare    = publishLogShare;
                results.PublishSourceShare = publishSourceShare;
                GeneralFileOperations.CreateDirectory(publishShare);
                GeneralFileOperations.CreateDirectory(signedXapFolder);
                GeneralFileOperations.CreateDirectory(unsignedXapFolder);
                GeneralFileOperations.CreateDirectory(publishLogShare);
                GeneralFileOperations.CreateDirectory(publishSourceShare);
                string[] files = null;
                // Copy Signed xaps and license files
                try
                {
                    files = Directory.GetFiles(configuration.BuildOutputPath);
                    foreach (string file in files)
                    {
                        try
                        {
                            string extension = Path.GetExtension(file);
                            if (extension == ".xap" ||
                                file.ToLower().Contains("license"))
                            {
                                string targetFile =
                                    Path.Combine(signedXapFolder, Path.GetFileName(file));
                                File.Copy(file, targetFile);
                            }
                        }
                        catch (Exception e)
                        {
                            ProgramExecutionLog.AddEntry(
                                "Failed to copy result file " + file + " Error message: " + e.Message);
                            results.PublishToReleaseShare.SetResults(false, e.Message);
                        }
                    }
                }
                catch (Exception e1)
                {
                    ProgramExecutionLog.AddEntry(
                        "Failed to access directory " + configuration.BuildOutputPath +
                        " Error message: " + e1.Message);
                    results.PublishToReleaseShare.SetResults(false, e1.Message);
                }

                // Copy unigned xaps
                try
                {
                    files = Directory.GetFiles(
                        Path.Combine(configuration.BuildOutputPath, UnsignedXapFolder));
                    foreach (string file in files)
                    {
                        try
                        {
                            string extension = Path.GetExtension(file);
                            if (extension == ".xap")
                            {
                                string targetFile =
                                    Path.Combine(unsignedXapFolder, Path.GetFileName(file));
                                File.Copy(file, targetFile);
                            }
                        }
                        catch (Exception e)
                        {
                            ProgramExecutionLog.AddEntry(
                                "Failed to copy result file " + file + " Error message: " + e.Message);
                            results.PublishToReleaseShare.SetResults(false, e.Message);
                        }
                    }
                }
                catch (Exception e1)
                {
                    ProgramExecutionLog.AddEntry(
                        "Failed to access directory " + configuration.BuildOutputPath +
                        " " + UnsignedXapFolder +
                        " Error message: " + e1.Message);
                    results.PublishToReleaseShare.SetResults(false, e1.Message);
                }

                // Copy License Files
                try
                {
                    files = Directory.GetFiles(
                        Path.Combine(configuration.BuildOutputPath, LicenseSubDirectory));
                    foreach (string file in files)
                    {
                        try
                        {
                            string extension = Path.GetExtension(file);
                            if (extension == ".xap" ||
                                file.ToLower().Contains("license"))
                            {
                                string targetFile =
                                    Path.Combine(signedXapFolder, Path.GetFileName(file));
                                File.Copy(file, targetFile);
                            }
                        }
                        catch (Exception e)
                        {
                            ProgramExecutionLog.AddEntry(
                                "Failed to copy result file " + file + " Error message: " + e.Message);
                            results.PublishToReleaseShare.SetResults(false, e.Message);
                        }
                    }
                }
                catch (Exception e1)
                {
                    ProgramExecutionLog.AddEntry(
                        "Failed to access directory " + configuration.BuildOutputPath +
                        " " + LicenseSubDirectory +
                        " Error message: " + e1.Message);
                    results.PublishToReleaseShare.SetResults(false, e1.Message);
                }

                // Copy Log Files
                try
                {
                    files = Directory.GetFiles(configuration.LogPath);
                    foreach (string file in files)
                    {
                        try
                        {
                            string logTargetFile =
                                Path.Combine(publishLogShare, Path.GetFileName(file));
                            File.Copy(file, logTargetFile);
                        }
                        catch (Exception e)
                        {
                            ProgramExecutionLog.AddEntry(
                                "Failed to copy result file " + file + " Error message: " + e.Message);
                            results.PublishToReleaseShare.SetResults(false, e.Message);
                        }
                    }
                }
                catch (Exception e1)
                {
                    ProgramExecutionLog.AddEntry(
                        "Failed to access directory " + configuration.LogPath +
                        " Error message: " + e1.Message);
                    results.PublishToReleaseShare.SetResults(false, e1.Message);
                }

                for (Int32 i = 0; i < results.BuildTargets.Length; i++)
                {
                    results.BuildTargets[i].BuildLogFileLink =
                        Path.Combine(publishLogShare,
                                     Path.GetFileName(results.BuildTargets[i].BuildLogFileLink));
                }
            }
            catch (Exception e)
            {
                ProgramExecutionLog.AddEntry(
                    "General fail during Publish" + "Error message: " + e.Message);
                results.PublishToReleaseShare.SetResults(false, e.Message);
            }
            if (configuration.ArchiveSource)
            {
                ZipSourceFiles(configuration, results, results.PublishSourceShare);
            }
            Console.WriteLine("Finish publish operation. Overall status was " +
                              results.PublishToReleaseShare.Success.ToString());
        }