move the Terminal API into terminal/impl/

This also does some renaming in the Terminal module:

Interface => Command
simple => command
complex => app

So now you have a Terminal.app made up of a bunch of Terminal.Command
specifications.
This commit is contained in:
Evan Czaplicki 2019-10-07 16:02:10 -04:00
parent 3f919f21a6
commit 171e83427e
8 changed files with 80 additions and 78 deletions

View File

@ -45,6 +45,7 @@ Executable elm
Hs-Source-Dirs:
compiler/src
builder/src
terminal/impl
terminal/src
other-extensions:
@ -64,11 +65,11 @@ Executable elm
Repl
-- terminal args
Terminal.Args
Terminal.Args.Chomp
Terminal.Args.Error
Terminal.Args.Helpers
Terminal.Args.Internal
Terminal
Terminal.Chomp
Terminal.Error
Terminal.Helpers
Terminal.Internal
-- from terminal/
Develop.Generate.Help

View File

@ -1,8 +1,7 @@
module Terminal.Args
( simple
, Interface(..)
module Terminal
( app
, Command(..)
, Summary(..)
, complex
, Flags, noFlags, flags, (|--)
, Flag, flag, onOff
, Parser(..)
@ -26,17 +25,17 @@ import qualified Text.PrettyPrint.ANSI.Leijen as P
import qualified Text.Read as Read
import qualified Elm.Version as V
import Terminal.Args.Internal
import qualified Terminal.Args.Chomp as Chomp
import qualified Terminal.Args.Error as Error
import Terminal.Internal
import qualified Terminal.Chomp as Chomp
import qualified Terminal.Error as Error
-- GET
-- COMMAND
simple :: String -> P.Doc -> Args args -> Flags flags -> (args -> flags -> IO ()) -> IO ()
simple details example args_ flags_ callback =
_command :: String -> P.Doc -> Args args -> Flags flags -> (args -> flags -> IO ()) -> IO ()
_command details example args_ flags_ callback =
do setLocaleEncoding utf8
argStrings <- Env.getArgs
case argStrings of
@ -57,34 +56,38 @@ simple details example args_ flags_ callback =
Error.exitWithError err
complex :: P.Doc -> P.Doc -> [Interface] -> IO ()
complex intro outro interfaces =
-- APP
app :: P.Doc -> P.Doc -> [Command] -> IO ()
app intro outro commands =
do setLocaleEncoding utf8
argStrings <- Env.getArgs
case argStrings of
[] ->
Error.exitWithOverview intro outro interfaces
Error.exitWithOverview intro outro commands
["--help"] ->
Error.exitWithOverview intro outro interfaces
Error.exitWithOverview intro outro commands
["--version"] ->
do hPutStrLn stdout (V.toChars V.compiler ++ "-rc-1")
Exit.exitSuccess
command : chunks ->
do case List.find (\iface -> toName iface == command) interfaces of
do case List.find (\cmd -> toName cmd == command) commands of
Nothing ->
Error.exitWithUnknown command (map toName interfaces)
Error.exitWithUnknown command (map toName commands)
Just (Interface _ _ details example args_ flags_ callback) ->
Just (Command _ _ details example args_ flags_ callback) ->
if elem "--help" chunks then
Error.exitWithHelp (Just command) details example args_ flags_
else
case snd $ Chomp.chomp Nothing chunks args_ flags_ of
Right (argsValue, flagValue) ->
callback argsValue flagValue
Right (argsValue, flagsValue) ->
callback argsValue flagsValue
Left err ->
Error.exitWithError err
@ -159,21 +162,21 @@ findIndex index point chunks =
findIndex (index + 1) point cs
_complexSuggest :: [Interface] -> Int -> [String] -> IO [String]
_complexSuggest interfaces index strings =
_complexSuggest :: [Command] -> Int -> [String] -> IO [String]
_complexSuggest commands index strings =
case strings of
[] ->
return (map toName interfaces)
return (map toName commands)
command : chunks ->
if index == 1 then
return (filter (List.isPrefixOf command) (map toName interfaces))
return (filter (List.isPrefixOf command) (map toName commands))
else
case List.find (\iface -> toName iface == command) interfaces of
case List.find (\cmd -> toName cmd == command) commands of
Nothing ->
return []
Just (Interface _ _ _ _ args_ flags_ _) ->
Just (Command _ _ _ _ args_ flags_ _) ->
fst $ Chomp.chomp (Just (index-1)) chunks args_ flags_

View File

@ -1,6 +1,5 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
module Terminal.Args.Chomp
{-# LANGUAGE GADTs, Rank2Types #-}
module Terminal.Chomp
( chomp
)
where
@ -8,8 +7,8 @@ module Terminal.Args.Chomp
import qualified Data.List as List
import Terminal.Args.Error
import Terminal.Args.Internal
import Terminal.Error
import Terminal.Internal

View File

@ -1,6 +1,5 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
module Terminal.Args.Error
{-# LANGUAGE GADTs, OverloadedStrings #-}
module Terminal.Error
( Error(..)
, ArgError(..)
, FlagError(..)
@ -24,7 +23,7 @@ import System.IO (hPutStrLn, stderr)
import qualified Text.PrettyPrint.ANSI.Leijen as P
import Reporting.Suggest as Suggest
import Terminal.Args.Internal
import Terminal.Internal
@ -187,22 +186,22 @@ flagsToDocs flags docs =
-- OVERVIEW
exitWithOverview :: P.Doc -> P.Doc -> [Interface] -> IO a
exitWithOverview intro outro interfaces =
exitWithOverview :: P.Doc -> P.Doc -> [Command] -> IO a
exitWithOverview intro outro commands =
do exeName <- getExeName
exitSuccess
[ intro
, "The most common commands are:"
, P.indent 4 $ stack $ Maybe.mapMaybe (toSummary exeName) interfaces
, P.indent 4 $ stack $ Maybe.mapMaybe (toSummary exeName) commands
, "There are a bunch of other commands as well though. Here is a full list:"
, P.indent 4 $ P.dullcyan $ toCommandList exeName interfaces
, P.indent 4 $ P.dullcyan $ toCommandList exeName commands
, "Adding the --help flag gives a bunch of additional details about each one."
, outro
]
toSummary :: String -> Interface -> Maybe P.Doc
toSummary exeName (Interface name summary _ _ (Args args) _ _) =
toSummary :: String -> Command -> Maybe P.Doc
toSummary exeName (Command name summary _ _ (Args args) _ _) =
case summary of
Uncommon ->
Nothing
@ -215,10 +214,10 @@ toSummary exeName (Interface name summary _ _ (Args args) _ _) =
]
toCommandList :: String -> [Interface] -> P.Doc
toCommandList exeName interfaces =
toCommandList :: String -> [Command] -> P.Doc
toCommandList exeName commands =
let
names = map toName interfaces
names = map toName commands
width = maximum (map length names)
toExample name =

View File

@ -1,5 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
module Terminal.Args.Helpers
module Terminal.Helpers
( version
, elmFile
, package
@ -14,7 +14,7 @@ import qualified Data.Map as Map
import qualified Data.Utf8 as Utf8
import qualified System.FilePath as FP
import Terminal.Args (Parser(..))
import Terminal (Parser(..))
import qualified Deps.Registry as Registry
import qualified Elm.Package as Pkg
import qualified Elm.Version as V

View File

@ -1,6 +1,6 @@
{-# LANGUAGE GADTs #-}
module Terminal.Args.Internal
( Interface(..)
module Terminal.Internal
( Command(..)
, toName
, Summary(..)
, Flags(..)
@ -17,11 +17,11 @@ import Text.PrettyPrint.ANSI.Leijen (Doc)
-- INTERFACE
-- COMMAND
data Interface where
Interface
data Command where
Command
:: String
-> Summary
-> String
@ -29,11 +29,11 @@ data Interface where
-> Args args
-> Flags flags
-> (args -> flags -> IO ())
-> Interface
-> Command
toName :: Interface -> String
toName (Interface name _ _ _ _ _ _) =
toName :: Command -> String
toName (Command name _ _ _ _ _ _) =
name

View File

@ -12,8 +12,8 @@ import Text.PrettyPrint.ANSI.Leijen ((<>))
import Text.Read (readMaybe)
import qualified Elm.Version as V
import Terminal.Args
import Terminal.Args.Helpers
import Terminal
import Terminal.Helpers
import qualified Bump
import qualified Develop
@ -31,7 +31,7 @@ import qualified Repl
main :: IO ()
main =
complex intro outro
Terminal.app intro outro
[ repl
, init
, reactor
@ -72,7 +72,7 @@ outro =
-- INIT
init :: Interface
init :: Terminal.Command
init =
let
summary =
@ -87,14 +87,14 @@ init =
"It will ask permission to create an elm.json file, the one thing common\
\ to all Elm projects. It also provides a link explaining what to do from there."
in
Interface "init" (Common summary) details example noArgs noFlags Init.run
Terminal.Command "init" (Common summary) details example noArgs noFlags Init.run
-- REPL
repl :: Interface
repl :: Terminal.Command
repl =
let
summary =
@ -115,7 +115,7 @@ repl =
|-- flag "interpreter" interpreter "Path to a alternate JS interpreter, like node or nodejs."
|-- onOff "no-colors" "Turn off the colors in the REPL. This can help if you are having trouble reading the values. Some terminals use a custom color scheme that diverges significantly from the standard ANSI colors, so another path may be to pick a more standard color scheme."
in
Interface "repl" (Common summary) details example noArgs replFlags Repl.run
Terminal.Command "repl" (Common summary) details example noArgs replFlags Repl.run
interpreter :: Parser String
@ -133,7 +133,7 @@ interpreter =
-- REACTOR
reactor :: Interface
reactor :: Terminal.Command
reactor =
let
summary =
@ -154,7 +154,7 @@ reactor =
flags Develop.Flags
|-- flag "port" port_ "The port of the server (default: 8000)"
in
Interface "reactor" (Common summary) details example noArgs reactorFlags Develop.run
Terminal.Command "reactor" (Common summary) details example noArgs reactorFlags Develop.run
port_ :: Parser Int
@ -172,7 +172,7 @@ port_ =
-- MAKE
make :: Interface
make :: Terminal.Command
make =
let
details =
@ -196,14 +196,14 @@ make =
|-- flag "report" Make.reportType "You can say --report=json to get error messages as JSON. This is only really useful if you are an editor plugin. Humans should avoid it!"
|-- flag "docs" Make.docsFile "Generate a JSON file of documentation for a package. Eventually it will be possible to preview docs with `reactor` because it is quite hard to deal with these JSON files directly."
in
Interface "make" Uncommon details example (zeroOrMore elmFile) makeFlags Make.run
Terminal.Command "make" Uncommon details example (zeroOrMore elmFile) makeFlags Make.run
-- INSTALL
install :: Interface
install :: Terminal.Command
install =
let
details =
@ -232,14 +232,14 @@ install =
, require1 Install.Install package
]
in
Interface "install" Uncommon details example installArgs noFlags Install.run
Terminal.Command "install" Uncommon details example installArgs noFlags Install.run
-- PUBLISH
publish :: Interface
publish :: Terminal.Command
publish =
let
details =
@ -269,14 +269,14 @@ publish =
"Check out <https://package.elm-lang.org/help/design-guidelines> for guidance on how to create great packages!"
]
in
Interface "publish" Uncommon details example noArgs noFlags Publish.run
Terminal.Command "publish" Uncommon details example noArgs noFlags Publish.run
-- BUMP
bump :: Interface
bump :: Terminal.Command
bump =
let
details =
@ -289,14 +289,14 @@ bump =
\ it is a MAJOR change, and bump your version number to 2.0.0. I do this with\
\ all packages, so there cannot be MAJOR changes hiding in PATCH releases in Elm!"
in
Interface "bump" Uncommon details example noArgs noFlags Bump.run
Terminal.Command "bump" Uncommon details example noArgs noFlags Bump.run
-- DIFF
diff :: Interface
diff :: Terminal.Command
diff =
let
details =
@ -321,7 +321,7 @@ diff =
, require3 Diff.GlobalInquiry package version version
]
in
Interface "diff" Uncommon details example diffArgs noFlags Diff.run
Terminal.Command "diff" Uncommon details example diffArgs noFlags Diff.run

View File

@ -29,7 +29,7 @@ import qualified Reporting
import qualified Reporting.Exit as Exit
import qualified Reporting.Task as Task
import qualified Stuff
import Terminal.Args (Parser(..))
import Terminal (Parser(..))