Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Side by Side Diff: visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/PropertyManager.cs

Issue 10836143: Refactored the VS add-in (Closed) Base URL: https://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 namespace NativeClientVSAddIn
6 {
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10
11 using EnvDTE;
12 using EnvDTE80;
13 using Microsoft.VisualStudio.VCProjectEngine;
14
15 /// <summary>
16 /// This class handles reading and writing properties on property pages.
17 /// </summary>
18 public class PropertyManager
19 {
20 /// <summary>
21 /// The target project to read from.
22 /// </summary>
23 private VCProject project_;
24
25 /// <summary>
26 /// The target configuration and platform to read from.
27 /// </summary>
28 private VCConfiguration configuration_;
29
30 /// <summary>
31 /// Constructs the property manager. Sets the platform type to Other which i nvalidates use
32 /// of the property manager until SetTarget is called with a valid target.
33 /// </summary>
34 public PropertyManager()
35 {
36 ProjectPlatform = ProjectPlatformType.Other;
37 }
38
39 /// <summary>
40 /// Specifies the type of plug-in being run in this debug session.
41 /// </summary>
42 public enum ProjectPlatformType
43 {
44 /// <summary>
45 /// Represents all non-pepper/non-nacl platform types.
46 /// </summary>
47 Other,
48
49 /// <summary>
50 /// Indicates project platform is a trusted plug-in (nexe).
51 /// </summary>
52 NaCl,
53
54 /// <summary>
55 /// Indicates project platform is an untrusted plug-in.
56 /// </summary>
57 Pepper
58 }
59
60 /// <summary>
61 /// Gets or sets the current project platform type. This indicates Pepper, N aCl, or Other type
62 /// of project. If this is set to Other then it is invalid to read the prope rties.
63 /// </summary>
64 public ProjectPlatformType ProjectPlatform { get; protected set; }
65
66 /// <summary>
67 /// Gets or sets the full path to the output assembly.
68 /// </summary>
69 public virtual string PluginAssembly
70 {
71 get
72 {
73 AssertValidPlatform();
74 VCLinkerTool linker = configuration_.Tools.Item("VCLinkerTool");
75 return configuration_.Evaluate(linker.OutputFile);
76 }
77
78 protected set
79 {
80 }
81 }
82
83 /// <summary>
84 /// Gets or sets the main project directory.
85 /// </summary>
86 public virtual string ProjectDirectory
87 {
88 get
89 {
90 AssertValidPlatform();
91 return project_.ProjectDirectory;
92 }
93
94 protected set
95 {
96 }
97 }
98
99 /// <summary>
100 /// Gets or sets the directory where the output assembly is placed.
101 /// </summary>
102 public virtual string OutputDirectory
103 {
104 get
105 {
106 AssertValidPlatform();
107 return configuration_.Evaluate(configuration_.OutputDirectory);
108 }
109
110 protected set
111 {
112 }
113 }
114
115 /// <summary>
116 /// Gets or sets the Native Client VS Add-in version.
117 /// </summary>
118 public string NaClAddInVersion
119 {
120 get
121 {
122 AssertValidPlatform();
123 return GetProperty("ConfigurationGeneral", "NaClAddInVersion");
124 }
125
126 protected set
127 {
128 }
129 }
130
131 /// <summary>
132 /// Gets or sets this project's setting of where the NaCl SDK Root is.
133 /// </summary>
134 public string SDKRootDirectory
135 {
136 get
137 {
138 AssertValidPlatform();
139 string value = GetProperty("ConfigurationGeneral", "VSNaClSDKRoot");
140
141 if (string.IsNullOrEmpty(value))
142 {
143 System.Windows.Forms.MessageBox.Show(Strings.SDKPathNotSetError);
144 return null;
145 }
146
147 return value.TrimEnd("/\\".ToArray<char>());
148 }
149
150 protected set
151 {
152 }
153 }
154
155 /// <summary>
156 /// Gets or sets the port to use for the web server launched during debuggin g.
157 /// </summary>
158 public string WebServerPort
159 {
160 get
161 {
162 AssertValidPlatform();
163 return GetProperty("ConfigurationGeneral", "NaClWebServerPort");
164 }
165
166 protected set
167 {
168 }
169 }
170
171 /// <summary>
172 /// Gets or sets the full toolchain and platform name. Ex: win_x86_newlib
173 /// </summary>
174 public string PlatformToolset
175 {
176 get
177 {
178 AssertValidPlatform();
179 return GetProperty("ConfigurationGeneral", "PlatformToolset");
180 }
181
182 protected set
183 {
184 }
185 }
186
187 /// <summary>
188 /// Gets or sets the path to the NaCl IRT to use during debugging. (NaCl pla tform only).
189 /// </summary>
190 public string IrtPath
191 {
192 get
193 {
194 AssertNaCl();
195 return GetProperty("ConfigurationGeneral", "NaClIrtPath");
196 }
197
198 protected set
199 {
200 }
201 }
202
203 /// <summary>
204 /// Gets or sets the path to the NaCl manifest file (NaCl platform only).
205 /// </summary>
206 public string ManifestPath
207 {
208 get
209 {
210 AssertNaCl();
211 return GetProperty("ConfigurationGeneral", "NaClManifestPath");
212 }
213
214 protected set
215 {
216 }
217 }
218
219 /// <summary>
220 /// Sets the target project, platform, and configuration to get settings fro m.
221 /// </summary>
222 /// <param name="proj">Project to read settings from.</param>
223 /// <param name="targetPlatformName">Platform type to read settings from.</p aram>
224 /// <param name="targetConfigName">Configuration to read from (Debug or Rele ase).</param>
225 public void SetTarget(Project proj, string targetPlatformName, string target ConfigName)
226 {
227 // Set the project platform. If it is set to Other then no settings are v alid to be read.
228 if (string.Compare(targetPlatformName, Strings.PepperPlatformName, true) = = 0)
229 {
230 ProjectPlatform = ProjectPlatformType.Pepper;
231 }
232 else if (string.Compare(targetPlatformName, Strings.NaClPlatformName, true ) == 0)
233 {
234 ProjectPlatform = ProjectPlatformType.NaCl;
235 }
236 else
237 {
238 ProjectPlatform = ProjectPlatformType.Other;
239 return;
240 }
241
242 // We don't support non-visual C/C++ projects.
243 if (!Utility.IsVisualCProject(proj))
244 {
245 ProjectPlatform = ProjectPlatformType.Other;
246 return;
247 }
248
249 // Set the member variables for configuration and project to the target.
250 project_ = (VCProject)proj.Object;
251 foreach (VCConfiguration config in project_.Configurations)
252 {
253 if (config.ConfigurationName == targetConfigName &&
254 config.Platform.Name == targetPlatformName)
255 {
256 configuration_ = config;
257 break;
258 }
259 }
260 }
261
262 /// <summary>
263 /// Overload of SetTarget if the VCConfiguration is already known.
264 /// </summary>
265 /// <param name="config">Configuration to read settings from.</param>
266 public void SetTarget(VCConfiguration config)
267 {
268 if (config == null)
269 {
270 throw new ArgumentNullException("Config");
271 }
272
273 configuration_ = config;
274 project_ = config.project;
275
276 if (string.Compare(config.Platform.Name, Strings.PepperPlatformName, true) == 0)
277 {
278 ProjectPlatform = ProjectPlatformType.Pepper;
279 }
280 else if (string.Compare(config.Platform.Name, Strings.NaClPlatformName, tr ue) == 0)
281 {
282 ProjectPlatform = ProjectPlatformType.NaCl;
283 }
284 else
285 {
286 ProjectPlatform = ProjectPlatformType.Other;
287 }
288 }
289
290 /// <summary>
291 /// Sets the target project, platform, and configuration to the active start -up project and
292 /// selected platform and configuration so that settings are read from them.
293 /// </summary>
294 /// <param name="dte">The main Visual Studio object.</param>
295 public void SetTargetToActive(DTE2 dte)
296 {
297 // We require that there is only a single start-up project.
298 // If multiple start-up projects are specified then we use the first and d isplay a warning.
299 Array startupProjects = dte.Solution.SolutionBuild.StartupProjects as Arra y;
300 if (startupProjects == null || startupProjects.Length == 0)
301 {
302 throw new ArgumentOutOfRangeException("startupProjects.Length");
303 }
304 else if (startupProjects.Length > 1)
305 {
306 // Display a warning if multiple start-up projects and one is nacl/peppe r.
307 foreach (Project proj in startupProjects)
308 {
309 VCConfiguration config = Utility.GetActiveVCConfiguration(proj);
310 StringComparison ignoreCase = StringComparison.OrdinalIgnoreCase;
311 if (Strings.PepperPlatformName.Equals(config.Platform.Name, ignoreCase ) ||
312 Strings.NaClPlatformName.Equals(config.Platform.Name, ignoreCase))
313 {
314 System.Windows.Forms.MessageBox.Show(Strings.MultiStartProjectWarnin g);
315 break;
316 }
317 }
318 }
319
320 // Get the first start-up project object.
321 List<Project> projList = dte.Solution.Projects.OfType<Project>().ToList();
322 string startProjectName = startupProjects.GetValue(0) as string;
323 Project startProject = projList.Find(proj => proj.UniqueName == startProje ctName);
324
325 VCConfiguration activeConfig = Utility.GetActiveVCConfiguration(startProje ct);
326
327 // GetActiveVCConfiguration will return null if not a VC project.
328 if (activeConfig == null)
329 {
330 ProjectPlatform = ProjectPlatformType.Other;
331 return;
332 }
333
334 SetTarget(activeConfig);
335 }
336
337 /// <summary>
338 /// Reads any generic property from the current target properties.
339 /// </summary>
340 /// <param name="page">Name of the page where the property is located.</para m>
341 /// <param name="name">Name of the property.</param>
342 /// <returns>The property requested.</returns>
343 public virtual string GetProperty(string page, string name)
344 {
345 IVCRulePropertyStorage pageStorage = configuration_.Rules.Item(page);
346 return pageStorage.GetEvaluatedPropertyValue(name);
347 }
348
349 /// <summary>
350 /// Sets any generic property to the current target properties.
351 /// </summary>
352 /// <param name="page">Page where property is located.</param>
353 /// <param name="name">Name of the property.</param>
354 /// <param name="value">Unevaluated string value to set.</param>
355 public virtual void SetProperty(string page, string name, string value)
356 {
357 IVCRulePropertyStorage pageStorage = configuration_.Rules.Item(page);
358 pageStorage.SetPropertyValue(name, value);
359 }
360
361 /// <summary>
362 /// Ensures that the current target has the NaCl platform and throws if not.
363 /// </summary>
364 private void AssertNaCl()
365 {
366 if (ProjectPlatform != ProjectPlatformType.NaCl)
367 {
368 throw new Exception(string.Format(
369 "Cannot read NaCl only property on {0} platform", configuration_.Platf orm.Name));
370 }
371 }
372
373 /// <summary>
374 /// Ensures that the current target has the Pepper platform and throws if no t.
375 /// </summary>
376 private void AssertPepper()
377 {
378 if (ProjectPlatform != ProjectPlatformType.Pepper)
379 {
380 throw new Exception(string.Format(
381 "Cannot read Pepper only property on {0} platform", configuration_.Pla tform.Name));
382 }
383 }
384
385 /// <summary>
386 /// Ensures the current target is either the NaCl or Pepper platform. Throws if not.
387 /// </summary>
388 private void AssertValidPlatform()
389 {
390 if (ProjectPlatform == ProjectPlatformType.Other)
391 {
392 throw new Exception(string.Format(
393 "Unsupported platform type: {0} platform", configuration_.Platform.Nam e));
394 }
395 }
396 }
397 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698