Skip to content

Commit

Permalink
Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
kindaro committed Apr 10, 2024
1 parent f09e3d8 commit 2436eb8
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 241 deletions.
91 changes: 49 additions & 42 deletions executables/cabal-prettify/Main.hs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
module Main where

import Prelude
import Prelude.Unicode
import Prelude.Fancy
import Prelude.Unicode
import Prelude

import Control.Exception
import Control.Monad
import Control.Monad.Trans.Writer
import Data.Bifunctor
import Data.ByteString qualified as ByteArray
import Data.Maybe
import Distribution.Simple.Utils qualified as Cabal
import Distribution.Verbosity qualified as Cabal
import Options.Applicative
import Text.Parsec qualified as Parsec
import System.IO
import Data.Maybe
import System.Exit
import Control.Monad.Trans.Writer
import Control.Monad
import System.IO.Error
import GHC.Generics (Generic)
import Generics.Deriving.Monoid
import Generics.Deriving.Semigroup
import Options.Applicative
import Path
import Path.IO
import System.Exit
import System.IO
import System.IO.Error
import Text.Parsec qualified as Parsec

import Distribution.Prettify

instance Exception Parsec.ParseError
data CabalPrettifyException = CabalPrettifyException String deriving (Show)
instance Exception CabalPrettifyException

main IO ( )
main IO ()
main = do
root getCurrentDir
command execParser (info (parseCommand root <**> helper) (fullDesc <> progDesc "Prettify your Cabal package configuration files!"))
Expand All @@ -38,15 +38,16 @@ main = do
data Command = Command
{ targets Targets
, settings Settings
} deriving (Eq, Ord, Show)
}
deriving (Eq, Ord, Show)

parseCommand Path Abs Dir Parser Command
parseCommand root = do
targets parseTargets root
settings parseSettings
pure Command {..}

run Path Abs Dir Command IO ( )
run Path Abs Dir Command IO ()
run root Command {..} = do
actions processTargetsWithSettings root targets settings
outcomes traverse runAction actions
Expand All @@ -56,12 +57,13 @@ data Targets = Targets
{ thisPackage Any
, standardInput Any
, arguments [Path Abs File]
} deriving (Eq, Ord, Show, Generic)
}
deriving (Eq, Ord, Show, Generic)
instance Semigroup Targets where (<>) = gsappenddefault
instance Monoid Targets where mempty = gmemptydefault

checkFlag Mod FlagFields ( ) Parser ( )
checkFlag = flag' ( )
checkFlag Mod FlagFields () Parser ()
checkFlag = flag' ()

graft Path Abs Dir SomeBase folderOrFile Path Abs folderOrFile
graft path (Rel relativePath) = path </> relativePath
Expand All @@ -86,21 +88,23 @@ data Settings = Settings
{ check Bool
, move Bool
, expose Bool
} deriving (Eq, Ord, Show, Read)
}
deriving (Eq, Ord, Show, Read)

parseSettings Parser Settings
parseSettings = do
check switch (long "check" <> help "Only check, exit with status 1 when targets need formatting.")
move switch (long "move" <> help "Move source files to tidily named directories.")
expose
let helpMessage = "For library stanzas, find and put to `exposed-modules` all modules not listed in `other-modules`. For other stanzas, put all modules to `other-modules`."
in switch (long "expose" <> help helpMessage)
in switch (long "expose" <> help helpMessage)
pure Settings {..}

data Action = Action
{ target Maybe (Path Abs File)
, settings Settings
} deriving (Eq, Ord, Show)
}
deriving (Eq, Ord, Show)

processTargetsWithSettings Path Abs Dir Targets Settings IO [Action]
processTargetsWithSettings root Targets {..} settings = (fmap catMaybes sequence fmap sequence execWriter) do
Expand All @@ -110,7 +114,7 @@ processTargetsWithSettings root Targets {..} settings = (fmap catMaybes ∘ sequ
pathToCabalFile parseSomeFile filePathToCabalFile
pure Action {target = Just (graft root pathToCabalFile), ..}
say do whence (getAny standardInput) (pure Action {target = Nothing, ..})
tell do for arguments \ pathToCabalFile Just (pure Action {target = Just pathToCabalFile, ..})
tell do for arguments \pathToCabalFile Just (pure Action {target = Just pathToCabalFile, ..})

runAction Action IO Bool
runAction Action {settings = Settings {..}, ..} = do
Expand All @@ -124,15 +128,16 @@ runAction Action {settings = Settings {..}, ..} = do
(result, moves) format contents
if result contents
then pure True
else if check
then pure False
else do
case target of
Nothing ByteArray.hPut stdout result
Just target runEffects target result moves
pure True

runEffects :: Path Abs File ByteArray [(Path Rel Dir, Path Rel Dir)] IO ( )
else
if check
then pure False
else do
case target of
Nothing ByteArray.hPut stdout result
Just target runEffects target result moves
pure True

runEffects Path Abs File ByteArray [(Path Rel Dir, Path Rel Dir)] IO ()
runEffects target result moves = do
let root = parent target
backupFileName maybe (throwIO (CabalPrettifyException "Cannot build backup file name!")) pure do
Expand All @@ -142,18 +147,20 @@ runEffects target result moves = do
let backup = root </> backupFileName
renameFile target backup
ByteArray.writeFile (fromAbsFile target) result
forM_ moves \ (sourceRelativeFolder, targetRelativeFolder) let
forM_ moves \(sourceRelativeFolder, targetRelativeFolder)
let
sourceFolder = (parent target </> sourceRelativeFolder)
targetFolder = (parent target </> targetRelativeFolder)
in do
backupFolderName maybe (throwIO (CabalPrettifyException "Cannot build backup folder name!")) pure do
let targetFolderString = fromAbsDir targetFolder
parseAbsDir (targetFolderString <> ".backup")
resultOfRenaming try @IOError do renameDir targetFolder backupFolderName
case resultOfRenaming of
Right ( ) pure ( )
Left ioError | isDoesNotExistError ioError pure ( )
Left otherError throwIO otherError
createDirIfMissing True targetFolder
removeDir targetFolder
renameDir sourceFolder targetFolder
in
do
backupFolderName maybe (throwIO (CabalPrettifyException "Cannot build backup folder name!")) pure do
let targetFolderString = fromAbsDir targetFolder
parseAbsDir (targetFolderString <> ".backup")
resultOfRenaming try @IOError do renameDir targetFolder backupFolderName
case resultOfRenaming of
Right () pure ()
Left ioError | isDoesNotExistError ioError pure ()
Left otherError throwIO otherError
createDirIfMissing True targetFolder
removeDir targetFolder
renameDir sourceFolder targetFolder
12 changes: 12 additions & 0 deletions fourmolu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
indentation: 2
function-arrows: leading
comma-style: leading
import-export-style: leading
record-brace-space: false
indent-wheres: false
in-style: left-align
record-brace-space: true
respectful: true
haddock-style: single-line
newlines-between-decls: 1
unicode: detect
Loading

0 comments on commit 2436eb8

Please sign in to comment.