Skip to content

Commit 5c4a338

Browse files
committed
I wonder if we are reading files the wrong way
1 parent 2a854af commit 5c4a338

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ library:
108108
- safe
109109
- semver-range
110110
- stm
111+
- stringsearch
111112
- tar
112113
- template-haskell
113114
- temporary
@@ -117,6 +118,7 @@ library:
117118
- turtle
118119
- unliftio
119120
- unordered-containers
121+
- utf8-string
120122
- vector
121123
- versions
122124
- with-utf8

spago.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: d261f6a4edc91f161735e671dbcb8b6116d31f6a6dd95c88c1b277285484951d
7+
-- hash: 25f34ee90c40276d788a66523e0833707cf16645eadde749c8a953606886ab03
88

99
name: spago
1010
version: 0.16.0
@@ -102,6 +102,7 @@ library
102102
, safe
103103
, semver-range
104104
, stm
105+
, stringsearch
105106
, tar
106107
, template-haskell
107108
, temporary
@@ -111,6 +112,7 @@ library
111112
, turtle
112113
, unliftio
113114
, unordered-containers
115+
, utf8-string
114116
, vector
115117
, versions
116118
, with-utf8

src/Spago/Prelude.hs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ module Spago.Prelude
6767
) where
6868

6969

70-
import Control.Monad.Catch (MonadMask)
7170
import qualified Data.Text as Text
7271
import qualified Data.Text.Prettyprint.Doc as Pretty
7372
import qualified Data.Text.Prettyprint.Doc.Render.Text as PrettyText
@@ -91,7 +90,6 @@ import Data.Foldable as X
9190
import Data.Maybe as X
9291
import Data.Sequence (Seq (..))
9392
import Data.Text.Prettyprint.Doc (Pretty)
94-
import Data.Text.IO.Utf8 (readFile, writeFile)
9593
import Dhall.Optics (transformMOf)
9694
import Lens.Family ((^..))
9795
import RIO as X hiding (FilePath, first, force, second, (^..))
@@ -106,6 +104,13 @@ import UnliftIO.Directory (getModificationTime, mak
106104
import 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
111116
newtype SpagoError = SpagoError { _unError :: Text }
@@ -135,13 +140,20 @@ pathFromText = Turtle.fromText
135140
testfile :: MonadIO m => Text -> m Bool
136141
testfile = 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

146158
with :: MonadIO m => Turtle.Managed a -> (a -> IO r) -> m r
147159
with r f = liftIO $ Turtle.with r f

0 commit comments

Comments
 (0)