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
20import System.FilePath ((</>), normalise)
27import System.FilePath (isAbsolute, dropFileName)
28import System.Directory (doesDirectoryExist)
29import System.Environment (getEnv, setEnv, getExecutablePath)
32catchIO :: IO a -> (IOError -> IO a) -> IO a
35throwIO :: IOError -> IO a
39isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
41-- | Create a temp file with the extensiom ext (in the format ".xxx")
42tempfile :: String -> IO (FilePath, Handle)
43tempfile ext = do dir <- getTemporaryDirectory
44 openTempFile (normalise dir) $ "idris" ++ ext
46-- | Read a source file, same as readFile but make sure the encoding is utf-8.
47readSource :: FilePath -> IO String
48readSource f = do h <- openFile f ReadMode
52-- | Write a source file, same as writeFile except the encoding is set to utf-8
53writeSource :: FilePath -> String -> IO ()
54writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
56-- | Write a utf-8 source file from Text
57writeSourceText :: FilePath -> T.Text -> IO ()
58writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
60foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
64 tty <- isATTYRaw 1 -- fd stdout
67withTempdir :: String -> (FilePath -> IO a) -> IO a
68withTempdir subdir callback
69 = do dir <- getTemporaryDirectory
70 let tmpDir = normalise dir </> subdir
71 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
72 (\ ioError -> if isAlreadyExistsError ioError then return False
75 result <- callback tmpDir
76 when removeLater $ removeDirectoryRecursive tmpDir
79rmFile :: FilePath -> IO ()
81 result <- try (removeFile f)
83 Right _ -> putStrLn $ "Removed: " ++ f
84 Left err -> handleExists err
86 | isDoesNotExistError e = return ()
87 | otherwise = putStrLn $ "WARNING: Cannot remove file "
88 ++ f ++ ", Error msg:" ++ show e
92setupBundledCC = when hasBundledToolchain
94 exePath <- getExecutablePath
96 tcDir <- return getToolchainDir
97 absolute <- return $ isAbsolute tcDir
101 else dropFileName exePath ++ tcDir
102 let pathSep = if isWindows then ";" else ":"
103 present <- doesDirectoryExist target
105 $ do newPath <- return $ target ++ pathSep ++ path
106 setEnv "PATH" newPath
108setupBundledCC = 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