| Cabal-1.6.0.3: A framework for packaging Haskell software | Source code | Contents | Index
Distribution.Simple.Utils |
Portability | portable
Maintainer | cabal-devel@haskell.org
Contents
logging and errors running programs copying files file names finding files simple file globbing temp files and dirs .cabal and .buildinfo files reading and writing files safely Unicode generic utils
Description
A large and somewhat miscellaneous collection of utility functions used throughout the rest of the Cabal lib and in other tools that use the Cabal lib like `cabal-install`. It has a very simple set of logging actions. It has low level functions for running programs, a bunch of wrappers for various directory and file functions that do extra logging.
Synopsis
cabalVersion :: Version
cabalBootstrapping :: Bool
die :: String -> IO a
dieWithLocation :: FilePath -> Maybe Int -> String -> IO a
warn :: Verbosity -> String -> IO ()
notice :: Verbosity -> String -> IO ()
setupMessage :: Verbosity -> String -> PackageIdentifier -> IO ()
info :: Verbosity -> String -> IO ()
debug :: Verbosity -> String -> IO ()
chattyTry :: String -> IO () -> IO ()
rawSystemExit :: Verbosity -> FilePath -> [String] -> IO ()
rawSystemStdout :: Verbosity -> FilePath -> [String] -> IO String
rawSystemStdout' :: Verbosity -> FilePath -> [String] -> IO (String, ExitCode)
maybeExit :: IO ExitCode -> IO ()
xargs :: Int -> ([String] -> IO ()) -> [String] -> [String] -> IO ()
smartCopySources :: Verbosity -> [FilePath] -> FilePath -> [ModuleName] -> [String] -> IO ()
createDirectoryIfMissingVerbose :: Verbosity -> Bool -> FilePath -> IO ()
copyFileVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
copyDirectoryRecursiveVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
copyFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
currentDir :: FilePath
findFile :: [FilePath] -> FilePath -> IO FilePath
findFileWithExtension :: [String] -> [FilePath] -> FilePath -> IO (Maybe FilePath)
findFileWithExtension' :: [String] -> [FilePath] -> FilePath -> IO (Maybe (FilePath, FilePath))
matchFileGlob :: FilePath -> IO [FilePath]
matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath]
withTempFile :: FilePath -> String -> (FilePath -> Handle -> IO a) -> IO a
withTempDirectory :: Verbosity -> FilePath -> IO a -> IO a
defaultPackageDesc :: Verbosity -> IO FilePath
findPackageDesc :: FilePath -> IO FilePath
defaultHookedPackageDesc :: IO (Maybe FilePath)
findHookedPackageDesc :: FilePath -> IO (Maybe FilePath)
withFileContents :: FilePath -> (String -> IO a) -> IO a
writeFileAtomic :: FilePath -> String -> IO ()
rewriteFile :: FilePath -> String -> IO ()
fromUTF8 :: String -> String
toUTF8 :: String -> String
readUTF8File :: FilePath -> IO String
withUTF8FileContents :: FilePath -> (String -> IO a) -> IO a
writeUTF8File :: FilePath -> String -> IO ()
equating :: Eq a => (b -> a) -> b -> b -> Bool
comparing :: Ord a => (b -> a) -> b -> b -> Ordering
isInfixOf :: String -> String -> Bool
intercalate :: [a] -> [[a]] -> [a]
lowercase :: String -> String
wrapText :: String -> String
wrapLine :: Int -> [String] -> [[String]]
Documentation
cabalVersion :: Version | Source
cabalBootstrapping :: Bool | Source
logging and errors
die :: String -> IO a | Source
dieWithLocation :: FilePath -> Maybe Int -> String -> IO a | Source
warn :: Verbosity -> String -> IO () | Source
Non fatal conditions that may be indicative of an error or problem. We display these at the `normal` verbosity level.
notice :: Verbosity -> String -> IO () | Source
Useful status messages. We display these at the `normal` verbosity level. This is for the ordinary helpful status messages that users see. Just enough information to know that things are working but not floods of detail.
setupMessage :: Verbosity -> String -> PackageIdentifier -> IO () | Source
info :: Verbosity -> String -> IO () | Source
More detail on the operation of some action. We display these messages when the verbosity level is `verbose`
debug :: Verbosity -> String -> IO () | Source
Detailed internal debugging information We display these messages when the verbosity level is `deafening`
chattyTry | Source
:: String | a description of the action we were attempting
-> IO () | the action itself
-> IO () |
Perform an IO action, catching any IO exceptions and printing an error if one occurs.
running programs
rawSystemExit :: Verbosity -> FilePath -> [String] -> IO () | Source
rawSystemStdout :: Verbosity -> FilePath -> [String] -> IO String | Source
Run a command and return its output. The output is assumed to be encoded as UTF8.
rawSystemStdout' :: Verbosity -> FilePath -> [String] -> IO (String, ExitCode) | Source
maybeExit :: IO ExitCode -> IO () | Source
xargs :: Int -> ([String] -> IO ()) -> [String] -> [String] -> IO () | Source
Like the unix xargs program. Useful for when we've got very long command lines that might overflow an OS limit on command line length and so you need to invoke a command multiple times to get all the args in. Use it with either of the rawSystem variants above. For example:
     xargs (32*1024) (rawSystemExit verbosity) prog fixedArgs bigArgs
