cabal-file-th-0.2.4/0000755000000000000000000000000013017416461012276 5ustar0000000000000000cabal-file-th-0.2.4/cabal-file-th.cabal0000644000000000000000000000220613017416461015652 0ustar0000000000000000Name: cabal-file-th Version: 0.2.4 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, directory, template-haskell, pretty Ghc-options: -Wall Test-suite test Ghc-options: -Wall Main-is: Test.hs Hs-source-dirs: test Type: exitcode-stdio-1.0 Build-depends: base >= 4 && < 5, cabal-file-th, Cabal cabal-file-th-0.2.4/LICENSE0000644000000000000000000000276513017416461013315 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.4/README.md0000644000000000000000000000051213017416461013553 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.4/Setup.hs0000644000000000000000000000005613017416461013733 0ustar0000000000000000import Distribution.Simple main = defaultMain cabal-file-th-0.2.4/Distribution/0000755000000000000000000000000013017416461014755 5ustar0000000000000000cabal-file-th-0.2.4/Distribution/PackageDescription/0000755000000000000000000000000013017416461020514 5ustar0000000000000000cabal-file-th-0.2.4/Distribution/PackageDescription/TH.hs0000644000000000000000000000536413017416461021373 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, packageString, -- * 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 import Distribution.Compat.ReadP import Distribution.Verbosity (silent) import Text.PrettyPrint import Distribution.PackageDescription.Parse (readPackageDescription) import System.Directory (getCurrentDirectory, getDirectoryContents) import Data.List (isSuffixOf) import Language.Haskell.TH (Q, Exp, stringE, runIO) newtype DocString = DocString String instance Text DocString where parse = DocString <$> (readS_to_P read) disp (DocString s) = text s -- | Provides a Text instance for String, allowing text fields to be used -- in `packageVariable`. Use it composed with an accessor, eg. -- packageVariable (packageString . copyright) packageString :: String -> DocString packageString = DocString -- | 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 = renderFieldS pd (display . f) renderFieldS :: IO a -> (a -> String) -> Q Exp renderFieldS pd f = runIO pd >>= stringE . 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.4/test/0000755000000000000000000000000013017416461013255 5ustar0000000000000000cabal-file-th-0.2.4/test/test-version-interp.cabal0000644000000000000000000000037313017416461020205 0ustar0000000000000000Name: test-version-interp Version: 5.5.5 License: BSD3 Build-type: Simple Cabal-version: >=1.2 copyright: (c) 1953 Gumby Library Exposed-modules: Test build-depends: base, cabal-file-th cabal-file-th-0.2.4/test/Test.hs0000644000000000000000000000130613017416461014530 0ustar0000000000000000{-# LANGUAGE TemplateHaskell #-} module Main where import Distribution.PackageDescription.TH import Control.Monad (when) main :: IO () main = do let simpleAssert msg a b = when (a /= b) $ error (msg ++ ": expected " ++ b ++ ", read: " ++ a) putStrLn $ "This package is version " ++ $(packageVariable (pkgVersion . package)) let testVersion = $(packageVariableFrom "test/test-version-interp.cabal" (pkgVersion . package)) testCopyright = $(packageVariableFrom "test/test-version-interp.cabal" (packageString . copyright)) simpleAssert "version" testVersion "5.5.5" simpleAssert "copyright" testCopyright "(c) 1953 Gumby" putStrLn "Everything went better than expected."