Skip to content

Instantly share code, notes, and snippets.

View JayBazuzi's full-sized avatar

Jay Bazuzi JayBazuzi

View GitHub Profile
@JayBazuzi
JayBazuzi / gist:5230866
Created March 24, 2013 07:04
Add a copyright header to the all your source files, with PowerShell.
# 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 December 28, 2015 14:49
Simple MSBuild task w/ parameter logging
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 November 17, 2013 20:41
Simple MSBuild project to execute a simple custom MSBuild task.
<?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 November 17, 2013 20:45
Running a custom MSBuild task
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 November 17, 2013 21:24
Initial test for running MSBuild w/ custom task
[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 December 28, 2015 14:59
Custom MSBuild logger - grabs only High importance messages, returns them as a string.
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 November 17, 2013 22:19
Execute my test project in-proc
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 December 28, 2015 15:09
Create an MSBuild project and run it, within a unit test.
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 November 17, 2013 23:02
An extension method for MSBuild's ProjectRootElement to make adding a UsingTask have less duplication
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 November 17, 2013 23:17
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;