Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active August 29, 2015 13:56
Show Gist options
  • Save osa1/9150504 to your computer and use it in GitHub Desktop.
Save osa1/9150504 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ViewPatterns #-}
module Main where
import Control.Monad (forM, forM_, liftM)
import Data.List (isInfixOf)
import Data.List.Split (splitOn)
import System.Directory (doesFileExist)
import System.Environment (getArgs)
import System.FilePath (dropFileName, takeDirectory, (</>))
addBenchmarks :: [String] -> IO [String]
addBenchmarks tableData = do
forM tableData $ \(words -> line) -> do
let filePath = head line
benchmarkFile <- searchBenchmarkFile (dropFileName filePath)
(_ : benchmarkContents) <- liftM lines (readFile benchmarkFile)
return $ searchBenchmarkData filePath benchmarkContents
where
searchBenchmarkFile :: FilePath -> IO FilePath
searchBenchmarkFile dirPath = do
let filePath = dirPath </> "non-indentation.summary"
fe <- doesFileExist filePath
if fe then return filePath else searchBenchmarkFile (takeDirectory dirPath)
searchBenchmarkData :: FilePath -> [String] -> String
searchBenchmarkData filePath [] =
error $ "can't find benchmark data for " ++ filePath
searchBenchmarkData filePath (benchmarkContents : rest) = do
let (benchFile : benchData) = splitOn "," benchmarkContents
if filePath `isInfixOf` benchFile
then unwords benchData
else searchBenchmarkData filePath rest
main :: IO ()
main = do
(tableFilePath : _) <- getArgs
tableFileContents <- readFile tableFilePath
let (tableHeader : tableData) = lines tableFileContents
benchmarkData <- addBenchmarks tableData
putStrLn tableHeader
forM_ (zip tableData benchmarkData) $ \(td, bd) ->
putStrLn $ td ++ " " ++ bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment