Skip to content

Commit

Permalink
Fix #1479, encode .js files as UTF8.
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Nov 20, 2015
1 parent 3fd1574 commit 85a46e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
5 changes: 5 additions & 0 deletions examples/passing/UnicodeIdentifier.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Main where

f asgård = asgård

main = Control.Monad.Eff.Console.log (f "Done")
22 changes: 12 additions & 10 deletions src/Language/PureScript/Make.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ import Data.Traversable (for)
import Data.Version (showVersion)
import Data.Aeson (encode, decode)
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.UTF8 as BU8
import qualified Data.Set as S
import qualified Data.Map as M

import System.Directory
(doesFileExist, getModificationTime, createDirectoryIfMissing)
import System.FilePath ((</>), takeDirectory)
import System.IO.Error (tryIOError)
import System.IO.UTF8 (readUTF8File, writeUTF8File)

import Language.PureScript.Crash
import Language.PureScript.AST
Expand Down Expand Up @@ -123,7 +125,7 @@ data MakeActions m = MakeActions {
-- |
-- Read the externs file for a module as a string and also return the actual
-- path for the file.
, readExterns :: ModuleName -> m (FilePath, B.ByteString)
, readExterns :: ModuleName -> m (FilePath, Externs)
-- |
-- Run the code generator for the module and write any required output files.
--
Expand All @@ -137,7 +139,7 @@ data MakeActions m = MakeActions {
-- |
-- Generated code for an externs file.
--
type Externs = B.ByteString
type Externs = String

-- |
-- Determines when to rebuild a module
Expand Down Expand Up @@ -231,7 +233,7 @@ make MakeActions{..} ms = do
corefn = CF.moduleToCoreFn env' mod'
[renamed] = renameInModules [corefn]
exts = moduleToExternsFile mod' env'
evalSupplyT nextVar $ codegen renamed env' $ encode exts
evalSupplyT nextVar . codegen renamed env' . BU8.toString . B.toStrict . encode $ exts
return exts
markComplete (Just (warnings, exts)) Nothing

Expand All @@ -258,9 +260,9 @@ make MakeActions{..} ms = do
shouldExist (Just t) = t
shouldExist _ = internalError "make: dependency should already have been built."

decodeExterns :: B.ByteString -> Maybe ExternsFile
decodeExterns :: Externs -> Maybe ExternsFile
decodeExterns bs = do
externs <- decode bs
externs <- decode (fromString bs)
guard $ efVersion externs == showVersion Paths.version
return externs

Expand Down Expand Up @@ -335,7 +337,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
externsFile = outputDir </> filePath </> "externs.json"
min <$> getTimestamp jsFile <*> getTimestamp externsFile

readExterns :: ModuleName -> Make (FilePath, B.ByteString)
readExterns :: ModuleName -> Make (FilePath, Externs)
readExterns mn = do
let path = outputDir </> runModuleName mn </> "externs.json"
(path, ) <$> readTextFile path
Expand Down Expand Up @@ -371,13 +373,13 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
exists <- doesFileExist path
traverse (const $ getModificationTime path) $ guard exists

readTextFile :: FilePath -> Make B.ByteString
readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ B.readFile path
readTextFile :: FilePath -> Make String
readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ readUTF8File path

writeTextFile :: FilePath -> B.ByteString -> Make ()
writeTextFile :: FilePath -> String -> Make ()
writeTextFile path text = makeIO (const (ErrorMessage [] $ CannotWriteFile path)) $ do
mkdirp path
B.writeFile path text
writeUTF8File path text
where
mkdirp :: FilePath -> IO ()
mkdirp = createDirectoryIfMissing True . takeDirectory
Expand Down
24 changes: 20 additions & 4 deletions src/System/IO/UTF8.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
module System.IO.UTF8

where
import System.IO (hGetContents, hSetEncoding, openFile, utf8, IOMode (..))

import System.IO ( IOMode(..)
, hGetContents
, hSetEncoding
, hClose
, hPutStr
, openFile
, utf8
)

readUTF8File :: FilePath -> IO String
readUTF8File inFile = do
h <- openFile inFile ReadMode
hSetEncoding h utf8
hGetContents h
h <- openFile inFile ReadMode
hSetEncoding h utf8
hGetContents h

writeUTF8File :: FilePath -> String -> IO ()
writeUTF8File inFile text = do
h <- openFile inFile WriteMode
hSetEncoding h utf8
hPutStr h text
hClose h

0 comments on commit 85a46e1

Please sign in to comment.