1

I have post build events set up already for copying files for me dependent on ConfigurationName and want to be able to set an "environment variable" in a config (js) file on client side of an angular application to allow debug info to be visible or not dependent upon environment running application in.

To this end I've created a powershell script (ReplaceText.ps1):

function replace-file-content([String] $path, [String] $replace, [String] $replaceWith)
{
(Get-Content $path) | Foreach-Object {$_ -replace $replace,$replaceWith} | Out-File $path
}

and added this line to the post build event of my web project.

if "$(ConfigurationName)"=="LIVE" (powershell -File "$(ProjectDir)Tools\ReplaceText.ps1" "$(ProjectDir)app\application.config.js" "DEBUG" "LIVE")

which I was hoping to change the word "DEBUG" to "LIVE" when built against LIVE build configuration in my application.config.js file which contains this line:

$provide.constant('currentEnv', 'DEBUG');

Build succeeds but no changes occur on my file. Can anyone identify where I'm going wrong?

I do know I could do this sort of stuff with Gulp or another task runner BTW, but was trying to do it without bringing in another dependency and just use VS post build events & PS. :)

Cheers

CheGuevarasBeret
  • 1,364
  • 2
  • 14
  • 33

1 Answers1

2

Your PS code only defines a function but there's nothing that invokes it.

Use param as the first statement in the script to convert command line into the script's parameters:

param([String] $path, [String] $replace, [String] $replaceWith)

(Get-Content -literalPath $path -raw) -replace $replace, $replaceWith |
    Out-File $path -encoding UTF8
  • -literalPath - correctly handles paths with [] symbols otherwise interpreted as a wildcard;
  • -raw - reads the entire file as one string for speedup, available since PowerShell 3.
wOxxOm
  • 65,848
  • 11
  • 132
  • 136