Skip to content

Instantly share code, notes, and snippets.

View JayBazuzi's full-sized avatar

Jay Bazuzi JayBazuzi

View GitHub Profile
@JayBazuzi
JayBazuzi / ProjectRootElementExtensions.cs
Created November 17, 2013 23:21
An alternate implementation of ProjectRootElementExtensions that takes the task type as a generic parameter.
using System;
using Microsoft.Build.Construction;
public static class ProjectRootElementExtensions
{
public static void AddUsingTask<TTask>(this ProjectRootElement projectRootElement)
{
Type taskType = typeof(TTask);
projectRootElement
.AddUsingTask(taskType.Name, taskType.Assembly.Location, null);
@JayBazuzi
JayBazuzi / gist:7519763
Created November 17, 2013 23:28
Create an MSBuild project and run it, within a unit test, using some convenient extension methods.
var projectRootElement = ProjectRootElement.Create();
projectRootElement
.AddTarget("Build")
.AddTask<MyTask>()
.SetParameter("Foo", "hi");
var highImportanceStringLogger = new HighImportanceStringLogger();
var success = new ProjectInstance(projectRootElement).Build(new[] { highImportanceStringLogger });
@JayBazuzi
JayBazuzi / LoggingTask.cs
Last active December 28, 2015 15:29
Base class for MSBuild tasks which logs all properties marked `[Required]`
public abstract class LoggingTask : Task
{
protected void LogInputs()
{
Log.LogMessage(MessageImportance.High, "Inputs:");
foreach (var propertyInfo in GetInputProperties())
{
LogProperty(propertyInfo);
}
public class TaskWithRequiredInput : LoggingTask
{
[Required]
public string Foo { get; set; }
public override bool Execute()
{
LogInputs();
return true;
}
public static class PropertyInfoExtensions
{
public static bool HasAttribute<TAttribute>(this PropertyInfo propertyInfo)
where TAttribute : Attribute
{
return propertyInfo.GetCustomAttribute<TAttribute>() != null;
}
}
@JayBazuzi
JayBazuzi / MSBuildParameterLoggingTests.cs
Last active December 28, 2015 15:39
Unit test for running a custom MSBuild task.
[TestMethod]
public void RequiredInputGetsLogged()
{
var buildTarget = CreateBuildTarget();
buildTarget.AddTask<TaskWithRequiredInput>().SetParameter("Foo", "hi");
const string expected = @"Inputs:
Foo = hi
";
@JayBazuzi
JayBazuzi / LogTaskItems.cs
Last active December 28, 2015 15:39
test for logging MSBuild items items
private void LogProperty(PropertyInfo propertyInfo)
{
string name = propertyInfo.Name;
var value = propertyInfo.GetValue(this);
if (value is ITaskItem[])
{
LogTaskItems(value as ITaskItem[], name);
}
else
{
@JayBazuzi
JayBazuzi / AssertNoBindingErrorsTraceListener.cs
Last active December 29, 2015 12:19
Use this to write a unit test that asserts that your XAML correctly binds to your ViewModel.
class AssertNoBindingErrorsTraceListener : TraceListener
{
readonly StringBuilder messageBuilder = new StringBuilder();
public AssertNoBindingErrorsTraceListener(SourceLevels level)
{
PresentationTraceSources.DataBindingSource.Listeners.Add(this);
PresentationTraceSources.DataBindingSource.Switch.Level = level;
}
new Dictionary<int, string>
{
{ 3, "Fizz"},
{ 5, "Buzz"},
{15, "FizzBuzz"},
}
.Where(item => i % item.Key == 0)
.Select(item => item.Value)
.LastOrDefault() ?? i.ToString();
@JayBazuzi
JayBazuzi / FizzBuzz3.cs
Created January 2, 2014 22:32
Replaced all conditionals with polymorphism
interface I
{
string Get(int i);
}
private class Fizz : I
{
public string Get(int i)
{
return "Fizz";