fsutils (empty) → 0.1.0
raw patch · 4 files changed
+127/−0 lines, 4 filesdep +basedep +directorydep +filepathsetup-changed
Dependencies added: base, directory, filepath
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- fsutils.cabal +25/−0
- src/System/Path.hs +93/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2012 Anthony Grimes++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ fsutils.cabal view
@@ -0,0 +1,25 @@+-- Initial fsutils.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: fsutils+version: 0.1.0+synopsis: File system utilities for Haskell that are missing from built in libraries.+description: A collection of file system utilities such as recursive+ directory walks and listings.+homepage: https://github.com/Raynes/fsutils+license: MIT+license-file: LICENSE+author: Anthony Grimes+maintainer: i@raynes.me+category: System+build-type: Simple+cabal-version: >=1.8++library+ hs-source-dirs: src+ exposed-modules: System.Path + build-depends: base ==4.5.*, filepath ==1.3.0.*, directory ==1.1.0.*++source-repository head+ type: git+ location: https://github.com/Raynes/fsutils
+ src/System/Path.hs view
@@ -0,0 +1,93 @@+-- | A collection of file system utilities that appear to be missing from+-- Directory, FilePath, Prelude, etc. Some of these may overlap with MissingH+-- but the versions here will probably be more simplistic. Furthermore, this+-- library is focused on this one thing and not a whole bunch of things.+module System.Path+ ( mtreeList+ , fileList+ , walkDir+ , copyDir+ , replaceRoot+ , removeRoot+ , Directory+ , dirPath+ , subDirs+ , files+ , createDir+ ) where+ +import Control.Monad (liftM, filterM, forM_, mapM_)+import System.Directory+import System.FilePath ((</>), addTrailingPathSeparator)+import Data.List ((\\))++-- | Remove useless paths from a list of paths.+filterUseless :: [FilePath] -> [FilePath]+filterUseless = (\\ [".", ".."])++-- | Returns a list of nodes in a tree via a depth-first walk.+mtreeList :: Monad m => (a -> m [a]) -> a -> m [a]+mtreeList children root = do+ xs <- children root+ let subChildren = map (mtreeList children) xs+ joined <- sequence subChildren+ return $ root : concat joined++-- | Get a list of files in path, but not recursively. Removes '.' and '..'.+topFileList :: FilePath -> IO [FilePath]+topFileList path = liftM filterUseless (getDirectoryContents path) >>= return . map (path </>)++-- | Recursively list the contents of a directory. Depth-first.+fileList :: FilePath -> IO [FilePath]+fileList root = mtreeList children root+ where children path = do+ directory <- doesDirectoryExist path+ if directory+ then topFileList path+ else return []++-- | We can use this data type to represent the pieces of a directory.+data Directory = Directory+ { -- | The path of the directory itself.+ dirPath :: FilePath+ -- | All subdirectories of this directory.+ , subDirs :: [FilePath]+ -- | All files contained in this directory.+ , files :: [FilePath]+ }+ deriving (Show)++-- | Creates a Directory instance from a FilePath.+createDir :: FilePath -> IO Directory+createDir path = do+ contents <- topFileList path+ subdirs <- filterM doesDirectoryExist contents+ files <- filterM doesFileExist contents+ return (Directory path subdirs files)+ +-- | Walk a directory depth-first. Similar to Python's os.walk and fs.core/walk+-- from the fs Clojure library.+walkDir :: FilePath -> IO [Directory]+walkDir root = createDir root >>= mtreeList children+ where children path = do+ let dirs = subDirs path+ mapM createDir dirs++-- | Given a root (prefix), remove it from a path. This is useful+-- for getting the filename and subdirs of a path inside of a root.+removeRoot :: FilePath -> FilePath -> FilePath+removeRoot prefix path = drop (length $ addTrailingPathSeparator prefix) path++-- | Given a root path, a new root path, and a path to be changed,+-- removes the old root from the path and replaces it with to.+replaceRoot :: FilePath -> FilePath -> FilePath -> FilePath+replaceRoot root to path = to </> removeRoot root path++-- | Copy a directory recursively. Moves every file, creates every directory.+copyDir :: FilePath -> FilePath -> IO ()+copyDir from to = do+ createDirectoryIfMissing True to+ walked <- walkDir from+ forM_ walked $ \(Directory _ dirs files) -> do+ mapM_ (createDirectoryIfMissing True . (replaceRoot from to)) dirs+ mapM_ (\path -> copyFile path (replaceRoot from to path)) files