forked from evelinag/eliza-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
132 lines (106 loc) · 4.79 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
// --------------------------------------------------------------------------------------
// FAKE build script
// --------------------------------------------------------------------------------------
#r "packages/FAKE/tools/FakeLib.dll"
#r "packages/FSharp.Compiler.Service/lib/net45/FSharp.Compiler.Service.dll"
#r "packages/Suave/lib/net40/Suave.dll"
open Fake
open System
open System.IO
open FSharp.Data
open Suave
open Suave.Operators
open Suave.Web
open Microsoft.FSharp.Compiler.Interactive.Shell
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
// --------------------------------------------------------------------------------------
// For deployed run - compile as an executable
// --------------------------------------------------------------------------------------
Target "clean" (fun _ ->
CleanDirs ["bin"]
)
Target "build" (fun _ ->
[ "eliza.sln" ]
|> MSBuildRelease "" "Rebuild"
|> Log ""
)
"clean" ==> "build"
// --------------------------------------------------------------------------------------
// For local run - automatically reloads scripts
// --------------------------------------------------------------------------------------
Target "run" (fun _ ->
let sbOut = new Text.StringBuilder()
let sbErr = new Text.StringBuilder()
let fsiSession =
let inStream = new StringReader("")
let outStream = new StringWriter(sbOut)
let errStream = new StringWriter(sbErr)
let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
let argv = Array.append [|"/fake/fsi.exe"; "--quiet"; "--noninteractive" |] [||]
FsiEvaluationSession.Create(fsiConfig, argv, inStream, outStream, errStream)
let reportFsiError (e:exn) =
traceError "Reloading app.fsx script failed."
traceError (sprintf "Message: %s\nError: %s" e.Message (sbErr.ToString().Trim()))
sbErr.Clear() |> ignore
let reloadScript () =
try
traceImportant "Reloading 'app.fsx' script..."
fsiSession.EvalInteraction(sprintf "#load @\"%s\"" (__SOURCE_DIRECTORY__ </> "src" </> "app.fsx"))
match fsiSession.EvalExpression("App.app") with
| Some app -> Some(app.ReflectionValue :?> WebPart)
| None -> failwith "Couldn't get 'app' value."
with e -> reportFsiError e; None
let getLocalServerConfig port =
{ defaultConfig with
homeFolder = Some __SOURCE_DIRECTORY__
logger = Logging.Loggers.saneDefaultsFor Logging.LogLevel.Debug
bindings = [ HttpBinding.mkSimple HTTP "127.0.0.1" port ] }
let mutable currentApp : WebPart = Successful.OK "Loading..."
let reloadAppServer (changedFiles: string seq) =
reloadScript () |> Option.iter (fun app ->
currentApp <- app
traceImportant "Refreshed server." )
let port = 8899
let app =
Writers.setHeader "Cache-Control" "no-cache, no-store, must-revalidate"
>=> Writers.setHeader "Pragma" "no-cache"
>=> Writers.setHeader "Expires" "0"
>=> fun ctx -> currentApp ctx
let _, server = startWebServerAsync (getLocalServerConfig port) app
// Start Suave to host it on localhost
let sources = { BaseDirectory = __SOURCE_DIRECTORY__ </> "src"; Includes = [ "*.fs*" ]; Excludes = [] }
reloadAppServer sources
Async.Start(server)
// Watch for changes & reload when server.fsx changes
let watcher = sources |> WatchChanges (Seq.map (fun x -> x.FullPath) >> reloadAppServer)
traceImportant "Waiting for app.fsx edits. Press any key to stop."
System.Diagnostics.Process.Start("http://localhost:8899/") |> ignore
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
)
// --------------------------------------------------------------------------------------
// Azure - deploy copies the binary to wwwroot/bin
// --------------------------------------------------------------------------------------
let newName prefix f =
Seq.initInfinite (sprintf "%s_%d" prefix) |> Seq.skipWhile (f >> not) |> Seq.head
Target "deploy" (fun _ ->
// Pick a subfolder that does not exist
let wwwroot = "../wwwroot"
let subdir = newName "deploy" (fun sub -> not (Directory.Exists(wwwroot </> sub)))
// Deploy everything into new empty folder
let deployroot = wwwroot </> subdir
CleanDir deployroot
CleanDir (deployroot </> "bin")
CleanDir (deployroot </> "web")
CleanDir (deployroot </> "data")
CopyRecursive "bin" (deployroot </> "bin") false |> ignore
CopyRecursive "web" (deployroot </> "web") false |> ignore
CopyRecursive "data" (deployroot </> "data") false |> ignore
let config = File.ReadAllText("web.config").Replace("%DEPLOY_SUBDIRECTORY%", subdir)
File.WriteAllText(wwwroot </> "web.config", config)
// Try to delete previous folders, but ignore failures
for dir in Directory.GetDirectories(wwwroot) do
if Path.GetFileName(dir) <> subdir then
try CleanDir dir; DeleteDir dir with _ -> ()
)
"build" ==> "deploy"
RunTargetOrDefault "run"