1
1
mirror of https://github.com/google/ormolu.git synced 2024-11-30 14:46:39 +03:00
ormolu/app/Main.hs

195 lines
5.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
2019-02-24 23:38:10 +03:00
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
2019-02-24 23:38:10 +03:00
module Main
( main,
)
where
2018-11-25 16:34:28 +03:00
import Control.Exception (SomeException, displayException, try)
2019-02-24 23:38:10 +03:00
import Control.Monad
import Data.Either (lefts)
import Data.List (intercalate, sort)
import qualified Data.Text.IO as TIO
2019-02-24 23:38:10 +03:00
import Data.Version (showVersion)
import Development.GitRev
import Options.Applicative
import Ormolu
import Ormolu.Parser (manualExts)
import Ormolu.Utils (showOutputable)
2019-02-24 23:38:10 +03:00
import Paths_ormolu (version)
import System.Exit (ExitCode (..), exitWith)
2019-07-04 03:52:35 +03:00
import System.IO (hPutStrLn, stderr)
2019-02-24 23:38:10 +03:00
-- | Entry point of the program.
2018-11-25 16:34:28 +03:00
main :: IO ()
main = withPrettyOrmoluExceptions $ do
2019-02-24 23:38:10 +03:00
Opts {..} <- execParser optsParserInfo
let formatOne' = formatOne optMode optConfig
case optInputFiles of
[] -> formatOne' Nothing
["-"] -> formatOne' Nothing
[x] -> formatOne' (Just x)
xs -> do
-- It is possible to get IOException, error's and 'OrmoluException's
-- from 'formatOne', so we just catch everything.
errs <-
lefts <$> mapM (try @SomeException . formatOne' . Just) xs
unless (null errs) $ do
hPutStrLn stderr "Some files failed to format:\n"
mapM_ (hPutStrLn stderr . displayException) errs
exitWith (ExitFailure 102)
-- | Format a single input.
formatOne ::
-- | Mode of operation
Mode ->
-- | Configuration
Config ->
-- | File to format or stdin as 'Nothing'
Maybe FilePath ->
IO ()
formatOne mode config = \case
Nothing -> do
r <- ormoluStdin config
case mode of
Stdout -> TIO.putStr r
_ -> do
hPutStrLn
stderr
"This feature is not supported when input comes from stdin."
-- 101 is different from all the other exit codes we already use.
exitWith (ExitFailure 101)
Just inputFile -> do
r <- ormoluFile config inputFile
case mode of
Stdout ->
TIO.putStr r
InPlace ->
TIO.writeFile inputFile r
Check -> do
r' <- TIO.readFile inputFile
when (r /= r') $
-- 100 is different to all the other exit code that are emitted
-- either from an 'OrmoluException' or from 'error' and
-- 'notImplemented'.
exitWith (ExitFailure 100)
2019-02-24 23:38:10 +03:00
----------------------------------------------------------------------------
-- Command line options parsing.
data Opts
= Opts
{ -- | Mode of operation
optMode :: !Mode,
-- | Ormolu 'Config'
optConfig :: !Config,
-- | Haskell source files to format or stdin (when the list is empty)
optInputFiles :: ![FilePath]
}
2019-02-24 23:38:10 +03:00
-- | Mode of operation.
data Mode
= -- | Output formatted source code to stdout
Stdout
| -- | Overwrite original file
InPlace
| -- | Exit with non-zero status code if
-- source is not already formatted
Check
2019-02-24 23:38:10 +03:00
deriving (Eq, Show)
optsParserInfo :: ParserInfo Opts
optsParserInfo =
info (helper <*> ver <*> exts <*> optsParser) . mconcat $
[ fullDesc,
progDesc "",
header ""
]
2019-02-24 23:38:10 +03:00
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
],
"using ghc-lib-parser " ++ VERSION_ghc_lib_parser
2019-02-24 23:38:10 +03:00
]
exts :: Parser (a -> a)
exts =
infoOption displayExts . mconcat $
[ long "manual-exts",
help "Display extensions that need to be enabled manually"
]
displayExts = unlines $ sort (showOutputable <$> manualExts)
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' (default), 'inplace', or 'check'"
]
<*> configParser
<*> (many . strArgument . mconcat)
[ metavar "FILE",
help "Haskell source files to format or stdin (default)"
]
configParser :: Parser Config
configParser =
Config
<$> (fmap (fmap DynOption) . many . strOption . mconcat)
[ long "ghc-opt",
short 'o',
metavar "OPT",
help "GHC options to enable (e.g. language extensions)"
]
<*> (switch . mconcat)
[ long "unsafe",
short 'u',
help "Do formatting faster but without automatic detection of defects"
]
<*> (switch . mconcat)
[ long "debug",
short 'd',
help "Output information useful for debugging"
]
<*> (switch . mconcat)
[ long "tolerate-cpp",
short 'p',
help "Do not fail if CPP pragma is present"
]
<*> (switch . mconcat)
[ long "check-idempotency",
short 'c',
help "Fail if formatting is not idempotent"
]
2019-02-24 23:38:10 +03:00
----------------------------------------------------------------------------
-- Helpers
-- | Parse 'Mode'.
parseMode :: ReadM Mode
parseMode = eitherReader $ \case
"stdout" -> Right Stdout
"inplace" -> Right InPlace
"check" -> Right Check
s -> Left $ "unknown mode: " ++ s