how to modify source with NAnt?

╄→гoц情女王★ 提交于 2019-12-21 10:21:47

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/487451/how-to-modify-source-with-nant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!