Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active December 12, 2015 01:28
Show Gist options
  • Save Heimdell/4691264 to your computer and use it in GitHub Desktop.
Save Heimdell/4691264 to your computer and use it in GitHub Desktop.
Because I'm not the master of awk & sed, and they cannot into fs structure. Run it from the root of the railsapp, it will rebuild routes-like thing from tests. The method-from-controller parser is comin' soon.
import Data.List
import System.Environment
import System.IO
import System.Directory
import Data.String.Utils hiding (join)
import Control.Monad
import System.FilePath.Posix
data Node =
Dir { name :: String, dir :: [Node] }
| File { name :: String, file :: [String] }
load node = do
isDirectory <- doesDirectoryExist node
case isDirectory of
True -> loadDir node
False -> loadFile node
loadDir name = do
files <- getDirectoryContents name
let branches = every files $ not . startswith "."
nodes <- for branches $ \file ->
load (name </> file)
return $ Dir (fromDirectory name) nodes
fromDirectory = ("namespace :" ++) . parse_filename . strip_ext
loadFile file = do
if is_test_file file
then do
content <- readFile file
lines content |> filter (with_words ["test"]) -- test "should do things" do
|> map (untab ~> drop_lexem "test "
~> drop_lexem "\""
~> drop_lexem "should "
~> on_back drop_lexem "\" do")
|> File (fromFilename file)
|> return
else
return $ File file []
fromFilename = strip_ext
~> parse_filename
~> strip_controller
~> ("resource :" ++)
on_back f arg = reverse . f (reverse arg) . reverse
drop_lexem lexem str =
if startswith lexem str
then drop (length lexem) str
else str
untab = dropWhile (== ' ')
anyp ps x = or $ map (x |>) ps
allp ps x = and $ map (x |>) ps
for = flip mapM
every = flip filter
indent = (" " ++)
plain (File name content) =
name : map indent content ++ ["end"]
plain (Dir name content) =
let children = content |> map plain
|> intersperse [""]
|> join
|> map indent
in name : children ++ ["end"]
with_words set = words ~> any (`elem` set)
is_test_file = endswith "test.rb"
parse_filename = last . words . replace "/" " "
strip_ext = head . words . replace "." " "
strip_controller = on_back drop_lexem "_controller_test"
. on_back drop_lexem "_controler_test" -- the precedent exist!
instance Show Node where
show = unlines . plain
-- call f with x as arg
-- chaning: Map.empty |> Map.insert key val
-- |> Map.insert key2 val2
-- |> Map.union other_map
--
x |> f = f x
-- inverted function composition
-- the previous example redone:
-- chaning: (Map.insert key val
-- ~> Map.insert key2 val2
-- ~> Map.union other_map)
-- Map.empty
--
g ~> f = f . g
main = print =<< load "test/functional"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment