|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE KindSignatures #-} |
| 4 | +{-# LANGUAGE QuasiQuotes #-} |
| 5 | +{-# LANGUAGE TemplateHaskellQuotes #-} |
| 6 | + |
| 7 | +-- | Copyright : (c) Crown Copyright GCHQ |
| 8 | +-- Description : Represent dependencies of haskell projects |
| 9 | +module Bootstrap.Data.HaskellDependency |
| 10 | + ( HaskellDependency, |
| 11 | + VersionKnown (..), |
| 12 | + getHaskellDependencyVersions, |
| 13 | + hdep, |
| 14 | + ) |
| 15 | +where |
| 16 | + |
| 17 | +import Bootstrap.Cli (RunConfig) |
| 18 | +import Bootstrap.Data.ProjectType (HaskellOptions) |
| 19 | +import Bootstrap.Nix.Evaluate |
| 20 | + ( NixBinaryPaths, |
| 21 | + evaluateNixExpression, |
| 22 | + extractNixVersionString, |
| 23 | + ) |
| 24 | +import Bootstrap.Nix.Expr |
| 25 | + ( Expr (EGrouping, ELetIn), |
| 26 | + Identifier (Identifier), |
| 27 | + Property (PIdent), |
| 28 | + nixproperty, |
| 29 | + (|.), |
| 30 | + (|=), |
| 31 | + ) |
| 32 | +import Bootstrap.Nix.Expr.Haskell (haskellPackagesExpr) |
| 33 | +import Bootstrap.Nix.Expr.Nixpkgs (nixpkgsExpr) |
| 34 | +import Control.Exception (IOException) |
| 35 | +import Data.Aeson (ToJSON (toJSON)) |
| 36 | +import qualified Data.Aeson as Aeson |
| 37 | +import Language.Haskell.TH (ExpQ) |
| 38 | +import qualified Relude.Extra.Map as M |
| 39 | + |
| 40 | +-- | Whether the version of a particular dependency is known |
| 41 | +data VersionKnown = VersionKnown | VersionUnknown |
| 42 | + |
| 43 | +-- | Represents a library on which a haskell project depends |
| 44 | +data HaskellDependency (versionKnown :: VersionKnown) where |
| 45 | + HaskellDependencyBase :: HaskellDependency versionKnown |
| 46 | + HaskellDependencyBoot :: Text -> HaskellDependency versionKnown |
| 47 | + HaskellDependencyVersioned :: |
| 48 | + Text -> |
| 49 | + HaskellDependencyVersion versionKnown -> |
| 50 | + HaskellDependency versionKnown |
| 51 | + |
| 52 | +instance ToJSON (HaskellDependency 'VersionKnown) where |
| 53 | + toJSON = \case |
| 54 | + HaskellDependencyBase -> |
| 55 | + Aeson.object |
| 56 | + [ ("name", "base"), |
| 57 | + ("mixin", Aeson.Array $ fromList ["hiding (Prelude)"]) |
| 58 | + ] |
| 59 | + HaskellDependencyBoot d -> Aeson.String d |
| 60 | + HaskellDependencyVersioned d (HaskellDependencyVersionExact v) -> |
| 61 | + Aeson.String $ |
| 62 | + d <> " == " <> v |
| 63 | + |
| 64 | +data HaskellDependencyVersion (versionKnown :: VersionKnown) where |
| 65 | + HaskellDependencyVersionUnknown :: HaskellDependencyVersion 'VersionUnknown |
| 66 | + HaskellDependencyVersionExact :: Text -> HaskellDependencyVersion 'VersionKnown |
| 67 | + |
| 68 | +dependencies :: Map Text (HaskellDependency 'VersionUnknown) |
| 69 | +dependencies = |
| 70 | + fromList |
| 71 | + [ ("base", HaskellDependencyBase), |
| 72 | + hDepBoot "Cabal", |
| 73 | + hDepBoot "Cabal-syntax", |
| 74 | + hDepBoot "Win32", |
| 75 | + hDepBoot "array", |
| 76 | + hDepBoot "bin-package-db", |
| 77 | + hDepBoot "binary", |
| 78 | + hDepBoot "bytestring", |
| 79 | + hDepBoot "containers", |
| 80 | + hDepBoot "deepseq", |
| 81 | + hDepBoot "directory", |
| 82 | + hDepBoot "exceptions", |
| 83 | + hDepBoot "extensible-exceptions", |
| 84 | + hDepBoot "ffi", |
| 85 | + hDepBoot "filepath", |
| 86 | + hDepBoot "ghc", |
| 87 | + hDepBoot "ghc-bignum", |
| 88 | + hDepBoot "ghc-binary", |
| 89 | + hDepBoot "ghc-boot", |
| 90 | + hDepBoot "ghc-boot-th", |
| 91 | + hDepBoot "ghc-compact", |
| 92 | + hDepBoot "ghc-heap", |
| 93 | + hDepBoot "ghc-prim", |
| 94 | + hDepBoot "ghci", |
| 95 | + hDepBoot "haskeline", |
| 96 | + hDepBoot "haskell2010", |
| 97 | + hDepBoot "haskell98", |
| 98 | + hDepBoot "hoopl", |
| 99 | + hDepBoot "hpc", |
| 100 | + hDepBoot "integer-gmp", |
| 101 | + hDepBoot "libiserv", |
| 102 | + hDepBoot "mtl", |
| 103 | + hDepBoot "old-locale", |
| 104 | + hDepBoot "old-time", |
| 105 | + hDepBoot "parsec", |
| 106 | + hDepBoot "pretty", |
| 107 | + hDepBoot "process", |
| 108 | + hDepBoot "random", |
| 109 | + hDepBoot "rts", |
| 110 | + hDepBoot "stm", |
| 111 | + hDepBoot "system-cxx-std-lib", |
| 112 | + hDepBoot "template-haskell", |
| 113 | + hDepBoot "terminfo", |
| 114 | + hDepBoot "text", |
| 115 | + hDepBoot "time", |
| 116 | + hDepBoot "transformers", |
| 117 | + hDepBoot "unix", |
| 118 | + hDepBoot "xhtml", |
| 119 | + hDepUnknown "relude" |
| 120 | + ] |
| 121 | + where |
| 122 | + hDepBoot :: Text -> (Text, HaskellDependency 'VersionUnknown) |
| 123 | + hDepBoot name = (name, HaskellDependencyBoot name) |
| 124 | + hDepUnknown :: Text -> (Text, HaskellDependency 'VersionUnknown) |
| 125 | + hDepUnknown name = (name, HaskellDependencyVersioned name HaskellDependencyVersionUnknown) |
| 126 | + |
| 127 | +-- | Get a `HaskellDependency 'VersionUnknown` for the given dependency name |
| 128 | +hdep :: Text -> ExpQ |
| 129 | +hdep name = case M.lookup name dependencies of |
| 130 | + Just dep -> case dep of |
| 131 | + HaskellDependencyBase -> [|HaskellDependencyBase|] |
| 132 | + HaskellDependencyBoot _ -> |
| 133 | + [|HaskellDependencyBoot name|] |
| 134 | + HaskellDependencyVersioned _ HaskellDependencyVersionUnknown -> |
| 135 | + [|HaskellDependencyVersioned name HaskellDependencyVersionUnknown|] |
| 136 | + Nothing -> error $ "Could not find " <> name <> " in dependency map in Bootstrap.Data.HaskellDependency" |
| 137 | + |
| 138 | +getHaskellDependencyVersions :: |
| 139 | + MonadIO m => |
| 140 | + NixBinaryPaths -> |
| 141 | + RunConfig -> |
| 142 | + HaskellOptions -> |
| 143 | + [HaskellDependency 'VersionUnknown] -> |
| 144 | + m (Either IOException [HaskellDependency 'VersionKnown]) |
| 145 | +getHaskellDependencyVersions nixBinaryPaths rc haskellOptions deps = do |
| 146 | + results <- forM deps \case |
| 147 | + HaskellDependencyBase -> pure $ Right HaskellDependencyBase |
| 148 | + HaskellDependencyBoot d -> pure . Right $ HaskellDependencyBoot d |
| 149 | + HaskellDependencyVersioned d HaskellDependencyVersionUnknown -> do |
| 150 | + evaluateNixExpression |
| 151 | + nixBinaryPaths |
| 152 | + rc |
| 153 | + ( ELetIn |
| 154 | + (one $ [nixproperty|nixpkgs|] |= nixpkgsExpr rc) |
| 155 | + ( EGrouping (haskellPackagesExpr haskellOptions) |
| 156 | + |. PIdent (Identifier d) |
| 157 | + |. [nixproperty|version|] |
| 158 | + ) |
| 159 | + ) |
| 160 | + <&> fmap |
| 161 | + ( HaskellDependencyVersioned d |
| 162 | + . HaskellDependencyVersionExact |
| 163 | + . toText |
| 164 | + . extractNixVersionString |
| 165 | + ) |
| 166 | + pure $ foldr throwAnyErrors (Right []) results |
| 167 | + where |
| 168 | + -- If we hit any errors, fail the whole thing |
| 169 | + throwAnyErrors _ e@(Left _) = e |
| 170 | + throwAnyErrors (Left e) _ = Left e |
| 171 | + -- Keep building up the accumulator as long as we're succeeding |
| 172 | + throwAnyErrors (Right next) (Right acc) = Right (next : acc) |
0 commit comments