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
15import System.Directory (getTemporaryDirectory
17 , removeDirectoryRecursive
18 , createDirectoryIfMissing
21import System.FilePath ((</>), normalise, isAbsolute, dropFileName)
28import System.Environment (getEnv, setEnv, getExecutablePath)
31catchIO :: IO a -> (IOError -> IO a) -> IO a
34throwIO :: IOError -> IO a
38isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
40-- | Create a temp file with the extensiom ext (in the format ".xxx")
41tempfile :: String -> IO (FilePath, Handle)
42tempfile ext = do dir <- getTemporaryDirectory
43 openTempFile (normalise dir) $ "idris" ++ ext
45-- | Read a source file, same as readFile but make sure the encoding is utf-8.
46readSource :: FilePath -> IO String
47readSource f = do h <- openFile f ReadMode
51-- | Write a source file, same as writeFile except the encoding is set to utf-8
52writeSource :: FilePath -> String -> IO ()
53writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
55-- | Write a utf-8 source file from Text
56writeSourceText :: FilePath -> T.Text -> IO ()
57writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
59foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
63 tty <- isATTYRaw 1 -- fd stdout
66withTempdir :: String -> (FilePath -> IO a) -> IO a
67withTempdir subdir callback
68 = do dir <- getTemporaryDirectory
69 let tmpDir = (normalise dir) </> subdir
70 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
71 (\ ioError -> if isAlreadyExistsError ioError then return False
74 result <- callback tmpDir
75 when removeLater $ removeDirectoryRecursive tmpDir
78rmFile :: FilePath -> IO ()
79rmFile f = do putStrLn $ "Removing " ++ f
80 catchIO (removeFile f)
81 (\ioerr -> putStrLn $ "WARNING: Cannot remove file "
82 ++ f ++ ", Error msg:" ++ show ioerr)
86setupBundledCC = when hasBundledToolchain
88 exePath <- getExecutablePath
90 tcDir <- return getToolchainDir
91 absolute <- return $ isAbsolute tcDir
95 else dropFileName exePath ++ tcDir
96 let pathSep = if isWindows then ";" else ":"
97 present <- doesDirectoryExist target
99 $ do newPath <- return $ target ++ pathSep ++ path
100 setEnv "PATH" newPath
102setupBundledCC = return ()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list