Example #1
0
 private void UpdateDestinationDriveLetter(string driveName)
 {
     if (!string.IsNullOrWhiteSpace(driveName))
     {
         DestinationDirectory = driveName.Substring(0, 1) + DestinationDirectory.Substring(DestinationDirectory.IndexOf(':'));
     }
 }
Example #2
0
        /// <summary>
        /// Runs the backup
        /// </summary>
        public void RunBackup()
        {
            _log.Info("Beginning backup");
            ClearProgressCounts();
            _cancelToken = new CancellationTokenSource();
            LastRunTime  = DateTime.Now;

            Task.Factory.StartNew(() =>
            {
                if (CheckDriveExists(DestinationDirectory.Substring(0, 1)))
                {
                    BuildActionQueue();
                    int nonFileActions = ActionQueue
                                         .Where(aq => aq.GetType() == typeof(CreateFolderAction))
                                         .Count();
                    TotalFileCount = SkipFileActionQueue.Count + ActionQueue.Count + DeleteActionStack.Count - nonFileActions;
                }
                else
                {
                    _cancelToken.Cancel();
                }
            })
            .ContinueWith((t) =>
            {
                ApplyActions();
            })
            .ContinueWith((t) =>
            {
                ProcessingStatus = _cancelToken.IsCancellationRequested ? BackupProcessingStatus.Cancelled :
                                   BackupProcessingStatus.Finished;
            });
        }
Example #3
0
        public BackupSetViewModel(IBackupSet backupSet, IUIVisualizerService uiVisualizerService)
        {
            Argument.IsNotNull(() => backupSet);
            Argument.IsNotNull(() => uiVisualizerService);
            BackupSet            = backupSet;
            _uiVisualizerService = uiVisualizerService;
            _timer = new Timer(new TimerCallback((o) =>
            {
                RefreshLog();
            }), null, Timeout.Infinite, Timeout.Infinite);

            UpdateScheduleStatus();

            BrowseSourceCommand       = new Command(() => SourceDirectory = SetDirectory(SourceDirectory, "Select Source Directory"));
            BrowseDestinationCommand  = new Command(() => DestinationDirectory = SetDirectory(DestinationDirectory, "Select Destination Directory"));
            ExcludeDirectoriesCommand = new Command(OnExcludeDirectoriesExecute, () => !String.IsNullOrEmpty(SourceDirectory));
            RunBackupCommand          = new Command(() =>
            {
                if (BackupSet.DestinationType == BackupDestinationType.ExternalDrive)
                {
                    var typeFactory             = this.GetTypeFactory();
                    var driveSelectionViewModel = typeFactory.CreateInstanceWithParametersAndAutoCompletion <DriveSelectionViewModel>();
                    driveSelectionViewModel.SetDefaultDrive(DestinationDirectory.Substring(0, 1));
                    if (_uiVisualizerService.ShowDialog(driveSelectionViewModel) == true)
                    {
                        UpdateDestinationDriveLetter(driveSelectionViewModel.SelectedDrive.Name);
                    }
                    else
                    {
                        return;
                    }
                }
                _timer.Change(1000, 1000);
                BackupSet.RunBackup();
            }
                                                    , () => CanRunBackup);

            CancelBackupCommand = new Command(() =>
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
                BackupSet.CancelBackup();
            }
                                              , () => CanCancelBackup);

            EditBackupSetCommand = new RelayCommand((o) =>
            {
                StateService.RequestBackupSetEdit((string)o);
            }
                                                    , (o) => ProcessingStatus == BackupProcessingStatus.NotStarted ||
                                                    ProcessingStatus == BackupProcessingStatus.Cancelled ||
                                                    ProcessingStatus == BackupProcessingStatus.Finished);

            FinishEditingBackupSetCommand = new RelayCommand((o) =>
            {
                StateService.RequestBackupSetEdit((string)o);
            });

            BackupSet.PropertyChanged += BackupSetPropertyChanged;
        }
        public override void RunFinishedExtention()
        {
            if (DestinationDirectory.EndsWith(SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName))
            {
                //The projects were created under a seperate folder -- lets fix it

                //todo this appears to do something incorrect if create directory for solution was not selected

                //first move each projects up a directory
                var projectsObjects = new List <Project>();
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        var projectBadPath  = fileName;
                        var projectGoodPath = projectBadPath.Replace(
                            SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName + Path.DirectorySeparatorChar,
                            SafeProjectName + Path.DirectorySeparatorChar);

                        DTE.Solution.Remove(childProject);

                        Directory.Move(Path.GetDirectoryName(projectBadPath), Path.GetDirectoryName(projectGoodPath));

                        DTE.Solution.AddFromFile(projectGoodPath);
                    }
                }
                //now add the references to the plugin project
                //because they got removed when we move the project folders
                Project pluginProject = null;
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        if (fileName.EndsWith(".Plugins.csproj"))
                        {
                            pluginProject = childProject;
                        }
                    }
                }
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        if (fileName.EndsWith(".Test.csproj") ||
                            fileName.EndsWith(".Console.csproj"))
                        {
                            VSProject vsProj = (VSProject)childProject.Object;
                            vsProj.References.AddProject(pluginProject);
                        }
                    }
                }
            }

            if (DestinationDirectory.EndsWith(SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName))
            {
                DestinationDirectory = DestinationDirectory.Substring(0, DestinationDirectory.Length - (Path.DirectorySeparatorChar + SafeProjectName).Length);
            }

            var consoleFileName = DestinationDirectory + Path.DirectorySeparatorChar + SafeProjectName + ".Console" + Path.DirectorySeparatorChar + "Encrypt XRM Connection.bat";

            if (File.Exists(consoleFileName))
            {
                var read = File.ReadAllText(consoleFileName);
                read = read.Replace("$ext_safeprojectname$", SafeProjectName);
                File.WriteAllText(consoleFileName, read);
            }

            var visualStudioService = new VisualStudioService(DTE, useSolutionDirectory: DestinationDirectory);

            //add xrm connection and package settings to solution items
            visualStudioService.AddSolutionItem("xrmpackage.xrmsettings", XrmPackageSettings);
            if (XrmPackageSettings.Connections.Any())
            {
                visualStudioService.AddSolutionItem("solution.xrmconnection", XrmPackageSettings.Connections.First());
            }
            visualStudioService.CloseAllDocuments();

            RemoveEmptyFolders(DestinationDirectory);
        }
