@@ -1,5 +1,5 @@
11module Distribution.Nixpkgs.Haskell.PackageSourceSpec
2− ( Package(..), getPackage, getPackage', loadHackageDB, sourceFromHackage
2+ ( HpackUse(..), Package(..), getPackage, getPackage', loadHackageDB, sourceFromHackage
33 ) where
44
55import qualified Control.Exception as Exception
@@ -32,6 +32,11 @@ import System.IO
3232import qualified Data.Text as T
3333import qualified Data.Text.Encoding as T
3434
35+data HpackUse
36+ = ForceHpack
37+ | PackageYamlHpack
38+ | NoHpack
39+
3540data Package = Package
3641 { pkgSource :: DerivationSource
3742 , pkgRanHpack :: Bool -- ^ If hpack generated a new cabal file
@@ -39,8 +44,8 @@ data Package = Package
3944 }
4045 deriving (Show)
4146
42−getPackage :: Bool
43− -- ^ Whether hpack should regenerate the cabal file.
47+getPackage :: HpackUse
48+ -- ^ the way hpack should be used.
4449 -> Bool
4550 -- ^ Whether to fetch submodules if fetching from git
4651 -> Maybe FilePath
@@ -52,8 +57,8 @@ getPackage :: Bool
5257getPackage optHpack optSubmodules optHackageDB optHackageSnapshot =
5358 getPackage' optHpack optSubmodules (loadHackageDB optHackageDB optHackageSnapshot)
5459
55−getPackage' :: Bool
56− -- ^ Whether hpack should regenerate the cabal file.
60+getPackage' :: HpackUse
61+ -- ^ the way hpack should be used.
5762 -> Bool
5863 -- ^ Whether to fetch submodules if fetching from git
5964 -> IO DB.HackageDB
@@ -63,8 +68,8 @@ getPackage' optHpack optSubmodules hackageDB source = do
6368 (derivSource, ranHpack, pkgDesc) <- fetchOrFromDB optHpack optSubmodules hackageDB source
6469 (\s -> Package s ranHpack pkgDesc) <$> maybe (sourceFromHackage (sourceHash source) (showPackageIdentifier pkgDesc) $ sourceCabalDir source) return derivSource
6570
66−fetchOrFromDB :: Bool
67− -- ^ Whether hpack should regenerate the cabal file
71+fetchOrFromDB :: HpackUse
72+ -- ^ the way hpack should be used
6873 -> Bool
6974 -- ^ Whether to fetch submodules if fetching from git
7075 -> IO DB.HackageDB
@@ -175,7 +180,7 @@ showPackageIdentifier pkgDesc = name ++ "-" ++ display version where
175180 name = Cabal.unPackageName (Cabal.packageName pkgId)
176181 version = Cabal.packageVersion pkgId
177182
178−cabalFromPath :: Bool -- ^ Whether hpack should regenerate the cabal file
183+cabalFromPath :: HpackUse -- ^ the way hpack should be used
179184 -> FilePath -> MaybeT IO (Bool, Bool, Cabal.GenericPackageDescription)
180185cabalFromPath optHpack path = do
181186 d <- liftIO $ doesDirectoryExist path
@@ -185,21 +190,25 @@ cabalFromPath optHpack path = do
185190 return (d, ranHpack, pkg)
186191 else (,,) d False <$> cabalFromFile False path
187192
188−cabalFromDirectory :: Bool -- ^ Whether hpack should regenerate the cabal file
193+cabalFromDirectory :: HpackUse -- ^ the way hpack should be used
189194 -> FilePath -> MaybeT IO (Bool, Cabal.GenericPackageDescription)
190−cabalFromDirectory True dir = hpackDirectory dir
191−cabalFromDirectory False dir = do
195+cabalFromDirectory ForceHpack dir = hpackDirectory dir
196+cabalFromDirectory NoHpack dir = onlyCabalFromDirectory dir "*** No .cabal file was found. Exiting."
197+cabalFromDirectory PackageYamlHpack dir = do
192198 useHpack <- liftIO $ doesFileExist (dir </> "package.yaml")
193199 if useHpack
194200 then do
195201 liftIO $ hPutStrLn stderr "*** found package.yaml. Using hpack..."
196202 hpackDirectory dir
197− else do
198− cabals <- liftIO $ getDirectoryContents dir >>= filterM doesFileExist . map (dir </>) . filter (".cabal" `isSuffixOf`)
199− case cabals of
200− [] -> fail "*** Found neither a .cabal file nor package.yaml. Exiting."
201− [cabalFile] -> (,) False <$> cabalFromFile True cabalFile
202− _ -> liftIO $ fail ("*** found more than one cabal file (" ++ show cabals ++ "). Exiting.")
203+ else onlyCabalFromDirectory dir "*** Found neither a .cabal file nor package.yaml. Exiting."
204+
205+onlyCabalFromDirectory :: FilePath -> String -> MaybeT IO (Bool, Cabal.GenericPackageDescription)
206+onlyCabalFromDirectory dir errMsg = do
207+ cabals <- liftIO $ getDirectoryContents dir >>= filterM doesFileExist . map (dir </>) . filter (".cabal" `isSuffixOf`)
208+ case cabals of
209+ [] -> fail errMsg
210+ [cabalFile] -> (,) False <$> cabalFromFile True cabalFile
211+ _ -> liftIO $ fail ("*** found more than one cabal file (" ++ show cabals ++ "). Exiting.")
203212
204213handleIO :: (Exception.IOException -> IO a) -> IO a -> IO a
205214handleIO = Exception.handle