forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptlib.fsx
More file actions
202 lines (161 loc) · 7.45 KB
/
scriptlib.fsx
File metadata and controls
202 lines (161 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
//
// Scripting utilities
//=========================================================================================
namespace global
open System
open System.IO
open System.Diagnostics
[<AutoOpen>]
module Scripting =
let isNullOrEmpty s = String.IsNullOrEmpty s
let executeProcess filename arguments =
let processWriteMessage (chan:TextWriter) (message:string) =
if message <> null then
chan.WriteLine(message)
printfn "%s %s" filename arguments
let info = ProcessStartInfo(Arguments=arguments, UseShellExecute=false,
RedirectStandardOutput=true, RedirectStandardError=true,
CreateNoWindow=true, FileName=filename)
let p = new Process(StartInfo=info)
p.OutputDataReceived.Add(fun x -> processWriteMessage stdout x.Data)
p.ErrorDataReceived.Add(fun x -> processWriteMessage stderr x.Data)
if p.Start() then
p.BeginOutputReadLine()
p.BeginErrorReadLine()
p.WaitForExit()
p.ExitCode
else
0
#if INTERACTIVE
let argv = Microsoft.FSharp.Compiler.Interactive.Settings.fsi.CommandLineArgs |> Seq.skip 1 |> Seq.toArray
let getCmdLineArgOptional (switchName: string) =
argv |> Array.filter(fun t -> t.StartsWith(switchName)) |> Array.map(fun t -> t.Remove(0, switchName.Length).Trim()) |> Array.tryHead
let getCmdLineArg switchName defaultValue =
match getCmdLineArgOptional switchName with
| Some file -> file
| _ -> defaultValue
let getCmdLineArgReqd switchName =
match getCmdLineArg switchName null with
| null -> failwith (sprintf "The argument %s is required" switchName)
| x -> x
let getCmdLineExtraArgs isSwitch = argv |> Array.skipWhile isSwitch
#endif
let makeDirectory output =
if not (Directory.Exists(output)) then
Directory.CreateDirectory(output) |> ignore
let (++) a b = Path.Combine(a,b)
let getBasename a = Path.GetFileNameWithoutExtension a
let getFullPath a = Path.GetFullPath a
let getFilename a = Path.GetFileName a
let getDirectoryName a = Path.GetDirectoryName a
let copyFile source dir =
let dest =
if not (Directory.Exists dir) then Directory.CreateDirectory dir |>ignore
let result = Path.Combine(dir, Path.GetFileName source)
result
//printfn "Copy %s --> %s" source dest
File.Copy(source, dest, true)
let deleteDirectory output =
if Directory.Exists output then
Directory.Delete(output, true)
let log format = printfn format
type FilePath = string
type CmdResult =
| Success
| ErrorLevel of string * int
type CmdArguments =
{ RedirectOutput : (string -> unit) option
RedirectError : (string -> unit) option
RedirectInput : (StreamWriter -> unit) option }
module Process =
let processExePath baseDir exe =
if Path.IsPathRooted(exe) then exe
else
match Path.GetDirectoryName(exe) with
| "" -> exe
| _ -> Path.Combine(baseDir,exe) |> Path.GetFullPath
let exec cmdArgs (workDir: FilePath) envs (path: FilePath) arguments =
let exePath = path |> processExePath workDir
let processInfo = new ProcessStartInfo(exePath, arguments)
processInfo.CreateNoWindow <- true
processInfo.UseShellExecute <- false
processInfo.WorkingDirectory <- workDir
#if NETSTANDARD1_6
ignore envs // work out what to do about this
#else
envs
|> Map.iter (fun k v -> processInfo.EnvironmentVariables.[k] <- v)
#endif
let p = new Process()
p.EnableRaisingEvents <- true
p.StartInfo <- processInfo
cmdArgs.RedirectOutput|> Option.iter (fun f ->
processInfo.RedirectStandardOutput <- true
p.OutputDataReceived.Add (fun ea -> if ea.Data <> null then f ea.Data)
)
cmdArgs.RedirectError |> Option.iter (fun f ->
processInfo.RedirectStandardError <- true
p.ErrorDataReceived.Add (fun ea -> if ea.Data <> null then f ea.Data)
)
cmdArgs.RedirectInput
|> Option.iter (fun _ -> p.StartInfo.RedirectStandardInput <- true)
p.Start() |> ignore
cmdArgs.RedirectOutput |> Option.iter (fun _ -> p.BeginOutputReadLine())
cmdArgs.RedirectError |> Option.iter (fun _ -> p.BeginErrorReadLine())
cmdArgs.RedirectInput |> Option.iter (fun input ->
async {
let inputWriter = p.StandardInput
do! inputWriter.FlushAsync () |> Async.AwaitIAsyncResult |> Async.Ignore
input inputWriter
do! inputWriter.FlushAsync () |> Async.AwaitIAsyncResult |> Async.Ignore
inputWriter.Dispose ()
}
|> Async.Start)
p.WaitForExit()
match p.ExitCode with
| 0 -> Success
| err ->
let msg = sprintf "Error running command '%s' with args '%s' in directory '%s'" exePath arguments workDir
ErrorLevel (msg, err)
type OutPipe (writer: TextWriter) =
member x.Post (msg:string) = lock writer (fun () -> writer.WriteLine(msg))
interface System.IDisposable with
member __.Dispose() = writer.Flush()
let redirectTo (writer: TextWriter) = new OutPipe (writer)
let redirectToLog () = redirectTo System.Console.Out
#if !FSHARP_SUITE_DRIVES_CORECLR_TESTS
let defaultPlatform =
match Environment.OSVersion.Platform, Environment.Is64BitOperatingSystem with
| PlatformID.MacOSX, true -> "osx.10.11-x64"
| PlatformID.Unix,true -> "ubuntu.14.04-x64"
| _, true -> "win7-x64"
| _, false -> "win7-x86"
#endif
let executeProcessNoRedirect filename arguments =
let info = ProcessStartInfo(Arguments=arguments, UseShellExecute=false,
RedirectStandardOutput=true, RedirectStandardError=true,RedirectStandardInput=true,
CreateNoWindow=true, FileName=filename)
let p = new Process(StartInfo=info)
if p.Start() then
async { try
let buffer = Array.zeroCreate 4096
while not p.StandardOutput.EndOfStream do
let n = p.StandardOutput.Read(buffer, 0, buffer.Length)
if n > 0 then System.Console.Out.Write(buffer, 0, n)
with _ -> () } |> Async.Start
async { try
let buffer = Array.zeroCreate 4096
while not p.StandardError.EndOfStream do
let n = p.StandardError.Read(buffer, 0, buffer.Length)
if n > 0 then System.Console.Error.Write(buffer, 0, n)
with _ -> () } |> Async.Start
async { try
while true do
let c = System.Console.In.ReadLine()
p.StandardInput.WriteLine(c)
with _ -> () } |> Async.Start
p.WaitForExit()
p.ExitCode
else
0