@@ -67,7 +67,6 @@ module Spago.Prelude
6767 ) where
6868
6969
70- import Control.Monad.Catch (MonadMask )
7170import qualified Data.Text as Text
7271import qualified Data.Text.Prettyprint.Doc as Pretty
7372import qualified Data.Text.Prettyprint.Doc.Render.Text as PrettyText
@@ -91,7 +90,6 @@ import Data.Foldable as X
9190import Data.Maybe as X
9291import Data.Sequence (Seq (.. ))
9392import Data.Text.Prettyprint.Doc (Pretty )
94- import Data.Text.IO.Utf8 (readFile , writeFile )
9593import Dhall.Optics (transformMOf )
9694import Lens.Family ((^..) )
9795import RIO as X hiding (FilePath , first , force , second , (^..) )
@@ -106,6 +104,13 @@ import UnliftIO.Directory (getModificationTime, mak
106104import UnliftIO.Process (callCommand )
107105
108106
107+
108+ import qualified Data.ByteString as BS
109+ import qualified Data.ByteString.Lazy as BSL
110+ import qualified Data.ByteString.Search as BSS
111+ import qualified Data.ByteString.UTF8 as UTF8
112+
113+
109114-- | Generic Error that we throw on program exit.
110115-- We have it so that errors are displayed nicely to the user
111116newtype SpagoError = SpagoError { _unError :: Text }
@@ -135,13 +140,20 @@ pathFromText = Turtle.fromText
135140testfile :: MonadIO m => Text -> m Bool
136141testfile = Turtle. testfile . pathFromText
137142
138- readTextFile :: MonadIO m => Turtle. FilePath -> m Text
139- readTextFile = readFile . Turtle. encodeString
140-
141-
142- writeTextFile :: (MonadIO m , MonadMask m ) => Text -> Text -> m ()
143- writeTextFile path text = writeFile (Text. unpack path) text
143+ -- | Unfortunately ByteString's readFile does not convert line endings on
144+ -- Windows, so we have to do it ourselves
145+ fixCRLF :: BS. ByteString -> BS. ByteString
146+ fixCRLF = BSL. toStrict . BSS. replace " \r\n " (" \n " :: BS. ByteString )
144147
148+ readTextFile :: MonadIO m => Turtle. FilePath -> m Text
149+ readTextFile inFile' = do
150+ content <- liftIO $ BS. readFile $ Turtle. encodeString inFile'
151+ pure (Text. pack $ UTF8. toString $ fixCRLF content)
152+
153+ writeTextFile :: MonadIO m => Text -> Text -> m ()
154+ writeTextFile inFile text = liftIO $ BS. writeFile
155+ (Text. unpack inFile)
156+ (UTF8. fromString $ Text. unpack text)
145157
146158with :: MonadIO m => Turtle. Managed a -> (a -> IO r ) -> m r
147159with r f = liftIO $ Turtle. with r f
0 commit comments