Skip to content

Commit 2a7fe7a

Browse files
committed
Use RIO as our Prelude
1 parent 0cc0f44 commit 2a7fe7a

File tree

14 files changed

+86
-87
lines changed

14 files changed

+86
-87
lines changed

app/main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Main (main) where
22

3+
import Prelude
4+
35
import Restyler.CLI (restylerCLI)
46

57
main :: IO ()

package.yaml

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,60 @@ name: restyler
22
version: 0.1.0.0
33
license: MIT
44

5-
# Dependencies shared in *all* targets
5+
default-extensions:
6+
- AutoDeriveTypeable
7+
- BangPatterns
8+
- BinaryLiterals
9+
- ConstraintKinds
10+
- DataKinds
11+
- DefaultSignatures
12+
- DeriveDataTypeable
13+
- DeriveFoldable
14+
- DeriveFunctor
15+
- DeriveGeneric
16+
- DeriveTraversable
17+
- DoAndIfThenElse
18+
- EmptyDataDecls
19+
- ExistentialQuantification
20+
- FlexibleContexts
21+
- FlexibleInstances
22+
- FunctionalDependencies
23+
- GADTs
24+
- GeneralizedNewtypeDeriving
25+
- InstanceSigs
26+
- KindSignatures
27+
- LambdaCase
28+
- MonadFailDesugaring
29+
- MultiParamTypeClasses
30+
- MultiWayIf
31+
- NamedFieldPuns
32+
- NoImplicitPrelude
33+
- OverloadedStrings
34+
- PartialTypeSignatures
35+
- PatternGuards
36+
- PolyKinds
37+
- RankNTypes
38+
- RecordWildCards
39+
- ScopedTypeVariables
40+
- StandaloneDeriving
41+
- TupleSections
42+
- TypeFamilies
43+
- TypeSynonymInstances
44+
- ViewPatterns
45+
46+
ghc-options:
47+
-Wall
48+
-Wcompat
49+
-Widentities
50+
-Wincomplete-record-updates
51+
-Wincomplete-uni-patterns
52+
-Wpartial-fields
53+
-Wredundant-constraints
54+
655
dependencies:
756
- base
57+
- rio
58+
859

960
library:
1061
source-dirs: src
@@ -41,11 +92,6 @@ library:
4192
- unordered-containers
4293
- vector
4394
- yaml
44-
default-extensions:
45-
# I don't like putting extensions in the packaging and prefer to see them in
46-
# the modules where they are used. However, this extension must be here to
47-
# avoid forgetting it and nudge me to use Restyler.Prelude everywhere.
48-
- NoImplicitPrelude
4995

5096
executables:
5197
restyler:

src/Restyler/App/Class.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ module Restyler.App.Class
2222
import Restyler.Prelude
2323

2424
import Conduit (runResourceT, sinkFile)
25-
import qualified Data.Text as T
26-
import qualified Data.Text.IO as T
27-
import qualified Data.Vector as V
2825
import GitHub.Endpoints.Issues.Comments hiding (comment, comments)
2926
import GitHub.Endpoints.Issues.Labels
3027
import GitHub.Endpoints.PullRequests hiding (pullRequest)
@@ -34,7 +31,9 @@ import GitHub.Request
3431
import Network.HTTP.Client.TLS
3532
import Network.HTTP.Simple hiding (Request)
3633
import Restyler.App.Type
37-
import qualified System.Directory as Directory
34+
import qualified RIO.Directory as Directory
35+
import qualified RIO.Text as T
36+
import qualified RIO.Vector as V
3837
import qualified System.Exit as Exit
3938
import qualified System.Process as Process
4039

@@ -91,7 +90,7 @@ instance MonadIO m => MonadApp (AppT m) where
9190

9291
readFile path = do
9392
logDebugN $ "readFile: " <> tshow path
94-
appIO SystemError $ T.readFile path
93+
appIO SystemError $ readFileUtf8 path
9594

9695
exitSuccess = do
9796
logDebugN "exitSuccess"
@@ -107,9 +106,9 @@ instance MonadIO m => MonadApp (AppT m) where
107106
logDebugN $ pack $ "call: " <> cmd <> " " <> show args
108107
appIO SystemError $ Process.callProcess cmd args
109108

110-
readProcess cmd args stdin = do
109+
readProcess cmd args stdin' = do
111110
logDebugN $ pack $ "read: " <> cmd <> " " <> show args
112-
output <- appIO SystemError $ Process.readProcess cmd args stdin
111+
output <- appIO SystemError $ Process.readProcess cmd args stdin'
113112
output <$ logDebugN ("output: " <> pack output)
114113