copying files
smartCopySources | Source
:: Verbosity | verbosity
-> [FilePath] | build prefix (location of objects)
-> FilePath | Target directory
-> [ModuleName] | Modules
-> [String] | search suffixes
-> IO () |
Copy the source files into the right directory. Looks in the build prefix for files that look like the input modules, based on the input search suffixes. It copies the files into the target directory.
createDirectoryIfMissingVerbose :: Verbosity -> Bool -> FilePath -> IO () | Source
copyFileVerbose :: Verbosity -> FilePath -> FilePath -> IO () | Source
copyDirectoryRecursiveVerbose :: Verbosity -> FilePath -> FilePath -> IO () | Source
copyFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO () | Source
Copies a bunch of files to a target directory, preserving the directory structure in the target location. The target directories are created if they do not exist. The files are identified by a pair of base directory and a path relative to that base. It is only the relative part that is preserved in the destination. For example:
     copyFiles normal "dist/src"
        [("", "src/Foo.hs"), ("dist/build/", "src/Bar.hs")]
This would copy "src/Foo.hs" to "dist/src/src/Foo.hs" and copy "dist/build/src/Bar.hs" to "dist/src/src/Bar.hs". This operation is not atomic. Any IO failure during the copy (including any missing source files) leaves the target in an unknown state so it is best to use it with a freshly created directory so that it can be simply deleted if anything goes wrong.
file names
currentDir :: FilePath | Source
The path name that represents the current directory. In Unix, it's `"."`, but this is system-specific. (E.g. AmigaOS uses the empty string `""` for the current directory.)
finding files
findFile | Source
:: [FilePath] | search locations
-> FilePath | File Name
-> IO FilePath |
findFileWithExtension :: [String] -> [FilePath] -> FilePath -> IO (Maybe FilePath) | Source
findFileWithExtension' :: [String] -> [FilePath] -> FilePath -> IO (Maybe (FilePath, FilePath)) | Source
simple file globbing
matchFileGlob :: FilePath -> IO [FilePath] | Source
matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath] | Source
temp files and dirs
withTempFile | Source
:: |
=> FilePath | Temp dir to create the file in
-> String | File name template. See `openTempFile`.
-> FilePath -> Handle -> IO a |
-> IO a |
Use a temporary filename that doesn't already exist.
withTempDirectory :: Verbosity -> FilePath -> IO a -> IO a | Source
Use a temporary directory. Use this exact given dir which must not already exist.
.cabal and .buildinfo files
defaultPackageDesc :: Verbosity -> IO FilePath | Source
Package description file (pkgname`.cabal`)
findPackageDesc | Source
:: FilePath | Where to look
-> IO FilePath | pkgname.cabal
Find a package description file in the given directory. Looks for `.cabal` files.
defaultHookedPackageDesc :: IO (Maybe FilePath) | Source
Optional auxiliary package information file (pkgname`.buildinfo`)
findHookedPackageDesc | Source
:: FilePath | Directory to search
-> IO (Maybe FilePath) | dir`/`pkgname`.buildinfo`, if present
Find auxiliary package information in the given directory. Looks for `.buildinfo` files.
reading and writing files safely
withFileContents :: FilePath -> (String -> IO a) -> IO a | Source
Gets the contents of a file, but guarantee that it gets closed. The file is read lazily but if it is not fully consumed by the action then the remaining input is truncated and the file is closed.
writeFileAtomic :: FilePath -> String -> IO () | Source
Writes a file atomically. The file is either written sucessfully or an IO exception is raised and the original file is left unchanged. Warning: On Windows this operation is very nearly but not quite atomic. See below. On Posix it works by writing a temporary file and atomically renaming over the top any pre-existing target file with the temporary one. On Windows it is not possible to rename over an existing file so the target file has to be deleted before the temporary file is renamed to the target. Therefore there is a race condition between the existing file being removed and the temporary file being renamed. Another thread could write to the target or change the permission on the target directory between the deleting and renaming steps. An exception would be raised but the target file would either no longer exist or have the content as written by the other thread. On windows it is not possible to delete a file that is open by a process. This case will give an IO exception but the atomic property is not affected.
rewriteFile :: FilePath -> String -> IO () | Source
Write a file but only if it would have new content. If we would be writing the same as the existing content then leave the file as is so that we do not update the file's modification time.
Unicode
fromUTF8 :: String -> String | Source
toUTF8 :: String -> String | Source
readUTF8File :: FilePath -> IO String | Source
Reads a UTF8 encoded text file as a Unicode String Reads lazily using ordinary `readFile`.
withUTF8FileContents :: FilePath -> (String -> IO a) -> IO a | Source
Reads a UTF8 encoded text file as a Unicode String Same behaviour as `withFileContents`.
writeUTF8File :: FilePath -> String -> IO () | Source
Writes a Unicode String as a UTF8 encoded text file. Uses `writeFileAtomic`, so provides the same guarantees.
generic utils
equating :: Eq a => (b -> a) -> b -> b -> Bool | Source
comparing :: Ord a => (b -> a) -> b -> b -> Ordering | Source
isInfixOf :: String -> String -> Bool | Source
intercalate :: [a] -> [[a]] -> [a] | Source
lowercase :: String -> String | Source
wrapText :: String -> String | Source
Wraps text to the default line width. Existing newlines are preserved.
wrapLine :: Int -> [String] -> [[String]] | Source
Wraps a list of words to a list of lines of words of a particular width.
Produced by Haddock version 2.4.2