Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Created November 17, 2013 23:17
Show Gist options
  • Save JayBazuzi/7519648 to your computer and use it in GitHub Desktop.
Save JayBazuzi/7519648 to your computer and use it in GitHub Desktop.
An extension method for ProjectTargetElement that ensures the project has the correct UsingTask
using System.Linq;
using Microsoft.Build.Construction;
static class ProjectTargetElementExtensions
{
public static ProjectTaskElement AddTask<TTask>(this ProjectTargetElement projectTargetElement)
{
string taskName = typeof (TTask).Name;
ProjectRootElement projectRootElement = projectTargetElement.ContainingProject;
if (!projectRootElement.UsingTasks.Any(ut => ut.TaskName == taskName))
{
projectRootElement.AddUsingTask<TTask>();
}
return projectTargetElement.AddTask(taskName);
}
}
@JayBazuzi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment