1{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2module Util.System(tempfile,withTempdir,rmFile,catchIO, isWindows,
3 writeSource, writeSourceText, readSource,
4 setupBundledCC, isATTY) where
5-- System helper functions.
7import Control.Exception as CE
8import Control.Monad (when)
9import qualified Data.Text as T
10import qualified Data.Text.IO as TIO
14import System.Directory (getTemporaryDirectory
16 , removeDirectoryRecursive
17 , createDirectoryIfMissing
19import System.FilePath ((</>), normalise)
26import System.FilePath (isAbsolute, dropFileName)
27import System.Directory (doesDirectoryExist)
28import System.Environment (getEnv, setEnv, getExecutablePath)
31catchIO :: IO a -> (IOError -> IO a) -> IO a
35isWindows = os `elem` ["win32", "mingw32", "cygwin32"]
37-- | Create a temp file with the extensiom ext (in the format ".xxx")
38tempfile :: String -> IO (FilePath, Handle)
39tempfile ext = do dir <- getTemporaryDirectory
40 openTempFile (normalise dir) $ "idris" ++ ext
42-- | Read a source file, same as readFile but make sure the encoding is utf-8.
43readSource :: FilePath -> IO String
44readSource f = do h <- openFile f ReadMode
48-- | Write a source file, same as writeFile except the encoding is set to utf-8
49writeSource :: FilePath -> String -> IO ()
50writeSource f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> hPutStr h s)
52-- | Write a utf-8 source file from Text
53writeSourceText :: FilePath -> T.Text -> IO ()
54writeSourceText f s = withFile f WriteMode (\h -> hSetEncoding h utf8 >> TIO.hPutStr h s)
56foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
60 tty <- isATTYRaw 1 -- fd stdout
63withTempdir :: String -> (FilePath -> IO a) -> IO a
64withTempdir subdir callback
65 = do dir <- getTemporaryDirectory
66 let tmpDir = normalise dir </> subdir
67 removeLater <- catchIO (createDirectoryIfMissing True tmpDir >> return True)
68 (\ ioError -> if isAlreadyExistsError ioError then return False
71 result <- callback tmpDir
72 when removeLater $ removeDirectoryRecursive tmpDir
75rmFile :: FilePath -> IO ()
77 result <- try (removeFile f)
79 Right _ -> putStrLn $ "Removed: " ++ f
80 Left err -> handleExists err
82 | isDoesNotExistError e = return ()
83 | otherwise = putStrLn $ "WARNING: Cannot remove file "
84 ++ f ++ ", Error msg:" ++ show e
88setupBundledCC = when hasBundledToolchain
90 exePath <- getExecutablePath
92 tcDir <- return getToolchainDir
93 absolute <- return $ isAbsolute tcDir
97 else dropFileName exePath ++ tcDir
98 let pathSep = if isWindows then ";" else ":"
99 present <- doesDirectoryExist target
101 $ do newPath <- return $ target ++ pathSep ++ path
102 setEnv "PATH" newPath
104setupBundledCC = 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