我使用蛋糕 0.22.0。
取决于传入 build.cake
的参数, 我要加载不同的 .cake
文件。例如,如果参数 VisualStudioVersion
具有值 2013
, 我要加载 vs2013dlls.cake
文件;如果它具有值 2015
,然后我想加载 vs2015dlls.cake
文件;等等。
看着 the CAKE page on preprocessor directives ,我没有看到有关预处理器关键字的任何信息,例如 #if
和 #else
.
我宁愿不复制其他 .cake
的内容文件到 build.cake
, 以免 build.cake
变得过于臃肿。
我如何加载 .cake
有条件的文件?
最佳答案
目前在脚本加载时不支持条件,如果 def 支持,0.23.0 将添加,但预处理器指令被处理为相同的级别/优先级,因此对您的特定问题没有帮助。
不过,您可以做的是创建一个小的 bootstrap 蛋糕脚本,该脚本可以为您的特定场景提供所需的片段。
使用 CakeExecuteExpression 的示例
var visualStudioVersion = Argument("VisualStudioVersion", "2017");
var statements = new List<string>();
var currentDir = MakeAbsolute(Directory("./"));
statements.Add("#load \"{0}/common.cake\"");
switch(visualStudioVersion)
{
case "2013":
statements.Add("#load \"{0}/vs2013.cake\"");
break;
case "2017":
statements.Add("#load \"{0}/vs2017.cake\"");
break;
default:
throw new Exception(string.Format("Unknown VisualStudioVersion: {0}", visualStudioVersion));
}
var expression = string.Format(
string.Join(
"\r\n",
statements
),
currentDir
);
CakeExecuteExpression(
expression
);
对于上述,如果参数 VisualStudioVersion 设置为 2017 或未指定参数,则它将加载
如果参数 VisualStudioVersion 设置为 2013 则它将加载
使用 CakeExecuteScript 的示例
也许不那么复杂的是只提供不同的入口点,即有一个
build.cake
文件调用 vs2013.cake
或 vs2017.cake
取决于论点。普通蛋糕
Information("This will execute regardless version!") ;
vs2013.cake
#load "common.cake"
Information("Hello VS2013!");
vs2017.cake
#load "common.cake"
Information("Hello VS2017!");
构建蛋糕
var visualStudioVersion = Argument("VisualStudioVersion", "2017");
switch(visualStudioVersion)
{
case "2013":
CakeExecuteScript("vs2013.cake");
break;
case "2017":
CakeExecuteScript("vs2017.cake");
break;
default:
throw new Exception(string.Format("Unknown VisualStudioVersion: {0}", visualStudioVersion));
}
2017年产量
cake .\executescript.cake
会输出
This will execute regardless version!
Hello VS2017!
2013年产量
cake .\executescript.cake --VisualStudioVersion=2013
会输出
This will execute regardless version!
Hello VS2013!
关于cakebuild - 有条件地加载 .cake 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648455/