2020-01-24 18:06:51 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2019-02-24 23:38:10 +03:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2020-01-27 23:47:14 +03:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2019-02-24 23:38:10 +03:00
|
|
|
|
2020-04-24 20:43:45 +03:00
|
|
|
module Main (main) where
|
2018-11-25 16:34:28 +03:00
|
|
|
|
2020-01-27 23:47:14 +03:00
|
|
|
import Control.Exception (SomeException, displayException, try)
|
2019-02-24 23:38:10 +03:00
|
|
|
import Control.Monad
|
2020-01-27 23:47:14 +03:00
|
|
|
import Data.Either (lefts)
|
2019-09-02 18:10:08 +03:00
|
|
|
import Data.List (intercalate, sort)
|
2020-01-24 18:06:51 +03:00
|
|
|
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
|
2019-02-24 22:50:42 +03:00
|
|
|
import Ormolu
|
2019-07-10 05:56:15 +03:00
|
|
|
import Ormolu.Parser (manualExts)
|
|
|
|
import Ormolu.Utils (showOutputable)
|
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-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 ()
|
2019-02-24 22:50:42 +03:00
|
|
|
main = withPrettyOrmoluExceptions $ do
|
2019-02-24 23:38:10 +03:00
|
|
|
Opts {..} <- execParser optsParserInfo
|
2019-08-16 23:36:34 +03:00
|
|
|
let formatOne' = formatOne optMode optConfig
|
|
|
|
case optInputFiles of
|
|
|
|
[] -> formatOne' Nothing
|
|
|
|
["-"] -> formatOne' Nothing
|
2020-01-27 23:47:14 +03:00
|
|
|
[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 <-
|
2020-04-14 21:30:40 +03:00
|
|
|
lefts <$> mapM (try @SomeException . formatOne' . Just) (sort xs)
|
2020-01-27 23:47:14 +03:00
|
|
|
unless (null errs) $ do
|
|
|
|
mapM_ (hPutStrLn stderr . displayException) errs
|
|
|
|
exitWith (ExitFailure 102)
|
2019-08-16 23:36:34 +03:00
|
|
|
|
|
|
|
-- | Format a single input.
|
2020-01-24 18:06:51 +03:00
|
|
|
formatOne ::
|
|
|
|
-- | Mode of operation
|
|
|
|
Mode ->
|
|
|
|
-- | Configuration
|
2020-04-23 19:41:32 +03:00
|
|
|
Config RegionIndices ->
|
2020-01-24 18:06:51 +03:00
|
|
|
-- | File to format or stdin as 'Nothing'
|
|
|
|
Maybe FilePath ->
|
|
|
|
IO ()
|
2019-08-16 23:36:34 +03:00
|
|
|
formatOne mode config = \case
|
|
|
|
Nothing -> do
|
|
|
|
r <- ormoluStdin config
|
|
|
|
case mode of
|
|
|
|
Stdout -> TIO.putStr r
|
2020-01-24 18:06:51 +03:00
|
|
|
_ -> do
|
|
|
|
hPutStrLn
|
|
|
|
stderr
|
2019-08-16 23:36:34 +03:00
|
|
|
"This feature is not supported when input comes from stdin."
|
2020-01-24 18:06:51 +03:00
|
|
|
-- 101 is different from all the other exit codes we already use.
|
2019-08-16 23:36:34 +03:00
|
|
|
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.
|
|
|
|
|
2020-04-10 18:52:25 +03:00
|
|
|
data Opts = Opts
|
|
|
|
{ -- | Mode of operation
|
|
|
|
optMode :: !Mode,
|
|
|
|
-- | Ormolu 'Config'
|
2020-04-23 19:41:32 +03:00
|
|
|
optConfig :: !(Config RegionIndices),
|
2020-04-10 18:52:25 +03:00
|
|
|
-- | 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
|
2020-01-24 18:06:51 +03:00
|
|
|
= -- | 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
|
2020-01-24 18:06:51 +03:00
|
|
|
optsParserInfo =
|
|
|
|
info (helper <*> ver <*> exts <*> optsParser) . mconcat $
|
|
|
|
[ fullDesc,
|
|
|
|
progDesc "",
|
|
|
|
header ""
|
|
|
|
]
|
2019-02-24 23:38:10 +03:00
|
|
|
where
|
|
|
|
ver :: Parser (a -> a)
|
2020-01-24 18:06:51 +03:00
|
|
|
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
|
|
|
]
|
2019-07-10 05:56:15 +03:00
|
|
|
exts :: Parser (a -> a)
|
2020-01-24 18:06:51 +03:00
|
|
|
exts =
|
|
|
|
infoOption displayExts . mconcat $
|
|
|
|
[ long "manual-exts",
|
|
|
|
help "Display extensions that need to be enabled manually"
|
|
|
|
]
|
2019-09-02 18:10:08 +03:00
|
|
|
displayExts = unlines $ sort (showOutputable <$> manualExts)
|
2019-02-24 23:38:10 +03:00
|
|
|
|
|
|
|
optsParser :: Parser Opts
|
2020-01-24 18:06:51 +03:00
|
|
|
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)"
|
|
|
|
]
|
2019-03-01 00:16:15 +03:00
|
|
|
|
2020-04-23 19:41:32 +03:00
|
|
|
configParser :: Parser (Config RegionIndices)
|
2020-01-24 18:06:51 +03:00
|
|
|
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)
|
2020-04-27 16:06:12 +03:00
|
|
|
[ long "check-idempotence",
|
2020-01-24 18:06:51 +03:00
|
|
|
short 'c',
|
|
|
|
help "Fail if formatting is not idempotent"
|
|
|
|
]
|
2020-04-23 19:41:32 +03:00
|
|
|
<*> ( RegionIndices
|
|
|
|
<$> (optional . option auto . mconcat)
|
|
|
|
[ long "start-line",
|
2020-04-25 00:10:23 +03:00
|
|
|
metavar "START",
|
|
|
|
help "Start line of the region to format (starts from 1)"
|
2020-04-23 19:41:32 +03:00
|
|
|
]
|
|
|
|
<*> (optional . option auto . mconcat)
|
|
|
|
[ long "end-line",
|
2020-04-25 00:10:23 +03:00
|
|
|
metavar "END",
|
2020-04-23 19:41:32 +03:00
|
|
|
help "End line of the region to format (inclusive)"
|
|
|
|
]
|
|
|
|
)
|
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
|