Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConfigurationMerger.cs « MonoDevelop.Components.MainToolbar « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 965ed07fe2c22a455c8ecbc5940774acb5d1c33d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// ConfigurationMerger.cs
//
// Author:
//       Lluis Sanchez Gual <lluis@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using MonoDevelop.Projects;
using MonoDevelop.Core.Execution;
using System.Linq;

namespace MonoDevelop.Components.MainToolbar
{
	/// <summary>
	/// This class is used to generate a list of configurations to show in the configuration
	/// selector of the MonoDevelop toolbar. The class tries to reduce the number of configurations
	/// by merging those which have the same prefix and build the same project configurations
	/// for the current startup project. It also can be used to get a list of execution targets.
	/// </summary>
	class ConfigurationMerger
	{
		List<TargetPartition> currentTargetPartitions = new List<TargetPartition> ();
		List<string> currentSolutionConfigurations = new List<string> ();
		HashSet<string> reducedConfigurations = new HashSet<string> ();
		DummyExecutionTarget dummyExecutionTarget = new DummyExecutionTarget ();

		/// <summary>
		/// Resulting list of configurations. Some of them may be merged.
		/// </summary>
		public List<string> SolutionConfigurations {
			get { return currentSolutionConfigurations; }
		}

		/// <summary>
		/// Load configuration information for a solution
		/// </summary>
		public void Load (Solution sol)
		{
			currentSolutionConfigurations.Clear ();
			currentTargetPartitions.Clear ();
			reducedConfigurations.Clear ();

			if (sol == null)
				return;

			var project = sol.StartupItem;

			// Create a set of configuration partitions. Each partition will contain configurations
			// which are implicitly selected when selecting an execution target. For example, in
			// an iOS project we would have two partitions: 
			//   1) Debug|IPhoneSimulator, Release|IPhoneSimulator
			//      targets: iPhone, iPad
			//   2) Debug|IPhone, Release|IPhone
			//      targets: device

			List<TargetPartition> partitions = new List<TargetPartition> ();
			if (project != null) {
				foreach (var conf in project.Configurations) {
					var targets = project.GetExecutionTargets (conf.Selector);
					if (!targets.Any ()) {
						targets = new ExecutionTarget[] { dummyExecutionTarget };
					}
					var parts = partitions.Where (p => targets.Any (t => p.Targets.Contains (t))).ToArray();
					if (parts.Length == 0) {
						// Create a new partition for this configuration
						var p = new TargetPartition ();
						p.Configurations.Add (conf.Id);
						p.Targets.UnionWith (targets);
						partitions.Add (p);
					}
					else if (parts.Length == 1) {
						// Register the configuration into an existing partition
						parts[0].Configurations.Add (conf.Id);
						parts[0].Targets.UnionWith (targets);
					}
					else {
						// The partitions have to be merged into a single one
						for (int n=1; n<parts.Length; n++) {
							parts[0].Configurations.UnionWith (parts[n].Configurations);
							parts[0].Targets.UnionWith (parts[n].Targets);
							partitions.Remove (parts[n]);
						}
					}
				}

				// The startup project configuration partitions are used to create solution configuration partitions

				foreach (var solConf in sol.Configurations) {
					var pconf = solConf.GetEntryForItem (project);
					if (pconf != null && pconf.Build) {
						var part = partitions.FirstOrDefault (p => p.Configurations.Contains (pconf.ItemConfiguration));
						if (part != null) {
							part.SolutionConfigurations.Add (solConf.Id);
							continue;
						}
					}
					// The solution configuration is not bound to the startup project
					// Add it to all partitions so that it can still take part of
					// the solution configuration simplification process
					foreach (var p in partitions)
						p.SolutionConfigurations.Add (solConf.Id);
				}
			}

			if (partitions.Count == 0) {
				// There is no startup project, just use all solution configurations in this case
				var p = new TargetPartition ();
				p.SolutionConfigurations.AddRange (sol.GetConfigurations ());
				partitions.Add (p);
			}

			// There can be several configurations with the same prefix and different platform but which build the same projects.
			// If all configurations with the same prefix are identical, all of them can be reduced into a single configuration
			// with no platform name. This loop detects such configurations

			var notReducibleConfigurations = new HashSet<string> ();

			foreach (var p in partitions) {
				var groupedConfigs = p.SolutionConfigurations.GroupBy (sc => {
					string name, plat;
					ItemConfiguration.ParseConfigurationId (sc, out name, out plat);
					return name;
				}).ToArray ();
				foreach (var confGroup in groupedConfigs) {
					var configs = confGroup.ToArray ();
					var baseConf = sol.Configurations[configs[0]];
					if (configs.Skip (1).All (c => ConfigurationEquals (sol, baseConf, sol.Configurations[c])))
						p.ReducedConfigurations.Add (confGroup.Key);
					else
						notReducibleConfigurations.Add (confGroup.Key);
				}
			}

			// To really be able to use reduced configuration names, all partitions must have that reduced configuration
			// Find the configurations that have been reduced in all partitions

			reducedConfigurations = new HashSet<string> (partitions.SelectMany (p => p.ReducedConfigurations));
			reducedConfigurations.ExceptWith (notReducibleConfigurations);

			// Final merge of configurations

			var result = new HashSet<string> ();
			foreach (var p in partitions)
				result.UnionWith (p.SolutionConfigurations);

			// Replace reduced configurations

			foreach (var reducedConf in reducedConfigurations) {
				result.RemoveWhere (c => {
					string name, plat;
					ItemConfiguration.ParseConfigurationId (c, out name, out plat);
					return name == reducedConf;
				});
				result.Add (reducedConf);
			}
			currentTargetPartitions = partitions;
			currentSolutionConfigurations.AddRange (result);
			currentSolutionConfigurations.Sort ();
		}

