on c8eeedf help sign in
elk/compiler/Build/Interface.hs
41 lines 1.6 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 Build.Print as Print
8import qualified Elm.Internal.Version as Version
9import System.Directory (doesFileExist)
10import AST.Module as Module
12load :: Binary.Binary a => FilePath -> IO a
13load filePath =
14 do exists <- doesFileExist filePath
15 case exists of
16 False -> Print.failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
17 True -> do
18 bytes <- L.readFile filePath
19 case Binary.decodeOrFail bytes of
20 Right (_, _, binaryInfo) -> return binaryInfo
21 Left (_, offset, err) -> Print.failure $ msg offset err
23 where
24 msg offset err = concat
25 [ "Error reading build artifact: ", filePath, "\n"
26 , " '", err, "' at offset ", show offset, "\n"
27 , " The file was generated by a previous build and may be outdated or corrupt.\n"
28 , " Please remove the file and try again."
29 ]
31isValid :: FilePath -> (String, Module.Interface) -> Either String (String, Module.Interface)
32isValid filePath (name, interface) =
33 let version = iVersion interface in
34 if version == Version.elmVersion
35 then Right (name, interface)
36 else Left $ concat
37 [ "Error reading build artifact: ", filePath, "\n"
38 , " It was generated by version ", show version, " of the compiler,\n"
39 , " but you are using version ", show Version.elmVersion, "\n"
40 , " Please remove the file and try again.\n"
41 ]