3Description : Utilities for interacting with the system.
6Maintainer : The Idris Community.
8{-# LANGUAGE CPP, ForeignFunctionInterface #-}
9module Util.System( tempfile
22import Control.Exception as CE
23import Control.Monad (when)
24import qualified Data.Text as T
25import qualified Data.Text.IO as TIO
27import System.Directory (createDirectoryIfMissing, getTemporaryDirectory,
28 removeDirectoryRecursive, removeFile)
29import System.FilePath (normalise, (</>))
35import Data.List (intercalate)
36import System.Directory (doesDirectoryExist)
37import System.Environment (getEnv, getExecutablePath, setEnv)
38import System.FilePath (dropFileName, isAbsolute, searchPathSeparator)
43import Graphics.Win32.Misc (getStdHandle, sTD_OUTPUT_HANDLE)
44import System.Console.MinTTY (isMinTTYHandle)
47catchIO :: IO a -> (IOError -> IO a) -> IO a
51isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
53-- | Create a temp file with the extensiom ext (in the format ".xxx")
54tempfile :: String -> IO (FilePath, Handle)
55tempfile ext = do dir <- getTemporaryDirectory
56 openTempFile (normalise dir) $ "idris" ++ ext
58-- | Read a source file, same as readFile but make sure the encoding is utf-8.
59readSource :: FilePath -> IO String
60readSource f = do h <- openFile f ReadMode
64-- | Read a source file, make sure that the it all has been read before exiting the function.
65-- | This is useful when we want to write the file again and need it to be closed.
66readSourceStrict :: FilePath -> IO String
67readSourceStrict f = withFile f ReadMode $
71 length src `seq` return src
73-- | Write a source file, same as writeFile except the encoding is set to utf-8
74writeSource :: FilePath -> String -> IO ()
75writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
77-- | Write a utf-8 source file from Text
78writeSourceText :: FilePath -> T.Text -> IO ()
79writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
81foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
85 tty <- isATTYRaw 1 -- fd stdout
87 return $ (tty /= 0) || mintty
89-- | Return 'True' if the process's standard output is attached to a MinTTY
90-- console (e.g., Cygwin or MSYS) on Windows. Return 'False' otherwise.
92-- Unfortunately, we must check this separately since 'isATTY' always returns
93-- 'False' on MinTTY consoles.
97 h <- getStdHandle sTD_OUTPUT_HANDLE
100isMinTTY = return False
103withTempdir :: String -> (FilePath -> IO a) -> IO a
104withTempdir subdir callback
105 = do dir <- getTemporaryDirectory
106 let tmpDir = normalise dir </> subdir
107 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
108 (\ ioError -> if isAlreadyExistsError ioError then return False
111 result <- callback tmpDir
112 when removeLater $ removeDirectoryRecursive tmpDir
115rmFile :: FilePath -> IO ()
117 result <- try (removeFile f)
119 Right _ -> putStrLn $ "Removed: " ++ f
120 Left err -> handleExists err
122 | isDoesNotExistError e = return ()
123 | otherwise = putStrLn $ "WARNING: Cannot remove file "
124 ++ f ++ ", Error msg:" ++ show e
126setupBundledCC :: IO()
128setupBundledCC = when hasBundledToolchain
130 exePath <- getExecutablePath
131 path <- getEnv "PATH"
132 tcDir <- return getToolchainDir
133 absolute <- return $ isAbsolute tcDir
137 else dropFileName exePath ++ tcDir
138 present <- doesDirectoryExist target
140 newPath <- return $ intercalate [searchPathSeparator] [target, path]
141 setEnv "PATH" newPath
143setupBundledCC = 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