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

namespace DotNetOpenAuth.BuildTasks {
	using System;
	using System.Linq;
	using Microsoft.Build.Framework;
	using Microsoft.Build.Utilities;
	using Microsoft.Web.Administration;

	/// <summary>
	/// Deletes a web application from IIS.
	/// </summary>
	public class DeleteWebApplication : Task {
		/// <summary>
		/// Gets or sets the name of the web site under which to create the web application.
		/// </summary>
		/// <value>The name of the existing web site.</value>
		[Required]
		public string WebSiteName { get; set; }

		/// <summary>
		/// Gets or sets the virtual paths within the web site that will access these applications.
		/// </summary>
		/// <value>The virtual path, which must start with '/'.</value>
		[Required]
		public ITaskItem[] VirtualPaths { get; set; }

		/// <summary>
		/// Executes this instance.
		/// </summary>
		/// <returns>A value indicating whether the task completed successfully.</returns>
		public override bool Execute() {
			var serverManager = new ServerManager();

			// Find the root web site that this web application will be created under.
			var site = serverManager.Sites.FirstOrDefault(s => string.Equals(s.Name, this.WebSiteName, StringComparison.OrdinalIgnoreCase));
			if (site == null) {
				Log.LogMessage(MessageImportance.Low, TaskStrings.NoMatchingWebSiteFound, this.WebSiteName);
				return true;
			}

			if (this.VirtualPaths.Length == 0) {
				// Nothing to do.
				return true;
			}

			foreach (ITaskItem path in this.VirtualPaths) {
				var app = site.Applications.FirstOrDefault(a => string.Equals(a.Path, path.ItemSpec, StringComparison.OrdinalIgnoreCase));
				if (app != null) {
					site.Applications.Remove(app);
					Log.LogMessage(MessageImportance.Normal, TaskStrings.DeletedWebApplication, app.Path);
				} else {
					Log.LogMessage(MessageImportance.Low, TaskStrings.WebApplicationNotFoundSoNotDeleted, path.ItemSpec);
				}
			}

			serverManager.CommitChanges();

			return true;
		}
	}
}