@@ -1,25 +1,26 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
9import Prelude hiding (init)
8import Prelude hiding (init)
10import Control.Monad.Trans (liftIO)
9import Control.Monad.Trans (liftIO)
11−import qualified Data.ByteString.Lazy as LBS
10+import qualified Data.Map as Map
12−import Data.Monoid ((<>))
13−import qualified Network.HTTP.Client as Client
14import qualified System.Directory as Dir
11import qualified System.Directory as Dir
13+import qualified Deps.Cache as Cache
14+import qualified Deps.Explorer as Explorer
15+import qualified Deps.Solver as Solver
16import qualified Elm.Compiler.Version as Compiler
16import qualified Elm.Compiler.Version as Compiler
17import qualified Elm.Package as Pkg
17import qualified Elm.Package as Pkg
18+import qualified Elm.Project.Constraint as Con
19+import qualified Elm.Project.Json as Project
18import qualified Reporting.Doc as D
20import qualified Reporting.Doc as D
19import qualified Reporting.Exit as Exit
21import qualified Reporting.Exit as Exit
20import qualified Reporting.Exit.Init as E
22import qualified Reporting.Exit.Init as E
21import qualified Reporting.Task as Task
23import qualified Reporting.Task as Task
22−import qualified Reporting.Task.Http as Http
23import qualified Reporting.Progress.Terminal as Terminal
24import qualified Reporting.Progress.Terminal as Terminal
@@ -27,81 +28,73 @@ import qualified Reporting.Progress.Terminal as Terminal
37−run :: () -> Flags -> IO ()
31+run :: () -> () -> IO ()
38−run () flags =
32+run () () =
39 do reporter <- Terminal.create
33 do reporter <- Terminal.create
34+ exists <- Dir.doesFileExist "elm.json"
40 Task.run reporter $
35 Task.run reporter $
37+ Task.throw (Exit.Init E.AlreadyStarted)
41− do approved <- Task.getApproval question
39+ do approved <- Task.getApproval question
42− if approved
40+ if approved
44− do init flags
42+ do init
45− liftIO $ putStrLn "Okay, I created them!"
43+ liftIO $ putStrLn "Okay, I created it. Now read that link!"
47− liftIO $ putStrLn "Okay, I left everything the same!"
45+ liftIO $ putStrLn "Okay, I did not make any changes!"
50question :: D.Doc
48question :: D.Doc
53− [ "Hello! Elm projects are pretty simple. They include:"
51+ [ D.fillSep
54− , D.indent 2 $ D.vcat $
52+ ["Hello!"
55− [ "1. " <> D.green "elm.json" <> D.black " ------------ describes your dependencies"
53+ ,"Elm","projects","always","start","with","an",D.green "elm.json","file."
56− , "2. " <> D.green "src/Main.elm" <> D.black " ------ a small Elm program to expand"
54+ ,"I","can","create","them!"
58− , customReflow
56+ , D.reflow
59− "Now you may be wondering, what will be in these files? How do I see\
57+ "Now you may be wondering, what will be in this file? How do I add Elm files to\
60− \ it in the browser? How will my code grow? Do I need more\
58+ \ my project? How do I see it in the browser? How will my code grow? Do I need\
61− \ directories? What about tests? Etc. Check out"
59+ \ more directories? What about tests? Etc."
62− (D.dullyellow (D.fromString (D.makeLink "init")))
61+ ["Check","out",D.cyan (D.fromString (D.makeLink "init"))
63− "for all the answers!"
62+ ,"for","all","the","answers!"
64− , "So, would you like me to create these two files now? [Y/n]: "
64+ , "Knowing all that, would you like me to create an elm.json file now? [Y/n]: "
68−customReflow :: String -> D.Doc -> String -> D.Doc
69−customReflow before doc after =
71− map D.fromString (words before) ++ [doc] ++ map D.fromString (words after)
78−init :: Flags -> Task.Task ()
72+init :: Task.Task ()
80− case flags of
74+ do registry <- Cache.optionalUpdate
81− Flags False False -> download "sandbox"
82− Flags True False -> download "element"
83− Flags False True -> download "document"
84− Flags _ _ -> Task.throw $ Exit.Init E.ClashingFlags
87−download :: String -> Task.Task ()
76+ maybeSolution <-
88−download projectType =
77+ Explorer.run registry $ Solver.run $ Solver.solve defaults
89− do fetch projectType "elm.json" $ return ()
90− fetch projectType "src/Main.elm" $ Dir.createDirectoryIfMissing True "src"
79+ case maybeSolution of
82+ directs = Map.intersection solution defaults
83+ indirects = Map.difference solution defaults
86+ do Dir.createDirectoryIfMissing True "src"
87+ Project.write "." $ Project.App $
88+ Project.AppInfo Compiler.version ["src"] directs indirects Map.empty Map.empty
93−fetch :: String -> FilePath -> IO () -> Task.Task ()
90+ Nothing ->
94−fetch projectType path setup =
91+ Task.throw (Exit.Init (E.NoSolution (Map.keys defaults)))
97− ("https://experiment.elm-lang.org/" ++ vsn ++ "/init/" ++ projectType ++ "/" ++ path)
99− do response <- Client.httpLbs request manager
101− Right <$> LBS.writeFile path (Client.responseBody response)
105−vsn :: String
94+defaults :: Map.Map Pkg.Name Con.Constraint
107− Pkg.versionToString Compiler.version
96+ Map.fromList
97+ [ (Pkg.core, Con.anything)
98+ , (Pkg.browser, Con.anything)
99+ , (Pkg.html, Con.anything)