@@ -2,7 +2,8 @@ module Cabal2Nix.Hackage ( hashPackage, readCabalFile ) where
22
33import Control.Monad ( unless )
44import Data.List ( isPrefixOf )
5−import Data.Version ( showVersion )
5+import Data.Maybe ( fromJust )
6+import Data.Version ( showVersion, versionBranch )
67import Distribution.Package ( PackageIdentifier(..), PackageName(..) )
78import Distribution.Text
89import Network.HTTP ( getRequest, rspBody )
@@ -11,6 +12,8 @@ import System.Directory ( doesFileExist, getHomeDirectory, createDirectoryIfMiss
1112import System.FilePath ( dropFileName, (</>), (<.>) )
1213import System.Process ( readProcess )
1314
15+import qualified Distribution.Hackage.DB as DB
16+
1417data Ext = TarGz | Cabal deriving Eq
1518
1619showExt :: Ext -> String
@@ -48,7 +51,17 @@ hashCachePath (PackageIdentifier (PackageName name) version') = do
4851
4952readCabalFile :: FilePath -> IO String
5053readCabalFile path
51− | "cabal://" `isPrefixOf` path = let Just pid = simpleParse (drop 8 path) in readCabalFile (hackagePath pid Cabal)
54+ | "cabal://" `isPrefixOf` path = do let pid p = fromJust $ simpleParse p
55+ packageName = drop 8 path
56+ case versionBranch $ pkgVersion $ pid packageName of
57+ [] -> do
58+ packageDescription <- DB.lookup packageName `fmap` DB.readHackage
59+ case packageDescription of
60+ Just d -> do
61+ let version = showVersion $ last $ DB.keys d
62+ readCabalFile $ hackagePath (pid $ packageName ++ "-" ++ version) Cabal
63+ Nothing -> error "No such package"
64+ _ -> readCabalFile $ hackagePath (pid packageName) Cabal
5265 | "http://" `isPrefixOf` path = fetchUrl path
5366 | "file://" `isPrefixOf` path = readCabalFile (drop 7 path)
5467 | otherwise = readFile path