2019-02-24 23:38:10 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2018-11-25 16:34:28 +03:00
|
|
|
module Main (main) where
|
|
|
|
|
2019-02-24 23:38:10 +03:00
|
|
|
import Control.Monad
|
|
|
|
import Data.List (intercalate)
|
|
|
|
import Data.Version (showVersion)
|
|
|
|
import Development.GitRev
|
|
|
|
import Options.Applicative
|
2019-02-24 22:50:42 +03:00
|
|
|
import Ormolu
|
2019-02-24 23:38:10 +03:00
|
|
|
import Paths_ormolu (version)
|
2019-06-17 01:21:43 +03:00
|
|
|
import System.Exit (ExitCode (..), exitWith)
|
2019-01-30 20:36:52 +03:00
|
|
|
import qualified Data.Text.IO as TIO
|
2019-02-24 23:38:10 +03:00
|
|
|
import qualified Data.Yaml as Yaml
|
|
|
|
|
|
|
|
-- | Entry point of the program.
|
2018-12-08 13:34:26 +03:00
|
|
|
|
2018-11-25 16:34:28 +03:00
|
|
|
main :: IO ()
|
2019-02-24 22:50:42 +03:00
|
|
|
main = withPrettyOrmoluExceptions $ do
|
2019-02-24 23:38:10 +03:00
|
|
|
Opts {..} <- execParser optsParserInfo
|
|
|
|
config <- case optConfigFile of
|
2019-03-01 00:16:15 +03:00
|
|
|
Nothing -> return optConfig
|
2019-02-24 23:38:10 +03:00
|
|
|
Just path -> do
|
2019-03-01 00:16:15 +03:00
|
|
|
config <- Yaml.decodeFileThrow path
|
|
|
|
return (config <> optConfig)
|
|
|
|
r <- ormoluFile config optInputFile
|
2019-02-24 23:38:10 +03:00
|
|
|
case optMode of
|
|
|
|
Stdout ->
|
|
|
|
TIO.putStr r
|
|
|
|
InPlace ->
|
|
|
|
TIO.writeFile optInputFile r
|
|
|
|
Check -> do
|
|
|
|
r' <- TIO.readFile optInputFile
|
2019-06-17 01:21:43 +03:00
|
|
|
when (r /= r') . exitWith $
|
|
|
|
ExitFailure 100 -- 100 is different to all the other exit code that
|
|
|
|
-- are emitted either from an 'OrmoluException' or
|
|
|
|
-- from 'error' and 'notImplemented'.
|
2019-02-24 23:38:10 +03:00
|
|
|
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
-- Command line options parsing.
|
|
|
|
|
|
|
|
data Opts = Opts
|
|
|
|
{ optMode :: !Mode
|
|
|
|
-- ^ Mode of operation
|
|
|
|
, optConfigFile :: !(Maybe FilePath)
|
|
|
|
-- ^ Location of configuration file (optional)
|
2019-03-01 00:16:15 +03:00
|
|
|
, optConfig :: !Config
|
|
|
|
-- ^ Ormolu 'Config'
|
2019-02-24 23:38:10 +03:00
|
|
|
, optInputFile :: !FilePath
|
|
|
|
-- ^ Input source file
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Mode of operation.
|
|
|
|
|
|
|
|
data Mode
|
|
|
|
= Stdout -- ^ Output formatted source code to stdout
|
|
|
|
| InPlace -- ^ Overwrite original file
|
|
|
|
| Check -- ^ Exit with non-zero status code if
|
|
|
|
-- source is not already formatted
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
optsParserInfo :: ParserInfo Opts
|
|
|
|
optsParserInfo = info (helper <*> ver <*> optsParser) . mconcat $
|
|
|
|
[ fullDesc
|
|
|
|
, progDesc ""
|
|
|
|
, header ""
|
|
|
|
]
|
|
|
|
where
|
|
|
|
ver :: Parser (a -> a)
|
|
|
|
ver = infoOption verStr . mconcat $
|
|
|
|
[ long "version"
|
|
|
|
, short 'v'
|
|
|
|
, help "Print version of the program"
|
|
|
|
]
|
|
|
|
verStr = intercalate "\n"
|
|
|
|
[ unwords
|
|
|
|
[ "ormolu"
|
|
|
|
, showVersion version
|
|
|
|
, $gitBranch
|
|
|
|
, $gitHash
|
|
|
|
]
|
2019-05-02 22:24:24 +03:00
|
|
|
, "using ghc " ++ VERSION_ghc
|
2019-02-24 23:38:10 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
optsParser :: Parser Opts
|
|
|
|
optsParser = Opts
|
|
|
|
<$> (option parseMode . mconcat)
|
|
|
|
[ long "mode"
|
|
|
|
, short 'm'
|
|
|
|
, metavar "MODE"
|
|
|
|
, value Stdout
|
|
|
|
, help "Mode of operation: 'stdout', 'inplace', or 'check'"
|
|
|
|
]
|
|
|
|
<*> (optional . strOption . mconcat)
|
|
|
|
[ long "config"
|
|
|
|
, short 'c'
|
|
|
|
, metavar "CONFIG"
|
|
|
|
, help "Location of configuration file"
|
|
|
|
]
|
2019-03-01 00:16:15 +03:00
|
|
|
<*> configParser
|
|
|
|
<*> (strArgument . mconcat)
|
|
|
|
[ metavar "FILE"
|
|
|
|
, help "Haskell source file to format"
|
2019-02-24 23:38:10 +03:00
|
|
|
]
|
2019-03-01 00:16:15 +03:00
|
|
|
|
|
|
|
configParser :: Parser Config
|
|
|
|
configParser = Config
|
|
|
|
<$> (fmap (fmap DynOption) . many . strOption . mconcat)
|
2019-02-24 23:38:10 +03:00
|
|
|
[ long "ghc-opt"
|
|
|
|
, short 'o'
|
|
|
|
, metavar "OPT"
|
|
|
|
, help "GHC options to enable (e.g. language extensions)"
|
|
|
|
]
|
2019-03-01 00:16:15 +03:00
|
|
|
<*> (switch . mconcat)
|
|
|
|
[ long "unsafe"
|
|
|
|
, short 'u'
|
|
|
|
, help "Do formatting faster but without automatic detection of defects"
|
|
|
|
]
|
2019-02-24 23:38:10 +03:00
|
|
|
<*> (switch . mconcat)
|
|
|
|
[ long "debug"
|
|
|
|
, short 'd'
|
|
|
|
, help "Output information useful for debugging"
|
|
|
|
]
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
-- Helpers
|
|
|
|
|
|
|
|
-- | Parse 'Mode'.
|
|
|
|
|
|
|
|
parseMode :: ReadM Mode
|
|
|
|
parseMode = eitherReader $ \case
|
|
|
|
"stdout" -> Right Stdout
|
|
|
|
"inplace" -> Right InPlace
|
|
|
|
"check" -> Right Check
|
|
|
|
s -> Left $ "unknown mode: " ++ s
|