0

Every time I switch branches in Git I've got to update one line in one XML file so I can see my images (pulled from an image server) locally. I don't want a permanent solution to change the file (such as gitignore) because that file will change from time to time.

I want to run a batch that will simply alter this one line:

<add key="Images.RootUrl" value="//images.dev.foo.com:7000/" />

to

<add key="Images.RootUrl" value="//imagesperf.foo.com/" />

I'm trying to implement one of two methods I've found here on SO, but so far they elude me, partly because of all the quotes.

  1. creates new file but does not change line:

cd c:\Projects\Git\foo\Foo.Web.Store FINDSTR /I /V /B "Images.RootUrl" Web.config > config.new ECHO <add key="Images.RootUrl" value="//imagesperf.foo.com/" /> >> config.new REM DEL Web.config REM REN Web.new Web.config PAUSE

  1. pretty sure I lost the plot about halfway through:

cd c:\Projects\Git\foo\Foo.Web.Store for %%f in (Web.config) do ( for /f "tokens=1* delims=:" %%g in ('type "%%f" ^| findstr /n /v "images.dev.foo.com:7000"') do ( if "%%hx"=="x" ( echo.>>"tempWeb.config" ) else ( for /f "tokens=1* delims==" %%i in ('echo.%%h') do ( if "%%i"=="images.dev.foo.com:7000" ( echo.%%i=%%jimagesperf.foo.com>>"tempWeb.config" ) else ( if "%%jx"=="x" ( echo.%%i>>"tempWeb.config" ) else ( echo.%%i=%%j>>"tempWeb.config" ) ) ) ) ) ) REM ren Web.config oldWeb.config REM ren tempWeb.config Web.config

DaveC426913
  • 2,012
  • 6
  • 35
  • 63
  • Why not use a simpler and less invasive way of handling something like this. Since this is iis/asp.net why not use the built in ability to have different configs for different environments? http://stackoverflow.com/questions/305447/using-different-web-config-in-development-and-production-environment – Robert Moskal Jun 12 '15 at 15:43
  • If a value changes and I pull it without noticing, it will slip through the cracks and not end up in my dev config. – DaveC426913 Jun 12 '15 at 15:59
  • The transform is partial. You don't need to reproduce the entire file for each environment. – Robert Moskal Jun 12 '15 at 16:01

2 Answers2

0

Create a version of the file for the environments you work in and add _ENVNAME at the end of each file. Set .gitignore to ignore the .xml file. Then, symlink the .xml file in each environ as appropriate.

For example:

some.xml_LOCAL
some.xml_STAGING
some.xml_PRODUCTION

On local:

ln -s some.xml_LOCAL some.xml

In .gitignore

/some.xml

To be safe, you may also want to remove some.xml from git.

Finally, document this for others.

user2182349
  • 9,569
  • 3
  • 29
  • 41
0

This Batch file do what you want:

@echo off
setlocal EnableDelayedExpansion

set "replace=<add key="Images.RootUrl" value="//imagesperf.foo.com/" />"

rem Get the number of the target line minus one
for /F "delims=:" %%a in ('findstr /I /N "Images.RootUrl" Web.config') do set /A lines=%%a-1

rem Redirect the input file to read it via SET /P
< Web.config (

   rem Copy lines before the target one
   for /L %%i in (1,1,%lines%) do (
      set "line="
      set /P "line="
      echo/!line!
   )

   rem Read the target line and replace it
   set /P "line="
   echo !replace!

   rem Copy the rest of lines
   findstr "^"

rem Store the output in a temporary file
) > Web.new

move /Y Web.new Web.config

However, if the replaced line does not have to be in the same place than the original, then your first method should work; you just have two small errors:

FINDSTR /I /V "Images.RootUrl" Web.config > config.new
ECHO ^<add key="Images.RootUrl" value="//imagesperf.foo.com/" /^> >> config.new
REM DEL Web.config
REM REN Web.new Web.config
PAUSE
  • The /B switch in FINDSTR look for the string at beginning of the line!
  • The < and > character in ECHO command must be escaped with ^
Aacini
  • 65,180
  • 12
  • 72
  • 108