tie/bin/Main.hs

108 lines
2.2 KiB
Haskell
Raw Normal View History

2022-02-18 10:22:19 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main (main) where
import Data.Version (showVersion)
2022-02-18 10:22:19 +03:00
import Options.Applicative
( Parser,
auto,
execParser,
flag,
fullDesc,
header,
help,
helper,
info,
infoOption,
2022-02-18 10:22:19 +03:00
long,
metavar,
option,
progDesc,
short,
showDefault,
str,
strArgument,
strOption,
switch,
value,
)
import Paths_tie (version)
2022-02-18 10:22:19 +03:00
import System.Environment (getArgs)
import Tie (fileWriter, generate)
import Prelude hiding (Option)
data Input = Input
{ outputDirectory :: FilePath,
moduleName :: Text,
packageName :: Text,
2022-03-24 19:29:49 +03:00
extraPackages :: [Text],
2022-02-18 10:22:19 +03:00
inputFile :: FilePath
}
options :: Parser Input
options =
Input
<$> option
str
( long "output"
<> short 'o'
<> metavar "DIR"
<> showDefault
<> value "out"
2022-06-13 15:31:33 +03:00
<> help "The directory output"
2022-02-18 10:22:19 +03:00
)
<*> option
str
( long "module-name"
<> metavar "MODULE"
<> showDefault
<> value "OpenAPI"
2022-06-13 15:31:33 +03:00
<> help "Name of the generated top level module"
2022-02-18 10:22:19 +03:00
)
<*> option
str
( long "package-name"
<> metavar "PACKAGE"
<> showDefault
<> value "open-api"
2022-06-13 15:31:33 +03:00
<> help "Name of the generated cabal project"
2022-02-18 10:22:19 +03:00
)
2022-03-24 19:29:49 +03:00
<*> many
( option
str
( long "extra-package"
<> metavar "PACKAGE"
2022-06-13 15:31:33 +03:00
<> help "Extra packages to include in the generated cabal project"
2022-03-24 19:29:49 +03:00
)
)
2022-02-18 10:22:19 +03:00
<*> strArgument
( metavar "FILE"
2022-06-13 15:31:33 +03:00
<> help "OpenAPI specification file"
2022-02-18 10:22:19 +03:00
)
versioner :: Parser (a -> a)
versioner =
infoOption
("tie " <> showVersion version)
( long "version"
<> help "Print Tie version"
)
2022-02-18 10:22:19 +03:00
main :: IO ()
main = do
Input {..} <-
execParser $
info
(helper <*> versioner <*> options)
2022-02-18 10:22:19 +03:00
( fullDesc
<> progDesc "Generate a Haskell server from an OpenAPI3 specification"
<> header "tie - openapi3 server code generator"
)
generate
(fileWriter outputDirectory)
packageName
moduleName
2022-03-24 19:29:49 +03:00
extraPackages
2022-02-18 10:22:19 +03:00
inputFile