elm/compiler/Build/Interface.hs
2014-01-20 14:01:53 +01:00

41 lines
1.6 KiB
Haskell

{-# OPTIONS_GHC -W #-}
module Build.Interface (load,isValid) where
import qualified Data.ByteString.Lazy as L
import qualified Data.Binary as Binary
import qualified Build.Print as Print
import qualified Elm.Internal.Version as Version
import System.Directory (doesFileExist)
import SourceSyntax.Module
load :: Binary.Binary a => FilePath -> IO a
load filePath =
do exists <- doesFileExist filePath
case exists of
False -> Print.failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
True -> do
bytes <- L.readFile filePath
case Binary.decodeOrFail bytes of
Right (_, _, binaryInfo) -> return binaryInfo
Left (_, offset, err) -> Print.failure $ msg offset err
where
msg offset err = concat
[ "Error reading build artifact: ", filePath, "\n"
, " '", err, "' at offset ", show offset, "\n"
, " The file was generated by a previous build and may be outdated or corrupt.\n"
, " Please remove the file and try again."
]
isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
isValid filePath (name, interface) =
let version = iVersion interface in
if version == Version.elmVersion
then Right (name, interface)
else Left $ concat
[ "Error reading build artifact: ", filePath, "\n"
, " It was generated by version ", show version, " of the compiler,\n"
, " but you are using version ", show Version.elmVersion, "\n"
, " Please remove the file and try again.\n"
]