cabal-file-th (empty) → 0.1
raw patch · 7 files changed
+169/−0 lines, 7 filesdep +Cabaldep +basedep +cabal-file-thsetup-changed
Dependencies added: Cabal, base, cabal-file-th, directory, template-haskell
Files
- Distribution/PackageDescription/TH.hs +64/−0
- LICENSE +30/−0
- README.md +18/−0
- Setup.hs +2/−0
- cabal-file-th.cabal +34/−0
- test/Test.hs +12/−0
- test/test-version-interp.cabal +9/−0
+ Distribution/PackageDescription/TH.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}++-- | Utility functions for reading cabal file fields through template haskell.+module Distribution.PackageDescription.TH (+ -- * Template Haskell functions+ packageVariable,+ packageVariableFrom,+ -- * Cabal file data structures+ -- | The data structures for the cabal file are re-exported here for ease of use.+ PackageDescription(..),+ PackageIdentifier(..),+ Version(..)+ ) where++import Distribution.PackageDescription +import Distribution.Package+import Distribution.Version++import Distribution.Text (Text, display)+import Distribution.Verbosity (silent)+import Distribution.PackageDescription.Parse (readPackageDescription)+import System.Directory (getCurrentDirectory, getDirectoryContents)+import Data.List (isSuffixOf)+import Language.Haskell.TH (Q, Exp, stringE, runIO)++-- | Renders the package variable specified by the function.+-- The cabal file interrogated is the first one that is found +-- in the current working directory.+packageVariable :: Text a => (PackageDescription -> a) -> Q Exp+packageVariable = renderField currentPackageDescription++-- | Renders the package variable specified by the function, from a cabal file+-- and the given path.+packageVariableFrom :: Text a => FilePath -> (PackageDescription -> a) -> Q Exp+packageVariableFrom s = renderField $ fmap packageDescription (readPackageDescription silent s)++------+renderField :: Text b => IO a -> (a -> b) -> Q Exp+renderField pd f = runIO pd >>= stringE . display . f ++currentPackageDescription :: IO PackageDescription+currentPackageDescription = fmap packageDescription $ do+ dir <- getCurrentDirectory+ cs <- cabalFiles dir+ case cs of+ (c:_) -> readPackageDescription silent c+ [] -> error $ "Couldn't find a cabal file in the current working directory (" ++ dir ++ ")"++cabalFiles dir = do+ files <- getDirectoryContents dir+ return $ filter (".cabal" `isSuffixOf`) files++{-++Smart ways of getting the cabal file:+ * Get this module name, use TH.location and loc_module. Parse each+ cabal file in the cwd and look for references to this module+ in each thing.+ ++ -}+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Nick Partridge++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Nick Partridge nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,18 @@++cabal-file-th+=============++Use template haskell to bring fields from your cabal file into your haskell source files.++Usage+-----++ import qualified Distribution.PackageDescription.TH as P++ myVersion :: String+ myVersion = $(packageVariable (pkgVersion . package))++Install+-------++ $ cabal install cabal-file-th
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cabal-file-th.cabal view
@@ -0,0 +1,34 @@+Name: cabal-file-th+Version: 0.1+Synopsis: Template Haskell expressions for reading fields from a project's cabal file.+Description: Template Haskell expressions for reading fields from a project's cabal file.+Homepage: http://github.com/nkpart/cabal-file-th+License: BSD3+License-file: LICENSE+Author: Nick Partridge+Maintainer: nkpart@gmail.com+Category: Development+Build-type: Simple+Extra-source-files: README.md, test/Test.hs, test/test-version-interp.cabal+Cabal-version: >= 1.9++source-repository head+ type: git+ location: git://github.com/nkpart/cabal-file-th.git+ ++Library+ Exposed-modules: Distribution.PackageDescription.TH+ Build-depends: base >= 4 && < 5,+ Cabal >= 1.10 && < 1.11,+ directory,+ template-haskell+ Ghc-options: -Wall+ +Test-suite test+ Main-is: Test.hs+ Hs-source-dirs: test+ Type: exitcode-stdio-1.0+ Build-depends: base >= 4 && < 5,+ cabal-file-th+
+ test/Test.hs view
@@ -0,0 +1,12 @@+{-# LANGUAGE TemplateHaskell #-}+module Main where++import Distribution.PackageDescription.TH++main = do+ putStrLn $ "This package is version " ++ $(packageVariable (pkgVersion . package))+ let testVersion = $(packageVariableFrom "test/test-version-interp.cabal" (pkgVersion . package))+ let expectedVersion = "5.5.5"+ if testVersion /= expectedVersion+ then error ("Expected " ++ expectedVersion ++ ", read: " ++ testVersion)+ else putStrLn "Everything went better than expected."
+ test/test-version-interp.cabal view
@@ -0,0 +1,9 @@+Name: test-version-interp+Version: 5.5.5+License: BSD3+License-file: LICENSE+Build-type: Simple+Cabal-version: >=1.2++Library+ Exposed-modules: Test