115114
downloadFile url path = do

src/Restyler/App/Type.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Restyler.App.Type
1414

1515
import Restyler.Prelude
1616

17+
import Control.Monad.Logger (LoggingT)
1718
import qualified Data.Yaml as Yaml
1819
import Restyler.Config
1920
import Restyler.Options

src/Restyler/CLI.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import Restyler.Options
1616
import Restyler.PullRequest.Status
1717
import Restyler.Setup
1818
import System.Exit (die)
19-
import System.IO (BufferMode(..), hSetBuffering, stderr, stdout)
20-
import System.IO.Temp (withSystemTempDirectory)
2119

2220
-- | The main entrypoint for the restyler CLI
2321
--
@@ -34,14 +32,14 @@ restylerCLI = do
3432
hSetBuffering stderr LineBuffering
3533

3634
options <- parseOptions
37-
withTempDirectory $ \path -> runExceptT $ do
35+
withRestylerDirectory $ \path -> runExceptT $ do
3836
app <- bootstrapApp options path restylerSetup
3937
runApp app $ restylerMain `catchError` \ex -> do
4038
traverse_ (sendPullRequestStatus_ . ErrorStatus) $ oJobUrl options
4139
throwError ex
4240

43-
withTempDirectory :: (FilePath -> IO (Either AppError a)) -> IO a
44-
withTempDirectory f = do
41+
withRestylerDirectory :: (FilePath -> IO (Either AppError a)) -> IO a
42+
withRestylerDirectory f = do
4543
result <- tryIO $ withSystemTempDirectory "restyler-" f
4644
innerResult <- either (dieAppError . SystemError) pure result
4745
either dieAppError pure innerResult

src/Restyler/Config/ExpectedKeys.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ validateExpectedKeyBy label f as k = note msg $ find ((== k) . f) as
5656
)
5757

5858
nearestElem :: String -> [String] -> Maybe (String, Int)
59-
nearestElem x = minimumBy (compare `on` snd) . map (id &&& editDistance x)
59+
nearestElem x = minimumByMaybe (compare `on` snd) . map (id &&& editDistance x)
6060

6161
editDistance :: String -> String -> Int
6262
editDistance = levenshteinDistance defaultEditCosts

src/Restyler/Config/Interpreter.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ instance ToJSON Interpreter where
3131
-- | Does that path start with a /shebang/ for the given @'Interpreter'@
3232
hasInterpreter :: Text -> Interpreter -> Bool
3333
contents `hasInterpreter` interpreter = fromMaybe False $ do
34-
line <- headMay $ T.lines contents
34+
line <- headMaybe $ T.lines contents
3535
foundInterpreter <- parseInterpreter . unpack $ T.strip line
3636
pure $ foundInterpreter == interpreter
3737

src/Restyler/Logger.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Restyler.Logger
1111

1212
import Restyler.Prelude hiding (takeWhile)
1313

14+
import Control.Monad.Logger
15+
(LoggingT, defaultLogStr, filterLogger, runLoggingT, runStdoutLoggingT)
1416
import Data.ByteString (ByteString)
1517
import qualified Data.ByteString as BS
1618
import Restyler.App

src/Restyler/Prelude.hs

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,35 @@ module Restyler.Prelude
44
)
55
where
66

7-
--------------------------------------------------------------------------------
8-
-- Safe(r) re-exports
9-
--------------------------------------------------------------------------------
10-
import Prelude as X hiding
11-
(head, init, last, maximum, minimum, pred, read, readFile, succ, tail)
7+
import RIO as X hiding (LogLevel(..), first, second)
128

