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 System.FilePath (isAbsolute, dropFileName)
41import System.Directory (doesDirectoryExist)
42import System.Environment (getEnv, setEnv, getExecutablePath)
45catchIO :: IO a -> (IOError -> IO a) -> IO a
49isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
51-- | Create a temp file with the extensiom ext (in the format ".xxx")
52tempfile :: String -> IO (FilePath, Handle)
53tempfile ext = do dir <- getTemporaryDirectory
54 openTempFile (normalise dir) $ "idris" ++ ext
56-- | Read a source file, same as readFile but make sure the encoding is utf-8.
57readSource :: FilePath -> IO String
58readSource f = do h <- openFile f ReadMode
62-- | Write a source file, same as writeFile except the encoding is set to utf-8
63writeSource :: FilePath -> String -> IO ()
64writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
66-- | Write a utf-8 source file from Text
67writeSourceText :: FilePath -> T.Text -> IO ()
68writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
70foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
74 tty <- isATTYRaw 1 -- fd stdout
77withTempdir :: String -> (FilePath -> IO a) -> IO a
78withTempdir subdir callback
79 = do dir <- getTemporaryDirectory
80 let tmpDir = normalise dir </> subdir
81 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
82 (\ ioError -> if isAlreadyExistsError ioError then return False
85 result <- callback tmpDir
86 when removeLater $ removeDirectoryRecursive tmpDir
89rmFile :: FilePath -> IO ()
91 result <- try (removeFile f)
93 Right _ -> putStrLn $ "Removed: " ++ f
94 Left err -> handleExists err
96 | isDoesNotExistError e = return ()
97 | otherwise = putStrLn $ "WARNING: Cannot remove file "
98 ++ f ++ ", Error msg:" ++ show e
100setupBundledCC :: IO()
102setupBundledCC = when hasBundledToolchain
104 exePath <- getExecutablePath
105 path <- getEnv "PATH"
106 tcDir <- return getToolchainDir
107 absolute <- return $ isAbsolute tcDir
111 else dropFileName exePath ++ tcDir
112 let pathSep = if isWindows then ";" else ":"
113 present <- doesDirectoryExist target
115 $ do newPath <- return $ target ++ pathSep ++ 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