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

namespace DotNetOpenAuth.BuildTasks {
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using Microsoft.Build.BuildEngine;
	using Microsoft.Build.Framework;
	using Microsoft.Build.Utilities;

	public class AddProjectItems : Task {
		/// <summary>
		/// Gets or sets the projects to add items to.
		/// </summary>
		/// <value>The projects.</value>
		[Required]
		public ITaskItem[] Projects { get; set; }

		/// <summary>
		/// Gets or sets the items to add to each project.
		/// </summary>
		/// <value>The items.</value>
		/// <remarks>
		/// Use the metadata "ItemType" on each item to specify the item type to use for the new
		/// project item.  If the metadata is absent, "None" is used as the item type.
		/// </remarks>
		[Required]
		public ITaskItem[] Items { get; set; }

		/// <summary>
		/// Executes this instance.
		/// </summary>
		public override bool Execute() {
			foreach (var projectTaskItem in this.Projects) {
				var project = new Project();
				project.Load(projectTaskItem.ItemSpec);

				foreach (var projectItem in this.Items) {
					string itemType = projectItem.GetMetadata("ItemType");
					if (string.IsNullOrEmpty(itemType)) {
						itemType = "None";
					}
					BuildItem newItem = project.AddNewItem(itemType, projectItem.ItemSpec, false);
					var customMetadata = projectItem.CloneCustomMetadata();
					foreach (DictionaryEntry entry in customMetadata) {
						string value = (string)entry.Value;
						if (value.Length > 0) {
							newItem.SetMetadata((string)entry.Key, value);
						}
					}
				}

				project.Save(projectTaskItem.ItemSpec);
			}

			return !this.Log.HasLoggedErrors;
		}
	}
}