13-
import Control.Applicative as X ((<|>))
14-
import Control.Arrow as X ((&&&), (***))
159
import Control.Error.Util as X (hush, note)
16-
import Control.Exception.Safe as X
17-
import Control.Monad as X
1810
import Control.Monad.Except as X
19-
import Control.Monad.Logger as X
20-
import Control.Monad.Reader as X
21-
import Data.Bifunctor as X
22-
import Data.Char as X (isSpace)
23-
import Data.Foldable as X hiding (maximumBy, minimumBy)
24-
import Data.List as X
25-
(dropWhileEnd, find, foldl', isInfixOf, isPrefixOf, isSuffixOf)
26-
import Data.Maybe as X hiding (fromJust)
27-
import Data.Proxy as X
28-
import Data.Semigroup as X ((<>))
29-
import Data.Text as X (Text, pack, strip, unpack)
30-
import Data.Text.Encoding as X
31-
import Data.Traversable as X
32-
import Data.Vector as X (Vector)
33-
import Data.Void as X
34-
import GHC.Generics as X
35-
import GHC.Stack as X
11+
import Data.Bifunctor as X (first, second)
3612
import GitHub.Data as X hiding (command)
37-
import Safe as X
13+
import RIO.Char as X (isSpace)
14+
import RIO.List as X (dropWhileEnd, find, headMaybe, minimumByMaybe)
15+
import RIO.Text as X (encodeUtf8, pack, unpack)
16+
import Safe as X (fromJustNote)
17+
18+
-- TODO: Move to RIO and its own logging facilities
19+
import Control.Monad.Logger as X
20+
(LogLevel(..), MonadLogger, logDebugN, logErrorN, logInfoN, logWarnN)
3821

3922
--------------------------------------------------------------------------------
4023
-- Globally-useful utilities
4124
--------------------------------------------------------------------------------
42-
import qualified Data.Foldable as F
43-
import qualified Data.Text as T
25+
import qualified RIO.Text as T
4426

45-
-- | @'when'@ with a monadic condition
46-
--
47-
-- > x <- someMonadicConditional
48-
-- > when x $ do
49-
-- > someMonadicAction
50-
-- >
51-
-- > whenM someMonadicConditional someMonadicAction
52-
--
53-
whenM :: Monad m => m Bool -> m () -> m ()
54-
whenM condition action = do
55-
result <- condition
56-
when result action
57-
58-
-- | Same for @'unless'@
59-
unlessM :: Monad m => m Bool -> m () -> m ()
60-
unlessM condition action = do
61-
result <- condition
62-
unless result action
63-
64-
-- | @'Show'@ as @'Text'@
65-
tshow :: Show a => a -> Text
66-
tshow = T.pack . show
27+
decodeUtf8 :: ByteString -> Text
28+
decodeUtf8 = T.decodeUtf8With T.lenientDecode
6729

6830
infixl 4 <$$>
6931

7032
-- | @'fmap'@ for doubly-wrapped values
7133
(<$$>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
7234
f <$$> a = fmap f <$> a
7335

74-
minimumBy :: (a -> a -> Ordering) -> [a] -> Maybe a
75-
minimumBy _ [] = Nothing
76-
minimumBy f xs = Just $ F.minimumBy f xs
77-
78-
maximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a
79-
maximumBy _ [] = Nothing
80-
maximumBy f xs = Just $ F.maximumBy f xs
81-
8236
-- | Strip whitespace from the end of a @'Text'@
8337
chomp :: Text -> Text
8438
chomp = T.dropWhileEnd isSpace

src/Restyler/PullRequest.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pullRequestIsClosed = (== StateClosed) . pullRequestState
7676
pullRequestIsFork :: PullRequest -> Bool
7777
pullRequestIsFork = (/=) <$> pullRequestHeadRepo <*> pullRequestBaseRepo
7878

79-
pullRequestIsNonDefaultBranch :: HasCallStack => PullRequest -> Bool
79+
pullRequestIsNonDefaultBranch :: PullRequest -> Bool
8080
pullRequestIsNonDefaultBranch =
8181
(/=) <$> pullRequestBaseRef <*> pullRequestDefaultBranch
8282

src/Restyler/PullRequestSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616
import Restyler.Prelude
1717

1818
import qualified Prelude as Unsafe
19-
import Text.Megaparsec
19+
import Text.Megaparsec hiding (some)
2020
import Text.Megaparsec.Char
2121

2222
data PullRequestSpec = PullRequestSpec

test/Restyler/ConfigSpec.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ where
77

88
import SpecHelper
99

10-
import Data.Bifunctor (first)
1110
import Data.ByteString (ByteString)
1211
import qualified Data.ByteString.Char8 as C8
1312
import Data.List (isInfixOf)

test/Restyler/PullRequestSpecSpec.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,3 @@ spec = describe "parseSpec" $ do
2525
it "round-trips" $ property $ \(owner, name, Positive num) ->
2626
let prSpec = PullRequestSpec owner name num
2727
in parseSpec (T.unpack $ showSpec prSpec) == Right prSpec
28-
29-
isLeft :: Either a b -> Bool
30-
isLeft (Left _) = True
31-
isLeft _ = False

test/SpecHelper.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ module SpecHelper
66
)
77
where
88

9+
import RIO as X hiding (first)
10+
11+
import Data.Bifunctor as X (first)
912
import Test.Hspec as X
1013

1114
import Data.Char (isSpace)
12-
import Data.Proxy
1315
import qualified Data.Text as T
1416
import Data.Text.Arbitrary ()
1517
import GitHub.Data (Id, Name, mkId, mkName)

0 commit comments

Comments
 (0)