on master help sign in
Add code for `elm init` command
4074a98Evan Czaplicki
builder/src/Reporting/Exit.hs+5
@@ -20,6 +20,7 @@ import qualified Reporting.Exit.Diff as Diff
20import qualified Reporting.Exit.Help as Help
20import qualified Reporting.Exit.Help as Help
21import qualified Reporting.Exit.Http as Http
21import qualified Reporting.Exit.Http as Http
22import qualified Reporting.Exit.Make as Make
22import qualified Reporting.Exit.Make as Make
23+import qualified Reporting.Exit.Init as Init
23import qualified Reporting.Exit.Install as Install
24import qualified Reporting.Exit.Install as Install
24import qualified Reporting.Exit.Publish as Publish
25import qualified Reporting.Exit.Publish as Publish
25
26
@@ -38,6 +39,7 @@ data Exit
38 | Deps Deps.Exit
39 | Deps Deps.Exit
39 | Diff Diff.Exit
40 | Diff Diff.Exit
40 | Make Make.Exit
41 | Make Make.Exit
42+ | Init Init.Exit
41 | Install Install.Exit
43 | Install Install.Exit
42 | Publish Publish.Exit
44 | Publish Publish.Exit
43 | BadHttp String Http.Exit
45 | BadHttp String Http.Exit
@@ -103,6 +105,9 @@ toReport exit =
103 Make makeExit ->
105 Make makeExit ->
104 Make.toReport makeExit
106 Make.toReport makeExit
105
107
108+ Init initExit ->
109+ Init.toReport initExit
110+
106 Install installExit ->
111 Install installExit ->
107 Install.toReport installExit
112 Install.toReport installExit
108
113
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
5 , (P.<+>), (<>)
5 , (P.<+>), (<>)
6 , P.align, P.cat, P.empty, P.fill, P.fillSep, P.hang
6 , P.align, P.cat, P.empty, P.fill, P.fillSep, P.hang
7 , P.hcat, P.hsep, P.indent, P.sep, P.vcat
7 , 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
9 , P.dullred, P.dullcyan, P.dullyellow
9 , P.dullred, P.dullcyan, P.dullyellow
10
10
11 , fromString, fromText, fromName, fromInt
11 , fromString, fromText, fromName, fromInt
elm.cabal+2
@@ -59,6 +59,7 @@ Executable elm
59 Develop.StaticFiles
59 Develop.StaticFiles
60 Develop.StaticFiles.Build
60 Develop.StaticFiles.Build
61 Diff
61 Diff
62+ Init
62 Install
63 Install
63 Make
64 Make
64 Publish
65 Publish
@@ -118,6 +119,7 @@ Executable elm
118 Reporting.Exit.Help
119 Reporting.Exit.Help
119 Reporting.Exit.Http
120 Reporting.Exit.Http
120 Reporting.Exit.Make
121 Reporting.Exit.Make
122+ Reporting.Exit.Init
121 Reporting.Exit.Install
123 Reporting.Exit.Install
122 Reporting.Exit.Publish
124 Reporting.Exit.Publish
123 Reporting.Progress
125 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