on cf47310 help sign in
Idris-dev/src/Util/System.hs
102 lines 3.7 kB history raw
1{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2module Util.System(tempfile,withTempdir,rmFile,catchIO, isWindows,
3 writeSource, writeSourceText, readSource,
4 setupBundledCC, isATTY) where
6-- System helper functions.
8import Control.Exception as CE
9import Control.Monad (when)
10import qualified Data.Text as T
11import qualified Data.Text.IO as TIO
13import Foreign.C
15import System.Directory (getTemporaryDirectory
16 , removeFile
17 , removeDirectoryRecursive
18 , createDirectoryIfMissing
19 , doesDirectoryExist
20 )
21import System.FilePath ((</>), normalise, isAbsolute, dropFileName)
22import System.IO
23import System.Info
24import System.IO.Error
26#ifdef FREESTANDING
27import Tools_idris
28import System.Environment (getEnv, setEnv, getExecutablePath)
29#endif
31catchIO :: IO a -> (IOError -> IO a) -> IO a
32catchIO = CE.catch
34throwIO :: IOError -> IO a
35throwIO = CE.throw
37isWindows :: Bool
38isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
40tempfile :: IO (FilePath, Handle)
41tempfile = do dir <- getTemporaryDirectory
42 openTempFile (normalise dir) "idris"
44-- | Read a source file, same as readFile but make sure the encoding is utf-8.
45readSource :: FilePath -> IO String
46readSource f = do h <- openFile f ReadMode
47 hSetEncoding h utf8
48 hGetContents h
50-- | Write a source file, same as writeFile except the encoding is set to utf-8
51writeSource :: FilePath -> String -> IO ()
52writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
54-- | Write a utf-8 source file from Text
55writeSourceText :: FilePath -> T.Text -> IO ()
56writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
58foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
60isATTY :: IO Bool
61isATTY = do
62 tty <- isATTYRaw 1 -- fd stdout
63 return $ tty /= 0
65withTempdir :: String -> (FilePath -> IO a) -> IO a
66withTempdir subdir callback
67 = do dir <- getTemporaryDirectory
68 let tmpDir = (normalise dir) </> subdir
69 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
70 (\ ioError -> if isAlreadyExistsError ioError then return False
71 else throw ioError
72 )
73 result <- callback tmpDir
74 when removeLater $ removeDirectoryRecursive tmpDir
75 return result
77rmFile :: FilePath -> IO ()
78rmFile f = do putStrLn $ "Removing " ++ f
79 catchIO (removeFile f)
80 (\ioerr -> putStrLn $ "WARNING: Cannot remove file "
81 ++ f ++ ", Error msg:" ++ show ioerr)
83setupBundledCC :: IO()
84#ifdef FREESTANDING
85setupBundledCC = when hasBundledToolchain
86 $ do
87 exePath <- getExecutablePath
88 path <- getEnv "PATH"
89 tcDir <- return getToolchainDir
90 absolute <- return $ isAbsolute tcDir
91 target <- return $
92 if absolute
93 then tcDir
94 else dropFileName exePath ++ tcDir
95 let pathSep = if isWindows then ";" else ":"
96 present <- doesDirectoryExist target
97 when present
98 $ do newPath <- return $ target ++ pathSep ++ path
99 setEnv "PATH" newPath
100#else
101setupBundledCC = return ()
102#endif