2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using System . Runtime . InteropServices ;
5
6
using System . Text ;
6
7
using System . Threading . Tasks ;
7
8
using EnvDTE ;
@@ -24,14 +25,128 @@ public override void QueryCallback(object sender, EventArgs e)
24
25
var menuCommand = sender as OleMenuCommand ;
25
26
if ( menuCommand != null )
26
27
{
27
- menuCommand . Visible = false ;
28
- menuCommand . Enabled = false ;
28
+ if ( GetSelectedProject ( ) != null )
29
+ {
30
+ menuCommand . Visible = true ;
31
+ menuCommand . Enabled = true ;
32
+ }
33
+ else
34
+ {
35
+ menuCommand . Visible = false ;
36
+ menuCommand . Enabled = false ;
37
+ }
29
38
}
30
39
}
31
40
41
+ public Project GetSelectedProject ( )
42
+ {
43
+ IntPtr hierarchyPointer = IntPtr . Zero ;
44
+ IntPtr selectionContainerPointer = IntPtr . Zero ;
45
+
46
+ try
47
+ {
48
+
49
+
50
+ Object selectedObject = null ;
51
+ IVsMultiItemSelect multiItemSelect ;
52
+ uint projectItemId ;
53
+
54
+ IVsMonitorSelection monitorSelection =
55
+ ( IVsMonitorSelection ) Package . GetGlobalService (
56
+ typeof ( SVsShellMonitorSelection ) ) ;
57
+
58
+ monitorSelection . GetCurrentSelection ( out hierarchyPointer ,
59
+ out projectItemId ,
60
+ out multiItemSelect ,
61
+ out selectionContainerPointer ) ;
62
+
63
+ IVsHierarchy selectedHierarchy = Marshal . GetTypedObjectForIUnknown (
64
+ hierarchyPointer ,
65
+ typeof ( IVsHierarchy ) ) as IVsHierarchy ;
66
+
67
+ if ( selectedHierarchy != null )
68
+ {
69
+ if ( ErrorHandler . Failed ( selectedHierarchy . GetProperty (
70
+ projectItemId ,
71
+ ( int ) __VSHPROPID . VSHPROPID_ExtObject ,
72
+ out selectedObject ) ) )
73
+ {
74
+ return null ;
75
+ }
76
+ }
77
+
78
+ Project project = selectedObject as Project ;
79
+
80
+ var vsproject = project ? . Object as VSProject ;
81
+
82
+ if ( vsproject != null )
83
+ {
84
+ //If we are an assembly, and its named WPILib, enable the deploy
85
+ if (
86
+ ( from Reference reference in vsproject . References
87
+ where reference . SourceProject == null
88
+ select reference . Name ) . Any ( name => name . Contains ( "WPILib" ) ) )
89
+ {
90
+ return project ;
91
+ }
92
+ }
93
+
94
+ return null ;
95
+ }
96
+ finally
97
+ {
98
+ if ( selectionContainerPointer != IntPtr . Zero )
99
+ {
100
+ Marshal . Release ( selectionContainerPointer ) ;
101
+ }
102
+
103
+ if ( hierarchyPointer != IntPtr . Zero )
104
+ {
105
+ Marshal . Release ( hierarchyPointer ) ;
106
+ }
107
+ }
108
+ }
109
+
110
+
32
111
public override void ButtonCallback ( object sender , EventArgs e )
33
112
{
34
- OutputWriter . Instance . WriteLine ( "Set Pressed" ) ;
113
+ Project project = GetSelectedProject ( ) ;
114
+
115
+ if ( project == null )
116
+ {
117
+ return ;
118
+ }
119
+
120
+ ClearAllProjectsInSolution ( ) ;
121
+
122
+ project . Globals [ "RobotProject" ] = "yes" ;
123
+ project . Globals . VariablePersists [ "RobotProject" ] = true ;
124
+ project . Save ( ) ;
125
+ }
126
+
127
+ private void ClearAllProjectsInSolution ( )
128
+ {
129
+ var dte = m_package . PublicGetService ( typeof ( DTE ) ) as DTE ;
130
+ var sb = dte ? . Solution ;
131
+ if ( sb == null )
132
+ {
133
+ return ;
134
+ }
135
+
136
+
137
+ foreach ( Project p in sb . Projects )
138
+ {
139
+ var globals = p . Globals ;
140
+ if ( globals == null )
141
+ {
142
+ continue ;
143
+ }
144
+ if ( globals . VariableExists [ "RobotProject" ] )
145
+ {
146
+ globals [ "RobotProject" ] = "no" ;
147
+ }
148
+ p . Save ( ) ;
149
+ }
35
150
}
36
151
}
37
152
}
0 commit comments