@@ -1,46 +1,46 @@
11{-# OPTIONS_GHC -W #-}
2−module Build.Interface (load,decode,isValid) where
2+module Build.Interface (load,isValid) where
33
44import qualified Data.ByteString.Lazy as L
55import qualified Data.Binary as Binary
66
77import qualified Elm.Internal.Version as Version
88import System.Directory (doesFileExist)
9+import System.Exit
10+import System.IO
911
1012import SourceSyntax.Module
1113
12−load :: FilePath -> IO (Either String L.ByteString)
13−load filePath = do
14− exists <- doesFileExist filePath
15− if exists
16− then do
17− byteString <- L.readFile filePath
18− return $ Right byteString
19−
20− else
21− return $ Left $ "Unable to find file " ++ filePath ++
22− " for deserialization!"
23−
24−decode :: Binary.Binary a => FilePath -> L.ByteString -> Either String a
25−decode filePath bytes =
26− case Binary.decodeOrFail bytes of
27− Right (_, _, binaryInfo) -> Right binaryInfo
28−
29− Left (_, offset, err) ->
30− Left $ concat $
31− [ "Error reading build artifact: ", filePath, "\n"
32− , " The exact error was '", err, "' at offset ", show offset, ".\n"
33− , " The file was generated by a previous build and may be outdated or corrupt.\n"
34− , " Please remove the file and try again."
35− ]
14+load :: Binary.Binary a => FilePath -> IO a
15+load filePath =
16+ do exists <- doesFileExist filePath
17+ case exists of
18+ False -> failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
19+ True -> do
20+ bytes <- L.readFile filePath
21+ case Binary.decodeOrFail bytes of
22+ Right (_, _, binaryInfo) -> return binaryInfo
23+ Left (_, offset, err) -> failure $ msg offset err
24+
25+ where
26+ failure err = do hPutStrLn stderr err
27+ exitFailure
28+
29+ msg offset err = concat
30+ [ "Error reading build artifact: ", filePath, "\n"
31+ , " '", err, "' at offset ", show offset, "\n"
32+ , " The file was generated by a previous build and may be outdated or corrupt.\n"
33+ , " Please remove the file and try again."
34+ ]
3635
3736isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
3837isValid filePath (name, interface) =
39− if iVersion interface == Version.elmVersion
38+ let version = iVersion interface in
39+ if version == Version.elmVersion
4040 then Right (name, interface)
4141 else Left $ concat
4242 [ "Error reading build artifact: ", filePath, "\n"
43− , " It was generated by a different version of the compiler: "
44− , show (iVersion interface), "\n"
43+ , " It was generated by version ", show version, " of the compiler,\n"
44+ , " but you are using version ", show Version.elmVersion, "\n"
4545 , " Please remove the file and try again.\n"
4646 ]