如何使用 NAnt 修改源代码?

发布于 2024-07-13 00:53:11 字数 188 浏览 3 评论 0原文

我想在构建解决方案之前使用 NAnt 修改 .h 文件中的字符串。

.h 文件中有一个宏:#define SERVER_ADDRESS "www.customserver.net",我想在部署软件之前修改该字符串,以便通过在命令行中传递地址来为自定义地址进行每个构建。

有谁知道如何做到这一点?

谢谢!

I would like to modify the string in a .h file with NAnt before building the solution.

There is a macro in .h file: #define SERVER_ADDRESS "www.customserver.net" and I would like to modify the string before deploying software so each build could be made for custom address by passing the address in command line.

Does anyone know how this could be done?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-07-20 00:53:11

人们可以使用 loadfile 任务来帮助解决此问题。 此任务将给定文件加载到属性中。 真正有用的是当您应用带有 filterchain 时href="http://nant.sourceforge.net/release/latest/help/filters/replacetokens.html" rel="noreferrer">replacetokens 替换文件的某些区域。 例如,如果要定义一个类似于模板的头文件,如下所示:

#ifndef MyMacros_h
#define MyMacros_h

#define SERVER_ADDRESS "@SERVER_ADDRESS_TOKEN@"

#endif

可以使用 loadfile 任务将 @SERVER_ADDRESS_TOKEN@ 替换为任何字符串,然后使用 echo 任务来实际写入真实头文件退出。

<loadfile file="MyMacrosTemplate.h" property="theMacrosFileContents">
    <filterchain>
        <replacetokens>
            <token key="SERVER_ADDRESS_TOKEN" value="www.customerserver.net" />
        </replacetokens>
    </filterchain>
</loadfile>
<echo file="MyMacros.h" message="${theMacrosFileContents}" />

这将生成一个 MyMacros.h 文件,其中包含修改后的 SERVER_ADDRESS 字符串。

One could use the loadfile task to help with this. This task loads the given file into a property. What is really useful is when you apply a filterchain with replacetokens to replace certain areas of the file. For example if one were to define a template-like header file that looked something like this:

#ifndef MyMacros_h
#define MyMacros_h

#define SERVER_ADDRESS "@SERVER_ADDRESS_TOKEN@"

#endif

One could the use the loadfile task to replace the @SERVER_ADDRESS_TOKEN@ with any string, and then use the echo task to actually write the real header file back out.

<loadfile file="MyMacrosTemplate.h" property="theMacrosFileContents">
    <filterchain>
        <replacetokens>
            <token key="SERVER_ADDRESS_TOKEN" value="www.customerserver.net" />
        </replacetokens>
    </filterchain>
</loadfile>
<echo file="MyMacros.h" message="${theMacrosFileContents}" />

This will generate a MyMacros.h file with the modified string for the SERVER_ADDRESS.

北方的韩爷 2024-07-20 00:53:11

我认为这不是使用 NAnt 的正确方法。 我不想那样修改文件内容。 我不相信这是可能的。

也许您可以为每种情况使用不同的文件,并根据输入参数指定它的路径。

就我个人而言,我认为这样的字符串不应该被硬编码到应用程序中。 如果它们要更改,最好将它们外部化到启动时读取的配置或属性文件中。 这样您就可以更改它们,而无需更改源或重新编译。

I don't think that's the proper way to be using NAnt. I wouldn't want to modify file contents that way. I don't believe it's possible.

Perhaps you can have a different file for each case and specify the path to it depending on an input parameter.

Personally, I think strings like that should not be hard-coded into the app. If they're going to change, better to externalize them into configuration or property files that are read on startup. That way you can change them without having to change source or recompile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文