2022-09-14 17:16:15 +03:00
|
|
|
module TopCommand.Options where
|
|
|
|
|
2023-11-06 14:49:43 +03:00
|
|
|
import Commands.Clean.Options
|
2022-09-14 17:16:15 +03:00
|
|
|
import Commands.Compile.Options
|
2023-10-03 19:09:13 +03:00
|
|
|
import Commands.Dependencies.Options qualified as Dependencies
|
2023-03-14 18:24:07 +03:00
|
|
|
import Commands.Dev.Options qualified as Dev
|
2022-09-14 17:16:15 +03:00
|
|
|
import Commands.Doctor.Options
|
2023-03-14 18:24:07 +03:00
|
|
|
import Commands.Eval.Options
|
2023-03-29 16:51:04 +03:00
|
|
|
import Commands.Format.Options
|
2022-09-14 17:16:15 +03:00
|
|
|
import Commands.Html.Options
|
2022-11-07 16:47:56 +03:00
|
|
|
import Commands.Repl.Options
|
2022-09-14 17:16:15 +03:00
|
|
|
import Commands.Typecheck.Options
|
|
|
|
import CommonOptions hiding (Doc)
|
|
|
|
import Data.Generics.Uniplate.Data
|
|
|
|
import GlobalOptions
|
|
|
|
import Options.Applicative.Help.Pretty
|
|
|
|
|
|
|
|
data TopCommand
|
|
|
|
= DisplayVersion
|
2023-03-24 12:45:53 +03:00
|
|
|
| DisplayNumericVersion
|
2022-09-14 17:16:15 +03:00
|
|
|
| DisplayHelp
|
|
|
|
| Typecheck TypecheckOptions
|
|
|
|
| Compile CompileOptions
|
2023-11-06 14:49:43 +03:00
|
|
|
| Clean CleanOptions
|
2023-03-14 18:24:07 +03:00
|
|
|
| Eval EvalOptions
|
2022-09-14 17:16:15 +03:00
|
|
|
| Html HtmlOptions
|
2023-03-14 18:24:07 +03:00
|
|
|
| Dev Dev.DevCommand
|
2022-09-14 17:16:15 +03:00
|
|
|
| Doctor DoctorOptions
|
|
|
|
| Init
|
2022-11-07 16:47:56 +03:00
|
|
|
| JuvixRepl ReplOptions
|
2023-03-29 16:51:04 +03:00
|
|
|
| JuvixFormat FormatOptions
|
2023-10-03 19:09:13 +03:00
|
|
|
| Dependencies Dependencies.DependenciesCommand
|
2022-09-14 17:16:15 +03:00
|
|
|
deriving stock (Data)
|
|
|
|
|
2023-05-15 12:03:09 +03:00
|
|
|
topCommandInputPath :: TopCommand -> IO (Maybe (SomePath Abs))
|
|
|
|
topCommandInputPath (JuvixFormat fopts) = case fopts ^. formatInput of
|
|
|
|
Just f -> getInputPathFromPrepathFileOrDir f
|
2023-05-10 09:52:23 +03:00
|
|
|
Nothing -> return Nothing
|
2023-05-15 12:03:09 +03:00
|
|
|
topCommandInputPath t = do
|
2023-04-19 17:56:48 +03:00
|
|
|
d <- firstJustM getInputFileOrDir (universeBi t)
|
2023-05-15 12:03:09 +03:00
|
|
|
f <- firstJustM getInputFile (universeBi t)
|
2023-04-19 17:56:48 +03:00
|
|
|
return (f <|> d)
|
2022-09-14 17:16:15 +03:00
|
|
|
where
|
2023-05-15 12:03:09 +03:00
|
|
|
getInputFile :: AppPath File -> IO (Maybe (SomePath Abs))
|
|
|
|
getInputFile p
|
2023-04-19 17:56:48 +03:00
|
|
|
| p ^. pathIsInput = do
|
|
|
|
cwd <- getCurrentDir
|
2023-05-15 12:03:09 +03:00
|
|
|
Just . File <$> prepathToAbsFile cwd (p ^. pathPath)
|
2023-04-19 17:56:48 +03:00
|
|
|
| otherwise = return Nothing
|
2022-09-14 17:16:15 +03:00
|
|
|
|
2023-05-15 12:03:09 +03:00
|
|
|
getInputFileOrDir :: AppPath FileOrDir -> IO (Maybe (SomePath Abs))
|
2023-04-19 17:56:48 +03:00
|
|
|
getInputFileOrDir p
|
2023-05-15 12:03:09 +03:00
|
|
|
| p ^. pathIsInput = getInputPathFromPrepathFileOrDir (p ^. pathPath)
|
2023-04-19 17:56:48 +03:00
|
|
|
| otherwise = return Nothing
|
2023-03-29 16:51:04 +03:00
|
|
|
|
2023-05-15 12:03:09 +03:00
|
|
|
getInputPathFromPrepathFileOrDir :: Prepath FileOrDir -> IO (Maybe (SomePath Abs))
|
|
|
|
getInputPathFromPrepathFileOrDir p = do
|
2023-05-10 09:52:23 +03:00
|
|
|
cwd <- getCurrentDir
|
|
|
|
lr <- fromPreFileOrDir cwd p
|
|
|
|
return . Just $ case lr of
|
2023-05-15 12:03:09 +03:00
|
|
|
Left file -> File file
|
|
|
|
Right dir -> Dir dir
|
2023-05-10 09:52:23 +03:00
|
|
|
|
2022-09-14 17:16:15 +03:00
|
|
|
parseDisplayVersion :: Parser TopCommand
|
|
|
|
parseDisplayVersion =
|
|
|
|
flag'
|
|
|
|
DisplayVersion
|
|
|
|
( long "version"
|
|
|
|
<> short 'v'
|
2023-03-24 12:45:53 +03:00
|
|
|
<> help "Show version information"
|
|
|
|
)
|
|
|
|
|
|
|
|
parseDisplayNumericVersion :: Parser TopCommand
|
|
|
|
parseDisplayNumericVersion =
|
|
|
|
flag'
|
|
|
|
DisplayNumericVersion
|
|
|
|
( long "numeric-version"
|
|
|
|
<> help "Show only the version number"
|
2022-09-14 17:16:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
parseDisplayHelp :: Parser TopCommand
|
|
|
|
parseDisplayHelp =
|
|
|
|
flag'
|
|
|
|
DisplayHelp
|
|
|
|
( long "help"
|
|
|
|
<> short 'h'
|
|
|
|
<> help "Show the help text"
|
|
|
|
)
|
|
|
|
|
|
|
|
parseUtility :: Parser TopCommand
|
|
|
|
parseUtility =
|
|
|
|
hsubparser
|
|
|
|
( mconcat
|
|
|
|
[ commandGroup "Utility commands:",
|
|
|
|
metavar "UTILITY_CMD",
|
|
|
|
commandInit,
|
2023-03-29 16:51:04 +03:00
|
|
|
commandRepl,
|
2023-04-21 15:21:31 +03:00
|
|
|
commandFormat,
|
2023-10-03 19:09:13 +03:00
|
|
|
commandClean,
|
|
|
|
commandDependencies,
|
|
|
|
commandDoctor,
|
|
|
|
commandDev
|
2022-09-14 17:16:15 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
where
|
|
|
|
commandInit :: Mod CommandFields TopCommand
|
|
|
|
commandInit =
|
|
|
|
command
|
|
|
|
"init"
|
|
|
|
( info
|
|
|
|
(pure Init)
|
2023-04-03 17:46:53 +03:00
|
|
|
(progDesc "Interactively initialize a Juvix project in the current directory")
|
2022-09-14 17:16:15 +03:00
|
|
|
)
|
|
|
|
commandDoctor :: Mod CommandFields TopCommand
|
|
|
|
commandDoctor =
|
|
|
|
command
|
|
|
|
"doctor"
|
|
|
|
( info
|
|
|
|
(Doctor <$> parseDoctorOptions)
|
|
|
|
(progDesc "Perform checks on your Juvix development environment")
|
|
|
|
)
|
2022-11-07 16:47:56 +03:00
|
|
|
commandRepl :: Mod CommandFields TopCommand
|
|
|
|
commandRepl =
|
|
|
|
command
|
|
|
|
"repl"
|
|
|
|
( info
|
|
|
|
(JuvixRepl <$> parseRepl)
|
|
|
|
(progDesc "Run the Juvix REPL")
|
|
|
|
)
|
2022-09-14 17:16:15 +03:00
|
|
|
|
2023-03-29 16:51:04 +03:00
|
|
|
commandFormat :: Mod CommandFields TopCommand
|
|
|
|
commandFormat =
|
|
|
|
command "format" $
|
|
|
|
info
|
|
|
|
(JuvixFormat <$> parseFormat)
|
2023-04-21 15:21:31 +03:00
|
|
|
( headerDoc
|
2023-03-29 16:51:04 +03:00
|
|
|
( Just
|
|
|
|
( vsep
|
2023-04-21 15:21:31 +03:00
|
|
|
[ "juvix format is used to format Juvix source files.",
|
2023-03-29 16:51:04 +03:00
|
|
|
"",
|
2023-06-19 17:14:59 +03:00
|
|
|
"Given a file, it prints the reformatted source to standard output.",
|
2023-06-20 08:32:17 +03:00
|
|
|
"Given a project directory it prints a list of unformatted files in the project.",
|
|
|
|
"Given no argument it prints a list of unformatted files in the project which contains the current directory."
|
2023-03-29 16:51:04 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2023-04-21 15:21:31 +03:00
|
|
|
<> progDesc "Format a Juvix file or Juvix project"
|
2023-03-29 16:51:04 +03:00
|
|
|
)
|
|
|
|
|
2023-04-21 15:21:31 +03:00
|
|
|
commandClean :: Mod CommandFields TopCommand
|
|
|
|
commandClean =
|
|
|
|
command
|
|
|
|
"clean"
|
2023-11-06 14:49:43 +03:00
|
|
|
(info (Clean <$> parseCleanOptions) (progDesc "Delete build artifacts"))
|
2023-04-21 15:21:31 +03:00
|
|
|
|
2023-10-03 19:09:13 +03:00
|
|
|
commandDependencies :: Mod CommandFields TopCommand
|
|
|
|
commandDependencies =
|
|
|
|
command
|
|
|
|
"dependencies"
|
|
|
|
(info (Dependencies <$> Dependencies.parseDependenciesCommand) (progDesc "Subcommands related to dependencies"))
|
|
|
|
|
2022-09-14 17:16:15 +03:00
|
|
|
commandCheck :: Mod CommandFields TopCommand
|
|
|
|
commandCheck =
|
|
|
|
command "typecheck" $
|
|
|
|
info
|
|
|
|
(Typecheck <$> parseTypecheck)
|
|
|
|
(progDesc "Typecheck a Juvix file")
|
|
|
|
|
|
|
|
commandCompile :: Mod CommandFields TopCommand
|
|
|
|
commandCompile =
|
|
|
|
command "compile" $
|
|
|
|
info
|
2023-04-13 15:16:07 +03:00
|
|
|
(Compile <$> parseMainCompileOptions)
|
2022-09-14 17:16:15 +03:00
|
|
|
(progDesc "Compile a Juvix file")
|
|
|
|
|
2023-03-14 18:24:07 +03:00
|
|
|
commandEval :: Mod CommandFields TopCommand
|
|
|
|
commandEval =
|
|
|
|
command "eval" $
|
|
|
|
info
|
|
|
|
(Eval <$> parseEvalOptions)
|
|
|
|
(progDesc "Evaluate a Juvix file")
|
|
|
|
|
2022-09-14 17:16:15 +03:00
|
|
|
commandHtml :: Mod CommandFields TopCommand
|
|
|
|
commandHtml =
|
|
|
|
command "html" $
|
|
|
|
info
|
|
|
|
(Html <$> parseHtml)
|
|
|
|
(progDesc "Generate HTML for a Juvix file")
|
|
|
|
|
|
|
|
commandDev :: Mod CommandFields TopCommand
|
|
|
|
commandDev =
|
|
|
|
command "dev" $
|
|
|
|
info
|
2023-03-14 18:24:07 +03:00
|
|
|
(Dev <$> Dev.parseDevCommand)
|
2022-09-14 17:16:15 +03:00
|
|
|
(progDesc "Commands for the developers")
|
|
|
|
|
|
|
|
parseCompilerCommand :: Parser TopCommand
|
|
|
|
parseCompilerCommand =
|
|
|
|
hsubparser
|
|
|
|
( mconcat
|
|
|
|
[ commandGroup "Compiler commands:",
|
|
|
|
metavar "COMPILER_CMD",
|
|
|
|
commandCheck,
|
|
|
|
commandCompile,
|
2023-03-14 18:24:07 +03:00
|
|
|
commandEval,
|
2022-09-14 17:16:15 +03:00
|
|
|
commandHtml
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
parseTopCommand :: Parser TopCommand
|
|
|
|
parseTopCommand =
|
|
|
|
parseDisplayVersion
|
2023-03-24 12:45:53 +03:00
|
|
|
<|> parseDisplayNumericVersion
|
2022-09-14 17:16:15 +03:00
|
|
|
<|> parseDisplayHelp
|
|
|
|
<|> parseCompilerCommand
|
|
|
|
<|> parseUtility
|
|
|
|
|
|
|
|
descr :: ParserInfo (GlobalOptions, TopCommand)
|
|
|
|
descr =
|
|
|
|
info
|
|
|
|
( do
|
|
|
|
cli <- parseTopCommand
|
|
|
|
opts <- parseGlobalFlags
|
|
|
|
return (opts, cli)
|
|
|
|
)
|
|
|
|
( fullDesc
|
|
|
|
<> progDesc "The Juvix compiler."
|
|
|
|
<> footerDoc (Just foot)
|
|
|
|
)
|
|
|
|
where
|
|
|
|
foot :: Doc
|
|
|
|
foot = bold "maintainers: " <> "The Juvix Team"
|