Permalink
Browse files

includeDirs option allowing imports from directories other than snapl…

…ets/fay/src.xs
  • Loading branch information...
1 parent ab19468 commit 7d57748c06c837a076c5da9d15de002c2fdb12ca @bergmark bergmark committed Aug 18, 2012
View
@@ -32,7 +32,7 @@ Executable example
, snap-server == 0.9.*
, snap-loader-static == 0.9.*
, snaplet-fay == 0.1.0.0
- , fay
+ , fay == 0.6.1.*
, text == 0.11.*
, time >= 1.1 && < 1.5
, xmlhtml >= 0.1
@@ -4,9 +4,20 @@
# "CompileOnDemand": Compile only the requested file.
#
# Both methods will write files to disk for debugging purposes.
+# Default is "CompileOnDemand".
compileMethod = "CompileOnDemand"
# Print more or less information about what the snaplet is doing.
+# Default is on.
verbose = on
# Run js-beautify on the results if it is available in the PATH.
# If it isn't available the compiled file will be used as is.
+# Default is on.
prettyPrint = on
+# A comma-separated list, default "".
+# Additional directories to look for fay files in, this is useful if
+# If a directory is outside snaplets/src/fay it cannot be served by
+# `fayServe`. You can also share modules by adding them to
+# snaplets/src/fay and add the appropriate sub directories to
+# hs-source-dirs in the .cabal file.
+# snaplets/src/fay will always be checked for imports.
+includeDirs = ""
@@ -4,11 +4,16 @@
module Index where
-import Language.Fay.FFI
-import Language.Fay.Prelude
-
-import Application.SharedTypes
-import Dom
+import Language.Fay.FFI
+import Language.Fay.Prelude
+
+-- | Time is shared between Snap and Fay
+-- | Location: src/Application/SharedTypes.hs
+-- import Time
+import Application.SharedTypes
+-- | Dom is a Fay only module
+-- | Location: snaplets/fay/src
+import Dom
main :: Fay ()
main = addOnload onload
View
@@ -9,26 +9,26 @@ module Site
) where
------------------------------------------------------------------------------
-import Control.Monad.Trans
-import Data.Aeson
-import Data.ByteString (ByteString)
-import Data.Time.Clock
-import Language.Fay.Show
-import Snap.Core
-import Snap.Snaplet
-import Snap.Snaplet.Fay
-import Snap.Snaplet.Heist
-import Snap.Util.FileServe
+import Control.Monad.Trans
+import Data.Aeson
+import Data.ByteString (ByteString)
+import Data.Time.Clock
+import Language.Fay.Convert
+import Snap.Core
+import Snap.Snaplet
+import Snap.Snaplet.Fay
+import Snap.Snaplet.Heist
+import Snap.Util.FileServe
------------------------------------------------------------------------------
-import Application
-import Application.SharedTypes
+import Application
+import Application.SharedTypes
currentTimeAjax :: AppHandler ()
currentTimeAjax = do
- time <- liftIO getCurrentTime
+ t <- liftIO getCurrentTime
modifyResponse . setContentType $ "text/json;charset=utf-8"
- writeLBS $ encode $ showToFay (CTR (show time))
+ writeLBS $ encode $ showToFay (CTR (show t))
------------------------------------------------------------------------------
View
@@ -4,9 +4,18 @@
# "CompileOnDemand": Compile only the requested file.
#
# Both methods will write files to disk for debugging purposes.
+# Default is "CompileOnDemand".
compileMethod = "CompileOnDemand"
# Print more or less information about what the snaplet is doing.
+# Default is on.
verbose = on
# Run js-beautify on the results if it is available in the PATH.
# If it isn't available the compiled file will be used as is.
+# Default is on.
prettyPrint = on
+# A comma-separated list of directories to look for fay imports in.
+# This is useful if you want to share modules between Fay and Snap.
+# Paths are relative to the root of the snap application.
+# snaplets/src/fay will always be checked for imports.
+# Default is "" meaning no additional directories will be checked.
+includeDirs = ""
View
@@ -68,7 +68,7 @@ Executable test
configurator == 0.2.*,
data-default == 0.5.*,
directory == 1.1.*,
- fay == 0.6.*,
+ fay == 0.6.1.*,
filepath == 1.3.*,
mtl == 2.1.*,
snap == 0.9.*,
View
@@ -45,12 +45,14 @@ initFay = makeSnaplet "fay" description datadir $ do
Nothing -> return Nothing
verbose <- logErr "Must specify verbose" $ C.lookup config "verbose"
prettyPrint <- logErr "Must specify prettyPrint" $ C.lookup config "prettyPrint"
-
- return (verbose, compileMethod, prettyPrint)
+ includeDirs <- logErr "Must specify includeDirs" $ C.lookup config "includeDirs"
+ let inc = maybe [] (split ',') includeDirs
+ inc' <- liftIO $ mapM canonicalizePath inc
+ return (verbose, compileMethod, prettyPrint, inc')
let fay = case opts of
- (Just verbose, Just compileMethod, Just prettyPrint) ->
- Fay fp verbose compileMethod prettyPrint
+ (Just verbose, Just compileMethod, Just prettyPrint, includeDirs) ->
+ Fay fp verbose compileMethod prettyPrint (fp : includeDirs)
_ -> error $ intercalate "\n" errs
-- Make sure snaplet/fay, snaplet/fay/src, snaplet/fay/js are present.
@@ -59,6 +61,11 @@ initFay = makeSnaplet "fay" description datadir $ do
return fay
where
+ -- TODO Use split package
+ split :: Eq a => a -> [a] -> [[a]]
+ split _ [] = []
+ split a as = takeWhile (/= a) as : split a (drop 1 $ dropWhile (/= a) as)
+
createDirUnlessExists fp = do
dirExists <- doesDirectoryExist fp
unless dirExists $ createDirectory fp
@@ -1,12 +1,12 @@
-{-# LANGUAGE ViewPatterns #-}
+{-# LANGUAGE ViewPatterns #-}
module Snap.Snaplet.Fay.Internal where
import Control.Applicative
import Control.Monad
import Data.Default
import qualified Language.Fay.Compiler as F
-import qualified Language.Fay.Types as F
+import qualified Language.Fay.Types as F
import System.Directory
import System.FilePath
@@ -18,6 +18,7 @@ data Fay = Fay {
, verbose :: Bool
, compileMethod :: CompileMethod
, prettyPrint :: Bool
+ , _includeDirs :: [FilePath]
}
-- | Location of .hs files
@@ -28,9 +29,9 @@ srcDir = (</> "src") . snapletFilePath
destDir :: Fay -> FilePath
destDir = (</> "js") . snapletFilePath
--- | Where Fay should look for includes
+-- | Where to check for imports
includeDirs :: Fay -> [FilePath]
-includeDirs = (:[]) . srcDir
+includeDirs config = srcDir config : _includeDirs config
data CompileMethod = CompileOnDemand | CompileAll

0 comments on commit 7d57748

Please sign in to comment.