forked from lamdera/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompile.hs
More file actions
175 lines (147 loc) · 5.5 KB
/
Compile.hs
File metadata and controls
175 lines (147 loc) · 5.5 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
{-# LANGUAGE OverloadedStrings #-}
module Lamdera.Compile where
import qualified System.Directory as Dir
import qualified System.FilePath as FP
import Make (Flags(..))
import qualified Make
import Control.Concurrent.Async
import qualified Data.Text as T
import Lamdera
import qualified Ext.Common
-- Runs `lamdera make --optimize` of given path with no output
makeOptimized :: FilePath -> FilePath -> IO ()
makeOptimized root path = do
makeOptimizedWithCleanup (pure ()) root path
-- Runs `lamdera make --optimize` of given files with no output, followed by the cleanup IO
makeOptimizedWithCleanup :: IO () -> FilePath -> FilePath -> IO ()
makeOptimizedWithCleanup cleanup root path = do
debug $ "🏗 makeOptimizedWithCleanup: lamdera make --optimize " <> root <> "/" <> path
let
tmp = lamderaCache root <> "/tmp.js"
scaffold = lamderaCache root <> "/Main_.elm"
writeUtf8 scaffold $ "module Main_ exposing (..)\n\nimport " <> (T.pack $ FP.takeFileName $ FP.dropExtensions path) <> "\nimport Html\n\nmain = Html.text \"\""
r <- async $
Ext.Common.withProjectRoot root $
Make.run_cleanup cleanup [scaffold] $
Make.Flags
{ _debug = False
, _optimize = True
-- We don't use Make.DevNull as it does not actually compile to JS,
-- thus we never get warnings about Debug.* usage which we want.
, _output = Just (Make.JS tmp)
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
remove tmp
remove scaffold
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10
-- Runs `lamdera make` with no JS output
make_ :: FilePath -> IO ()
make_ root = do
debug $ "🏗 make_: lamdera make " <> root <> "/"
r <- async $
Ext.Common.withProjectRoot root $
Make.run [] $
Make.Flags
{ _debug = False
, _optimize = True
, _output = Just Make.DevNull
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = True
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10
-- Runs `lamdera make` of given files with no JS file output
makeDev :: FilePath -> [FilePath] -> IO ()
makeDev root paths = do
debug $ "🏗 makeDev: lamdera make " <> root <> "/" <> show paths
absRoot <- Dir.makeAbsolute root
r <- async $
Ext.Common.withProjectRoot absRoot $ do
mapM touch paths
Make.run paths $
Make.Flags
{ _debug = True
, _optimize = False
, _output = Just Make.DevNull
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10
makeDev_ :: FilePath -> IO ()
makeDev_ path =
makeDev (FP.takeDirectory path) [path]
makeDevHtml :: FilePath -> [FilePath] -> IO ()
makeDevHtml root paths = do
debug $ "🏗 makeDevHtml: lamdera make " <> root <> "/"
r <- async $
Ext.Common.withProjectRoot root $ do
Make.run paths $
Make.Flags
{ _debug = True
, _optimize = False
, _output = Nothing
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10
-- Runs `lamdera make` of harness file with JS file output
makeHarnessDevJs :: FilePath -> IO ()
makeHarnessDevJs root = do
let
tmp = lamderaCache root <> "/tmp.js"
scaffold = lamderaCache root <> "/Main_.elm"
debug $ "🏗 makeHarnessDevJs: lamdera make " <> scaffold
writeUtf8 scaffold "module Main_ exposing (..)\n\nimport Frontend\nimport Backend\nimport Types\nimport Html\n\nmain = Html.text \"\""
r <- async $
Ext.Common.withProjectRoot root $
Make.run [scaffold] $
Make.Flags
{ _debug = True
, _optimize = False
, _output = Just (Make.JS tmp)
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
remove tmp
remove scaffold
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10