mirror of
https://github.com/tweag/ormolu.git
synced 2024-09-11 13:16:13 +03:00
Add locking around text output
This commit is contained in:
parent
7c6355b6df
commit
ae8808105b
19
app/Main.hs
19
app/Main.hs
@ -7,6 +7,7 @@
|
||||
|
||||
module Main (main) where
|
||||
|
||||
import Control.Concurrent (MVar, newMVar, withMVar)
|
||||
import Control.Exception (throwIO)
|
||||
import Control.Monad
|
||||
import Data.Bool (bool)
|
||||
@ -39,12 +40,16 @@ import UnliftIO.Async (pooledMapConcurrently)
|
||||
main :: IO ()
|
||||
main = do
|
||||
Opts {..} <- execParser optsParserInfo
|
||||
-- We use this to guard writes to stdout in order to avoid
|
||||
-- garbled output from concurrent formatting processes.
|
||||
outputLock <- newMVar ()
|
||||
let formatOne' =
|
||||
formatOne
|
||||
optConfigFileOpts
|
||||
optMode
|
||||
optSourceType
|
||||
optConfig
|
||||
outputLock
|
||||
exitCode <- case optInputFiles of
|
||||
[] -> formatOne' Nothing
|
||||
["-"] -> formatOne' Nothing
|
||||
@ -76,10 +81,12 @@ formatOne ::
|
||||
Maybe SourceType ->
|
||||
-- | Configuration
|
||||
Config RegionIndices ->
|
||||
-- | Lock for writing to output handles
|
||||
MVar () ->
|
||||
-- | File to format or stdin as 'Nothing'
|
||||
Maybe FilePath ->
|
||||
IO ExitCode
|
||||
formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
|
||||
formatOne ConfigFileOpts {..} mode reqSourceType rawConfig outputLock mpath =
|
||||
withPrettyOrmoluExceptions (cfgColorMode rawConfig) $ do
|
||||
let getCabalInfoForSourceFile' sourceFile = do
|
||||
cabalSearchResult <- getCabalInfoForSourceFile sourceFile
|
||||
@ -87,6 +94,7 @@ formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
|
||||
case cabalSearchResult of
|
||||
CabalNotFound -> do
|
||||
when debugEnabled $
|
||||
withMVar outputLock $ \_ ->
|
||||
hPutStrLn stderr $
|
||||
"Could not find a .cabal file for " <> sourceFile
|
||||
return Nothing
|
||||
@ -94,6 +102,7 @@ formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
|
||||
when debugEnabled $ do
|
||||
relativeCabalFile <-
|
||||
makeRelativeToCurrentDirectory (ciCabalFilePath cabalInfo)
|
||||
withMVar outputLock $ \_ ->
|
||||
hPutStrLn stderr $
|
||||
"Found .cabal file "
|
||||
<> relativeCabalFile
|
||||
@ -118,7 +127,9 @@ formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
|
||||
config <- patchConfig Nothing mcabalInfo mdotOrmolu
|
||||
case mode of
|
||||
Stdout -> do
|
||||
ormoluStdin config >>= T.Utf8.putStr
|
||||
output <- ormoluStdin config
|
||||
withMVar outputLock $ \_ ->
|
||||
T.Utf8.putStr output
|
||||
return ExitSuccess
|
||||
InPlace -> do
|
||||
hPutStrLn
|
||||
@ -147,7 +158,9 @@ formatOne ConfigFileOpts {..} mode reqSourceType rawConfig mpath =
|
||||
mdotOrmolu
|
||||
case mode of
|
||||
Stdout -> do
|
||||
ormoluFile config inputFile >>= T.Utf8.putStr
|
||||
output <- ormoluFile config inputFile
|
||||
withMVar outputLock $ \_ ->
|
||||
T.Utf8.putStr output
|
||||
return ExitSuccess
|
||||
InPlace -> do
|
||||
-- ormoluFile is not used because we need originalInput
|
||||
|
Loading…
Reference in New Issue
Block a user