简体   繁体   English

Silent Fake (F# Make) 控制台应用程序

[英]Silent Fake (F# Make) Console Application

I would like to run a standalone console application with Fake targets less noisy.我想运行一个独立的控制台应用程序,假目标的噪音较小。

For example例如

open Fake.Core
open Fake.IO

let initializeContext () =
    let execContext = Context.FakeExecutionContext.Create false "build.fsx" []
    Context.setExecutionContext (Context.RuntimeContext.Fake execContext)

let runOrDefault args =
    try
        match args with
        | [| target |] -> Target.runOrDefault target
        | _ -> Target.runOrDefault "Run"
        0
    with e ->
        printfn "%A" e
        1

initializeContext()

// *** Define Targets ***
Target.create "Hello" (fun _ ->
  printfn "hello from FAKE!"
)

[<EntryPoint>]
let main args = runOrDefault args

Shell Shell

-> dotnet run -- Hello

run Hello
bla bla

The output should be "hello from FAKE.". output 应该是“来自 FAKE 的你好”。

There doesn't seem to be any knobs to turn this off.似乎没有任何旋钮可以关闭它。 For example, the "Building project with version..." message is always printed .例如, 始终打印“Building project with version...”消息

You can work around it though by changing what the standard do.net printf functions do with a no-op writer set with Console.SetOut , replacing the default trace listener in FAKE's CoreTracing , and explicitly writing to stdout for your result.您可以通过更改标准 do.net printf函数对设置为Console.SetOut的无操作编写器执行的操作、替换 FAKE 的CoreTracing中的默认跟踪侦听器并明确写入 stdout 来解决此问题。

module private ExecutionContext =
  open System
  open System.IO
  open System.Text

  module Out =

    let stdout = Console.Out

    let noop =
      { new TextWriter() with
          override _.Encoding = Encoding.UTF8
      }

    /// Supress all logging from FAKE.
    let silence () =
      CoreTracing.setTraceListeners []
      Console.SetOut noop

    let private monitor = Object ()

    /// Print something to stdout.
    let print str =
      lock monitor (fun () ->
        let prev = Console.Out
        Console.SetOut stdout
        printfn $"{str}"
        Console.SetOut prev
      )

let init () =
  Target.create "example" (fun _ ->
    ExecutionContext.Out.print "Hello.")

[<EntryPoint>]
let main argv =
  argv
  |> Array.toList
  |> Context.FakeExecutionContext.Create false "build.fsx"
  |> Context.RuntimeContext.Fake
  |> Context.setExecutionContext

  if Array.contains "--silent" argv then
    ExecutionContext.Out.silence ()

  let _ = init ()
  let ctx = Target.WithContext.runOrDefaultWithArguments "build"
  Target.updateBuildStatus ctx

  match Target.results ctx with
  | Ok () -> 0
  | Error _ -> 1

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

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