使用 FAKE (F# make) 执行 shell 命令(grunt 构建)

Execute shell command (grunt build) with FAKE (F# make)

我想使用 FAKE 自动化我的项目的构建过程,这需要我 运行 g运行t 任务。

特别是,我想在解决方案文件夹的子文件夹中创建一个 运行s g运行t 构建任务的目标。由于我缺乏 F# 知识,我无法将多个参数传递给 Shell class 的静态 Exec 方法。 https://fsharp.github.io/FAKE/apidocs/fake-processhelper-shell.html

这是我目前得到的:

Target "RunGrunt" (fun _ ->

      let errorCode = Shell.Exec "grunt" "build" "\Frontend"
      ()
 )

失败并显示以下错误消息:

build.fsx(38,23): error FS0003: This value is not a function and cannot be applied

如果我删除最后 2 个参数,它可以工作,但在 运行 时间找不到 g运行t:

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at Fake.ProcessHelper.start(Process proc) in C:\code\fake\src\app\FakeLib\ProcessHelper.fs:line 22
   at Fake.ProcessHelper.asyncShellExec@424-2.Invoke(Process _arg1) in C:\code\fake\src\app\FakeLib\ProcessHelper.fs:line 428
   at Microsoft.FSharp.Control.AsyncBuilderImpl.callA@851.Invoke(AsyncParams`1 args)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.FSharp.Control.AsyncBuilderImpl.commit[a](Result`1 res)
   at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronously[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
   at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken)
   at FSI_0001.Build.clo@34-6.Invoke(Unit _arg5) in D:\Development\Repos\FMVEAv2\Fmvea2-frontend\build.fsx:line 36
   at Fake.TargetHelper.runSingleTarget(TargetTemplate`1 target) in C:\code\fake\src\app\FakeLib\TargetHelper.fs:line 483

G运行t 包含在路径变量中。 (如果从命令行调用它会起作用)

我的问题是:

  1. 如何将多个参数传递给 Shell.Exec 方法?

  2. 如何 运行 g运行t,而不包括完整的路径?

这两个问题现在都解决了。

  1. 在评论中指出使用元组样式而不是柯里化形式导致以下代码:

    Shell.Exec("grunt","build","\FrontEnd")

  2. FAKE 提供了一种在路径上查找文件的方法。 http://fsharp.github.io/FAKE/apidocs/fake-processhelper.html

目标定义因此如下所示:

Target "RunGrunt" (fun _ ->
    let grunt = tryFindFileOnPath if isUnix then "grunt" else "grunt.cmd"

    let errorCode = match grunt with
                      | Some g -> Shell.Exec(g, "build", "FrontEnd")
                      | None -> -1
    ()
)

made a good point in the comments about cross platform compatiblity: Using the EnvironmentHelper 确定平台并搜索 grunt 的正确可执行文件。