Example #5
0
        public override void RunFinishedExtention()
        {
            if (DestinationDirectory.EndsWith(SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName))
            {
                //The projects were created under a seperate folder -- lets fix it

                //first move each projects up a directory
                var projectsObjects = new List <Project>();
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        var projectBadPath  = fileName;
                        var projectGoodPath = projectBadPath.Replace(
                            SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName + Path.DirectorySeparatorChar,
                            SafeProjectName + Path.DirectorySeparatorChar);

                        DTE.Solution.Remove(childProject);

                        Directory.Move(Path.GetDirectoryName(projectBadPath), Path.GetDirectoryName(projectGoodPath));

                        DTE.Solution.AddFromFile(projectGoodPath);
                    }
                }
                //now add the references to the plugin project
                //because they got removed when we move the project folders
                Project pluginProject = null;
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        if (fileName.EndsWith(".Plugins.csproj"))
                        {
                            pluginProject = childProject;
                        }
                    }
                }
                foreach (Project childProject in DTE.Solution.Projects)
                {
                    var fileName = childProject.FileName;
                    if (!string.IsNullOrEmpty(fileName)) //Solution Folder
                    {
                        if (fileName.EndsWith(".Test.csproj") ||
                            fileName.EndsWith(".Console.csproj"))
                        {
                            VSProject vsProj = (VSProject)childProject.Object;
                            vsProj.References.AddProject(pluginProject);
                        }
                    }
                }
            }

            if (DestinationDirectory.EndsWith(SafeProjectName + Path.DirectorySeparatorChar + SafeProjectName))
            {
                DestinationDirectory = DestinationDirectory.Substring(0, DestinationDirectory.Length - (Path.DirectorySeparatorChar + SafeProjectName).Length);
            }

            //okay so lets update the encrypt connection bat and the xrmsetting.txt files in the console project
            var consoleProjectPath = DestinationDirectory + Path.DirectorySeparatorChar + SafeProjectName + ".Console";
            var encryptBatFileName = consoleProjectPath + Path.DirectorySeparatorChar + "Encrypt XRM Connection.bat";

            if (File.Exists(encryptBatFileName))
            {
                var read = File.ReadAllText(encryptBatFileName);
                read = read.Replace("$ext_safeprojectname$", SafeProjectName);
                File.WriteAllText(encryptBatFileName, read);
            }
            var consoleConnectionFileName = consoleProjectPath + Path.DirectorySeparatorChar + "XrmSetting.txt";

            if (File.Exists(consoleConnectionFileName))
            {
                if (XrmPackageSettings != null && XrmPackageSettings.Connections != null && XrmPackageSettings.Connections.Any())
                {
                    try
                    {
                        var connection = XrmPackageSettings.Connections.First();
                        var serialise  = ObjectToJsonString(connection);
                        File.WriteAllText(consoleConnectionFileName, serialise);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error setting console application connection: " + ex.DisplayString());
                    }
                }
            }

            var visualStudioService = new VisualStudioService(DTE, useSolutionDirectory: DestinationDirectory);

            //add xrm connection and package settings to solution items
            visualStudioService.AddSolutionItem("xrmpackage.xrmsettings", XrmPackageSettings);
            if (XrmPackageSettings.Connections.Any())
            {
                visualStudioService.AddSolutionItem("solution.xrmconnection", XrmPackageSettings.Connections.First());
            }
            visualStudioService.CloseAllDocuments();

            RemoveEmptyFolders(DestinationDirectory);
        }
