3

I'm currently working on a re-platforming project, part of which involves a few dozen websites, all that use a common header. Several of these sites all lived on the same machine so a symbolic link was setup so they could all reference the same content. Certain other sites were external and so a common "includes.company.com" was setup and utilized.

Fast forward to today and I'm attempting to setup up the "includes.company.com" site in Visual Studio. The content is simple enough. I've boiled it down to one HTML file and one CSS file along with all the necessary images.

What I'm trying to accomplish now is based on current build configuration, to output the HTML and CSS with proper links that can be referenced from external sites.

I currently have a post-build event that looks like so...

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "$(MSBuildProjectDirectory)\pbuild.ps1" "$(Configuration)" $(MSBuildProjectDirectory)

where pbuild.ps1 looks like

param ([string]$config, [string]$target)

If ($config -eq "Debug")
{
    (get-content $target\common\css\stylesheet.min.css) -replace '{SERVER}', 'http://dev.includes.company.com' | set-content $target\Builds\$config\common\css\stylesheet.min.css
    (get-content $target\header.html) -replace '{SERVER}', 'http://dev.includes.company.com' | set-content $target\Builds\$config\header.html
}

If ($config -eq "QA")
{
    (get-content $target\common\css\stylesheet.min.css) -replace '{SERVER}', 'http://qa.includes.company.com' | set-content $target\Builds\$config\common\css\stylesheet.min.css
    (get-content $target\header.html) -replace '{SERVER}', 'http://qa.includes.company.com' | set-content $target\Builds\$config\header.html
}

If ($config -eq "Release")
{
    (get-content $target\common\css\stylesheet.min.css) -replace '{SERVER}', 'http://includes.company.com' | set-content  $target\Builds\$config\common\css\stylesheet.min.css
    (get-content $target\header.html) -replace '{SERVER}', 'http://includes.company.com' | set-content $target\Builds\$config\header.html
}

This works great for Debug, and Release, but not QA. As I've come to find out, the variable $Configuration seems to only ever contain 'Debug' or 'Release' and not the actual Active Configuration I'm looking for.

FlavorToBuild seemed promising but I'm having a hell of a time getting that pulled out into a variable that I can use.

Is there a variable that is setup to give me what I'm looking for, the name of the active build configuration? Is there a way to assign a variable in the project file that I can then feed into my Post-Build event? This is my first real foray into this world so any help or guidance on how to achieve this goal is highly appreciated.

Khepri
  • 9,547
  • 5
  • 45
  • 61
  • Shouldn't it be "$(ConfigurationName)" instead of "$(Configuration). – David Brabant Mar 27 '12 at 06:59
  • 1. [AfterBuild](http://msdn.microsoft.com/en-us/library/ms366724.aspx) target is way better than postbuild event. 2. Your script violates the [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself) rule – KMoraz Mar 27 '12 at 13:12
  • Both $(Configuration) and $(ConfigurationName) seem to give the same thing. – Khepri Mar 27 '12 at 14:17

1 Answers1

0

I seem to have solved this by adding a property or two to the project file for each build.

I ended up with something like the following:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "$(MSBuildProjectDirectory)\pbuild.ps1" $(PowershellBuildEnv) $(MSBuildProjectDirectory) $(TargetSite)

with pbuild.ps1 now stripped down to:

param ([string]$config, [string]$target, [string]$replaceWith)

(get-content $target\common\css\stylesheet.min.css) -replace '{SERVER}', $replaceWith | set-content $target\Builds\$config\common\css\stylesheet.min.css
(get-content $target\header.html) -replace '{SERVER}', $replaceWith | set-content $target\Builds\$config\header.html

In the project file I defined both $ReplaceWith and $PowershellBuildEnv as I saw fit for each build configuration:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PowershellBuildEnv>Debug</PowershellBuildEnv>
    <TargetSite>http://dev.includes.company.com</TargetSite>

So, seems to work just like I'm looking for at this point. Definitely been a learning experience.

Khepri
  • 9,547
  • 5
  • 45
  • 61