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