예제 #1
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            if (!TaskAvailable())
            {
                return(CommandResult.Disabled);
            }

            IMarkdownFlavorPublishHandler flavorHandler = GetFlavorHandler(TextView.TextBuffer);

            if (flavorHandler == null)
            {
                return(CommandResult.Disabled);
            }

            var workflow = _workflowProvider.GetOrCreate();

            _lastCommandTask = Task.Run(async() => {
                // Get list of installed packages and verify that all the required ones are installed
                var packages = await workflow.Packages.GetInstalledPackagesAsync();
                if (packages.Any(p => p.Package.EqualsIgnoreCase(flavorHandler.RequiredPackageName)))
                {
                    // Text buffer operations should be performed in UI thread
                    await AppShell.SwitchToMainThreadAsync();
                    if (await CheckPrerequisitesAsync())
                    {
                        var textBuffer = SaveFile();
                        if (textBuffer != null)
                        {
                            var inputFilePath = textBuffer.GetFilePath();
                            _outputFilePath   = Path.ChangeExtension(inputFilePath, FileExtension);

                            try {
                                _fs.DeleteFile(_outputFilePath);
                            } catch (IOException ex) {
                                AppShell.ShowErrorMessage(ex.Message);
                                return;
                            }

                            var session = workflow.RSession;
                            await flavorHandler.PublishAsync(session, AppShell, _fs, inputFilePath, _outputFilePath, Format, textBuffer.GetEncoding()).ContinueWith(t => LaunchViewer());
                        }
                    }
                }
                else
                {
                    await AppShell.ShowErrorMessageAsync(Resources.Error_PackageMissing.FormatInvariant(flavorHandler.RequiredPackageName));
                }
            });

            return(CommandResult.Executed);
        }