		static ExecutionTarget FirstRealExecutionTarget (IEnumerable<ExecutionTarget> targets)
		{
			foreach (var target in targets) {
				if (target is ExecutionTargetGroup) {
					var first = FirstRealExecutionTarget ((ExecutionTargetGroup) target);
					if (first != null)
						return first;
				} else if (!(target is DummyExecutionTarget)) {
					if (target.Enabled)
						return target;
				}
			}

			return null;
		}

		static bool ExecutionTargetsContains (IEnumerable<ExecutionTarget> targets, ExecutionTarget desired)
		{
			foreach (var target in targets) {
				if (target == desired)
					return true;

				if ((target is ExecutionTargetGroup) && ExecutionTargetsContains ((ExecutionTargetGroup) target, desired))
					return true;
			}

			return false;
		}

		/// <summary>
		/// Gets the full configuration name given a possibly merged configuration name and execution target
		/// </summary>
		/// <param name='currentConfig'>
		/// A configuration name (can be a merged configuration name)
		/// </param>
		/// <param name='currentTarget'>
		/// Selected execution target
		/// </param>
		/// <param name='resolvedConfig'>
		/// Resolved configuration
		/// </param>
		/// <param name='resolvedTarget'>
		/// If the provided target is not valid for the provided configuration, this returns a valid target
		/// </param>
		public void ResolveConfiguration (string currentConfig, ExecutionTarget currentTarget, out string resolvedConfig, out ExecutionTarget resolvedTarget)
		{
			resolvedConfig = null;
			resolvedTarget = currentTarget;

			if (!reducedConfigurations.Contains (currentConfig)) {
				// The selected configuration is not reduced, just use it as full config name
				resolvedConfig = currentConfig;
				var part = currentTargetPartitions.FirstOrDefault (p => p.SolutionConfigurations.Contains (currentConfig));
				if (part != null) {
					if (!ExecutionTargetsContains (part.Targets, resolvedTarget))
						resolvedTarget = FirstRealExecutionTarget (part.Targets);
				} else {
					resolvedTarget = null;
				}
			} else {
				// Reduced configuration. Find the partition and guess the implicit project configuration
				var part = currentTargetPartitions.FirstOrDefault (p => ExecutionTargetsContains (p.Targets, currentTarget ?? dummyExecutionTarget));
				if (part != null) {
					resolvedConfig = part.SolutionConfigurations.FirstOrDefault (c => {
						string name, plat;
						ItemConfiguration.ParseConfigurationId (c, out name, out plat);
						return name == currentConfig;
					});
				}
				if (resolvedConfig == null) {
					part = currentTargetPartitions.FirstOrDefault (p => p.ReducedConfigurations.Contains (currentConfig));
					if (part == null)
						part = currentTargetPartitions.FirstOrDefault (p => p.SolutionConfigurations.Contains (currentConfig));
					if (part != null) {
						resolvedTarget = FirstRealExecutionTarget (part.Targets);
						resolvedConfig = part.SolutionConfigurations.FirstOrDefault (c => {
							string name, plat;
							ItemConfiguration.ParseConfigurationId (c, out name, out plat);
							return name == currentConfig;
						});
						if (resolvedConfig == null)
							resolvedConfig = currentConfig;
					} else {
						resolvedTarget = null;
						resolvedConfig = currentConfig;
					}
				}
			}
			if (resolvedTarget == dummyExecutionTarget)
				resolvedTarget = null;
		}

