summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.BuildTasks/FixupShippingToolSamples.cs
blob: 92b023528504593ac904ebaf57350365bef2d5ae (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
//-----------------------------------------------------------------------
// <copyright file="FixupShippingToolSamples.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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

	/// <summary>
	/// Removes imports that only apply when a shipping tool sample builds as part of
	/// the entire project, but not when it's part of a source code sample.
	/// </summary>
	public class FixupShippingToolSamples : Task {
		[Required]
		public ITaskItem[] Projects { get; set; }

		/// <summary>
		/// Executes this instance.
		/// </summary>
		/// <returns></returns>
		public override bool Execute() {
			foreach (ITaskItem projectTaskItem in this.Projects) {
				this.Log.LogMessage("Fixing up the {0} sample for shipping as source code.", Path.GetFileNameWithoutExtension(projectTaskItem.ItemSpec));

				var project = new Project();
				Uri projectUri = new Uri(projectTaskItem.GetMetadata("FullPath"));
				project.Load(projectTaskItem.ItemSpec, ProjectLoadSettings.IgnoreMissingImports);

				project.Imports.Cast<Import>()
					.Where(import => import.ProjectPath.StartsWith(@"..\..\tools\", StringComparison.OrdinalIgnoreCase))
					.ToList()
					.ForEach(import => project.Imports.RemoveImport(import));

				project.Save(projectTaskItem.ItemSpec);
			}

			return !this.Log.HasLoggedErrors;
		}
	}
}