[英]modify an element in web.config
每次我在Git中切換分支時,都必須在一個XML文件中更新一行,以便可以在本地看到我的圖像(從圖像服務器中拉出)。 我不希望使用永久解決方案來更改文件(例如gitignore),因為該文件會不時更改。
我想運行一個批處理,只需更改這一行即可:
<add key="Images.RootUrl" value="//images.dev.foo.com:7000/" />
至
<add key="Images.RootUrl" value="//imagesperf.foo.com/" />
我正在嘗試實現在SO上找到的兩種方法之一,但是到目前為止,它們還是使我難以理解,部分原因是因為所有引號。
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
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
為您使用的環境創建文件的版本,並在每個文件的末尾添加_ENVNAME。 設置.gitignore以忽略.xml文件。 然后,根據需要在每個環境中對.xml文件進行符號鏈接。
例如:
some.xml_LOCAL
some.xml_STAGING
some.xml_PRODUCTION
在本地:
ln -s some.xml_LOCAL some.xml
在.gitignore中
/some.xml
為了安全起見,您可能還需要從git中刪除some.xml。
最后,將其記錄下來供他人使用。
該批處理文件可以執行以下操作:
@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
但是,如果被替換的行不必與原始行位於同一位置,則您的第一種方法應該可以工作; 您只有兩個小錯誤:
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
<
和>
字符必須用^
轉義^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.