Ejemplo n.º 1
0
        private void SendReceiveButtonClick(object sender, EventArgs e)
        {
            var form = FindForm();

            switch (ImportFailureServices.GetFailureStatus(_liftProject))
            {
            default:
                throw new InvalidOperationException("Failure Status not recognized.");

            case ImportFailureStatus.BasicImportNeeded:
                // Initial basic import failed, so do the safe import again.
                if (BasicImportLexicon == null)
                {
                    return;                             // No handler.
                }
                var eventArgs = new LiftBridgeEventArgs(_liftProject.LiftPathname);
                BasicImportLexicon(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    ImportFailureServices.RegisterBasicImportFailure(form, _liftProject);
                    form.Close();
                    return;
                }
                break;

            case ImportFailureStatus.StandardImportNeeded:
                // A standard import failed, so retry it.
                if (ImportLexicon == null)
                {
                    return;                             // No handler.
                }
                var args = new LiftBridgeEventArgs(_liftProject.LiftPathname);
                ImportLexicon(this, args);
                if (args.Cancel)
                {
                    // FLEx cancelled the import for reasons known only to FLEx.
                    ImportFailureServices.RegisterStandardImportFailure(form, _liftProject);
                    form.Close();
                    return;
                }
                break;

            case ImportFailureStatus.NoImportNeeded:
                // Everything is fine, so go on.
                break;
            }
            ImportFailureServices.ClearImportFailure(_liftProject);

            if (!_haveExportedFromFlex)
            {
                // Export Lift data, but only once per launch, if no real import was done.
                if (ExportLexicon != null)
                {
                    // 1. Keep track of all extant files in all folders, except the .hg (or .git) folder.
                    var baseProjectDir         = Path.GetDirectoryName(_liftProject.LiftPathname);
                    var extantFileBeforeExport = FileAndDirectoryServices.EnumerateExtantFiles(baseProjectDir);
// ReSharper disable AssignNullToNotNullAttribute
                    var tempPathname = Path.Combine(baseProjectDir, _liftProject.LiftPathname + ".tmp");
// ReSharper restore AssignNullToNotNullAttribute

                    var eventArgs = new LiftBridgeEventArgs(tempPathname);
                    ExportLexicon(this, eventArgs);
                    if (eventArgs.Cancel)
                    {
                        MessageBox.Show(form,
                                        Resources.kFlexExportProblemMessage,
                                        Resources.kFlexExportProblemTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        try
                        {
                            // 2. Do search of all files and folders (except .hg) and delete any files not found in #1, above.
                            // This will delete any new files that may have been written by the aborted export process.
                            FileAndDirectoryServices.WipeOutNewStuff(
                                extantFileBeforeExport,
                                FileAndDirectoryServices.EnumerateExtantFiles(baseProjectDir));

                            // 3. This will restore all files in repo that may have been changed in the aborted Export process.
                            _chorusSystem.Repository.RollbackWorkingDirectoryToLastCheckin();
                        }
// ReSharper disable EmptyGeneralCatchClause
                        catch
                        {
                            // Eat exception.
                        }
// ReSharper restore EmptyGeneralCatchClause
                        form.Close();
                        return;
                    }

                    // 2 (if no Cancel was done). Fix up the newly exported file.
                    LiftFileServices.PrettyPrintFile(_liftProject.LiftPathname, tempPathname);

                    _haveExportedFromFlex = true;
                }
            }

            // Use SyncDialog to do the S/R stuff.
            using (var syncDlg = (SyncDialog)_chorusSystem.WinForms.CreateSynchronizationDialog(SyncUIDialogBehaviors.Lazy, SyncUIFeatures.NormalRecommended | SyncUIFeatures.PlaySoundIfSuccessful))
            {
                // Commit/Pull/[Merg]e/Send(Push) is the order Chorus does it.
                // (Setting the options here has no effect on Chorus processing order, but it does help humans know the order.)
                syncDlg.SyncOptions.DoPullFromOthers  = true;
                syncDlg.SyncOptions.DoMergeWithOthers = true;
                syncDlg.SyncOptions.DoSendToOthers    = true;
                var myForm = FindForm();
                syncDlg.ShowDialog(myForm);
                if (syncDlg.DialogResult != DialogResult.OK || !syncDlg.SyncResult.DidGetChangesFromOthers)
                {
                    return;                     // User canceled or nothing came from 'afar'.
                }
                if (ImportLexicon == null)
                {
                    return;                    // No event handler.
                }
                var eventArgs = new LiftBridgeEventArgs(_liftProject.LiftPathname);
                ImportLexicon(this, eventArgs);
                if (eventArgs.Cancel)
                {
                    // FLEx cancelled the import for reasons known only to FLEx.
                    ImportFailureServices.RegisterStandardImportFailure(form, _liftProject);
                    form.Close();
                    return;
                }
                ImportFailureServices.ClearImportFailure(_liftProject);
                if (_liftProject.RepositoryIdentifier == null)
                {
                    _liftProject.RepositoryIdentifier = _chorusSystem.Repository.Identifier;
                }

                // In case the user does another S/R to another repo, after the import,
                // we do want to have FLEx do the export again, just to get what it thinks is its latest.
                _haveExportedFromFlex = false;
            }
        }
        void Startup(object sender, StartupNewEventArgs e)
        {
            switch (e.SystemType)
            {
            default:
                throw new InvalidOperationException("Unrecognized type of shared system.");

            case SharedSystemType.New:
                // Create new repo with empty LIFT file.
                var newRepoPath     = LiftProjectServices.PathToProject(Liftproject);                     // DirectoryUtilities.GetUniqueFolderPath(LiftProjectServices.PathToProject(Liftproject));
                var newLiftPathname = Path.Combine(
                    newRepoPath,
                    Liftproject.LiftProjectName + ".lift");
                File.WriteAllText(newLiftPathname, Resources.kEmptyLiftFileXml);
                HgRepository.CreateRepositoryInExistingDir(newRepoPath, new NullProgress());
                var newRepo = new HgRepository(newRepoPath, new NullProgress());
                newRepo.AddAndCheckinFile(newLiftPathname);
                Liftproject.RepositoryIdentifier = newRepo.Identifier;
                break;

            case SharedSystemType.Extant:
                var result = _getSharedProject.GetSharedProjectUsing(MainForm, e.ExtantRepoSource, ProjectFilter, LiftProjectServices.BasePath, Liftproject.LiftProjectName);
                switch (result.CloneStatus)
                {
                //case CloneResult.OkToCreate: Not going to be returned.
                //  break;
                case CloneStatus.NotCreated:
                    // Clone not made for some reason.
                    MessageBox.Show(MainForm, Resources.kDidNotCloneSystem, Resources.kLiftSetUp, MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                    _liftBridgeView.Close();
                    return;

                case CloneStatus.Cancelled:
                    MessageBox.Show(MainForm, Resources.kUserCancelledCloneOperation, Resources.kSharingAttempCancelled, MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    _liftBridgeView.Close();
                    return;

                case CloneStatus.Created:
                    // Proceed
                    var clonedRepo = new HgRepository(result.ActualLocation, new NullProgress());
                    Liftproject.RepositoryIdentifier = clonedRepo.Identifier;
                    break;
                }

                if (BasicLexiconImport != null)
                {
                    var eventArgs = new LiftBridgeEventArgs(Liftproject.LiftPathname);
                    BasicLexiconImport(this, eventArgs);
                    if (eventArgs.Cancel)
                    {
                        // Event handler could not complete the basic import.
                        ImportFailureServices.RegisterBasicImportFailure((_liftBridgeView as Form), Liftproject);
                        _liftBridgeView.Close();
                        return;
                    }
                }
                break;
            }

            InstallExistingSystemControl();
        }