如何参数化和重复构建步骤

时间:2015-03-08 15:40:04

标签: f# f#-fake

我正在写一个基本上会执行以下操作的FAKE脚本: -

1. Clean build outputs
2. Modify a configuration file with a specific value.
3. Perform a build.
4. Upload the outputs somewhere.

我写了所有的个人任务。我现在需要做的是设置一组FAKE构建步骤以基本上重复上述步骤,对于每个配置值,例如我们假设配置文件有一个属性“color”。我想重复上述四个构建步骤,并在步骤2中使用其中一个值[ "black"; "blue"; "red"; "white" ]

实现这一目标的最佳方法是什么?我是否应该制作一个大型构建任务,在一个例子中执行所有这些操作。 for loop(似乎错了)?或创建多个构建步骤,例如“将配置设置为蓝色”和“将配置设置为红色”等,并重复每种颜色的整个构建流程(再次,似乎错误)?

5 个答案:

答案 0 :(得分:2)

文档记录不是很好,但您可以通过TargetTemplateWithDependecies

以编程方式创建目标

我会添加一些文档。

答案 1 :(得分:1)

你可以这样做

#r "tools/FAKE/tools/FakeLib.dll"
open Fake 


Target "Clean" (fun _ ->
    trace "Cleaning stuff..."
)

let config color = ignore()

Target "ConfigBlack" (fun _ ->
    config "black"
)

Target "ConfigRed" (fun _ ->
    config "red"
)

Target "Build" (fun _ ->
    trace "Build solution"
)

Target "Upload" (fun _ ->
    trace "Upload artifacts"
)


"Clean"
   =?> ("ConfigBlack",hasBuildParam "black")
   =?> ("ConfigRed",hasBuildParam "red")
   ==> "Build"
   ==> "Upload"

Run "Upload"

之后,您就可以像build Upload blackbuild Upload red

一样来电

答案 2 :(得分:1)

下面的脚本有效,但使用了我认为你不应该使用的ExecutedTargets

#I "tools/FAKE/tools"
#r "FakeLib.dll"

open Fake

let mutable value = "Foo"

Target "Clean" (fun _ ->
    trace "clean target"
)

Target "Modify config file" (fun _ ->
    trace (sprintf "===========> Modify config file: '%s'" value)
)

Target "Perform build" (fun _ ->
    trace "Perform build"
)

Target "Default" (fun _ -> ())

"Clean"
    ==> "Modify config file"
    ==> "Perform build"
    ==> "Default"

["black"; "blue"; "red"; "white"]
|> List.iter(fun v ->
    value <- v
    Run <| getBuildParamOrDefault "target" "Default"
    ExecutedTargets.Clear()
)

答案 3 :(得分:0)

您可以使用各种配置吗?例如:

BlueBuild:

[configsection] 
  [Color]Blue[/Color]
[/configsection]

RedBuild:

[configsection] 
  [Color]Red[/Color]
[/configsection]

请参阅以下文章: How to select different app.config for several build configurations

http://www.hanselman.com/blog/managingmultipleconfigurationfileenvironmentswithprebuildevents.aspx

答案 4 :(得分:0)

我这样做的方法是基本上编写一个生成目标的辅助函数 - 就像模板依赖一样 - 但也生成“常见”目标的多个实例;每一个都根据变量元素的名称而不同,例如

let targets = 
  [ "Blue: Clean"
    "Blue: Modify Config"
    "Blue: Perform Build"
    "Blue: Upload outputs"
    "Red: Clean"
    "Red: Modify Config"
    "Red: Perform Build"
    "Red: Upload outputs"
    "Yellow: Clean"
    "Yellow: Modify Config"
    "Yellow: Perform Build"
    "Yellow: Upload outputs" ]

然后,您可以轻松地构建一个顺序的依赖链,例如

targets |> List.reduce (==>)

虽然这不是我所希望的,但它对我来说效果还不错,让我看透假的事情。