cabal-file-th-0.2.3/0000755000000000000000000000000012027715470012277 5ustar0000000000000000cabal-file-th-0.2.3/cabal-file-th.cabal0000644000000000000000000000212612027715470015654 0ustar0000000000000000Name: cabal-file-th Version: 0.2.3 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.17, 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 cabal-file-th-0.2.3/LICENSE0000644000000000000000000000276512027715470013316 0ustar0000000000000000Copyright (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. cabal-file-th-0.2.3/README.md0000644000000000000000000000051212027715470013554 0ustar0000000000000000 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 cabal-file-th-0.2.3/Setup.hs0000644000000000000000000000005612027715470013734 0ustar0000000000000000import Distribution.Simple main = defaultMain cabal-file-th-0.2.3/Distribution/0000755000000000000000000000000012027715470014756 5ustar0000000000000000cabal-file-th-0.2.3/Distribution/PackageDescription/0000755000000000000000000000000012027715470020515 5ustar0000000000000000cabal-file-th-0.2.3/Distribution/PackageDescription/TH.hs0000644000000000000000000000432612027715470021371 0ustar0000000000000000{-# 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 :: FilePath -> IO [FilePath] 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. -} cabal-file-th-0.2.3/test/0000755000000000000000000000000012027715470013256 5ustar0000000000000000cabal-file-th-0.2.3/test/test-version-interp.cabal0000644000000000000000000000032712027715470020205 0ustar0000000000000000Name: test-version-interp Version: 5.5.5 License: BSD3 License-file: LICENSE Build-type: Simple Cabal-version: >=1.2 Library Exposed-modules: Test cabal-file-th-0.2.3/test/Test.hs0000644000000000000000000000075412027715470014537 0ustar0000000000000000{-# 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."