on 253bb13 help sign in
elk/terminal/src/Init.hs
103 lines 3.1 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Init
3 ( run
4 )
5 where
8import Prelude hiding (init)
9import qualified Data.Map as Map
10import qualified System.Directory as Dir
12import qualified Deps.Solver as Solver
13import qualified Elm.Constraint as Con
14import qualified Elm.Outline as Outline
15import qualified Elm.Package as Pkg
16import qualified Elm.Version as V
17import qualified Reporting
18import qualified Reporting.Doc as D
19import qualified Reporting.Exit as Exit
23-- RUN
26run :: () -> () -> IO ()
27run () () =
28 Reporting.attempt Exit.initToReport $
29 do exists <- Dir.doesFileExist "elm.json"
30 if exists
31 then return (Left Exit.InitAlreadyExists)
32 else
33 do approved <- Reporting.ask question
34 if approved
35 then init
36 else
37 do putStrLn "Okay, I did not make any changes!"
38 return (Right ())
41question :: D.Doc
42question =
43 D.stack
44 [ D.fillSep
45 ["Hello!"
46 ,"Elm","projects","always","start","with","an",D.green "elm.json","file."
47 ,"I","can","create","them!"
48 ]
49 , D.reflow
50 "Now you may be wondering, what will be in this file? How do I add Elm files to\
51 \ my project? How do I see it in the browser? How will my code grow? Do I need\
52 \ more directories? What about tests? Etc."
53 , D.fillSep
54 ["Check","out",D.cyan (D.fromChars (D.makeLink "init"))
55 ,"for","all","the","answers!"
56 ]
57 , "Knowing all that, would you like me to create an elm.json file now? [Y/n]: "
58 ]
62-- INIT
65init :: IO (Either Exit.Init ())
66init =
67 do eitherEnv <- Solver.initEnv
68 case eitherEnv of
69 Left problem ->
70 return (Left (Exit.InitRegistryProblem problem))
72 Right (Solver.Env cache _ connection registry) ->
73 do result <- Solver.verify cache connection registry defaults
74 case result of
75 Solver.Err exit ->
76 return (Left (Exit.InitSolverProblem exit))
78 Solver.NoSolution ->
79 return (Left (Exit.InitNoSolution (Map.keys defaults)))
81 Solver.NoOfflineSolution ->
82 return (Left (Exit.InitNoOfflineSolution (Map.keys defaults)))
84 Solver.Ok details ->
85 let
86 solution = Map.map (\(Solver.Details vsn _) -> vsn) details
87 directs = Map.intersection solution defaults
88 indirects = Map.difference solution defaults
89 in
90 do Dir.createDirectoryIfMissing True "src"
91 Outline.write "." $ Outline.App $
92 Outline.AppOutline V.compiler ["src"] directs indirects Map.empty Map.empty
93 putStrLn "Okay, I created it. Now read that link!"
94 return (Right ())
97defaults :: Map.Map Pkg.Name Con.Constraint
98defaults =
99 Map.fromList
100 [ (Pkg.core, Con.anything)
101 , (Pkg.browser, Con.anything)
102 , (Pkg.html, Con.anything)
103 ]