3Description : Utilities for interacting with the system.
6Maintainer : The Idris Community.
8{-# LANGUAGE CPP, ForeignFunctionInterface #-}
9module Util.System( tempfile
21import Control.Exception as CE
22import Control.Monad (when)
23import qualified Data.Text as T
24import qualified Data.Text.IO as TIO
28import System.Directory (getTemporaryDirectory
30 , removeDirectoryRecursive
31 , createDirectoryIfMissing
33import System.FilePath ((</>), normalise)
40import Data.List (intercalate)
41import System.FilePath (isAbsolute, dropFileName, searchPathSeparator)
42import System.Directory (doesDirectoryExist)
43import System.Environment (getEnv, setEnv, getExecutablePath)
46catchIO :: IO a -> (IOError -> IO a) -> IO a
50isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
52-- | Create a temp file with the extensiom ext (in the format ".xxx")
53tempfile :: String -> IO (FilePath, Handle)
54tempfile ext = do dir <- getTemporaryDirectory
55 openTempFile (normalise dir) $ "idris" ++ ext
57-- | Read a source file, same as readFile but make sure the encoding is utf-8.
58readSource :: FilePath -> IO String
59readSource f = do h <- openFile f ReadMode
63-- | Write a source file, same as writeFile except the encoding is set to utf-8
64writeSource :: FilePath -> String -> IO ()
65writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
67-- | Write a utf-8 source file from Text
68writeSourceText :: FilePath -> T.Text -> IO ()
69writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
71foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
75 tty <- isATTYRaw 1 -- fd stdout
78withTempdir :: String -> (FilePath -> IO a) -> IO a
79withTempdir subdir callback
80 = do dir <- getTemporaryDirectory
81 let tmpDir = normalise dir </> subdir
82 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
83 (\ ioError -> if isAlreadyExistsError ioError then return False
86 result <- callback tmpDir
87 when removeLater $ removeDirectoryRecursive tmpDir
90rmFile :: FilePath -> IO ()
92 result <- try (removeFile f)
94 Right _ -> putStrLn $ "Removed: " ++ f
95 Left err -> handleExists err
97 | isDoesNotExistError e = return ()
98 | otherwise = putStrLn $ "WARNING: Cannot remove file "
99 ++ f ++ ", Error msg:" ++ show e
101setupBundledCC :: IO()
103setupBundledCC = when hasBundledToolchain
105 exePath <- getExecutablePath
106 path <- getEnv "PATH"
107 tcDir <- return getToolchainDir
108 absolute <- return $ isAbsolute tcDir
112 else dropFileName exePath ++ tcDir
113 present <- doesDirectoryExist target
115 newPath <- return $ intercalate [searchPathSeparator] [target, path]
116 setEnv "PATH" newPath
118setupBundledCC = 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