Example #6
0
        private void BuildActionQueue()
        {
            _log.Info("Building Action Queue");
            ProcessingStatus = BackupProcessingStatus.BuildingActionQueue;
            //Clear the action Queues
            ActionQueue.Clear();
            SkipFileActionQueue.Clear();
            DeleteActionStack.Clear();

            if (_cancelToken.IsCancellationRequested)
            {
                _log.Info("Building Action Queue Cancelled");
                ProcessingStatus = BackupProcessingStatus.Cancelled;
                return;
            }

            if (CheckDriveExists(SourceDirectory.Substring(0, 1)) &&
                CheckSourceRootExists() &&
                CheckDriveExists(DestinationDirectory.Substring(0, 1)))
            {
                if (BackupMode == BackupSetMode.Backup && RemoveDeleted)
                {
                    List <DirectoryInfo> destDirs = IOHelper.GetDirectoriesPostOrder(DestinationDirectory);
                    if (destDirs != null)
                    {
                        foreach (DirectoryInfo di in destDirs)
                        {
                            if (_cancelToken.IsCancellationRequested)
                            {
                                ProcessingStatus = BackupProcessingStatus.Cancelled;
                                return;
                            }
                            ProcessDestinationDirectoryForPossibleDelete(di);
                        }
                    }
                }

                var dirs = IOHelper.GetDirectories(SourceDirectory);
                if (dirs != null)
                {
                    foreach (DirectoryInfo di in dirs)
                    {
                        //Check if the directory should be excluded
                        if (ExcludedDirectories == null || !ExcludedDirectories.Contains(di.FullName))
                        {
                            if (_cancelToken.IsCancellationRequested)
                            {
                                ProcessingStatus = BackupProcessingStatus.Cancelled;
                                return;
                            }
                            ProcessDirectoryForActions(di);
                        }
                        else
                        {
                            IOHelper.GetFiles(di.FullName)
                            .ForEach(fi => SkipFileActionQueue.Enqueue(new SkipFileAction(fi.FullName)));
                        }
                    }
                }
            }
            _log.Info("Finished Building Action Queue");
            ProcessingStatus = BackupProcessingStatus.ActionQueueBuilt;
        }