forked from Zaid-Ajaj/Npgsql.FSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
148 lines (116 loc) · 3.83 KB
/
build.fsx
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
#r @"packages/build/FAKE/tools/FakeLib.dll"
open System
open System.IO
open Fake
let libPath = "./src"
let testsPath = "./tests"
let databaseDockerContainerName = "npgsql_fsharp_db"
let databaseDockerImageName = "postgres"
let databasePassword = "postgres"
let platformTool tool winTool =
let tool = if isUnix then tool else winTool
tool
|> ProcessHelper.tryFindFileOnPath
|> function Some t -> t | _ -> failwithf "%s not found" tool
let dockerExePath = "docker"
let mutable dotnetCli = "dotnet"
let run cmd args workingDir =
let result =
ExecProcess (fun info ->
info.FileName <- cmd
info.WorkingDirectory <- workingDir
info.Arguments <- args) TimeSpan.MaxValue
if result <> 0 then failwithf "'%s %s' failed" cmd args
let delete file =
if File.Exists(file)
then DeleteFile file
else ()
type DockerDbStatus =
| NeverRan
| Running of string
| Stopped
| DockerNotInstalled
let runDockerResults args =
let result =
ExecProcessAndReturnMessages(fun info ->
info.FileName <- dockerExePath
info.Arguments <- args) TimeSpan.MaxValue
result
let runDocker args =
let result =
ExecProcess (fun info ->
info.FileName <- dockerExePath
info.Arguments <- args) TimeSpan.MaxValue
if result <> 0 then failwithf "docker %s failed" args
Target "Clean" <| fun _ ->
[ testsPath </> "bin"
testsPath </> "obj"
libPath </> "bin"
libPath </> "obj" ]
|> CleanDirs
Target "StartDatabase" (fun _ ->
let processResult (r : ProcessResult) =
match r.OK, r.Messages.Count with
| true, x when x = 2 ->
match (r.Messages.Item 1) with
| m when m.Contains("Exit") -> Stopped
| m when m.Contains("Up") -> Running m
| _ -> failwith (sprintf "Unable to process result from docker. Message don't contain Exit or up : %s" (r.Messages.Item 1))
| true, x when x = 1 -> NeverRan
| _ -> DockerNotInstalled
sprintf "ps -a --filter name=%s" databaseDockerContainerName
|> runDockerResults
|> processResult
|> function
| NeverRan ->
sprintf "run --name %s -e POSTGRES_PASSWORD=%s -p 5432:5432 -d %s" databaseDockerContainerName databasePassword databaseDockerImageName
|> runDocker
| Stopped ->
sprintf "start %s" databaseDockerContainerName
|> runDocker
| Running s ->
printf "Database already running : %s" s
| DockerNotInstalled ->
failwith "Docker not installed"
)
Target "StopDatabase" (fun _ ->
sprintf "stop %s" databaseDockerContainerName
|> runDocker
)
Target "RestoreLibProject" <| fun _ ->
run dotnetCli "restore" libPath
Target "RestoreTestProject" <| fun _ ->
run dotnetCli "restore" testsPath
let publish projectPath = fun () ->
[ projectPath </> "bin"
projectPath </> "obj" ] |> CleanDirs
run dotnetCli "restore --no-cache" projectPath
run dotnetCli "pack -c Release" projectPath
let nugetKey =
match environVarOrNone "NUGET_KEY" with
| Some nugetKey -> nugetKey
| None ->
printfn "The Nuget API key must be set in a NUGET_KEY environmental variable"
System.Console.Write("Nuget API Key: ")
System.Console.ReadLine()
let nupkg =
Directory.GetFiles(projectPath </> "bin" </> "Release")
|> Seq.head
|> Path.GetFullPath
let pushCmd = sprintf "nuget push %s -s nuget.org -k %s" nupkg nugetKey
run dotnetCli pushCmd projectPath
Target "PublishNuget" (publish libPath)
Target "Build" <| fun _ -> run "dotnet" "build" libPath
Target "Test" <| fun _ ->
run "dotnet" "run" testsPath
"Clean"
==> "RestoreTestProject"
==> "Test"
"Clean"
==> "RestoreLibProject"
==> "Build"
"Clean"
==> "RestoreLibProject"
==> "Build"
==> "PublishNuget"
RunTargetOrDefault "Build"