aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/VSIX/Tango.BuildExtensions/RemoteDebugCommand.cs
blob: d4f072635dfeed2846a326d526c38748068d4422 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//------------------------------------------------------------------------------
// <copyright file="RemoteDebugCommand.cs" company="Company">
//     Copyright (c) Company.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Linq;
using System.IO;
using System.Diagnostics;

namespace Tango.BuildExtensions
{
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class RemoteDebugCommand : VSIXBase
    {
        private const string SHARED_PATH = @"\\twine01\data\RemoteDebugging";

        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 4129;

        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("c03a7b01-8109-4ec5-8f90-858bed027e5d");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteDebugCommand"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private RemoteDebugCommand(Package package) : base(package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;

            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                commandService.AddCommand(menuItem);
            }
        }

        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static RemoteDebugCommand Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }

        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package)
        {
            Instance = new RemoteDebugCommand(package);
        }

        // %ifnot% $toolWindow$
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            RunRemote();
        }

        private void RunRemote()
        {
            try
            {
                String projectName = DTE.Solution.Properties.Item("StartupProject").Value.ToString();

                RemoteDebugForm dlg = new RemoteDebugForm(projectName);

                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        OpenProgressForm();

                        SetProgressText("Building " + projectName + "...");

                        var project = GetSolutionProjects().SingleOrDefault(x => x.Name == projectName);

                        String filePath = GetProjectOutputFilePath(project);
                        String folder = Path.GetDirectoryName(filePath);
                        String fileName = Path.GetFileName(filePath);
                        String remoteFolder = Path.Combine(SHARED_PATH, projectName);
                        String remoteFilePath = Path.Combine(remoteFolder, fileName);

                        DTE.Solution.SolutionBuild.BuildProject("Debug", project.FullName, true);

                        Directory.CreateDirectory(remoteFolder);

                        CopyDirectory(folder, remoteFolder, true, (file) =>
                         {
                             SetProgressText("Copying to " + file + "...");
                         });

                        String PsExecPath = GetFullPathToContentFile("PsExec.exe");

                        SetProgressText("Executing remote process...");

                        Process p = new Process();
                        p.StartInfo.FileName = PsExecPath;
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        p.StartInfo.CreateNoWindow = true;
                        p.EnableRaisingEvents = true;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.RedirectStandardOutput = false;
                        p.ErrorDataReceived += (x, e) =>
                        {
                            SetProgressText("The process will start shortly, please wait...");
                            Wait(20000);
                            CloseProgressForm();
                        };
                        p.StartInfo.Arguments = String.Format("-u {0} -p {1} -i {2} \"{3}\" -accepteula", dlg.UserName, dlg.Password, dlg.HostName, remoteFilePath);
                        p.Start();
                        p.BeginErrorReadLine();
                    });
                }
            }
            catch (Exception ex)
            {
                CloseProgressForm();
                ShowMessage(ex.Message);
            }
        }
    }
}