1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-10 15:06:54 +03:00
juvix/app/Command.hs
Jonathan Cubides 3b0cde27bb
Add CLI improvements and shell testing (#131)
* Remove input file fields from command opts

* [cli] Make version and help commands

* Fix on reviews

* Fixes for dealing with global options inside subcmds

* Fix minijuvix emacs mode and add some instance to GlobalOpts

* Remove unrelated code

* Propagate globals opts in each cmd parser

* Add initial shell tests

* Add test-shell to makefile and CI

* Fix CI: adding .local/bin to PATH

* Fixing CI

* Installing shelltest just before running it

* Install app for shell testing

* Hide global flags after cmd. Fix shell tests accordingly.

* Fixing CI

* Shell test only run on ubuntu for now
2022-06-09 16:36:07 +02:00

171 lines
4.8 KiB
Haskell

module Command
( module Command,
module Commands.Extra,
module Commands.Html,
module Commands.MicroJuvix,
module Commands.Parse,
module Commands.Scope,
module Commands.Termination,
module Commands.Compile,
)
where
import Commands.Compile
import Commands.Extra
import Commands.Html
import Commands.MicroJuvix
import Commands.Parse
import Commands.Scope
import Commands.Termination
import GlobalOptions
import MiniJuvix.Prelude hiding (Doc)
import MiniJuvix.Syntax.Concrete.Scoped.Pretty qualified as Scoper
import Options.Applicative
data Command
= Compile CompileOptions
| DisplayRoot
| Highlight
| Html HtmlOptions
| MicroJuvix MicroJuvixCommand
| MiniC
| MiniHaskell
| MonoJuvix
| Parse ParseOptions
| Scope ScopeOptions
| Termination TerminationCommand
data CommandGlobalOptions = CommandGlobalOptions
{ _cliCommand :: Command,
_cliGlobalOptions :: GlobalOptions
}
makeLenses ''CommandGlobalOptions
parseCommandGlobalOptions :: Parser CommandGlobalOptions
parseCommandGlobalOptions = do
opts <- parseGlobalFlags False
cmd <-
hsubparser
( mconcat
[ commandCompile,
commandHighlight,
commandHtml,
commandMicroJuvix,
commandMiniC,
commandMiniHaskell,
commandMonoJuvix,
commandParse,
commandScope,
commandShowRoot,
commandTermination
]
)
return (cmd {_cliGlobalOptions = opts <> cmd ^. cliGlobalOptions})
commandCompile :: Mod CommandFields CommandGlobalOptions
commandCompile =
command "compile" $
info
(addGlobalOptions (Compile <$> parseCompile))
(progDesc "Compile a MiniJuvix file")
commandHighlight :: Mod CommandFields CommandGlobalOptions
commandHighlight =
command "highlight" $
info
(addGlobalOptions (pure Highlight))
(progDesc "Highlight a MiniJuvix file")
commandHtml :: Mod CommandFields CommandGlobalOptions
commandHtml =
command "html" $
info
(addGlobalOptions (Html <$> parseHtml))
(progDesc "Generate HTML for a MiniJuvix file")
commandMiniC :: Mod CommandFields CommandGlobalOptions
commandMiniC =
command "minic" $
info
(addGlobalOptions (pure MiniC))
(progDesc "Translate a MiniJuvix file to MiniC")
commandMicroJuvix :: Mod CommandFields CommandGlobalOptions
commandMicroJuvix =
command "microjuvix" $
info
(addGlobalOptions (MicroJuvix <$> parseMicroJuvixCommand))
(progDesc "Subcommands related to MicroJuvix")
commandMiniHaskell :: Mod CommandFields CommandGlobalOptions
commandMiniHaskell =
command "minihaskell" $
info
(addGlobalOptions (pure MiniHaskell))
(progDesc "Translate a MiniJuvix file to MiniHaskell")
commandMonoJuvix :: Mod CommandFields CommandGlobalOptions
commandMonoJuvix =
command "monojuvix" $
info
(addGlobalOptions (pure MonoJuvix))
(progDesc "Translate a MiniJuvix file to MonoJuvix")
commandParse :: Mod CommandFields CommandGlobalOptions
commandParse =
command "parse" $
info
(addGlobalOptions (Parse <$> parseParse))
(progDesc "Parse a MiniJuvix file")
commandScope :: Mod CommandFields CommandGlobalOptions
commandScope =
command "scope" $
info
(addGlobalOptions (Scope <$> parseScope))
(progDesc "Parse and scope a MiniJuvix file")
commandShowRoot :: Mod CommandFields CommandGlobalOptions
commandShowRoot =
command "root" $
info
(liftParserCmd (pure DisplayRoot))
(progDesc "Show the root path for a Minijuvix project")
commandTermination :: Mod CommandFields CommandGlobalOptions
commandTermination =
command "termination" $
info
(addGlobalOptions $ Termination <$> parseTerminationCommand)
(progDesc "Subcommands related to termination checking")
--------------------------------------------------------------------------------
-- Misc
--------------------------------------------------------------------------------
cmdDefaultOptions :: Command -> CommandGlobalOptions
cmdDefaultOptions _cliCommand =
CommandGlobalOptions {_cliGlobalOptions = mempty, ..}
liftParserCmd :: Parser Command -> Parser CommandGlobalOptions
liftParserCmd cmd = cmdDefaultOptions <$> cmd
addGlobalOptions :: Parser Command -> Parser CommandGlobalOptions
addGlobalOptions parser = do
flags1 <- parseGlobalFlags True
~(opts2, _cliCommand) <- addParser (parseGlobalOptions True) parser
fs <- parserInputFiles
return
CommandGlobalOptions
{ _cliGlobalOptions = flags1 <> opts2 <> mempty {_globalInputFiles = fs},
..
}
mkScopePrettyOptions :: GlobalOptions -> ScopeOptions -> Scoper.Options
mkScopePrettyOptions g ScopeOptions {..} =
Scoper.defaultOptions
{ Scoper._optShowNameIds = g ^. globalShowNameIds,
Scoper._optInlineImports = _scopeInlineImports
}