module Haste.Environment (
hasteSysDir, jsmodSysDir, pkgSysDir, pkgSysLibDir, jsDir,
hasteUserDir, jsmodUserDir, pkgUserDir, pkgUserLibDir,
hasteGhcLibDir,
hostWordSize,
ghcPkgBinary, ghcBinary,
hasteBinDir, hasteBinary, hastePkgBinary, hasteCabalBinary,
closureCompiler, bootFile,
portableHaste, hasteNeedsReboot
) where
import System.IO.Unsafe
import Data.Bits
import Foreign.C.Types (CIntPtr)
import Control.Shell hiding (hClose)
import Paths_haste_compiler
import System.IO
import System.Info
import Haste.GHCPaths (ghcPkgBinary, ghcBinary)
import Haste.Version
#if defined(PORTABLE)
import System.Environment (getExecutablePath)
#endif
hasteVersionSubDir :: FilePath
hasteVersionSubDir = arch ++ "-" ++ myos ++ "-" ++ myver
where
myos
| os == "mingw32" = "windows"
| otherwise = os
myver = showBootVersion bootVersion
hasteGhcLibDir :: FilePath
hasteGhcLibDir = hasteSysDir
#if defined(PORTABLE)
portableHaste :: Bool
portableHaste = True
hasteSysDir :: FilePath
hasteSysDir = dir </> hasteVersionSubDir
where
dir = joinPath . init . init . splitPath $ unsafePerformIO getExecutablePath
hasteBinDir :: FilePath
hasteBinDir = takeDirectory $ unsafePerformIO getExecutablePath
jsDir :: FilePath
jsDir = hasteSysDir </> "js"
#else
portableHaste :: Bool
portableHaste = False
hasteSysDir :: FilePath
hasteSysDir = hasteUserDir
hasteBinDir :: FilePath
hasteBinDir = unsafePerformIO $ getBinDir
jsDir :: FilePath
jsDir = unsafePerformIO $ getDataDir
#endif
hasteUserDir :: FilePath
Right hasteUserDir =
unsafePerformIO . shell . withAppDirectory "haste" $ \d -> do
return $ d </> hasteVersionSubDir
jsmodSysDir :: FilePath
jsmodSysDir = hasteSysDir
pkgSysLibDir :: FilePath
pkgSysLibDir = hasteSysDir
pkgSysDir :: FilePath
pkgSysDir = hasteSysDir </> "package.conf.d"
jsmodUserDir :: FilePath
jsmodUserDir = hasteUserDir
pkgUserLibDir :: FilePath
pkgUserLibDir = hasteUserDir
pkgUserDir :: FilePath
pkgUserDir = hasteUserDir </> "package.conf.d"
hostWordSize :: Int
#if __GLASGOW_HASKELL__ >= 708
hostWordSize = finiteBitSize (undefined :: CIntPtr)
#else
hostWordSize = bitSize (undefined :: CIntPtr)
#endif
binaryExt :: String
binaryExt
| os == "mingw32" = ".exe"
| otherwise = ""
hasteBinary :: FilePath
hasteBinary = hasteBinDir </> "hastec" ++ binaryExt
hastePkgBinary :: FilePath
hastePkgBinary = hasteBinDir </> "haste-pkg" ++ binaryExt
hasteCabalBinary :: FilePath
hasteCabalBinary = hasteBinDir </> "haste-cabal" ++ binaryExt
closureCompiler :: FilePath
closureCompiler = hasteSysDir </> "compiler.jar"
bootFile :: FilePath
bootFile = hasteUserDir </> "booted"
hasteNeedsReboot :: Bool
#ifdef PORTABLE
hasteNeedsReboot = False
#else
hasteNeedsReboot = unsafePerformIO $ do
exists <- shell $ isFile bootFile
case exists of
Right True -> do
fh <- openFile bootFile ReadMode
bootedVerString <- hGetLine fh
hClose fh
case parseBootVersion bootedVerString of
Just (BootVer hasteVer ghcVer) ->
return $ hasteVer /= hasteVersion || ghcVer /= ghcVersion
_ ->
return True
_ -> do
return True
#endif