Permalink
Browse files

MyApp.bird.hs ==> 'get _ = body "hello, world"' is now a valid bird app.

  • Loading branch information...
1 parent 06f02fa commit 28d2af780c3bedefb3e944ddaed7e6b697c31c0b @moonmaster9000 committed Jul 31, 2010
Showing with 48 additions and 19 deletions.
  1. +46 −17 bin/bird.hs
  2. +2 −2 bird.cabal
View
@@ -1,36 +1,56 @@
module Main where
+import Directory
import System.Process
import System.Environment (getArgs)
-import Directory
+import List
main = do
args <- getArgs
runArg $ head args
runArg a =
case a of
- "nest" -> readProcess "ghc" ["--make", "-O2", "Main.hs"] "" >> return ()
- "fly" -> readProcess "./Main" [] "" >> return ()
+ "nest" -> do
+ appModuleNamePath <- getCurrentDirectory
+ appModuleName <- return $ head . reverse $ split '/' appModuleNamePath
+ partialRouteFile <- readFile $ appModuleName ++ ".bird.hs"
+ writeFile (appModuleName ++ ".hs") ((appModulePrelude appModuleName)++ "\n" ++ partialRouteFile ++ "\n" ++ appModuleEpilogue)
+ readProcess "ghc" ["--make", "-O2", "Main.hs"] ""
+ files <- getDirectoryContents appModuleNamePath
+ return $ map cleanGHC files
+ renameFile "Main" appModuleName
+ return ()
+ "fly" -> do
+ appModuleNamePath <- getCurrentDirectory
+ appModuleName <- return $ head . reverse $ split '/' appModuleNamePath
+ readProcess ("./" ++ appModuleName) [] ""
+ return ()
appName -> createBirdApp appName
-createBirdApp a = do
- createDirectory a
- writeFile (a ++ "/" ++ a ++ ".hs") (routeFile a)
- writeFile (a ++ "/" ++ "Main.hs") (mainFile a)
+cleanGHC file =
+ if any (`isSuffixOf` file) [".hi", ".o"]
+ then removeFile file
+ else return ()
+
+appModulePrelude appModuleName =
+ "module " ++ appModuleName ++ " where\n" ++
+ "import Bird\n\n"
+-- ++ "-- get, post, put, delete :: Path -> BirdResponder ()\n"
-routeFile a =
- "module " ++ a ++ " where\n" ++
- "import Data.Maybe\n" ++
- "import Bird\n\n" ++
- "get, post, put, delete :: Path -> BirdResponder ()\n" ++
- "get [] = do\n" ++
- " name <- param $ \"name\"\n" ++
- " body $ \"Hello, \" ++ (maybe \"Bird!\" id name)\n\n" ++
+appModuleEpilogue =
"get _ = status 404\n" ++
"post _ = status 404\n" ++
"put _ = status 404\n" ++
"delete _ = status 404\n"
+
+
+createBirdApp a = do
+ createDirectory a
+ writeFile (a ++ "/" ++ a ++ ".bird.hs") (routeFile a)
+ writeFile (a ++ "/" ++ "Main.hs") (mainFile a)
+
+routeFile a = "get [] = body \"Hello, Bird!\""
mainFile a =
"import Hack\n" ++
@@ -52,13 +72,22 @@ mainFile a =
" req = toBirdRequest e\n" ++
" response = do \n" ++
" reply <- runBirdResponder req matchRequest\n" ++
- " return $ fromBirdReply reply\n" ++ "\n" ++
+ " return $ fromBirdReply reply\n\n" ++
"matchRequest r = \n" ++
" case verb r of \n" ++
" Bird.GET -> get $ path r\n" ++
" Bird.POST -> post $ path r\n" ++
" Bird.PUT -> put $ path r\n" ++
- " Bird.DELETE -> delete $ path r\n" ++
+ " Bird.DELETE -> delete $ path r\n\n" ++
"main = run app\n"
+
+split :: Char -> String -> [String]
+split d s
+ | findSep == [] = []
+ | otherwise = t : split d s''
+ where
+ (t, s'') = break (== d) findSep
+ findSep = dropWhile (== d) s
+
View
@@ -1,5 +1,5 @@
Name: bird
-Version: 0.0.10
+Version: 0.0.11
Build-type: Simple
Synopsis: A simple, sinatra-inspired web framework.
Description: Bird is a hack-compatible framework for simple websites.
@@ -19,5 +19,5 @@ Executable bird
library
build-depends: haskell98, mtl >= 1.1.0.2, process, containers, parsec >= 2.1.0.1, bytestring, base >= 4.0 && < 5, hack >= 2009.10.30, hack-handler-happstack, data-default >= 0.2, rallod
- exposed-modules: Bird, Bird.Request, Bird.Reply, Bird.Reply.Codes, Bird.Request.QueryStringParser, Bird.BirdRouter, Bird.Translator, Bird.Translator.Hack
+ exposed-modules: Bird, Bird.Request, Bird.Reply, Bird.Request.QueryStringParser, Bird.BirdResponder, Bird.Translator, Bird.Translator.Hack
hs-source-dirs: src/

0 comments on commit 28d2af7

Please sign in to comment.