Skip to content

Instantly share code, notes, and snippets.

Avatar

Jay Bazuzi JayBazuzi

View GitHub Profile
@JayBazuzi
JayBazuzi / gist:5230866
Created Mar 24, 2013
Add a copyright header to the all your source files, with PowerShell.
View gist:5230866
# Create a file `~\header.cs` with the copyright or whatever you want
# in each file. Then run this script.
#
# Notes:
#
# Second step will force BOM at the start, and newline at the end, of each
# file. It will also push all file encodings to be the same. Those make
# diffs a little ugly.
#
# So, I do it in two steps: one to normalize encoding, and one to actually
@JayBazuzi
JayBazuzi / MyTask.cs
Last active Dec 28, 2015
Simple MSBuild task w/ parameter logging
View MyTask.cs
public class MyTask : Task
{
[Required]
public string Foo { get; set; }
public override bool Execute()
{
Log.LogMessage(MessageImportance.Low, "Inputs:");
Log.LogMessage(MessageImportance.Low, " Foo = {0}", this.Foo);
return true;
@JayBazuzi
JayBazuzi / My.proj
Created Nov 17, 2013
Simple MSBuild project to execute a simple custom MSBuild task.
View My.proj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="MyTask" AssemblyFile=".\bin\Debug\ClassLibrary2.dll" />
<Target Name="Build">
<MyTask Foo = "hi" />
</Target>
</Project>
@JayBazuzi
JayBazuzi / gist:7518058
Created Nov 17, 2013
Running a custom MSBuild task
View gist:7518058
PS> msbuild .\My.proj /v:d
...
Task "MyTask"
Inputs:
Foo = hi
Done executing task "MyTask".
PS> msbuild .\My.proj /v:diag
...
Task "MyTask" (TaskId:2)
@JayBazuzi
JayBazuzi / MyTaskTest.cs
Created Nov 17, 2013
Initial test for running MSBuild w/ custom task
View MyTaskTest.cs
[TestMethod]
public void TestMethod1()
{
var process = new Process
{
StartInfo =
{
FileName = @"C:\Program Files (x86)\MSBuild\12.0\bin\MSBuild.exe",
Arguments = @"..\..\my.proj /v:m /nologo",
UseShellExecute = false,
@JayBazuzi
JayBazuzi / HighImportanceStringLogger.cs
Last active Dec 28, 2015
Custom MSBuild logger - grabs only High importance messages, returns them as a string.
View HighImportanceStringLogger.cs
class HighImportanceStringLogger : ILogger
{
readonly private StringBuilder stringBuilder = new StringBuilder();
public void Initialize(IEventSource eventSource)
{
eventSource.MessageRaised += eventSource_MessageRaised;
}
void eventSource_MessageRaised(object sender, BuildMessageEventArgs e)
@JayBazuzi
JayBazuzi / gist:7519022
Created Nov 17, 2013
Execute my test project in-proc
View gist:7519022
var myProject = new ProjectInstance(@"..\..\my.proj");
var logger = new HighImportanceStringLogger();
var success = myProject.Build(new[] { logger });
Assert.IsTrue(success);
const string expected = @"Inputs:
Foo = hi
";
Assert.AreEqual(expected, logger.ToString());
@JayBazuzi
JayBazuzi / gist:7519460
Last active Dec 28, 2015
Create an MSBuild project and run it, within a unit test.
View gist:7519460
var projectRootElement = ProjectRootElement.Create();
projectRootElement.AddUsingTask(typeof (MyTask));
projectRootElement
.AddTarget("Build")
.AddTask(typeof(MyTask).Name)
.SetParameter("Foo", "hi");
var highImportanceStringLogger = new HighImportanceStringLogger();
@JayBazuzi
JayBazuzi / ProjectRootElementExtensions.cs
Created Nov 17, 2013
An extension method for MSBuild's ProjectRootElement to make adding a UsingTask have less duplication
View ProjectRootElementExtensions.cs
using System;
using Microsoft.Build.Construction;
public static class ProjectRootElementExtensions
{
public static void AddUsingTask(this ProjectRootElement projectRootElement, Type type)
{
projectRootElement
.AddUsingTask(type.Name, type.Assembly.Location, null);
}
@JayBazuzi
JayBazuzi / ProjectTargetElementExtensions.cs
Created Nov 17, 2013
An extension method for ProjectTargetElement that ensures the project has the correct UsingTask
View ProjectTargetElementExtensions.cs
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;