Friday, May 22, 2015

XUnit and MSBuild

Recently I needed to execute xUnit tests with MSBuild, so I've spent some time for creating a MSBuild project running the tests. Luckily xUnit already have MSBuild tasks so I just needed to hook it up.
I wanted to search for all xUnit unit test dlls inside a folder and run the tests there. So I'm searching for xunit.core.dll file to get all the folders eventually containing such dlls and then search all these folders for a pattern - *.Tests.dll. When the tests dlls are found xunit task is run for all of them. So here's the result .proj file:
<?xml version="1.0" encoding="utf-8"?>
<Project
    DefaultTargets="Test"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask
      AssemblyFile="xunit.runner.msbuild.dll"
      TaskName="Xunit.Runner.MSBuild.xunit"/>

    <UsingTask TaskName="GetAssemblies" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <Path ParameterType="System.String" Required="true" />
            <XUnitFileName ParameterType="System.String" Required="false"/>
            <TestsSearchPattern ParameterType="System.String" Required="false"/>
            <Assemblies ParameterType="System.String[]" Output="true" />
        </ParameterGroup>
        <Task>
            <Code Type="Fragment" Language="cs">
                <![CDATA[
                    var xUnitFileName = XUnitFileName ?? "xunit.core.dll";
                    var testsSearchPattern = TestsSearchPattern ?? "*.Tests.dll";
                
                    // get all the directories containing xUnit dlls
                    var directories = System.IO.Directory.GetFiles(Path, xUnitFileName, System.IO.SearchOption.AllDirectories)
                        .Select(file => System.IO.Path.GetDirectoryName(file));

                    var assembliesList = new List<string>();

                    foreach(string directory in directories)
                    {
                        // get all test dlls from the given paths
                        assembliesList.AddRange(System.IO.Directory.GetFiles(directory, testsSearchPattern, System.IO.SearchOption.TopDirectoryOnly));
                    }

                    Assemblies = assembliesList.ToArray();
                ]]>
            </Code>
        </Task>
    </UsingTask>

    <Target Name="Test">
        <GetAssemblies Path="$(BuildRoot).">
            <Output PropertyName="TestAssemblies" TaskParameter="Assemblies"/>
        </GetAssemblies>
        <xunit Assemblies="$(TestAssemblies)" />
    </Target>
</Project>

Friday, May 15, 2015

Git History tool

This is a small tool getting history for a path in a given local git repository and exporting it using RazorTemplates

The tool get all the changes between revisions or to a revision of a local git repository and pass the commits to a Razor template which then outputs the result in a given file. The commits can be filtered by a given pat in the repository. 

UsageGitHistory.App.exe [-gitRepo] [-templateFile] [-outputFile] [-endRevision ] [-gitInstallationFolder ] [-includeMerges] [-pageTitle ] [-pathInRepo ] [-startRevision ] 

To use the tool you can pass the following arguments:

  • gitRepo(or just r) - The local path to the git repository 
  • templateFile(or just t) - The path to the Razor template file that will be used for tansforming the list of commits 
  • outputFile(or just o) - The path to the file where the result content will be saved 
  • startRevision(or r or revision) - This argument is optional and is needed if you want to get the changes in a range or to a revision 
  • endRevision(or just er) - Optional argument that specifies the end revision 
  • includeMerges(or just im) - Whether or not the merge commits to be included in the output 
  • gitInstallationFolder(or just gi) - The path to the git executable if it is not set in the Path environment variable 
  • pathInRepo(or just p) - The relative path inside the local repository where you want to get the changes for 
  • pageTitle(or just pt) - This can be used for presentation purposes only. Can be passed to the command line and then output in the result 
You can find the tool on github here. Example usage(example.bat) and razor(example.razor) files inside the project folder.

Friday, April 24, 2015

ConfigurationTransformations Nuget

As in my recent work I had to deal with some configuration transformations from C# code I've created a nuget that can be used if you want to apply a transformation programmatically. To do that you can install the ConfigurationTransformation package from the official nuget repository.
In order to use it you can call the following from the package manager console:
PM> Install-Package ConfigurationTransformations

and then just create an instance of the ConfigTransformer class to apply the transformation.
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string destinationFile = @"C:\temp\resultweb.config";
TransformationResult transformationResult = configTransformer.ApplyTransformation(sourceFile, transformationFile, destinationFile);
If you don't specify the destination file the source file content will be changed with the result transformated content.

The other option is to get the result content without saving it to a file:
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string transformedContent = configTransformer.GetTransformedFileContent(sourceFile, transformationFile);

Tuesday, April 21, 2015

Small helper tool for configuration transformations

In my daily work I often use configuration transformations for different settings for the different environment and often I need to add a new transformation for a new environment. So I've decided to create a small tool that can automate adding new transformations to all the projects in a solution. In addition to this I've added functionality for applying config transformations as this can be useful if you want to test the transformations from command line. Here's a short description of the tool:

TransformHelper

Small tool helping with the usage of the configuration transformations

It has two

  • Add - passing solution, existing transformation and the new transformation the tool will go through all the projects in the solution and check for configuration files having the existing transformation and will add new transformation copying the files from the existing
  • Remove - passing solution, and existing transformation the tool will go through all the projects in the solution and check for configuration files having the existing transformation and will delete them
  • Apply - passing source file, transformation file and optional target file (if not passed the source file content will be replaced)
  • ApplySLN - passing solution, and existing transformation the tool will go through all the projects in the solution and apply the transformation on the original file
Usage
  • Add mode (you may not specify the mode here as the Add mode is the default behavior)
TransformHelper.exe [-mode add] -solution C:\Work\MySolution.sln -existing Dev -new Live

This will check all the projects in the solution for files like [fileName].Dev.config and will add the corresponding [fileName].Live.config files copying the content from the existing ones
  • Remove mode
TransformHelper.exe -mode remove -solution C:\Work\MySolution.sln -existing Dev

This will check all the projects in the solution for files like [fileName].Dev.config and will delete them and remove them from the project
  • Apply mode
TransformHelper.exe -mode apply -source "C:\Work\MySolution\WebProject\web.config" -transformFile "C:\Work\MySolution\WebProject\web.Dev.config" -target "C:\Temp\result.web.config"

This will apply the transformation file C:\Work\MySolution\WebProject\web.Dev.config on C:\Work\MySolution\WebProject\web.config and the result content will be saved in C:\Temp\result.web.config file
  • ApplySLN mode
TransformHelper.exe -mode applySLN -solution C:\Work\MySolution.sln -existing Dev

This will check all the projects in the solution for files like [fileName].Dev.config and if there are files like [fileName].config as well in the project they will be transformed using the Dev transformations

You can find the tool on github here

Wednesday, January 14, 2015

Online GUID converter

Sometimes I need to convert GUIDs without dashes to GUIDs with dashes or vice versa, so I've created this small page to help me with the conversion.

or