OLD | NEW |
| (Empty) |
1 using System; | |
2 using System.Diagnostics; | |
3 using System.Reflection; | |
4 using Microsoft.VisualStudio; | |
5 using Microsoft.VisualStudio.Project; | |
6 | |
7 namespace Google.NaClVsx.ProjectSupport { | |
8 public class ProjectPropertyAttribute : Attribute { | |
9 public ProjectPropertyAttribute(string propertyName, bool perConfig) { | |
10 PerConfig = perConfig; | |
11 PropertyName = propertyName; | |
12 } | |
13 | |
14 public string PropertyName { get; set; } | |
15 public bool PerConfig { get; set; } | |
16 } | |
17 | |
18 public class AutoSettingsPage : SettingsPage { | |
19 #region Overrides of SettingsPage | |
20 | |
21 protected override void BindProperties() { | |
22 Type derived = GetType(); | |
23 foreach (PropertyInfo prop in derived.GetProperties()) { | |
24 object[] attributes = prop.GetCustomAttributes( | |
25 typeof (ProjectPropertyAttribute), true); | |
26 | |
27 foreach (ProjectPropertyAttribute attribute in attributes) { | |
28 string value = GetPropertyFromMsBuild(attribute); | |
29 if (prop.PropertyType.IsEnum) { | |
30 prop.SetValue( | |
31 this, Enum.Parse(prop.PropertyType, value), null); | |
32 } else { | |
33 prop.SetValue( | |
34 this, | |
35 Convert.ChangeType(value, prop.PropertyType), | |
36 null); | |
37 } | |
38 } | |
39 } | |
40 IsDirty = false; | |
41 } | |
42 | |
43 protected override int ApplyChanges() { | |
44 Type derived = GetType(); | |
45 foreach (PropertyInfo prop in derived.GetProperties()) { | |
46 object[] attributes = prop.GetCustomAttributes( | |
47 typeof (ProjectPropertyAttribute), true); | |
48 | |
49 foreach (ProjectPropertyAttribute attribute in attributes) { | |
50 object value = prop.GetValue(this, null); | |
51 string persistValue = "ERROR"; | |
52 if (prop.PropertyType.IsEnum) { | |
53 persistValue = Enum.GetName(prop.PropertyType, value); | |
54 } else { | |
55 persistValue = Convert.ToString(value); | |
56 } | |
57 | |
58 SetMsBuildProperty(attribute, persistValue); | |
59 } | |
60 } | |
61 IsDirty = false; | |
62 return VSConstants.S_OK; | |
63 } | |
64 | |
65 #endregion | |
66 | |
67 // relative to active configuration. | |
68 public string GetUnevaluatedConfigProperty(string propertyName) { | |
69 if (ProjectMgr == null) { | |
70 return String.Empty; | |
71 } | |
72 | |
73 string unifiedResult = null; | |
74 bool cacheNeedReset = true; | |
75 | |
76 ProjectConfig[] configurations = GetProjectConfigurations(); | |
77 | |
78 for (int i = 0; i < configurations.Length; i++) { | |
79 var nacl_config = configurations[i] as NaClProjectConfig; | |
80 Debug.Assert(nacl_config != null); | |
81 | |
82 string property = | |
83 nacl_config.GetRawConfigurationProperty( | |
84 propertyName, cacheNeedReset); | |
85 cacheNeedReset = false; | |
86 | |
87 if (property != null) { | |
88 string text = property.Trim(); | |
89 | |
90 if (i == 0) { | |
91 unifiedResult = text; | |
92 } else if (unifiedResult != text) { | |
93 return ""; // tristate value is blank then | |
94 } | |
95 } | |
96 } | |
97 | |
98 return unifiedResult; | |
99 } | |
100 | |
101 #region Private Implementation | |
102 | |
103 private string GetPropertyFromMsBuild(ProjectPropertyAttribute attribute) { | |
104 if (attribute.PerConfig) { | |
105 return GetUnevaluatedConfigProperty(attribute.PropertyName); | |
106 } else { | |
107 return ProjectMgr.GetProjectProperty(attribute.PropertyName); | |
108 } | |
109 } | |
110 | |
111 private void SetMsBuildProperty(ProjectPropertyAttribute attribute, | |
112 string persistValue) { | |
113 if (attribute.PerConfig) { | |
114 SetConfigProperty(attribute.PropertyName, persistValue); | |
115 } else { | |
116 ProjectMgr.SetProjectProperty( | |
117 attribute.PropertyName, persistValue); | |
118 } | |
119 } | |
120 | |
121 #endregion | |
122 } | |
123 } | |
OLD | NEW |