summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.BuildTasks/PathSegment.cs
blob: f7a72b6449d5a3ee814017677c91ddbc3cc9d2ac (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
//-----------------------------------------------------------------------
// <copyright file="PathSegment.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.BuildTasks {
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.Diagnostics;
	using System.Diagnostics.Contracts;
	using System.IO;
	using System.Linq;
	using System.Text;

	internal class PathSegment {
		private const float ParentChildResizeThreshold = 0.30f;
		private readonly string originalName;
		private string currentName;
		private bool minimized;
		private static readonly string[] ReservedFileNames = "CON PRN AUX CLOCK$ NUL COM0 COM1 COM2 COM3 COM4 COM5 COM6 COM7 COM8 COM9 LPT0 LPT1 LPT2 LPT3 LPT4 LPT5 LPT6 LPT7 LPT8 LPT9".Split(' ');

		internal PathSegment() {
			this.currentName = string.Empty;
			this.originalName = string.Empty;
			this.minimized = true;
			this.Children = new Collection<PathSegment>();
		}

		private PathSegment(string originalName, PathSegment parent)
			: this() {
			Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalName));
			Contract.Requires<ArgumentNullException>(parent != null);
			this.currentName = this.originalName = originalName;
			this.Parent = parent;
			this.minimized = false;
		}

		internal PathSegment Parent { get; private set; }

		internal string OriginalPath {
			get {
				if (this.Parent != null) {
					return Path.Combine(this.Parent.OriginalPath, this.originalName);
				} else {
					return this.originalName;
				}
			}
		}

		internal string CurrentPath {
			get {
				if (this.Parent != null) {
					return Path.Combine(this.Parent.CurrentPath, this.currentName);
				} else {
					return this.currentName;
				}
			}
		}

		internal string CurrentName {
			get { return this.currentName; }
		}

		internal string OriginalName {
			get { return this.originalName; }
		}

		private int SegmentCount {
			get {
				int parents = this.Parent != null ? this.Parent.SegmentCount : 0;
				return parents + 1;
			}
		}

		internal int FullLength {
			get {
				if (this.Parent != null) {
					int parentLength = this.Parent.FullLength;
					if (parentLength > 0) {
						parentLength++; // allow for an in between slash
					}
					return parentLength + this.currentName.Length;
				} else {
					return this.currentName.Length;
				}
			}
		}

		internal bool NameChanged {
			get { return !string.Equals(this.currentName, this.originalName, StringComparison.OrdinalIgnoreCase); }
		}

		internal bool IsLeaf {
			get { return this.Children.Count == 0; }
		}

		internal IEnumerable<PathSegment> Descendents {
			get {
				IEnumerable<PathSegment> result = this.Children;
				foreach (PathSegment child in this.Children) {
					result = result.Concat(child.Descendents);
				}

				return result;
			}
		}

		internal IEnumerable<PathSegment> Ancestors {
			get {
				PathSegment parent = this.Parent;
				while (parent != null) {
					yield return parent;
					parent = parent.Parent;
				}
			}
		}

		internal IEnumerable<PathSegment> SelfAndDescendents {
			get {
				yield return this;
				foreach (var child in this.Descendents) {
					yield return child;
				}
			}
		}

		internal IEnumerable<PathSegment> SelfAndAncestors {
			get {
				yield return this;
				foreach (var parent in this.Ancestors) {
					yield return parent;
				}
			}
		}

		internal IEnumerable<PathSegment> LeafChildren {
			get { return this.Children.Where(child => child.IsLeaf); }
		}

		internal IEnumerable<PathSegment> LeafDescendents {
			get { return this.Descendents.Where(child => child.IsLeaf); }
		}

		internal IEnumerable<PathSegment> Siblings {
			get { return this.Parent != null ? this.Parent.Children : Enumerable.Empty<PathSegment>(); }
		}

		internal Collection<PathSegment> Children { get; private set; }

		public override string ToString() {
			string path;
			if (this.NameChanged) {
				path = "{" + this.originalName + " => " + this.currentName + "}";
			} else {
				path = this.currentName;
			}

			if (path.Length > 0 && !this.IsLeaf) {
				path += "\\";
			}

			if (this.Parent != null) {
				path = Parent.ToString() + path;
			}

			return path;
		}

		internal PathSegment Add(string originalPath) {
			Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalPath));
			Contract.Ensures(Contract.Result<PathSegment>() != null);
			string[] segments = originalPath.Split(Path.DirectorySeparatorChar);
			return this.Add(segments, 0);
		}

		internal void Add(IEnumerable<string> originalPaths) {
			foreach (string path in originalPaths) {
				this.Add(path);
			}
		}

		internal int EnsureSelfAndChildrenNoLongerThan(int maxLength) {
			Contract.Requires<ArgumentOutOfRangeException>(maxLength > 0, "A path can only have a positive length.");
			Contract.Requires<ArgumentOutOfRangeException>(this.Parent == null || maxLength > this.Parent.FullLength + 1, "A child path cannot possibly be made shorter than its parent.");
			Contract.Ensures(Contract.Result<int>() <= maxLength);
			const int uniqueBase = 16;

			// Find the items that are too long, and always work on the longest one
			var longPaths = this.SelfAndDescendents.Where(path => path.FullLength > maxLength).OrderByDescending(path => path.FullLength);
			PathSegment longPath;
			while ((longPath = longPaths.FirstOrDefault()) != null) {
				// Keep working on this one until it's short enough.
				do {
					int tooLongBy = longPath.FullLength - maxLength;
					var longSegments = longPath.SelfAndAncestors.Where(segment => !segment.minimized).OrderByDescending(segment => segment.CurrentName.Length);
					PathSegment longestSegment = longSegments.FirstOrDefault();
					if (longestSegment == null) {
						throw new InvalidOperationException("Unable to shrink path length sufficiently.");
					}
					PathSegment secondLongestSegment = longSegments.Skip(1).FirstOrDefault();
					int shortenByUpTo;
					if (secondLongestSegment != null) {
						shortenByUpTo = Math.Min(tooLongBy, Math.Max(1, longestSegment.CurrentName.Length - secondLongestSegment.CurrentName.Length));
					} else {
						shortenByUpTo = tooLongBy;
					}
					int minimumGuaranteedUniqueLength = Math.Max(1, RoundUp(Math.Log(longestSegment.Siblings.Count(), uniqueBase)));
					int allowableSegmentLength = Math.Max(minimumGuaranteedUniqueLength, longestSegment.CurrentName.Length - shortenByUpTo);
					if (allowableSegmentLength >= longestSegment.CurrentName.Length) {
						// We can't make this segment any shorter.
						longestSegment.minimized = true;
					}
					longestSegment.currentName = longestSegment.CreateUniqueShortFileName(longestSegment.CurrentName, allowableSegmentLength);
				} while (longPath.FullLength > maxLength);
			}

			// Return the total length of self or longest child.
			return this.SelfAndDescendents.Max(c => c.FullLength);
		}

		internal PathSegment FindByOriginalPath(string originalPath) {
			Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(originalPath));
			string[] segments = originalPath.Split(Path.DirectorySeparatorChar);
			return this.FindByOriginalPath(segments, 0);
		}

		private string GetUniqueShortName(string preferredPrefix, string preferredSuffix, int allowableLength) {
			Contract.Requires<ArgumentNullException>(preferredPrefix != null);
			Contract.Requires<ArgumentNullException>(preferredSuffix != null);
			Contract.Requires<ArgumentException>(allowableLength > 0);
			Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
			Contract.Ensures(Contract.Result<string>().Length <= allowableLength);
			string candidateName = string.Empty;
			int i;
			for (i = -1; candidateName.Length == 0 || ReservedFileNames.Contains(candidateName, StringComparer.OrdinalIgnoreCase) || this.Siblings.Any(child => string.Equals(child.CurrentName, candidateName, StringComparison.OrdinalIgnoreCase)); i++) {
				string unique = i < 0 ? string.Empty : i.ToString("x");
				if (allowableLength < unique.Length) {
					throw new InvalidOperationException("Unable to shorten path sufficiently to fit constraints.");
				}

				candidateName = unique;

				// Suffix gets higher priority than the prefix, but only if the entire suffix can be appended.
				if (candidateName.Length + preferredSuffix.Length <= allowableLength) {
					candidateName += preferredSuffix;
				}

				// Now prepend as much of the prefix as fits.
				candidateName = preferredPrefix.Substring(0, Math.Min(allowableLength - candidateName.Length, preferredPrefix.Length)) + candidateName;
			}

			return candidateName;
		}

		private static int RoundUp(double value) {
			int roundedValue = (int)value;
			if (roundedValue < value) {
				roundedValue++;
			}

			return roundedValue;
		}

		private string CreateUniqueShortFileName(string fileName, int targetLength) {
			Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(fileName));
			Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
			Contract.Ensures(Contract.Result<string>().Length <= targetLength);

			// The filename may already full within the target length.
			if (fileName.Length <= targetLength) {
				return fileName;
			}

			string preferredPrefix = Path.GetFileNameWithoutExtension(fileName);
			string preferredSuffix = Path.GetExtension(fileName);

			string shortenedFileName = GetUniqueShortName(preferredPrefix, preferredSuffix, targetLength);
			return shortenedFileName;
		}

		private void ShortenThis(int targetLength) {
			this.currentName = CreateUniqueShortFileName(this.originalName, targetLength);
		}

		private PathSegment Add(string[] segments, int segmentIndex) {
			Contract.Requires<ArgumentNullException>(segments != null);
			Contract.Requires<ArgumentOutOfRangeException>(segmentIndex < segments.Length);
			Contract.Ensures(Contract.Result<PathSegment>() != null);
			var match = this.Children.SingleOrDefault(child => String.Equals(child.originalName, segments[segmentIndex]));
			if (match == null) {
				match = new PathSegment(segments[segmentIndex], this);
				this.Children.Add(match);
			}

			return segments.Length == segmentIndex + 1 ? match : match.Add(segments, segmentIndex + 1);
		}

		private PathSegment FindByOriginalPath(string[] segments, int segmentIndex) {
			Contract.Requires<ArgumentNullException>(segments != null);
			Contract.Requires<ArgumentOutOfRangeException>(segmentIndex < segments.Length);
			if (string.Equals(this.originalName, segments[segmentIndex], StringComparison.OrdinalIgnoreCase)) {
				if (segmentIndex == segments.Length - 1) {
					return this;
				}

				foreach (var child in this.Children) {
					var match = child.FindByOriginalPath(segments, segmentIndex + 1);
					if (match != null) {
						return match;
					}
				}
			}

			return null;
		}
	}
}