Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Directory (getBugList, writeIssues) where
  2. import System.Directory
  3. import Control.Monad.Error
  4. import System.FilePath
  5. import Issue
  6.  
  7. startsWith ::  (Eq a) => [a] -> [a] -> Bool
  8. startsWith xs ys | xs == take (length xs) ys = True
  9.          | otherwise = False
  10.  
  11. createNewDirectory ::  ErrorT String IO FilePath
  12. createNewDirectory = do
  13.   dir <- fmap ((</> ".fiba")) $ liftIO $ getCurrentDirectory
  14.   liftIO $ createDirectory dir
  15.   return dir
  16.  
  17. getBugList :: ErrorT String IO [Issue]
  18. getBugList = do
  19.       maybedir <- findBugDir
  20.       case maybedir of
  21.        Nothing -> createNewDirectory >> return []
  22.        Just dir -> do
  23.          contents <- liftIO $ getDirectoryContents dir
  24.          files <- filterM (liftIO . notDirectory . (dir </>)) $ filter (not . flip elem [".config"]) contents
  25.          mapM (parseIssue . (dir </>)) files
  26.   where
  27.     parseIssue path = do
  28.       content <- liftIO $ readFile path
  29.       case reads content of
  30.        [] -> throwError $ "Could not parse: " ++ path
  31.        [(a,_)] -> return a
  32.     notDirectory = fmap not . doesDirectoryExist
  33.  
  34. findBugDir ::  ErrorT String IO (Maybe FilePath)
  35. findBugDir = do
  36.   curdir <- liftIO $ getCurrentDirectory
  37.   recurse curdir
  38.   where
  39.     recurse "/" = return Nothing
  40.     recurse path = do
  41.       contents <- liftIO $ getDirectoryContents path
  42.       if ".fiba" `elem` contents
  43.      then
  44.       let dir = path </> ".fiba"
  45.       in do
  46.         isdir <- liftIO $ doesDirectoryExist dir
  47.         if isdir
  48.            then return (Just dir)
  49.            else (throwError $ dir ++ " exists but is not a directory")
  50.       else recurse (takeDirectory path)
  51.  
  52. writeIssues :: [Issue] -> ErrorT String IO ()
  53. writeIssues issues = do
  54.       maybedir <- findBugDir
  55.       case maybedir of
  56.            Nothing -> createNewDirectory >>= \d -> mapM_ (write d) issues
  57.            Just d -> mapM_ (write d) issues
  58.   where
  59.       write :: FilePath -> Issue -> ErrorT String IO ()
  60.       write path i@(Issue _ _ _ _ _ ident) = liftIO (writeFile (path </> ("issue - " ++ ident)) (show i))
  61.       write path c@(Comment _ _ ident) = liftIO (writeFile (path </> ("comment - " ++ ident)) (show c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement