【问题标题】:Execute shell command (grunt build) with FAKE (F# make)使用 FAKE (F# make) 执行 shell 命令 (grunt build)
【发布时间】:2015-09-15 16:31:11
【问题描述】:

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

特别是,我想在解决方案文件夹的子文件夹中创建一个运行 grunt 构建任务的目标。由于缺乏 F# 知识,我无法将多个参数传递给 Shell 类的静态 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 个参数,它可以工作,但在运行时找不到 grunt:

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

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

我的问题是:

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

  2. 如何在不包含完整路径的情况下运行 grunt?

【问题讨论】:

  • 我认为元组形式而不是咖喱形式 - Shell.Exec( "grunt","build","\FrontEnd")
  • 好的,这解决了第一个问题,谢谢 :) 对第二个问题有什么建议吗?

标签: f# gruntjs f#-fake


【解决方案1】:

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

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

    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
    ()
)

neftedollar 在 cmets 中提出了一个关于跨平台兼容性的好观点:使用 EnvironmentHelper 确定平台并搜索 grunt 的正确可执行文件。

【讨论】:

猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2014-10-09
相关资源
最近更新 更多