A simple script that cleans and builds visual studio solution with psake

psake is a simple build automation tool written in powershell, and works well on Microsoft platform.

This is a really simple example that cleans bin folder, run msbuild to build the solution, and clean up pdb and xml files afterward.


properties {
    $BuildConfiguration = if ($BuildConfiguration -eq $null ) { "debug" } else {     
        $BuildConfiguration }
    $BuildScriptsPath = Resolve-Path .
    $base_dir = Resolve-Path ..
    $packages = "$base_dir\packages"
    $build_dir = "$base_dir\Sushiwa\bin"
    $sln_file = "$base_dir\Sushiwa.sln"
}

task default -depends CleanUp, Compile

task CleanUp {
    @($build_dir) | aWhere-Object { Test-Path $_ } | ForEach-Object {
    Write-Host "Cleaning folder $_..."
    Remove-Item $_ -Recurse -Force -ErrorAction Stop
    }
}

task Compile {
    Write-Host "Compiling $sln_file in $BuildConfiguration mode to $build_dir"
    Exec { msbuild "$sln_file" /t:Clean /t:Build /p:Configuration=$BuildConfiguration 
        /m /nr:false /v:q /nologo /p:OutputDir=$build_dir }

    Get-ChildItem -Path $build_dir -Rec | Where {$_.Extension -match "pdb"} | Remove-Item
    Get-ChildItem -Path $build_dir -Rec | Where {$_.Extension -match "xml"} | Remove-Item
}
A simple script that cleans and builds visual studio solution with psake

MS Build – Publish to FileSystem

I’m working for a simple project and wanted to deploy it to the webserver in my church. I installed TeamCity and set it up to pull the source code from github and build it.

The challenge was, unexpectedly, publish. Initially, I used MS Web Deploy. For some reason I couldn’t figure out, it didn’t work. Also, I had to install an extra service. So I went for a simple msbuild file system deploy.


msbuild .\WebDrive.csproj /t:Package
    /p:_PackageTempDir="\\webserver\output" /p:Configuration="Release"

On TeamCity, I added an additional build step and configure it like these

  • Runner type: MSBuild
  • Build file path: WebDrive/WebDriveWeb.csproj
  • Targets: Clean;Build;Publish
  • Command line parameters: /t:Package /p:_PackageTempDir=”\\web\output” /p:Configuration=”Release”

Much simpler that web deployment.

MS Build – Publish to FileSystem

MSBUild task: Get the latest code from TFS and build

Resource

This is a simple task, but it took some time for me to udnerstand, as MSBuild was a new thing to me.
First, Get the latest code from TFS is TFS task, not MSBUild task. So, you can use tf.exe command-line utility.

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" get $/root/MSBUILD/Team/Sapphire /force /recursive

the batch file is like this

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" get $/root/OccupyingSpace/GoYocal /force /recursive
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe" "C:\TEMP\Test_GoYocalBuild.proj" /t:Clean;Test
pause

You need SDC taks in order to build .sln file. Please download it from codeplex. I created a simple proj file that builds .sln file

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
    <Import Project="C:\vss\root\MSBUILD\bin\Microsoft.Sdc.Common.tasks"/>
    <Target Name="Test" >
        <Tools.DevEnv
            VisualStudio="9.0"
            Path="C:\VSS\root\OccupyingSpace\GoYocal\GoYocal.sln"
            Config="Release"
            OutputFolder="C:\VSS\root\OccupyingSpace\GoYocal\src\app\bin"
            Clean="true">
        </Tools.DevEnv>
    </Target>

    <PropertyGroup>
        <TFSServerName>http://rbi101:8080</TFSServerName>
        <SourceControlMainDirectory>$/root/OccupyingSpace/GoYocal</SourceControlMainDirectory>
        <WorkspaceName>myworkspace</WorkspaceName>
        <TfCommand>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe"</TfCommand>
        <DevenvCommand>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"</DevenvCommand>
        <WorkingDirectory>c:\</WorkingDirectory>
    </PropertyGroup>
</Project>

After I used the batch file, I realised I could use <Exce Command=”> for any dos command. So, I moved “GetLatest” bit into msbuild proj.
Also, included remote copy to the server. The final codes are like the below

batch file

"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe" MSBUld_Build.proj /t:GetLatest;Build;Deploy
pause

and msbuild proj file

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<Import Project="C:\MSBUILD\bin\Microsoft.Sdc.Common.tasks"/>

<PropertyGroup>
<TF>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe"</TF>
<TFSourceLocation>$/Occup/GoYocal</TFSourceLocation>
<SolutionRoot>C:\Occu\GoYocal</SolutionRoot>
<RemoteWebRoot>\\TJ103\d$\Occu\WebSites\GoYocal</RemoteWebRoot>
<Copy>xcopy /E /I /R /Y</Copy>
</PropertyGroup>

<Target Name="GetLatest">
<Exec Command="$(TF) get $(TFSourceLocation) /force /recursive /version:T /noprompt" ContinueOnError="true" />
</Target>

<Target Name="Build" >
<Tools.DevEnv VisualStudio="9.0" Path="$(SolutionRoot)\GoYocal.sln" Config="Release" OutputFolder="$(SolutionRoot)\src\app\bin" Clean="true">
</Tools.DevEnv>
</Target>

<Target Name="Deploy">
<Exec Command='net use "x:" "$(RemoteWebRoot)" /u:domain\username pwd' />
<Exec Command='$(Copy) "$(SolutionRoot)\src\app" "x:" '/>
<Exec Command='net use x: /d' />
</Target>

</Project>
MSBUild task: Get the latest code from TFS and build