Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This preprocessor splices in imports to the file you define it in. Haskell
source files are discovered according to a glob relative to the file the code
is defined in. This utility is useful when metaprogramming with a group of
related files, where you want to use TemplateHaskell
or similar
By default, the glob is for all modules in the directory containing the
source file. Imports are qualified with the full module name to avoid
potential import conflicts. The pre-processor will splice in a top-level
value _importedModules :: [String]
which contains the fully qualified
names of the modules that were imported.
You may want to disable warnings for redundant imports, if you are only using type class information. A future option to the library may only do empty import lists, to only get access to type class instances.
As an example, consider the
<https://hackage.haskell.org/package/persistent-discover
persistent-discover
> utility, which is inspired by hspec-discover
. That
utility will perform the following transformation:
-- srcPersistentModelsAll.hs {-# OPTIONS_GHC -F -pgmF persistent-discover #-}
Then it will translate to:
-- srcPersistentModelsAll.hs module PersistentModels.All where import PersistentModels.Foo () import PersistentModels.Bar () import PersistentModels.Baz () allEntityDefs :: [EntityDef] allEntityDefs = $(discoverEntities)
With this package, we can generalize the overall pattern. The new source module will look like this:
-- srcPersistentModelsAll.hs {-# OPTIONS_GHC -F -pgmF glob-imports #-} module PersistentModels.All where import Database.Persist.Sql {- GLOB_IMPORTS_SPLICE -} allEntityDefs :: [EntityDef] allEntityDefs = $(discoverEntities)
This preprocessor will convert this into this form:
-- srcPersistentModelsAll.hs module PersistentModels.All where import Database.Persist.Sql import qualified PersistentModels.Foo import qualified PersistentModels.Bar import qualified PersistentModels.Baz allEntityDefs :: [EntityDef] allEntityDefs = $(discoverEntities)
Note how the only difference is that imports have been spliced in. This allows you to more flexibly customize how the code works.
Since: 0.1.0.0
Synopsis
- newtype Source = Source {}
- newtype SourceContents = SourceContents {}
- newtype Destination = Destination {}
- data AllModelsFile = AllModelsFile {}
- spliceImports :: Source -> SourceContents -> Destination -> IO ()
- getFilesRecursive :: FilePath -> IO [FilePath]
- renderFile :: AllModelsFile -> String -> String
- data Module = Module {}
- mkModulePieces :: FilePath -> [String]
- isLowerFirst :: String -> Bool
- pathToModule :: FilePath -> Maybe Module
- isValidModuleName :: String -> Bool
- isValidModuleChar :: Char -> Bool
- casify :: String -> String
- stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
Documentation
The source file location. This is the first argument passed to the preprocessor.
newtype SourceContents Source #
The source file contents. This is the String
contained in the file of the
second argument passed to the preprocessor.
newtype Destination Source #
The destination file path to write the final source to. This is the third argument passed to the preprocessor.
data AllModelsFile Source #
spliceImports :: Source -> SourceContents -> Destination -> IO () Source #
Since: 0.1.0.0
Returns a list of relative paths to all files in the given directory.
renderFile :: AllModelsFile -> String -> String Source #
Module | |
|
mkModulePieces :: FilePath -> [String] Source #
isLowerFirst :: String -> Bool Source #
isValidModuleName :: String -> Bool Source #
Returns True if the given string is a valid task module name.
See ModuleName
(http:/git.iobj34)
isValidModuleChar :: Char -> Bool Source #
Returns True if the given Char is a valid taks module character.
stripSuffix :: Eq a => [a] -> [a] -> Maybe [a] Source #