2014-01-04 14:19:30 +00:00
|
|
|
{-# OPTIONS_GHC -W #-}
|
2014-01-04 14:59:24 +00:00
|
|
|
module Build.Interface (load,isValid) where
|
2014-01-04 14:19:30 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.Binary as Binary
|
|
|
|
|
|
|
|
import qualified Elm.Internal.Version as Version
|
|
|
|
import System.Directory (doesFileExist)
|
2014-01-04 14:59:24 +00:00
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
2014-01-04 14:19:30 +00:00
|
|
|
|
|
|
|
import SourceSyntax.Module
|
|
|
|
|
2014-01-04 14:59:24 +00:00
|
|
|
load :: Binary.Binary a => FilePath -> IO a
|
|
|
|
load filePath =
|
|
|
|
do exists <- doesFileExist filePath
|
|
|
|
case exists of
|
|
|
|
False -> 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) -> failure $ msg offset err
|
|
|
|
|
|
|
|
where
|
|
|
|
failure err = do hPutStrLn stderr err
|
|
|
|
exitFailure
|
|
|
|
|
|
|
|
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."
|
|
|
|
]
|
2014-01-04 14:19:30 +00:00
|
|
|
|
|
|
|
isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
|
|
|
|
isValid filePath (name, interface) =
|
2014-01-04 14:59:24 +00:00
|
|
|
let version = iVersion interface in
|
|
|
|
if version == Version.elmVersion
|
2014-01-04 14:19:30 +00:00
|
|
|
then Right (name, interface)
|
|
|
|
else Left $ concat
|
|
|
|
[ "Error reading build artifact: ", filePath, "\n"
|
2014-01-04 14:59:24 +00:00
|
|
|
, " It was generated by version ", show version, " of the compiler,\n"
|
|
|
|
, " but you are using version ", show Version.elmVersion, "\n"
|
2014-01-04 14:19:30 +00:00
|
|
|
, " Please remove the file and try again.\n"
|
|
|
|
]
|