forked from elm/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall.hs
425 lines (322 loc) · 12.3 KB
/
Install.hs
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
{-# LANGUAGE OverloadedStrings #-}
module Install
( Args(..)
, run
)
where
import Data.Map ((!))
import qualified Data.Map as Map
import qualified Data.Map.Merge.Strict as Map
import qualified BackgroundWriter as BW
import qualified Deps.Solver as Solver
import qualified Deps.Registry as Registry
import qualified Elm.Constraint as C
import qualified Elm.Details as Details
import qualified Elm.Package as Pkg
import qualified Elm.Outline as Outline
import qualified Elm.Version as V
import qualified Reporting
import Reporting.Doc ((<>), (<+>))
import qualified Reporting.Doc as D
import qualified Reporting.Exit as Exit
import qualified Reporting.Task as Task
import qualified Stuff
-- RUN
data Args
= NoArgs
| Install Pkg.Name
run :: Args -> () -> IO ()
run args () =
Reporting.attempt Exit.installToReport $
do maybeRoot <- Stuff.findRoot
case maybeRoot of
Nothing ->
return (Left Exit.InstallNoOutline)
Just root ->
case args of
NoArgs ->
do elmHome <- Stuff.getElmHome
return (Left (Exit.InstallNoArgs elmHome))
Install pkg ->
Task.run $
do env <- Task.eio Exit.InstallBadRegistry $ Solver.initEnv
oldOutline <- Task.eio Exit.InstallBadOutline $ Outline.read root
case oldOutline of
Outline.App outline ->
do changes <- makeAppPlan env pkg outline
attemptChanges root env oldOutline V.toChars changes
Outline.Pkg outline ->
do changes <- makePkgPlan env pkg outline
attemptChanges root env oldOutline C.toChars changes
-- ATTEMPT CHANGES
data Changes vsn
= AlreadyInstalled
| PromoteTest Outline.Outline
| PromoteIndirect Outline.Outline
| Changes (Map.Map Pkg.Name (Change vsn)) Outline.Outline
type Task = Task.Task Exit.Install
attemptChanges :: FilePath -> Solver.Env -> Outline.Outline -> (a -> String) -> Changes a -> Task ()
attemptChanges root env oldOutline toChars changes =
case changes of
AlreadyInstalled ->
Task.io $ putStrLn "It is already installed!"
PromoteIndirect newOutline ->
attemptChangesHelp root env oldOutline newOutline $
D.vcat
[ D.fillSep
["I","found","it","in","your","elm.json","file,"
,"but","in","the",D.dullyellow "\"indirect\"","dependencies."
]
, D.fillSep
["Should","I","move","it","into",D.green "\"direct\""
,"dependencies","for","more","general","use?","[Y/n]: "
]
]
PromoteTest newOutline ->
attemptChangesHelp root env oldOutline newOutline $
D.vcat
[ D.fillSep
["I","found","it","in","your","elm.json","file,"
,"but","in","the",D.dullyellow "\"test-dependencies\"","field."
]
, D.fillSep
["Should","I","move","it","into",D.green "\"dependencies\""
,"for","more","general","use?","[Y/n]: "
]
]
Changes changeDict newOutline ->
let
widths = Map.foldrWithKey (widen toChars) (Widths 0 0 0) changeDict
changeDocs = Map.foldrWithKey (addChange toChars widths) (Docs [] [] []) changeDict
in
attemptChangesHelp root env oldOutline newOutline $ D.vcat $
[ "Here is my plan:"
, viewChangeDocs changeDocs
, ""
, "Would you like me to update your elm.json accordingly? [Y/n]: "
]
attemptChangesHelp :: FilePath -> Solver.Env -> Outline.Outline -> Outline.Outline -> D.Doc -> Task ()
attemptChangesHelp root env oldOutline newOutline question =
Task.eio Exit.InstallBadDetails $
BW.withScope $ \scope ->
do approved <- Reporting.ask question
if approved
then
do Outline.write root newOutline
result <- Details.verifyInstall scope root env newOutline
case result of
Left exit ->
do Outline.write root oldOutline
return (Left exit)
Right () ->
do putStrLn "Success!"
return (Right ())
else
do putStrLn "Okay, I did not change anything!"
return (Right ())
-- MAKE APP PLAN
makeAppPlan :: Solver.Env -> Pkg.Name -> Outline.AppOutline -> Task (Changes V.Version)
makeAppPlan (Solver.Env cache _ connection registry) pkg outline@(Outline.AppOutline _ _ direct indirect testDirect testIndirect) =
if Map.member pkg direct then
return AlreadyInstalled
else
-- is it already indirect?
case Map.lookup pkg indirect of
Just vsn ->
return $ PromoteIndirect $ Outline.App $
outline
{ Outline._app_deps_direct = Map.insert pkg vsn direct
, Outline._app_deps_indirect = Map.delete pkg indirect
}
Nothing ->
-- is it already a test dependency?
case Map.lookup pkg testDirect of
Just vsn ->
return $ PromoteTest $ Outline.App $
outline
{ Outline._app_deps_direct = Map.insert pkg vsn direct
, Outline._app_test_direct = Map.delete pkg testDirect
}
Nothing ->
-- is it already an indirect test dependency?
case Map.lookup pkg testIndirect of
Just vsn ->
return $ PromoteTest $ Outline.App $
outline
{ Outline._app_deps_direct = Map.insert pkg vsn direct
, Outline._app_test_indirect = Map.delete pkg testIndirect
}
Nothing ->
-- finally try to add it from scratch
case Registry.getVersions' pkg registry of
Left suggestions ->
case connection of
Solver.Online _ -> Task.throw (Exit.InstallUnknownPackageOnline pkg suggestions)
Solver.Offline -> Task.throw (Exit.InstallUnknownPackageOffline pkg suggestions)
Right _ ->
do result <- Task.io $ Solver.addToApp cache connection registry pkg outline
case result of
Solver.Ok (Solver.AppSolution old new app) ->
return (Changes (detectChanges old new) (Outline.App app))
Solver.NoSolution ->
Task.throw (Exit.InstallNoOnlineAppSolution pkg)
Solver.NoOfflineSolution ->
Task.throw (Exit.InstallNoOfflineAppSolution pkg)
Solver.Err exit ->
Task.throw (Exit.InstallHadSolverTrouble exit)
-- MAKE PACKAGE PLAN
makePkgPlan :: Solver.Env -> Pkg.Name -> Outline.PkgOutline -> Task (Changes C.Constraint)
makePkgPlan (Solver.Env cache _ connection registry) pkg outline@(Outline.PkgOutline _ _ _ _ _ deps test _) =
if Map.member pkg deps then
return AlreadyInstalled
else
-- is already in test dependencies?
case Map.lookup pkg test of
Just con ->
return $ PromoteTest $ Outline.Pkg $
outline
{ Outline._pkg_deps = Map.insert pkg con deps
, Outline._pkg_test_deps = Map.delete pkg test
}
Nothing ->
-- try to add a new dependency
case Registry.getVersions' pkg registry of
Left suggestions ->
case connection of
Solver.Online _ -> Task.throw (Exit.InstallUnknownPackageOnline pkg suggestions)
Solver.Offline -> Task.throw (Exit.InstallUnknownPackageOffline pkg suggestions)
Right (Registry.KnownVersions _ _) ->
do let old = Map.union deps test
let cons = Map.insert pkg C.anything old
result <- Task.io $ Solver.verify cache connection registry cons
case result of
Solver.Ok solution ->
let
(Solver.Details vsn _) = solution ! pkg
con = C.untilNextMajor vsn
new = Map.insert pkg con old
changes = detectChanges old new
news = Map.mapMaybe keepNew changes
in
return $ Changes changes $ Outline.Pkg $
outline
{ Outline._pkg_deps = addNews (Just pkg) news deps
, Outline._pkg_test_deps = addNews Nothing news test
}
Solver.NoSolution ->
Task.throw (Exit.InstallNoOnlinePkgSolution pkg)
Solver.NoOfflineSolution ->
Task.throw (Exit.InstallNoOfflinePkgSolution pkg)
Solver.Err exit ->
Task.throw (Exit.InstallHadSolverTrouble exit)
addNews :: Maybe Pkg.Name -> Map.Map Pkg.Name C.Constraint -> Map.Map Pkg.Name C.Constraint -> Map.Map Pkg.Name C.Constraint
addNews pkg new old =
Map.merge
Map.preserveMissing
(Map.mapMaybeMissing (\k c -> if Just k == pkg then Just c else Nothing))
(Map.zipWithMatched (\_ _ n -> n))
old
new
-- CHANGES
data Change a
= Insert a
| Change a a
| Remove a
detectChanges :: (Eq a) => Map.Map Pkg.Name a -> Map.Map Pkg.Name a -> Map.Map Pkg.Name (Change a)
detectChanges old new =
Map.merge
(Map.mapMissing (\_ v -> Remove v))
(Map.mapMissing (\_ v -> Insert v))
(Map.zipWithMaybeMatched keepChange)
old
new
keepChange :: (Eq v) => k -> v -> v -> Maybe (Change v)
keepChange _ old new =
if old == new then
Nothing
else
Just (Change old new)
keepNew :: Change a -> Maybe a
keepNew change =
case change of
Insert a ->
Just a
Change _ a ->
Just a
Remove _ ->
Nothing
-- VIEW CHANGE DOCS
data ChangeDocs =
Docs
{ _doc_inserts :: [D.Doc]
, _doc_changes :: [D.Doc]
, _doc_removes :: [D.Doc]
}
viewChangeDocs :: ChangeDocs -> D.Doc
viewChangeDocs (Docs inserts changes removes) =
D.indent 2 $ D.vcat $ concat $
[ viewNonZero "Add:" inserts
, viewNonZero "Change:" changes
, viewNonZero "Remove:" removes
]
viewNonZero :: String -> [D.Doc] -> [D.Doc]
viewNonZero title entries =
if null entries then
[]
else
[ ""
, D.fromChars title
, D.indent 2 (D.vcat entries)
]
-- VIEW CHANGE
addChange :: (a -> String) -> Widths -> Pkg.Name -> Change a -> ChangeDocs -> ChangeDocs
addChange toChars widths name change (Docs inserts changes removes) =
case change of
Insert new ->
Docs (viewInsert toChars widths name new : inserts) changes removes
Change old new ->
Docs inserts (viewChange toChars widths name old new : changes) removes
Remove old ->
Docs inserts changes (viewRemove toChars widths name old : removes)
viewInsert :: (a -> String) -> Widths -> Pkg.Name -> a -> D.Doc
viewInsert toChars (Widths nameWidth leftWidth _) name new =
viewName nameWidth name <+> pad leftWidth (toChars new)
viewChange :: (a -> String) -> Widths -> Pkg.Name -> a -> a -> D.Doc
viewChange toChars (Widths nameWidth leftWidth rightWidth) name old new =
D.hsep
[ viewName nameWidth name
, pad leftWidth (toChars old)
, "=>"
, pad rightWidth (toChars new)
]
viewRemove :: (a -> String) -> Widths -> Pkg.Name -> a -> D.Doc
viewRemove toChars (Widths nameWidth leftWidth _) name old =
viewName nameWidth name <+> pad leftWidth (toChars old)
viewName :: Int -> Pkg.Name -> D.Doc
viewName width name =
D.fill (width + 3) (D.fromPackage name)
pad :: Int -> String -> D.Doc
pad width string =
D.fromChars (replicate (width - length string) ' ') <> D.fromChars string
-- WIDTHS
data Widths =
Widths
{ _name :: !Int
, _left :: !Int
, _right :: !Int
}
widen :: (a -> String) -> Pkg.Name -> Change a -> Widths -> Widths
widen toChars pkg change (Widths name left right) =
let
toLength a =
length (toChars a)
newName =
max name (length (Pkg.toChars pkg))
in
case change of
Insert new ->
Widths newName (max left (toLength new)) right
Change old new ->
Widths newName (max left (toLength old)) (max right (toLength new))
Remove old ->
Widths newName (max left (toLength old)) right