我需要使用NAnt来更新.js文件中的一行内容。这一行将是类似于:
global.ServerPath = 'http://server-path/';
我需要一种方法来更新该行中“server-path”部分为目标服务器的路径。
ReplaceString不好用,因为我不知道在更新时文件中的路径是什么。
有什么帮助吗?
提前感谢。
如果 string::replace
无法正常工作,<regex>
可以胜任。这就是它:
<?xml version="1.0" encoding="utf-8" ?>
<project name="replace.line" default="replace">
<target name="replace" descripton="replaces a line">
<property
name="js.file"
value="C:\foo.js" />
<loadfile file="${js.file}" property="js.file.content" />
<regex
input="${js.file.content}"
pattern="(?'BEFORE'.*)global\.ServerPath\s*=\s*'[^']*';(?'AFTER'.*)" />
<echo
file="${js.file}"
message="${BEFORE}global.ServerPath = 'http://bla/';${AFTER}"
append="false" />
</target>
</project>
(?'BEFORE'[\w\s\W]*)
代替(?'BEFORE'.*)
(对于AFTER匹配组也是如此)对我有用。我在想这是否与编码有关。 - Gerbus为了能够捕获所有行,AFTER和BEFORE中不应该使用.*,而应该使用[\w\s\W]*。
在我的情况下,.*只能捕获一行,而[\w\s\W]*可以适用于整个文件。
还可以使用复制任务与filterchain和替换令牌过滤器。
以下是一个示例:
<token key="WebConfig.EnvironmentName" value="${env_webconfig_EnvironmentName}" />
<token key="WebConfig.SMTPServerName" value="${env_webconfig_SMTPServerName}" />
<token key="WebConfig.DatabaseConnectionString" value="${env_drmportal_webconfig_DatabaseConnectionString}" />
</replacetokens>
</filterchain>
</copy>
我将所有的模板文件都存储在 /config/ 文件夹中(例如,web.config.template),我的复制任务使用时会在复制到相同的 /config/ 文件夹中替换值,但不包括 ".template" 文件扩展名。然后我会做必要的处理...
我承认,在这种方式中使用属性确实有点繁琐,但是您可以根据环境(例如,本地、暂存、生产等)加载不同的属性值集合,这样您就具有了灵活性,但我认为这可能不是您所要求的。
string::replace
无法工作,<regex>
可以完成任务。我需要解决这个问题。请继续关注... - The Chairman