我可以在csharp的编译时env变量中做一个常数吗? [英] Can I make a constant from a compile-time env variable in csharp?
问题描述
我们使用 Hudson 构建我们的项目,Hudson方便地将环境变量定义为%BUILD_NUMBER%编译时间。
We use Hudson to build our projects, and Hudson conveniently defines environment variables like "%BUILD_NUMBER%" at compile time.
我想在代码中使用该变量,因此我们可以在运行时做一些事情,如日志。然而,我不能做System.Environment.GetEnvironmentVariable,因为正在访问运行时环境,我想要的是:
I'd like to use that variable in code, so we can do things like log what build this is at run time. However I CAN NOT do System.Environment.GetEnvironmentVariable because that is accessing the run-time environment, what I want is something like:
#define BUILD_NUM = %BUILD_NUMBER%
或
const string BUILD_NUM = %BUILD_NUMBER%
除了我不知道语法。有人可以指出我正确的方向吗?谢谢!
Except I don't know the syntax. Can someone please point me in the right direction? Thanks!
推荐答案
好的,这是我做的事情。它不是很优雅,但它的作品。我创建了一个预构建步骤,如下所示:
Okay here's what I wound up doing. It's not very elegant, but it works. I created a pre-build step that looks like this:
echo namespace Some.Namespace > "$(ProjectDir)\CiInfo.cs"
echo { >> "$(ProjectDir)\CiInfo.cs"
echo ///^<summary^>Info about the continuous integration server build that produced this binary.^</summary^> >> "$(ProjectDir)\CiInfo.cs"
echo public static class CiInfo >> "$(ProjectDir)\CiInfo.cs"
echo { >> "$(ProjectDir)\CiInfo.cs"
echo ///^<summary^>The current build number, such as "153"^</summary^> >> "$(ProjectDir)\CiInfo.cs"
echo public const string BuildNumber = ("%BUILD_NUMBER%" == "" ? @"Unknown" : "%BUILD_NUMBER%"); >> "$(ProjectDir)\CiInfo.cs"
echo ///^<summary^>String of the build number and build date/time, and other useful info.^</summary^> >> "$(ProjectDir)\CiInfo.cs"
echo public const string BuildTag = ("%BUILD_TAG%" == "" ? @"nohudson" : "%BUILD_TAG%") + " built: %DATE%-%TIME%"; >> "$(ProjectDir)\CiInfo.cs"
echo } >> "$(ProjectDir)\CiInfo.cs"
echo } >> "$(ProjectDir)\CiInfo.cs"
然后我将CiInfo.cs添加到项目,但忽略了版本控制。这样我就不必编辑它或者提交它了,而且项目总是有一个可用的,是最新的版本号和时间。
Then I added "CiInfo.cs" to the project, but ignored it from version control. That way I never have to edit it or commit it, and the project always has a constant available that is the latest build number and time.
这篇关于我可以在csharp的编译时env变量中做一个常数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!