|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | + |
| 4 | +module Main |
| 5 | +where |
| 6 | + |
| 7 | +import Cabal2Nix |
| 8 | +import Cabal2Nix.Util ( quoted ) |
| 9 | +import Crypto.Hash.SHA256 ( hash |
| 10 | + , hashlazy |
| 11 | + ) |
| 12 | +import qualified Data.ByteString.Base16 as Base16 |
| 13 | +import qualified Data.ByteString.Char8 as BS |
| 14 | +import qualified Data.ByteString.Lazy as BL |
| 15 | +import Data.Foldable ( toList ) |
| 16 | +import qualified Data.Map as Map |
| 17 | +import qualified Data.Sequence as Seq |
| 18 | +import Data.String ( IsString(fromString) ) |
| 19 | +import Data.Text.Encoding ( decodeUtf8 ) |
| 20 | +import Distribution.Hackage.DB ( hackageTarball ) |
| 21 | +import qualified Distribution.Hackage.DB.Parsed |
| 22 | + as P |
| 23 | +import Distribution.Hackage.DB.Parsed ( parseMetaData |
| 24 | + , parseVersionData |
| 25 | + ) |
| 26 | +import Distribution.Hackage.DB.Unparsed |
| 27 | +import Distribution.Pretty ( prettyShow |
| 28 | + , Pretty |
| 29 | + ) |
| 30 | +import Nix.Expr |
| 31 | +import Nix.Pretty ( prettyNix ) |
| 32 | +import System.Directory ( createDirectoryIfMissing ) |
| 33 | +import System.Environment ( getArgs ) |
| 34 | +import System.FilePath ( (</>) |
| 35 | + , (<.>) |
| 36 | + ) |
| 37 | + |
| 38 | +main :: IO () |
| 39 | +main = do |
| 40 | + [out] <- getArgs |
| 41 | + db <- readTarball Nothing =<< hackageTarball |
| 42 | + |
| 43 | + let defaultNix = seqToSet $ Map.foldMapWithKey package2nix db |
| 44 | + createDirectoryIfMissing False out |
| 45 | + writeFile (out </> "default.nix") $ show $ prettyNix defaultNix |
| 46 | + |
| 47 | + _ <- forWithKey db $ \pname (PackageData { versions }) -> |
| 48 | + forWithKey versions $ \vnum vdata@(VersionData { cabalFileRevisions }) -> |
| 49 | + let parsedVData = parseVersionData pname vnum vdata |
| 50 | + writeFiles gpd cabalFile revNum = do |
| 51 | + let dir = out </> packagePath pname </> fromPretty vnum |
| 52 | + revPath = dir </> revName revNum |
| 53 | + createDirectoryIfMissing True dir |
| 54 | + BL.writeFile (revPath <.> "cabal") cabalFile |
| 55 | + writeFile (revPath <.> "nix") $ show $ prettyNix $ gpd2nix Nothing Nothing gpd |
| 56 | + in sequence $ zipWith3 writeFiles |
| 57 | + (toList (P.cabalFileRevisions parsedVData)) |
| 58 | + cabalFileRevisions |
| 59 | + [(0 :: Int) ..] |
| 60 | + return () |
| 61 | + where |
| 62 | + forWithKey :: Applicative f => Map.Map k v -> (k -> v -> f x) -> f (Map.Map k x) |
| 63 | + forWithKey = flip Map.traverseWithKey |
| 64 | + seqToSet = mkNonRecSet . toList |
| 65 | + fromPretty :: (Pretty a, IsString b) => a -> b |
| 66 | + fromPretty = fromString . prettyShow |
| 67 | + package2nix pname (PackageData { versions }) = |
| 68 | + Seq.singleton $ quoted (fromPretty pname) $= seqToSet |
| 69 | + (Map.foldMapWithKey (version2nix pname) versions) |
| 70 | + version2nix pname vnum (VersionData { cabalFileRevisions, metaFile }) = |
| 71 | + Seq.singleton $ quoted (fromPretty vnum) $= mkRecSet |
| 72 | + ( ("revision" $= mkSym (revName $ length cabalFileRevisions - 1)) |
| 73 | + : ("sha256" $= mkStr (fromString $ parseMetaData pname vnum metaFile Map.! "sha256")) |
| 74 | + : zipWith (revBinding (packagePath pname) vnum) cabalFileRevisions [(0 :: Int) ..] |
| 75 | + ) |
| 76 | + revName revNum = "r" <> fromString (show revNum) |
| 77 | + revBinding ppath vnum cabalFile revNum = |
| 78 | + let name :: (IsString a, Semigroup a) => a |
| 79 | + name = revName revNum |
| 80 | + revPath = "." </> ppath </> fromPretty vnum </> name |
| 81 | + in name $= mkNonRecSet |
| 82 | + [ "outPath" $= mkRelPath (revPath <.> "nix") |
| 83 | + , "cabalFile" $= mkRelPath (revPath <.> "cabal") |
| 84 | + , "cabalSha256" $= mkStr (decodeUtf8 $ Base16.encode $ hashlazy cabalFile) |
| 85 | + ] |
| 86 | + packagePath pname = |
| 87 | + BS.unpack (BS.take 30 $ Base16.encode $ hash $ fromPretty pname) ++ "-" ++ fromPretty pname |
0 commit comments