		/// <summary>
		/// Gets the targets which are valid for a configuration
		/// </summary>
		public IEnumerable<ExecutionTarget> GetTargetsForConfiguration (string fullConfigurationId, bool ignorePlatform)
		{
			string conf,plat;
			ItemConfiguration.ParseConfigurationId (fullConfigurationId, out conf, out plat);

			if (ignorePlatform && reducedConfigurations.Contains (conf)) {
				// Reduced configuration. Show all targets since they will be used to guess the implicit platform
				return currentTargetPartitions.Where (p => p.ReducedConfigurations.Contains (conf)).SelectMany (p => p.Targets);
			} else {
				// Show targets for the configuration
				var part = currentTargetPartitions.FirstOrDefault (p => p.SolutionConfigurations.Contains (fullConfigurationId));
				if (part != null)
					return part.Targets;
				else
					return new ExecutionTarget[0];
			}
		}

		/// <summary>
		/// Given a full configuration id, returns the merged configuration
		/// </summary>
		public string GetUnresolvedConfiguration (string fullConfigurationId)
		{
			string conf,plat;
			ItemConfiguration.ParseConfigurationId (fullConfigurationId, out conf, out plat);

			if (reducedConfigurations.Contains (conf))
				return conf;
			else
				return fullConfigurationId;
		}

		bool ConfigurationEquals (Solution sol, SolutionConfiguration s1, SolutionConfiguration s2)
		{
			foreach (var p in sol.GetAllSolutionItems<SolutionEntityItem> ()) {
				var c1 = s1.GetEntryForItem (p);
				var c2 = s2.GetEntryForItem (p);
				if (c1 == null && c2 == null)
					continue;
				if (c1 == null || c2 == null)
					return false;
				if (c1.Build != c2.Build || c1.ItemConfiguration != c2.ItemConfiguration)
					return false;
			}
			return true;
		}

		class DummyExecutionTarget: ExecutionTarget
		{
			public override string Name {
				get { return "Default"; }
			}

			public override string Id {
				get {
					return "MonoDevelop.Default";
				}
			}
		}

		class TargetPartition
		{
			/// <summary>
			/// Targets included in this partition
			/// </summary>
			public HashSet<ExecutionTarget> Targets = new HashSet<ExecutionTarget> ();

			/// <summary>
			/// Project configurations included in this partition
			/// </summary>
			public HashSet<string> Configurations = new HashSet<string> ();

			/// <summary>
			/// Solution configurations included in this partition (configurations which are bound to the project configurations)
			/// </summary>
			public List<string> SolutionConfigurations = new List<string> ();

			/// <summary>
			/// Configurations (without platform) that have been reduced
			/// </summary>
			public HashSet<string> ReducedConfigurations = new HashSet<string> ();
		}
	}
}