packages feed

lio-simple 0.0.0.1 → 0.0.0.2

raw patch · 8 files changed

+283/−1 lines, 8 filesdep +aesondep +attoparsecdep +cmdargsdep ~simple-templatesnew-component:exe:lio-simple

Dependencies added: aeson, attoparsec, cmdargs, unordered-containers

Dependency ranges changed: simple-templates

Files

lio-simple.cabal view
@@ -1,5 +1,5 @@ name:                lio-simple-version:             0.0.0.1+version:             0.0.0.2 synopsis:            LIO support for the Simple web framework description: @@ -15,6 +15,8 @@ build-type:          Simple cabal-version:       >=1.10 +data-files:          template/*.tmpl+ library   build-depends:       base < 6@@ -41,6 +43,23 @@     LIO.Web.Simple.DCLabel     LIO.Web.Simple.TCB   default-language: Haskell2010++executable lio-simple+  Main-Is: lio-simple.hs+  ghc-options: -Wall -fno-warn-unused-do-bind+  build-depends:+      base < 6+    , aeson+    , attoparsec+    , bytestring+    , cmdargs+    , directory+    , filepath+    , simple-templates >= 0.7.0+    , text+    , unordered-containers+  default-language: Haskell2010+  source-repository head   type: git
+ lio-simple.hs view
@@ -0,0 +1,106 @@+{-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}++-- | The `lio-simple` utility for helping a user setup a lio-simple+-- web project, built on the Simple 'smpl' utility.+module Main (main) where++import Prelude hiding (writeFile, FilePath, all)+import Control.Applicative+import Control.Monad (when)+import Data.Aeson+import Data.Char+import qualified Data.ByteString.Char8 as S8+import qualified Data.Text.Encoding as T+import Data.Monoid (mempty)+import Data.Version+import System.Console.CmdArgs+import System.Directory+import System.FilePath+import Web.Simple.Templates.Language++import Paths_lio_simple++data LIOSimple = LIOSimple { appDir           :: FilePath+                           , includeTemplates :: Bool+                           , includeFS        :: Bool+                           , includeAll       :: Bool }+    deriving (Show, Data, Typeable)++main :: IO ()+main = do+  let lioSimple = +        LIOSimple { appDir = "" &= argPos 0 &= typ "app_dir"+                  , includeTemplates = False+                            &= help "include templates"+                            &= explicit &= name "templates"+                  , includeFS = False+                            &= help "include lio-fs support"+                            &= explicit &= name "fs"+                  , includeAll = False+                            &= help+                                 "include templates and lio-fs support"+                            &= explicit &= name "all" }+                 &= help "Create a new application in app_dir"+                 &= program "lio-simple"+                 &= (summary $ "lio-simple " ++ showVersion version)+  (LIOSimple dir tmpls fs all) <- cmdArgs lioSimple+  createApplication dir (all || tmpls) (all || fs)++humanize :: String -> String+humanize = capitalize+  where go [] = []+        go ('_':xs) = ' ':(capitalize xs)+        go (x:xs) = x:(go xs)+        capitalize [] = []+        capitalize x@('_':_) = go x+        capitalize (x:xs) = (toUpper x):(go xs)++moduleCase :: String -> String+moduleCase = capitalize+  where go [] = []+        go ('_':xs) = capitalize xs+        go (x:xs) = x:(go xs)+        capitalize [] = []+        capitalize ('_':xs) = go xs+        capitalize (x:xs) = (toUpper x):(go xs)++createApplication :: FilePath -> Bool -> Bool -> IO ()+createApplication dir tmpls fs = do+  let myAppName = takeBaseName $ dropTrailingPathSeparator dir+      modName = moduleCase myAppName+      mappings = object+                  [ "appname" .= myAppName+                  , "name" .= humanize myAppName+                  , "module" .= modName+                  , "include_templates" .= tmpls+                  , "include_fs" .= fs ]++  createDirectory dir+  createDirectory $ dir </> modName+  copyTemplate ("template" </> "Main_hs.tmpl")+               (dir </> "Main.hs") mappings+  copyTemplate ("template" </> "Application_hs.tmpl")+               (dir </> "Application.hs") mappings+  copyTemplate ("template" </> "package_cabal.tmpl")+               (dir </> myAppName ++ ".cabal") mappings+  copyTemplate ("template" </> "Common_hs.tmpl")+               (dir </> modName </> "Common.hs") mappings++  when (tmpls || fs) $ createDirectory $ dir </> "liofs"+  when tmpls $ do+    createDirectory $ dir </> "liofs" </> "views"+    createDirectory $ dir </> "liofs" </> "layouts"+    copyTemplate ("template" </> "main_html.tmpl")+                 (dir </> "liofs" </> "layouts" </> "main.html") mappings+    copyTemplate ("template" </> "index_html.tmpl")+                 (dir </> "liofs" </> "views" </> "index.html") mappings++copyTemplate :: FilePath -> FilePath -> Value -> IO ()+copyTemplate orig target mappings = do+  etmpl <- compileTemplate <$> T.decodeUtf8 <$>+    (S8.readFile =<< getDataFileName orig)+  case etmpl of+    Left err -> fail err+    Right tmpl -> S8.writeFile target $ T.encodeUtf8 $+      renderTemplate tmpl mempty mappings+
+ template/Application_hs.tmpl view
@@ -0,0 +1,22 @@+{-# LANGUAGE OverloadedStrings #-}+module Application where++import $module$.Common++import LIO+import LIO.DCLabel+import LIO.Web.Simple+import LIO.Web.Simple.DCLabel+import Web.Frank++app :: (SimpleApplication DC -> DC ()) -> DC ()+app runner = do+  settings <- newAppSettings++  runner $$ controllerApp settings $$ do+    get "/" $$ do$if(include_templates)$+      render "index.html" ()+$else$+      respond $$ okHtml "Hello World"+$endif$+
+ template/Common_hs.tmpl view
@@ -0,0 +1,49 @@+{-# LANGUAGE Trustworthy #-}+$if(include_templates)$+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+$endif$+module $module$.Common where++import Prelude hiding (readFile, appendFile)+import LIO+import LIO.DCLabel+$if(include_templates)$+import LIO.Web.Simple+import LIO.Web.Simple.DCLabel+import Web.Simple.Templates+import Control.Applicative+import System.FilePath+$if(include_fs)$+import LIO.FS.Simple+import Data.Text.Encoding+import Web.Simple.Templates.Language+$else$+import LIO.Web.Simple.TCB (lioGetTemplateTCB)+$endif$+$endif$++data AppSettings = AppSettings { }++newAppSettings :: DC AppSettings+newAppSettings = do+  return AppSettings++$if(include_templates)$+instance HasTemplates DC AppSettings where+$if(include_fs)$+  viewDirectory = return $$ "views"+  defaultLayout = Just <$$> getTemplate ("layouts" </> "main.html")+  getTemplate fp = do+    eres <- compileTemplate . decodeUtf8 <$$> liftLIO (liftLIO $$ readFile fp)+    case eres of+      Left str -> fail str+      Right tmpl -> return tmpl+$else$+  viewDirectory = return $$ "liofs" </> "views"+  defaultLayout = Just <$$> getTemplate ("liofs" </> "layouts" </> "main.html")+  getTemplate = liftLIO . lioGetTemplateTCB+  -- NOTE: We assume that "liofs" only contains public data, DO NOT+  -- store any sensitive data in this directory+$endif$+$endif$
+ template/Main_hs.tmpl view
@@ -0,0 +1,33 @@+{-# LANGUAGE OverloadedStrings #-}+module Main where++import Application+import Control.Monad+import Network.Wai.Middleware.RequestLogger+import System.Environment+import Web.Simple++import LIO.DCLabel+import LIO.Web.Simple.TCB (run)++$if(include_fs)$+import LIO.FS.Simple+import LIO.FS.Simple.DCLabel+import System.Directory+import System.FilePath+$endif$+++main :: IO ()+main = do$if(include_fs)$+  fsRoot <- (\d -> d </> "liofs") `liftM` getCurrentDirectory$if(include_templates)$++  -- Always label views and layouts public:+  labelDirectoryRecursively dcPublic $$ fsRoot </> "views"+  labelDirectoryRecursively dcPublic $$ fsRoot </> "layouts"+$endif$+  withDCFS fsRoot $$ do$endif$+    -- Run app+    env <- getEnvironment+    let port = maybe 3000 read $$ lookup "PORT" env+    evalDC $$ app $$ run port logStdout
+ template/index_html.tmpl view
@@ -0,0 +1,1 @@+Welcome to your new app! This file lives in &quot;views/index.html&quot;
+ template/main_html.tmpl view
@@ -0,0 +1,28 @@+<!DOCTYPE HTML>+<html>+  <head>+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">+    <title>$name$</title>+    <style>+      body {+        width: 25em;+        font-size: 13pt;+        margin: 0 auto;+        font-family: sans-serif;+      }++      h1 {+        text-align: center;+      }++      h1 a {+        color: inherit;+        text-decoration: none;+      }+    </style>+</head>+<body>+  <h1><a href="/">$name$</a></h1>+$$yield$$+</body>+</html>
+ template/package_cabal.tmpl view
@@ -0,0 +1,24 @@+name:                $module$+version:             0.0.0.0+--author:              YOUR NAME+--maintainer:          your@email.com+category:            Web+build-type:          Simple+cabal-version:       >=1.8++executable $appname$+  main-is: Main.hs+  ghc-options: -threaded -O2+  build-depends:+    base+    , simple >= 0.8.0+    , lio+    , lio-simple+    , wai-extra+    , warp$if(include_fs)$+    , lio-fs+    , filepath+    , directory$if(include_templates)$+    , simple-templates+    , text$endif$$else$$if(include_templates)$+    , filepath$endif$$endif$