Thursday, January 5, 2012

Did you know? – NuGetGallery Build

Today I read over the NuGetGallery source and naturally, started with the first entry point according to the documentation: their build script.
Build-Solution.ps1
There is a lot of PowerShell stuff that I didn’t know about in this short file, so let's look at it line by line:
1. The param() function is used to process named parameters, so the connection string can be passed when calling into this script.
param($connectionString = "")

2. Next it gets the path to the directory that contains this script file and uses it to invoke another .ps1 file. Note that the “.” is used to set PowerShell to parse the line in command mode. We’ll look at what Get-ConnectionString.ps1 does some other time, for now just be aware that it is going to load a new function to be used.
$scriptPath = Split-Path $MyInvocation.MyCommand.Path
. (join-path $scriptPath Scripts\Get-ConnectionString.ps1)

3. This is where the function is used, note that the web.config file is passed to it, so apparently that script is capable of parsing the xml to retrieve the connection string.
if ($connectionString.Trim() -eq "") 
{
  $connectionString = Get-ConnectionString -configPath (join-path $scriptPath Website\web.config) -connectionStringName NuGetGallery
}

4. Finally msbuild is invoked. Three things to note:
-  it is not building the .sln file directly but instead a custom .msbuild file is used (take a look at that for some nice usage of targets).
-  it uses the get-content command let to retrieve the value of an environment variable for the windows directory
- note the “&” at the begnning of the line? This turns PowerShell into expression mode so that the string is evaluated as an expression. First time I ever saw that.
$projFile = join-path $scriptPath Scripts\NuGetGallery.msbuild
 
& "$(get-content env:windir)\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" $projFile /p:DbConnection=$connectionString /t:FullBuild

Pretty cool stuff, source file here.

2 comments:

  1. Nice to see you still keeping tabs with us over here :) One thing interesting thing you'll find as you dig deeper into our scripts is the fact that we use env variables to hide our secrets. Since The gallery is open source we don't want to check in things like our API keys or passwords so we "sneaker net" some ps1 profiles that set up env vars for those things and then our scripts look at those. Pretty handy trick if you ask me :)

    ReplyDelete
  2. The good old sneaker net!

    Long time no see Matt, glad the devs are treating you well. :)

    ReplyDelete