Created
April 17, 2014 04:25
-
-
Save prakashk/10952754 to your computer and use it in GitHub Desktop.
rename files in git lost-found directory - v2
This file contains 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
-- rename Project Euler files in git lost-found directory | |
-- get the file type and the problem number from the initial comments | |
-- in each file | |
import System.Directory | |
import Text.Regex.Posix | |
import Control.Monad (forM_) | |
lostFilesDir = ".git/lost-found/other" | |
fileType s = | |
let firstToken = head $ fst $ head $ lex s | |
in case firstToken of | |
';' -> "clj" | |
'-' -> "hs" | |
'#' -> "pl" | |
_ -> "??" | |
problemId contents = | |
safeHead $ map snd $ problemIds $ pairwise $ words contents | |
where pairwise ws = zip ws $ tail ws | |
problemIds = filter (\(a, b) -> a `elem` ["Problem", "problem"]) | |
safeHead [] = Nothing | |
safeHead (x:_) = Just x | |
findFileName contents = | |
fmap (\x -> ft ++ "/" ++ x ++ "." ++ ft) prid | |
where ft = fileType contents | |
prid = problemId contents | |
inferFileName path = do | |
contents <- readFile path | |
return $ findFileName contents | |
main = do | |
dirContents <- getDirectoryContents lostFilesDir | |
let lfiles = filter (\file -> not $ file `elem` [".", ".."]) dirContents | |
forM_ lfiles (\lfile -> do | |
fname <- inferFileName $ lostFilesDir ++ "/" ++ lfile | |
putStrLn $ | |
case fname of | |
Just x -> lfile ++ " -> " ++ x | |
_ -> lfile ++ ": No program??" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment