File: NuGetCommandTarget.cs

package info (click to toggle)
nuget 2.8.7%2Bmd510%2Bdhx1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 8,952 kB
  • sloc: cs: 86,650; xml: 1,297; makefile: 25; ansic: 9; sh: 6
file content (143 lines) | stat: -rw-r--r-- 4,334 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.WebMatrix.Extensibility;

namespace NuGet.WebMatrix
{
    /// <summary>
    /// An ICommandTarget implementation for the NuGet gallery
    /// </summary>
    internal class NuGetCommandTarget : ICommandTarget
    {
        public NuGetCommandTarget(ModuleGlobals globals)
        {
            this.Globals = globals;

            this.OpenNuGetGalleryCommand = this.Globals.CommandManager.AddCommand(
                this,
                NuGetCommands.OpenNuGetGalleryCommandId,
                Resources.GalleryCommandName,
                Resources.GalleryCommandTooltip,
                Resources.nuget_32.ConvertToImageSource(),
                Resources.nuget_32.ConvertToImageSource());
        }

        public Microsoft.WebMatrix.Core.Command OpenNuGetGalleryCommand
        {
            get;
            private set;
        }

        private IHostCommands Commands
        {
            get
            {
                return this.Globals.Host.HostCommands;
            }
        }

        private IWebSite CurrentSite
        {
            get
            {
                return this.Host == null ? null : this.Host.WebSite;
            }
        }

        private ModuleGlobals Globals
        {
            get;
            set;
        }

        private IWebMatrixHost Host
        {
            get
            {
                return this.Globals.Host;
            }
        }

        public CommandStatus CanExecute(ICommandId commandId, object parameter)
        {
            if (commandId == null || commandId.GroupId != NuGetCommands.GroupId)
            {
                return CommandStatus.UnSupported;
            }

            if (commandId == NuGetCommands.OpenNuGetGalleryCommandId)
            {
                return CommandStatus.EnabledAndSupported;
            }
            else
            {
                Debug.Fail("Unknown CommandId detected.");
                return CommandStatus.UnSupported;
            }
        }

        public void Execute(ICommandId commandId, object parameter)
        {
            if (commandId == null || commandId.GroupId != NuGetCommands.GroupId)
            {
                return;
            }

            if (commandId == NuGetCommands.OpenNuGetGalleryCommandId)
            {
                this.OpenNuGetGallery();
            }
        }

        private void OpenNuGetGallery()
        {
            try
            {
                INuGetGalleryDescriptor descriptor = GalleryDescriptors.NuGet;
                Task<bool?> galleryTask = this.Globals.Gallery.ShowGallery(descriptor, this.Host.WebSite.Path);

                galleryTask.ContinueWith((task) =>
                {
                    Exception exception = null;

                    if (task.IsFaulted)
                    {
                        exception = task.Exception.Flatten().InnerException;
                    }
                    else
                    {
                        try
                        {
                            // Send "Refresh" command to update the file tree
                            var refreshCommand = this.Host.HostCommands.GetCommand(CommonCommandIds.GroupId, (int)CommonCommandIds.Ids.Refresh);

                            // Passing true to the command forces a refresh from the root of the tree
                            if (refreshCommand.CanExecute(true))
                            {
                                refreshCommand.Execute(true);
                            }
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                    }

                    if (exception != null)
                    {
                        this.Host.ShowExceptionMessage(Resources.String_Error, Resources.String_ErrorOccurred, exception);
                    }
                },
                NuGetExtension.GetCurrentTaskScheduler());
            }
            catch (Exception ex)
            {
                this.Host.ShowExceptionMessage(
                    Resources.String_Error,
                    Resources.String_ErrorOccurred,
                    ex);
            }
        }
    }
}