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

namespace DotNetOpenAuth.BuildTasks {
	using Microsoft.Build.Framework;
	using Microsoft.Build.Utilities;

	/// <summary>
	/// Trims item identities or metadata.
	/// </summary>
	public class Trim : Task {
		/// <summary>
		/// Gets or sets the name of the metadata to trim.  Leave empty or null to operate on itemspec.
		/// </summary>
		/// <value>The name of the metadata.</value>
		public string MetadataName { get; set; }

		/// <summary>
		/// Gets or sets the characters that should be trimmed off if found at the start of items' ItemSpecs.
		/// </summary>
		public string StartCharacters { get; set; }

		/// <summary>
		/// Gets or sets the characters that should be trimmed off if found at the end of items' ItemSpecs.
		/// </summary>
		public string EndCharacters { get; set; }

		/// <summary>
		/// Gets or sets the substring that should be trimmed along with everything that appears after it.
		/// </summary>
		public string AllAfter { get; set; }

		/// <summary>
		/// Gets or sets the items with ItemSpec's to be trimmed.
		/// </summary>
		[Required]
		public ITaskItem[] Inputs { get; set; }

		/// <summary>
		/// Gets or sets the items with trimmed ItemSpec strings.
		/// </summary>
		[Output]
		public ITaskItem[] Outputs { get; set; }

		/// <summary>
		/// Executes this instance.
		/// </summary>
		/// <returns>A value indicating whether the task completed successfully.</returns>
		public override bool Execute() {
			this.Outputs = new ITaskItem[this.Inputs.Length];
			for (int i = 0; i < this.Inputs.Length; i++) {
				this.Outputs[i] = new TaskItem(this.Inputs[i]);
				string value = string.IsNullOrEmpty(this.MetadataName) ? this.Outputs[i].ItemSpec : this.Outputs[i].GetMetadata(this.MetadataName);
				if (!string.IsNullOrEmpty(this.StartCharacters)) {
					value = value.TrimStart(this.StartCharacters.ToCharArray());
				}
				if (!string.IsNullOrEmpty(this.EndCharacters)) {
					value = value.TrimEnd(this.EndCharacters.ToCharArray());
				}
				if (!string.IsNullOrEmpty(this.AllAfter)) {
					int index = value.IndexOf(this.AllAfter);
					if (index >= 0) {
						value = value.Substring(0, index);
					}
				}
				if (string.IsNullOrEmpty(this.MetadataName)) {
					this.Outputs[i].ItemSpec = value;
				} else {
					this.Outputs[i].SetMetadata(this.MetadataName, value);
				}
			}

			return true;
		}
	}
}