on master help sign in
Revamp Main to use new Terminal.Args implementation
commit2ca3cd939a420dbf78a1697c39f2c8d0c8e93517 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 26 Mar 2018, 18:18 (9 years ago) parente76d040
1 file changed+198−82
ui/terminal/src/Main.hs+198−82
ui/terminal/src/Main.hs+198−82
@@ -1,16 +1,22 @@
1−{-# OPTIONS_GHC -Wall #-}
2−module Main where
1+{-# LANGUAGE OverloadedStrings #-}
2+module Main
3+ ( main
4+ )
5+ where
6+
37
4−import qualified System.Directory as Dir
5−import System.Exit (exitWith)
6−import qualified System.IO as IO
7−import qualified System.Process as Proc
88import GHC.IO.Encoding (setLocaleEncoding, utf8)
9+import qualified Text.PrettyPrint.ANSI.Leijen as P
10+import Text.PrettyPrint.ANSI.Leijen ((<>))
11+import Text.Read (readMaybe)
912
1013import qualified Elm.Compiler as Compiler
14+import qualified Elm.Diff as Diff
1115import qualified Elm.Package as Pkg
16+import qualified Generate.Output as Output
17+import Terminal.Args
18+import Terminal.Args.Helpers
1219
13−import CommandLine.Args as Args
1420import qualified Bump
1521import qualified Develop
1622import qualified Diff
@@ -27,79 +33,189 @@ import qualified Repl
2733main :: IO ()
2834main =
2935 do setLocaleEncoding utf8
36+ complex intro outro
37+ [ repl
38+ , reactor
39+ , make
40+ , install
41+ , bump
42+ , diff
43+ , publish
44+ ]
45+
46+
47+intro :: P.Doc
48+intro =
49+ P.vcat
50+ [ P.fillSep
51+ ["Hi,","thank","you","for","trying","out","Elm"
52+ ,P.green (P.text (Pkg.versionToString Compiler.version)) <> "."
53+ ,"I hope you like it!"
54+ ]
55+ , ""
56+ , P.dullblack "-------------------------------------------------------------------------------"
57+ , P.dullblack "I highly recommend working through <https://guide.elm-lang.org> to get started."
58+ , P.dullblack "It teaches many important concepts, including how to use `elm` in the terminal."
59+ , P.dullblack "-------------------------------------------------------------------------------"
60+ ]
61+
62+
63+outro :: P.Doc
64+outro =
65+ P.fillSep $ map P.text $ words $
66+ "Be sure to ask on the Elm slack if you run into trouble! Folks are friendly and\
67+ \ happy to help out. They hang out there because it is fun, so be kind to get the\
68+ \ best results!"
69+
70+
71+
72+-- REPL
73+
74+
75+repl :: Interface
76+repl =
77+ let
78+ summary =
79+ "Open up an interactive programming session. You can type in Elm\
80+ \ expressions and it will tell you what they evaluate to."
81+
82+ details =
83+ Details $ error "TODO"
84+
85+ replFlags =
86+ flags Repl.Flags
87+ |-- flag "interpreter" interpreter "Path to a alternate JS interpreter, like node or nodejs."
88+ in
89+ Interface "repl" (Common summary) details noArgs replFlags Repl.run
90+
91+
92+interpreter :: Parser String
93+interpreter =
94+ Parser
95+ { _singular = "interpreter"
96+ , _plural = "interpreters"
97+ , _parser = Just
98+ , _suggest = \_ -> return []
99+ , _examples = \_ -> return ["node","nodejs"]
100+ }
101+
102+
103+
104+-- REACTOR
105+
106+
107+reactor :: Interface
108+reactor =
109+ let
110+ summary =
111+ "Easy way to get started with Elm. It shows a file viewer, and when you\
112+ \ click on an Elm file, it compiles it and shows the result."
113+
114+ details =
115+ Details $ error "TODO"
116+
117+ reactorFlags =
118+ flags Develop.Flags
119+ |-- flag "port" port_ "The port of the server (default: 8000)"
120+ in
121+ Interface "reactor" (Common summary) details noArgs reactorFlags Develop.run
122+
123+
124+port_ :: Parser Int
125+port_ =
126+ Parser
127+ { _singular = "port"
128+ , _plural = "ports"
129+ , _parser = readMaybe
130+ , _suggest = \_ -> return []
131+ , _examples = \_ -> return ["3000","8000"]
132+ }
133+
134+
135+
136+-- MAKE
137+
138+
139+make :: Interface
140+make =
141+ let
142+ summary =
143+ "Compile files by hand. Once you get comfortable with Elm projects, you\
144+ \ will probably want this level of control during development."
145+
146+ details =
147+ Details $ error "TODO"
148+
149+ makeFlags =
150+ flags Make.Flags
151+ |-- onOff "warn" "Report warnings to improve code quality."
152+ |-- onOff "debug" "Generate programs in debug mode."
153+ |-- flag "output" Output.output "Specify the name of the resulting JS file (use --output=/dev/null for no output)"
154+ in
155+ Interface "make" (Common summary) details (zeroOrMore elmFile) makeFlags Make.run
156+
157+
158+
159+-- INSTALL
160+
161+
162+install :: Interface
163+install =
164+ let
165+ summary =
166+ "Install <https://package.elm-lang.org> packages. Every project is\
167+ \ independent, so you can use different versions in different projects."
168+
169+ details =
170+ Details $ error "TODO"
171+ in
172+ Interface "install" (Common summary) details (required package) noFlags Install.run
173+
174+
175+
176+-- PUBLISH
177+
178+
179+publish :: Interface
180+publish =
181+ let
182+ details =
183+ Details $ error "TODO"
184+ in
185+ Interface "publish" Uncommon details noArgs noFlags Publish.run
186+
187+
188+
189+-- BUMP
190+
191+
192+bump :: Interface
193+bump =
194+ let
195+ details =
196+ Details $ error "TODO"
197+ in
198+ Interface "bump" Uncommon details noArgs noFlags Bump.run
199+
200+
201+
202+-- DIFF
203+
204+
205+diff :: Interface
206+diff =
207+ let
208+ details =
209+ Details $
210+ "The `diff` command detects API changes. Every Elm package has a known public\
211+ \ API, so I can tell you exactly what changed from version to version."
30212
31− route <- Args.chomp
32− case route of
33− Naked () () ->
34− putStrLn $
35− "Elm " ++ Pkg.versionToString Compiler.version
36− ++ " - Get started at <https://guide.elm-lang.org/>!"
37−
38− Version () () ->
39− putStrLn (Pkg.versionToString Compiler.version)
40−
41− Install pkg () ->
42− Install.run pkg
43−
44− Publish () () ->
45− Publish.run
46−
47− Make paths flags ->
48− Make.run paths flags
49−
50− Bump () () ->
51− Bump.run
52−
53− Diff args () ->
54− Diff.run args
55−
56− Repl () flags ->
57− Repl.run flags
58−
59− Dev () flags ->
60− Develop.run flags
61−
62−
63−attemptToRun :: String -> [String] -> IO ()
64−attemptToRun command args =
65− do found <- Dir.findExecutable ("elm-" ++ command)
66− case found of
67− Nothing ->
68− IO.hPutStrLn IO.stderr $
69− "Could not find command `" ++ command ++ "`. Maybe there is a typo?\n\n\
70− \Default commands include:\n\n"
71− ++ availableCommands ++
72− "\nWhen you try to run the command `" ++ command ++ "` we actually search for an\n\
73− \ executable named elm-" ++ command ++ ". Are you able to run elm-" ++ command ++ "?\n\
74− \ Is it on your PATH?"
75−
76− Just path ->
77− do (_, _, _, handle) <- Proc.createProcess (Proc.proc path args)
78− exitWith =<< Proc.waitForProcess handle
79−
80−
81−usageMessage :: String
82−usageMessage =
83− "Elm Platform " ++ Pkg.versionToString Compiler.version ++ " - a way to run all Elm tools\n\
84− \\n\
85− \Usage: elm <command> [<args>]\n\
86− \\n\
87− \Available commands include:\n\n"
88− ++ availableCommands ++
89− "\nYou can learn more about a specific command by running things like:\n\
90− \\n\
91− \ elm make --help\n\
92− \ elm package --help\n\
93− \ elm <command> --help\n\
94− \\n\
95− \In all these cases we are simply running 'elm-<command>' so if you create an\n\
96− \executable named 'elm-foobar' you will be able to run it as 'elm foobar' as\n\
97− \long as it appears on your PATH."
98−
99−
100−availableCommands :: String
101−availableCommands =
102− " make Compile an Elm file or project into JS or HTML\n\
103− \ package Manage packages from <http://package.elm-lang.org>\n\
104− \ reactor Develop with compile-on-refresh and time-travel debugging\n\
105− \ repl A REPL for running individual expressions\n"
213+ diffArgs =
214+ oneOf
215+ [ require0 Diff.CodeVsLatest
216+ , require1 Diff.CodeVsExactly version
217+ , require2 Diff.LocalInquiry version version
218+ , require3 Diff.GlobalInquiry package version version
219+ ]
220+ in
221+ Interface "diff" Uncommon details diffArgs noFlags Diff.run