简体   繁体   English

如何将此MSBuild脚本翻译为F#/ FAKE?

[英]How to translate this MSBuild script to F#/FAKE?

What is the translation of the following MSBuild script into F#/FAKE? 以下MSBuild脚本到F#/ FAKE的翻译是什么?

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup>
        <CurrentMode>None</CurrentMode>
    </PropertyGroup>

    <Target Name="Compile_A">
        <Message Text="Hello from Compile_A" />

        <CallTarget Targets="SetToA"/>
        <CallTarget Targets="IntermediateStage"/>
    </Target>

    <Target Name="Compile_B">
        <Message Text="Hello from Compile_B" />

        <CallTarget Targets="SetToB"/>
        <CallTarget Targets="IntermediateStage"/>
    </Target>

    <Target Name="IntermediateStage">
        <Message Text="Hello from IntermediateStage" />

        <CallTarget Targets="Compile"/>
    </Target>

    <Target Name="Compile">
        <Message Text="Hello from Compile. I'm using $(CurrentMode)" />
    </Target>

    <!-- The following Targets are only necessary due to a MSBuild bug (CallTarget and CreateProperty cannot be combined) -->
    <Target Name="SetToA">
        <CreateProperty Value="A">
            <Output TaskParameter="Value" PropertyName="CurrentMode" />
        </CreateProperty>
    </Target>

    <Target Name="SetToB">
        <CreateProperty Value="B">
            <Output TaskParameter="Value" PropertyName="CurrentMode" />
        </CreateProperty>
    </Target>
</Project>

My main goal is to set a property in the topmost targets with different values ( CurrentMode is either A or B ) and consume it in the deepest target ( Compile ). 我的主要目标是在最顶层的目标中设置一个具有不同值的属性( CurrentModeAB )并在最深的目标( Compile )中使用它。

The best answer will probably differ based on what is your actual scenario. 根据您的实际情况,最佳答案可能会有所不同。 In particular, you might not even need separate targets for the different steps of your process if you do not ever plan to run them separately (in which case, just using a function that takes the mode as a parameter and invoking that from CompileA and CompileB would work fine). 特别是,如果您不打算单独运行它们,则可能甚至不需要针对流程的不同步骤使用单独的目标(在这种情况下,只使用将模式作为参数并从CompileACompileB调用该模式的函数)会工作得很好)。

However, if you want to keep separate targets for all the steps, you could do something like this: 但是,如果要为所有步骤保留单独的目标,则可以执行以下操作:

#load ".fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.Core.TargetOperators

let mutable CurrentMode = "None"

Target.create "SetA" (fun _ ->
  CurrentMode <- "A"
)

Target.create "SetB" (fun _ ->
  CurrentMode <- "B"
)

Target.create "IntermediateStage" (fun _ ->
  printfn "In the intermediate stage"
)

Target.create "Compile" (fun _ ->
  printfn "Compiling using mode %s" CurrentMode
)

Target.create "CompileA" ignore
Target.create "CompileB" ignore

"SetA" ==> "CompileA"
"SetB" ==> "CompileB"
"IntermediateStage" ==> "Compile" ==> "CompileA"
"IntermediateStage" ==> "Compile" ==> "CompileB"
"SetA" ?=> "IntermediateStage"
"SetB" ?=> "IntermediateStage"

Target.runOrDefault "CompileA"

This uses mutable variable CurrentMode that is set by SetA or SetB targets (arguably, not very functional, but it captures what you're doing). 这使用由SetASetB目标设置的可变变量CurrentMode (可以说,它不是很有用,但它可以捕获你正在做的事情)。

The dependencies between targets are specified using ==> . 使用==>指定目标之间的依赖关系。 Note that SetA has to happen before CompileA (and similar for B) and IntermediateStage needs to go before Compile which is pre-requisite for both kinds of compiles. 请注意, SetA必须在CompileA (和B类似)之前发生,而IntermediateStage需要在Compile之前进行,这是两种编译的先决条件。

There is one subtle trick - you don't want to say that SetA and SetB are required for IntermediateStep , because then FAKE would run both in non-deterministic order. 有一个微妙的技巧 - 你不想说IntermediateStep需要SetASetB ,因为那时FAKE将以非确定性顺序运行。 the ?=> opreator lets you specify soft dependencies which say that if both IntermediateStep and SetA are to be executed, then SetA has to go first - so the last two lines are not adding dependencies, but making ordering explicit. ?=> opreator允许您指定软依赖关系 ,这表示如果要执行IntermediateStepSetA ,则SetA必须先行 - 所以最后两行不是添加依赖关系,而是使排序显式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM