diff options
author | Rob Mensching <rob@firegiant.com> | 2019-10-07 07:33:18 -0700 |
---|---|---|
committer | Rob Mensching <rob@firegiant.com> | 2019-10-07 11:27:21 -0700 |
commit | 7e87a6b75d0570c3ebfbf0e537fcf04e1cf84224 (patch) | |
tree | 2f88738485acf07e5b1318b21b7ce2b5c06a725b /src/WixToolset.Data | |
parent | cece10e037c6daacc8d2def1a9057882aec47fe4 (diff) | |
download | wix-7e87a6b75d0570c3ebfbf0e537fcf04e1cf84224.tar.gz wix-7e87a6b75d0570c3ebfbf0e537fcf04e1cf84224.tar.bz2 wix-7e87a6b75d0570c3ebfbf0e537fcf04e1cf84224.zip |
Support tagging tuples and definitions
Diffstat (limited to 'src/WixToolset.Data')
-rw-r--r-- | src/WixToolset.Data/IntermediateTuple.cs | 159 | ||||
-rw-r--r-- | src/WixToolset.Data/IntermediateTupleDefinition.cs | 163 |
2 files changed, 321 insertions, 1 deletions
diff --git a/src/WixToolset.Data/IntermediateTuple.cs b/src/WixToolset.Data/IntermediateTuple.cs index bc46e3b4..8a5858ee 100644 --- a/src/WixToolset.Data/IntermediateTuple.cs +++ b/src/WixToolset.Data/IntermediateTuple.cs | |||
@@ -9,6 +9,8 @@ namespace WixToolset.Data | |||
9 | [DebuggerDisplay("{DebuggerDisplay,nq}")] | 9 | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
10 | public class IntermediateTuple | 10 | public class IntermediateTuple |
11 | { | 11 | { |
12 | private object tags; | ||
13 | |||
12 | public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null) | 14 | public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null) |
13 | { | 15 | { |
14 | } | 16 | } |
@@ -33,12 +35,132 @@ namespace WixToolset.Data | |||
33 | 35 | ||
34 | private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}"; | 36 | private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}"; |
35 | 37 | ||
38 | public bool AddTag(string add) | ||
39 | { | ||
40 | if (this.tags == null) | ||
41 | { | ||
42 | this.tags = add; | ||
43 | } | ||
44 | else if (this.tags is string tag) | ||
45 | { | ||
46 | if (tag == add) | ||
47 | { | ||
48 | return false; | ||
49 | } | ||
50 | |||
51 | this.tags = new[] { tag, add }; | ||
52 | } | ||
53 | else | ||
54 | { | ||
55 | var tagsArray = (string[])this.tags; | ||
56 | var array = new string[tagsArray.Length + 1]; | ||
57 | |||
58 | for (var i = 0; i < tagsArray.Length; ++i) | ||
59 | { | ||
60 | if (tagsArray[i] == add) | ||
61 | { | ||
62 | return false; | ||
63 | } | ||
64 | |||
65 | array[i] = tagsArray[i]; | ||
66 | } | ||
67 | |||
68 | array[tagsArray.Length] = add; | ||
69 | |||
70 | this.tags = array; | ||
71 | } | ||
72 | |||
73 | return true; | ||
74 | } | ||
75 | |||
76 | public bool HasTag(string has) | ||
77 | { | ||
78 | if (this.tags == null) | ||
79 | { | ||
80 | return false; | ||
81 | } | ||
82 | else if (this.tags is string tag) | ||
83 | { | ||
84 | return tag == has; | ||
85 | } | ||
86 | else | ||
87 | { | ||
88 | foreach (var element in (string[])this.tags) | ||
89 | { | ||
90 | if (element == has) | ||
91 | { | ||
92 | return true; | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
97 | return false; | ||
98 | } | ||
99 | |||
100 | public bool RemoveTag(string remove) | ||
101 | { | ||
102 | if (this.tags is string tag) | ||
103 | { | ||
104 | if (tag == remove) | ||
105 | { | ||
106 | this.tags = null; | ||
107 | return true; | ||
108 | } | ||
109 | } | ||
110 | else if (this.tags is string[] tagsArray) | ||
111 | { | ||
112 | if (tagsArray.Length == 2) | ||
113 | { | ||
114 | if (tagsArray[0] == remove) | ||
115 | { | ||
116 | this.tags = tagsArray[1]; | ||
117 | return true; | ||
118 | } | ||
119 | else if (tagsArray[1] == remove) | ||
120 | { | ||
121 | this.tags = tagsArray[0]; | ||
122 | return true; | ||
123 | } | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | var array = new string[tagsArray.Length - 1]; | ||
128 | var arrayIndex = 0; | ||
129 | var found = false; | ||
130 | |||
131 | for (var i = 0; i < tagsArray.Length; ++i) | ||
132 | { | ||
133 | if (tagsArray[i] == remove) | ||
134 | { | ||
135 | found = true; | ||
136 | continue; | ||
137 | } | ||
138 | else if (arrayIndex == array.Length) | ||
139 | { | ||
140 | break; | ||
141 | } | ||
142 | |||
143 | array[arrayIndex++] = tagsArray[i]; | ||
144 | } | ||
145 | |||
146 | if (found) | ||
147 | { | ||
148 | this.tags = array; | ||
149 | return true; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | |||
154 | return false; | ||
155 | } | ||
156 | |||
36 | internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject) | 157 | internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject) |
37 | { | 158 | { |
38 | var definitionName = jsonObject.GetValueOrDefault<string>("type"); | 159 | var definitionName = jsonObject.GetValueOrDefault<string>("type"); |
39 | var idJson = jsonObject.GetValueOrDefault<JsonObject>("id"); | 160 | var idJson = jsonObject.GetValueOrDefault<JsonObject>("id"); |
40 | var sourceLineNumbersJson = jsonObject.GetValueOrDefault<JsonObject>("ln"); | 161 | var sourceLineNumbersJson = jsonObject.GetValueOrDefault<JsonObject>("ln"); |
41 | var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); | 162 | var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); |
163 | var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags"); | ||
42 | 164 | ||
43 | var id = (idJson == null) ? null : Identifier.Deserialize(idJson); | 165 | var id = (idJson == null) ? null : Identifier.Deserialize(idJson); |
44 | var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson); | 166 | var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson); |
@@ -54,6 +176,25 @@ namespace WixToolset.Data | |||
54 | } | 176 | } |
55 | } | 177 | } |
56 | 178 | ||
179 | if (tagsJson == null || tagsJson.Count == 0) | ||
180 | { | ||
181 | } | ||
182 | else if (tagsJson.Count == 1) | ||
183 | { | ||
184 | tuple.tags = (string)tagsJson[0]; | ||
185 | } | ||
186 | else | ||
187 | { | ||
188 | var tags = new string[tagsJson.Count]; | ||
189 | |||
190 | for (var i = 0; i < tagsJson.Count; ++i) | ||
191 | { | ||
192 | tags[i] = (string)tagsJson[i]; | ||
193 | } | ||
194 | |||
195 | tuple.tags = tags; | ||
196 | } | ||
197 | |||
57 | return tuple; | 198 | return tuple; |
58 | } | 199 | } |
59 | 200 | ||
@@ -86,6 +227,24 @@ namespace WixToolset.Data | |||
86 | 227 | ||
87 | jsonObject.Add("fields", fieldsJson); | 228 | jsonObject.Add("fields", fieldsJson); |
88 | 229 | ||
230 | if (this.tags is string || this.tags is string[]) | ||
231 | { | ||
232 | JsonArray tagsJson; | ||
233 | |||
234 | if (this.tags is string tag) | ||
235 | { | ||
236 | tagsJson = new JsonArray(1) { tag }; | ||
237 | } | ||
238 | else | ||
239 | { | ||
240 | var array = (string[])this.tags; | ||
241 | tagsJson = new JsonArray(array.Length); | ||
242 | tagsJson.AddRange(array); | ||
243 | } | ||
244 | |||
245 | jsonObject.Add("tags", tagsJson); | ||
246 | } | ||
247 | |||
89 | return jsonObject; | 248 | return jsonObject; |
90 | } | 249 | } |
91 | } | 250 | } |
diff --git a/src/WixToolset.Data/IntermediateTupleDefinition.cs b/src/WixToolset.Data/IntermediateTupleDefinition.cs index bc49bc7b..eb05c28b 100644 --- a/src/WixToolset.Data/IntermediateTupleDefinition.cs +++ b/src/WixToolset.Data/IntermediateTupleDefinition.cs | |||
@@ -7,6 +7,8 @@ namespace WixToolset.Data | |||
7 | 7 | ||
8 | public class IntermediateTupleDefinition | 8 | public class IntermediateTupleDefinition |
9 | { | 9 | { |
10 | private object tags; | ||
11 | |||
10 | public IntermediateTupleDefinition(string name, IntermediateFieldDefinition[] fieldDefinitions, Type strongTupleType) | 12 | public IntermediateTupleDefinition(string name, IntermediateFieldDefinition[] fieldDefinitions, Type strongTupleType) |
11 | : this(TupleDefinitionType.MustBeFromAnExtension, name, 0, fieldDefinitions, strongTupleType) | 13 | : this(TupleDefinitionType.MustBeFromAnExtension, name, 0, fieldDefinitions, strongTupleType) |
12 | { | 14 | { |
@@ -53,11 +55,131 @@ namespace WixToolset.Data | |||
53 | return result; | 55 | return result; |
54 | } | 56 | } |
55 | 57 | ||
58 | public bool AddTag(string add) | ||
59 | { | ||
60 | if (this.tags == null) | ||
61 | { | ||
62 | this.tags = add; | ||
63 | } | ||
64 | else if (this.tags is string tag) | ||
65 | { | ||
66 | if (tag == add) | ||
67 | { | ||
68 | return false; | ||
69 | } | ||
70 | |||
71 | this.tags = new[] { tag, add }; | ||
72 | } | ||
73 | else | ||
74 | { | ||
75 | var tagsArray = (string[])this.tags; | ||
76 | var array = new string[tagsArray.Length + 1]; | ||
77 | |||
78 | for (var i = 0; i < tagsArray.Length; ++i) | ||
79 | { | ||
80 | if (tagsArray[i] == add) | ||
81 | { | ||
82 | return false; | ||
83 | } | ||
84 | |||
85 | array[i] = tagsArray[i]; | ||
86 | } | ||
87 | |||
88 | array[tagsArray.Length] = add; | ||
89 | |||
90 | this.tags = array; | ||
91 | } | ||
92 | |||
93 | return true; | ||
94 | } | ||
95 | |||
96 | public bool HasTag(string has) | ||
97 | { | ||
98 | if (this.tags == null) | ||
99 | { | ||
100 | return false; | ||
101 | } | ||
102 | else if (this.tags is string tag) | ||
103 | { | ||
104 | return tag == has; | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | foreach (var element in (string[])this.tags) | ||
109 | { | ||
110 | if (element == has) | ||
111 | { | ||
112 | return true; | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||
117 | return false; | ||
118 | } | ||
119 | |||
120 | public bool RemoveTag(string remove) | ||
121 | { | ||
122 | if (this.tags is string tag) | ||
123 | { | ||
124 | if (tag == remove) | ||
125 | { | ||
126 | this.tags = null; | ||
127 | return true; | ||
128 | } | ||
129 | } | ||
130 | else if (this.tags is string[] tagsArray) | ||
131 | { | ||
132 | if (tagsArray.Length == 2) | ||
133 | { | ||
134 | if (tagsArray[0] == remove) | ||
135 | { | ||
136 | this.tags = tagsArray[1]; | ||
137 | return true; | ||
138 | } | ||
139 | else if (tagsArray[1] == remove) | ||
140 | { | ||
141 | this.tags = tagsArray[0]; | ||
142 | return true; | ||
143 | } | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | var array = new string[tagsArray.Length - 1]; | ||
148 | var arrayIndex = 0; | ||
149 | var found = false; | ||
150 | |||
151 | for (var i = 0; i < tagsArray.Length; ++i) | ||
152 | { | ||
153 | if (tagsArray[i] == remove) | ||
154 | { | ||
155 | found = true; | ||
156 | continue; | ||
157 | } | ||
158 | else if (arrayIndex == array.Length) | ||
159 | { | ||
160 | break; | ||
161 | } | ||
162 | |||
163 | array[arrayIndex++] = tagsArray[i]; | ||
164 | } | ||
165 | |||
166 | if (found) | ||
167 | { | ||
168 | this.tags = array; | ||
169 | return true; | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | |||
174 | return false; | ||
175 | } | ||
176 | |||
56 | internal static IntermediateTupleDefinition Deserialize(JsonObject jsonObject) | 177 | internal static IntermediateTupleDefinition Deserialize(JsonObject jsonObject) |
57 | { | 178 | { |
58 | var name = jsonObject.GetValueOrDefault<string>("name"); | 179 | var name = jsonObject.GetValueOrDefault<string>("name"); |
59 | var revision = jsonObject.GetValueOrDefault("rev", 0); | 180 | var revision = jsonObject.GetValueOrDefault("rev", 0); |
60 | var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); | 181 | var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); |
182 | var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags"); | ||
61 | 183 | ||
62 | var fieldDefinitions = new IntermediateFieldDefinition[definitionsJson.Count]; | 184 | var fieldDefinitions = new IntermediateFieldDefinition[definitionsJson.Count]; |
63 | 185 | ||
@@ -69,7 +191,28 @@ namespace WixToolset.Data | |||
69 | fieldDefinitions[i] = new IntermediateFieldDefinition(fieldName, fieldType); | 191 | fieldDefinitions[i] = new IntermediateFieldDefinition(fieldName, fieldType); |
70 | } | 192 | } |
71 | 193 | ||
72 | return new IntermediateTupleDefinition(name, revision, fieldDefinitions, null); | 194 | var definition = new IntermediateTupleDefinition(name, revision, fieldDefinitions, null); |
195 | |||
196 | if (tagsJson == null || tagsJson.Count == 0) | ||
197 | { | ||
198 | } | ||
199 | else if (tagsJson.Count == 1) | ||
200 | { | ||
201 | definition.tags = (string)tagsJson[0]; | ||
202 | } | ||
203 | else | ||
204 | { | ||
205 | var tags = new string[tagsJson.Count]; | ||
206 | |||
207 | for (var i = 0; i < tagsJson.Count; ++i) | ||
208 | { | ||
209 | tags[i] = (string)tagsJson[i]; | ||
210 | } | ||
211 | |||
212 | definition.tags = tags; | ||
213 | } | ||
214 | |||
215 | return definition; | ||
73 | } | 216 | } |
74 | 217 | ||
75 | internal JsonObject Serialize() | 218 | internal JsonObject Serialize() |
@@ -103,6 +246,24 @@ namespace WixToolset.Data | |||
103 | 246 | ||
104 | jsonObject.Add("fields", fieldsJson); | 247 | jsonObject.Add("fields", fieldsJson); |
105 | 248 | ||
249 | if (this.tags is string || this.tags is string[]) | ||
250 | { | ||
251 | JsonArray tagsJson; | ||
252 | |||
253 | if (this.tags is string tag) | ||
254 | { | ||
255 | tagsJson = new JsonArray(1) { tag }; | ||
256 | } | ||
257 | else | ||
258 | { | ||
259 | var array = (string[])this.tags; | ||
260 | tagsJson = new JsonArray(array.Length); | ||
261 | tagsJson.AddRange(array); | ||
262 | } | ||
263 | |||
264 | jsonObject.Add("tags", tagsJson); | ||
265 | } | ||
266 | |||
106 | return jsonObject; | 267 | return jsonObject; |
107 | } | 268 | } |
108 | } | 269 | } |