<commit_before>﻿[<AutoOpen>]
module Fake.ProcessHelper

open System
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading

let mutable redirectOutputToTrace = false

/// Runs the given process
/// returns the exit code
let execProcessAndReturnExitCode infoAction =
  use p = new Process()
  p.StartInfo.UseShellExecute <- false
  infoAction p.StartInfo
  p.StartInfo.RedirectStandardOutput <- true
  p.StartInfo.RedirectStandardError <- true

  p.ErrorDataReceived.Add(fun d -> if d.Data <> null then traceError d.Data)
  p.OutputDataReceived.Add(fun d -> if d.Data <> null then trace d.Data)
  try
    p.Start() |> ignore
  with
  | :? Win32Exception -> failwithf "Could not execute %s" p.StartInfo.FileName
  
  p.BeginErrorReadLine()
  p.BeginOutputReadLine()     
  
  p.WaitForExit()
    
  p.ExitCode

/// Runs the given process
/// returns if the exit code was 0
let execProcess3 infoAction = execProcessAndReturnExitCode infoAction = 0    

/// Runs the given process
/// returns the exit code
let execProcess2 infoAction silent =
  use p = new Process()
  p.StartInfo.UseShellExecute <- false
  infoAction p.StartInfo
  if silent then
    p.StartInfo.RedirectStandardError <- true
  try
    p.Start() |> ignore
  with
  | :? Win32Exception -> failwithf "Could not execute %s" p.StartInfo.FileName
  let error =
    if silent then
      p.StandardError.ReadToEnd()
    else
      String.Empty
    
  p.WaitForExit()
  if silent && p.ExitCode <> 0 then
    System.Diagnostics.Trace.WriteLine(error)
    
  p.ExitCode  

/// Runs the given process
/// returns the exit code
let ExecProcess infoAction  =
  execProcess2 infoAction redirectOutputToTrace
  
/// sets the environment Settings for the given startInfo
/// existing values will be overrriden
let setEnvironmentVariables (startInfo:ProcessStartInfo) environmentSettings = 
  for key,value in environmentSettings do
    if startInfo.EnvironmentVariables.ContainsKey key then
      startInfo.EnvironmentVariables.[key] <- value
    else
      startInfo.EnvironmentVariables.Add(key, value)
          
/// Runs the given process
/// returns true if the exit code was 0
let execProcess infoAction = ExecProcess infoAction = 0    

/// Adds quotes around the string   
let toParam x = " \"" + x + "\" " 
 
/// Use default Parameters
let UseDefaults = id
<commit_msg>Using findPath() for MSBuildHelper an d FSIHelper.<commit_after>﻿[<AutoOpen>]
module Fake.ProcessHelper

open System
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading

let mutable redirectOutputToTrace = false

/// Runs the given process
/// returns the exit code
let execProcessAndReturnExitCode infoAction =
  use p = new Process()
  p.StartInfo.UseShellExecute <- false
  infoAction p.StartInfo
  p.StartInfo.RedirectStandardOutput <- true
  p.StartInfo.RedirectStandardError <- true

  p.ErrorDataReceived.Add(fun d -> if d.Data <> null then traceError d.Data)
  p.OutputDataReceived.Add(fun d -> if d.Data <> null then trace d.Data)
  try
    p.Start() |> ignore
  with
  | :? Win32Exception -> failwithf "Could not execute %s" p.StartInfo.FileName
  
  p.BeginErrorReadLine()
  p.BeginOutputReadLine()     
  
  p.WaitForExit()
    
  p.ExitCode

/// Runs the given process
/// returns if the exit code was 0
let execProcess3 infoAction = execProcessAndReturnExitCode infoAction = 0    

/// Runs the given process
/// returns the exit code
let execProcess2 infoAction silent =
  use p = new Process()
  p.StartInfo.UseShellExecute <- false
  infoAction p.StartInfo
  if silent then
    p.StartInfo.RedirectStandardError <- true
  try
    p.Start() |> ignore
  with
  | exn -> failwithf "Could not execute %s. %s" p.StartInfo.FileName exn.Message

  let error = if silent then p.StandardError.ReadToEnd() else String.Empty
    
  p.WaitForExit()
  if silent && p.ExitCode <> 0 then
    System.Diagnostics.Trace.WriteLine error
    
  p.ExitCode  

/// Runs the given process
/// returns the exit code
let ExecProcess infoAction = execProcess2 infoAction redirectOutputToTrace
  
/// sets the environment Settings for the given startInfo
/// existing values will be overrriden
let setEnvironmentVariables (startInfo:ProcessStartInfo) environmentSettings = 
  for key,value in environmentSettings do
    if startInfo.EnvironmentVariables.ContainsKey key then
      startInfo.EnvironmentVariables.[key] <- value
    else
      startInfo.EnvironmentVariables.Add(key, value)
          
/// Runs the given process
/// returns true if the exit code was 0
let execProcess infoAction = ExecProcess infoAction = 0    

/// Adds quotes around the string   
let toParam x = " \"" + x + "\" " 
 
/// Use default Parameters
let UseDefaults = id

{'fraction_non_alphanumeric': 0.08530405405405406, 'fraction_numerical': 0.0046452702702702705, 'mean_word_length': 3.329981718464351, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930393', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1357, 'n_words': 445}