Created
January 13, 2015 11:16
-
-
Save sinelaw/f3d153f6cf9e08314020 to your computer and use it in GitHub Desktop.
Generates a json file describing the directory structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Main where | |
| import System.Directory (getDirectoryContents) | |
| import Control.Monad (forM_) | |
| import Data.Functor ((<$>)) | |
| import System.Posix.Files (getSymbolicLinkStatus, isDirectory) | |
| withIsLast :: [a] -> [(a, Bool)] | |
| withIsLast [] = [] | |
| withIsLast [x] = [(x, True)] | |
| withIsLast (x:xs) = (x, False) : withIsLast xs | |
| printFile :: String -> String -> Bool -> IO () | |
| printFile top d isLast = do | |
| let path = top ++ "/" ++ d | |
| quotedPath = "\"" ++ d ++ "\"" | |
| s <- getSymbolicLinkStatus path | |
| putStr $ quotedPath ++ ": " | |
| if isDirectory s | |
| then do putStrLn "{" | |
| traverseDir path | |
| putStr "}" | |
| else putStr "null" | |
| putStrLn $ if isLast then "" else "," | |
| traverseDir :: FilePath -> IO () | |
| traverseDir top = do | |
| ds <- filter (\n -> n /= "." && n /= "..") <$> getDirectoryContents top | |
| forM_ (withIsLast ds) $ \(d, isLast) -> printFile top d isLast | |
| main :: IO () | |
| main = do | |
| putStrLn "{" | |
| traverseDir "." | |
| putStrLn "}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment