Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
|
noelallen1
2012/08/08 00:22:15
Strange we keep going... Is this to ensure config
tysand
2012/08/08 04:15:59
Done.
| |
| 239 } | |
| 240 | |
| 241 // We don't support non-visual C/C++ projects. | |
| 242 if (!Utility.IsVisualCProject(proj)) | |
| 243 { | |
| 244 ProjectPlatform = ProjectPlatformType.Other; | |
| 245 return; | |
| 246 } | |
| 247 | |
| 248 // Set the member variables for configuration and project to the target. | |
| 249 project_ = (VCProject)proj.Object; | |
| 250 foreach (VCConfiguration config in project_.Configurations) | |
| 251 { | |
| 252 if (config.ConfigurationName == targetConfigName && | |
| 253 config.Platform.Name == targetPlatformName) | |
| 254 { | |
| 255 configuration_ = config; | |
| 256 break; | |
| 257 } | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 /// <summary> | |
| 262 /// Overload of SetTarget if the VCConfiguration is already known. | |
| 263 /// </summary> | |
| 264 /// <param name="config">Configuration to read settings from.</param> | |
| 265 public void SetTarget(VCConfiguration config) | |
| 266 { | |
| 267 if (config == null) | |
| 268 { | |
| 269 throw new ArgumentNullException("Config"); | |
| 270 } | |
| 271 | |
| 272 configuration_ = config; | |
| 273 project_ = config.project; | |
| 274 | |
| 275 if (string.Compare(config.Platform.Name, Strings.PepperPlatformName, true) == 0) | |
| 276 { | |
| 277 ProjectPlatform = ProjectPlatformType.Pepper; | |
| 278 } | |
| 279 else if (string.Compare(config.Platform.Name, Strings.NaClPlatformName, tr ue) == 0) | |
| 280 { | |
| 281 ProjectPlatform = ProjectPlatformType.NaCl; | |
| 282 } | |
| 283 else | |
| 284 { | |
| 285 ProjectPlatform = ProjectPlatformType.Other; | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 /// <summary> | |
| 290 /// Sets the target project, platform, and configuration to the active start -up project and | |
| 291 /// selected platform and configuration so that settings are read from them. | |
| 292 /// </summary> | |
| 293 /// <param name="dte">The main Visual Studio object.</param> | |
| 294 public void SetTargetToActive(DTE2 dte) | |
| 295 { | |
| 296 // We require that there is only a single start-up project. | |
| 297 // If multiple start-up projects are specified then we use the first and d isplay a warning. | |
| 298 Array startupProjects = dte.Solution.SolutionBuild.StartupProjects as Arra y; | |
| 299 if (startupProjects == null || startupProjects.Length == 0) | |
| 300 { | |
| 301 throw new ArgumentOutOfRangeException("startupProjects.Length"); | |
| 302 } | |
| 303 else if (startupProjects.Length > 1) | |
| 304 { | |
| 305 // Display a warning if multiple start-up projects and one is nacl/peppe r. | |
| 306 foreach (Project proj in startupProjects) | |
| 307 { | |
| 308 VCConfiguration config = Utility.GetActiveVCConfiguration(proj); | |
| 309 StringComparison ignoreCase = StringComparison.OrdinalIgnoreCase; | |
| 310 if (Strings.PepperPlatformName.Equals(config.Platform.Name, ignoreCase ) || | |
| 311 Strings.NaClPlatformName.Equals(config.Platform.Name, ignoreCase)) | |
| 312 { | |
| 313 System.Windows.Forms.MessageBox.Show(Strings.MultiStartProjectWarnin g); | |
| 314 break; | |
| 315 } | |
| 316 } | |
| 317 } | |
| 318 | |
| 319 // Get the first start-up project object. | |
| 320 List<Project> projList = dte.Solution.Projects.OfType<Project>().ToList(); | |
| 321 string startProjectName = startupProjects.GetValue(0) as string; | |
| 322 Project startProject = projList.Find(proj => proj.UniqueName == startProje ctName); | |
| 323 | |
| 324 VCConfiguration activeConfig = Utility.GetActiveVCConfiguration(startProje ct); | |
| 325 | |
| 326 // GetActiveVCConfiguration will return null if not a VC project. | |
| 327 if (activeConfig == null) | |
| 328 { | |
| 329 ProjectPlatform = ProjectPlatformType.Other; | |
| 330 return; | |
| 331 } | |
| 332 | |
| 333 SetTarget(activeConfig); | |
| 334 } | |
| 335 | |
| 336 /// <summary> | |
| 337 /// Reads any generic property from the current target properties. | |
| 338 /// </summary> | |
| 339 /// <param name="page">Name of the page where the property is located.</para m> | |
| 340 /// <param name="name">Name of the property.</param> | |
| 341 /// <returns>The property requested.</returns> | |
| 342 public virtual string GetProperty(string page, string name) | |
| 343 { | |
| 344 IVCRulePropertyStorage pageStorage = configuration_.Rules.Item(page); | |
| 345 return pageStorage.GetEvaluatedPropertyValue(name); | |
| 346 } | |
| 347 | |
| 348 /// <summary> | |
| 349 /// Sets any generic property to the current target properties. | |
| 350 /// </summary> | |
| 351 /// <param name="page">Page where property is located.</param> | |
| 352 /// <param name="name">Name of the property.</param> | |
| 353 /// <param name="value">Unevaluated string value to set.</param> | |
| 354 public virtual void SetProperty(string page, string name, string value) | |
| 355 { | |
| 356 IVCRulePropertyStorage pageStorage = configuration_.Rules.Item(page); | |
| 357 pageStorage.SetPropertyValue(name, value); | |
| 358 } | |
| 359 | |
| 360 /// <summary> | |
| 361 /// Ensures that the current target has the NaCl platform and throws if not. | |
| 362 /// </summary> | |
| 363 private void AssertNaCl() | |
| 364 { | |
| 365 if (ProjectPlatform != ProjectPlatformType.NaCl) | |
| 366 { | |
| 367 throw new Exception(string.Format( | |
| 368 "Cannot read NaCl only property on {0} platform", configuration_.Platf orm.Name)); | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 /// <summary> | |
| 373 /// Ensures that the current target has the Pepper platform and throws if no t. | |
| 374 /// </summary> | |
| 375 private void AssertPepper() | |
| 376 { | |
| 377 if (ProjectPlatform != ProjectPlatformType.Pepper) | |
| 378 { | |
| 379 throw new Exception(string.Format( | |
| 380 "Cannot read Pepper only property on {0} platform", configuration_.Pla tform.Name)); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 /// <summary> | |
| 385 /// Ensures the current target is either the NaCl or Pepper platform. Throws if not. | |
| 386 /// </summary> | |
| 387 private void AssertValidPlatform() | |
| 388 { | |
| 389 if (ProjectPlatform == ProjectPlatformType.Other) | |
| 390 { | |
| 391 throw new Exception(string.Format( | |
| 392 "Unsupported platform type: {0} platform", configuration_.Platform.Nam e)); | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 } | |
| OLD | NEW |