on master help sign in
Add code for `elm init` command
commit4074a9800d57c950af18c1c86566621a491200ed authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 22 May 2018, 09:00 (8 years ago) parent6254468
5 files changed+142−1
builder/src/Reporting/Exit.hs+5 builder/src/Reporting/Exit/Init.hs+32 compiler/src/Reporting/Doc.hs+1−1 elm.cabal+2 ui/terminal/src/Init.hs+102
builder/src/Reporting/Exit.hs+5
@@ -20,6 +20,7 @@ import qualified Reporting.Exit.Diff as Diff
2020import qualified Reporting.Exit.Help as Help
2121import qualified Reporting.Exit.Http as Http
2222import qualified Reporting.Exit.Make as Make
23+import qualified Reporting.Exit.Init as Init
2324import qualified Reporting.Exit.Install as Install
2425import qualified Reporting.Exit.Publish as Publish
2526
@@ -38,6 +39,7 @@ data Exit
3839 | Deps Deps.Exit
3940 | Diff Diff.Exit
4041 | Make Make.Exit
42+ | Init Init.Exit
4143 | Install Install.Exit
4244 | Publish Publish.Exit
4345 | BadHttp String Http.Exit
@@ -103,6 +105,9 @@ toReport exit =
103105 Make makeExit ->
104106 Make.toReport makeExit
105107
108+ Init initExit ->
109+ Init.toReport initExit
110+
106111 Install installExit ->
107112 Install.toReport installExit
108113
builder/src/Reporting/Exit/Init.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Reporting.Exit.Init
3+ ( Exit(..)
4+ , toReport
5+ )
6+ where
7+
8+
9+import qualified Reporting.Doc as D
10+import qualified Reporting.Exit.Help as Help
11+
12+
13+
14+-- EXITS
15+
16+
17+data Exit
18+ = ClashingFlags
19+
20+
21+
22+-- TO REPORT
23+
24+
25+toReport :: Exit -> Help.Report
26+toReport exit =
27+ case exit of
28+ ClashingFlags ->
29+ Help.report "CLASHING FLAGS" Nothing
30+ "Trim down to zero or one flag. If you are not sure which one you want, learn more about them with:"
31+ [ D.indent 4 $ D.green $ "elm init --help"
32+ ]
compiler/src/Reporting/Doc.hs+1−1
@@ -5,7 +5,7 @@ module Reporting.Doc
55 , (P.<+>), (<>)
66 , P.align, P.cat, P.empty, P.fill, P.fillSep, P.hang
77 , P.hcat, P.hsep, P.indent, P.sep, P.vcat
8− , P.red, P.magenta, P.green, P.blue, P.black
8+ , P.red, P.cyan, P.magenta, P.green, P.blue, P.black
99 , P.dullred, P.dullcyan, P.dullyellow
1010
1111 , fromString, fromText, fromName, fromInt
elm.cabal+2
@@ -59,6 +59,7 @@ Executable elm
5959 Develop.StaticFiles
6060 Develop.StaticFiles.Build
6161 Diff
62+ Init
6263 Install
6364 Make
6465 Publish
@@ -118,6 +119,7 @@ Executable elm
118119 Reporting.Exit.Help
119120 Reporting.Exit.Http
120121 Reporting.Exit.Make
122+ Reporting.Exit.Init
121123 Reporting.Exit.Install
122124 Reporting.Exit.Publish
123125 Reporting.Progress
ui/terminal/src/Init.hsnew file+102
@@ -0,0 +1,102 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Init
3+ ( Flags(..)
4+ , run
5+ )
6+ where
7+
8+
9+import Prelude hiding (init)
10+import Control.Monad.Trans (liftIO)
11+import qualified Data.ByteString.Lazy as LBS
12+import Data.Monoid ((<>))
13+import qualified Network.HTTP.Client as Client
14+
15+import qualified Elm.Compiler.Version as Compiler
16+import qualified Elm.Package as Pkg
17+import qualified Reporting.Doc as D
18+import qualified Reporting.Exit as Exit
19+import qualified Reporting.Exit.Init as E
20+import qualified Reporting.Task as Task
21+import qualified Reporting.Task.Http as Http
22+import qualified Reporting.Progress.Terminal as Terminal
23+
24+
25+
26+-- RUN
27+
28+
29+data Flags =
30+ Flags
31+ { _embed :: Bool
32+ , _fullscreen :: Bool
33+ }
34+
35+
36+run :: () -> Flags -> IO ()
37+run () flags =
38+ do reporter <- Terminal.create
39+ Task.run reporter $
40+ do approved <- Task.getApproval question
41+ if approved
42+ then init flags
43+ else liftIO $ putStrLn "TODO"
44+
45+
46+question :: D.Doc
47+question =
48+ D.stack
49+ [ "Hello! Elm projects are pretty simple. They include:"
50+ , D.indent 2 $ D.vcat $
51+ [ "1. " <> D.green "elm.json" <> D.black " ------------ describes your dependencies"
52+ , "2. " <> D.green "src/Main.elm" <> D.black " ------ a small Elm program to expand"
53+ ]
54+ , customReflow
55+ "Now you may be wondering, what will be in these files? How do I see\
56+ \ it in the browser? How will my code grow? Do I need more\
57+ \ directories? What about tests? Etc. Check out"
58+ (D.dullyellow (D.fromString (D.makeLink "init")))
59+ "for all the answers!"
60+ , "So, would you like me to create these two files now? [Y/n]: "
61+ ]
62+
63+
64+customReflow :: String -> D.Doc -> String -> D.Doc
65+customReflow before doc after =
66+ D.fillSep $
67+ map D.fromString (words before) ++ [doc] ++ map D.fromString (words after)
68+
69+
70+
71+-- INIT
72+
73+
74+init :: Flags -> Task.Task ()
75+init flags =
76+ case flags of
77+ Flags False False -> download "sandbox"
78+ Flags True False -> download "embed"
79+ Flags False True -> download "fullscreen"
80+ Flags _ _ -> Task.throw $ Exit.Init E.ClashingFlags
81+
82+
83+download :: String -> Task.Task ()
84+download projectType =
85+ do fetch (projectType ++ "/elm.json")
86+ fetch (projectType ++ "/src/Main.elm")
87+
88+
89+fetch :: FilePath -> Task.Task ()
90+fetch path =
91+ Http.run $
92+ Http.anything
93+ ("https://experiment.elm-lang.org/" ++ vsn ++ "/init/" ++ path)
94+ (\request manager ->
95+ do response <- Client.httpLbs request manager
96+ Right <$> LBS.writeFile path (Client.responseBody response)
97+ )
98+
99+
100+vsn :: String
101+vsn =
102+ Pkg.versionToString Compiler.version