on 63243ca help sign in
elk/compiler/Build/Interface.hs
46 lines 1.7 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Build.Interface (load,isValid) where
4import qualified Data.ByteString.Lazy as L
5import qualified Data.Binary as Binary
7import qualified Elm.Internal.Version as Version
8import System.Directory (doesFileExist)
9import System.Exit
10import System.IO
12import SourceSyntax.Module
14load :: Binary.Binary a => FilePath -> IO a
15load 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
25 where
26 failure err = do hPutStrLn stderr err
27 exitFailure
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 ]
36isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
37isValid filePath (name, interface) =
38 let version = iVersion interface in
39 if version == Version.elmVersion
40 then Right (name, interface)
41 else Left $ concat
42 [ "Error reading build artifact: ", filePath, "\n"
43 , " It was generated by version ", show version, " of the compiler,\n"
44 , " but you are using version ", show Version.elmVersion, "\n"
45 , " Please remove the file and try again.\n"
46 ]