@@ -8,16 +8,15 @@ module Stack2nix
8
8
9
9
import qualified Data.Text as T
10
10
import Data.String (fromString )
11
- import Data.Maybe (fromMaybe )
12
11
13
12
import Control.Monad.Trans.Maybe
14
13
import Control.Monad.IO.Class (liftIO )
15
- import Control.Monad (unless , forM )
14
+ import Control.Monad (unless , forM , forM_ )
16
15
import Extra (unlessM )
17
16
18
17
import qualified Data.Map as M (fromListWith , toList )
19
18
import System.FilePath ((<.>) , (</>) , takeDirectory , dropFileName )
20
- import System.Directory (createDirectoryIfMissing , doesDirectoryExist , doesFileExist , getCurrentDirectory )
19
+ import System.Directory (createDirectoryIfMissing , doesDirectoryExist , doesFileExist )
21
20
import System.IO (IOMode (.. ), openFile , hClose )
22
21
import Data.Yaml (decodeFileEither )
23
22
@@ -63,7 +62,7 @@ stackexpr args =
63
62
=<< resolveSnapshot (argStackYaml args) value
64
63
65
64
stack2nix :: Args -> Stack -> IO NExpr
66
- stack2nix args stack @ (Stack resolver compiler pkgs pkgFlags ghcOpts) =
65
+ stack2nix args (Stack resolver compiler pkgs pkgFlags ghcOpts) =
67
66
do let extraDeps = extraDeps2nix pkgs
68
67
flags = flags2nix pkgFlags
69
68
ghcOptions = ghcOptions2nix ghcOpts
@@ -148,7 +147,6 @@ writeDoc file doc =
148
147
-- makeRelativeToCurrentDirectory
149
148
packages2nix :: Args -> [Dependency ] -> IO [(T. Text , Binding NExpr )]
150
149
packages2nix args pkgs =
151
- do cwd <- getCurrentDirectory
152
150
fmap concat . forM pkgs $ \ case
153
151
(LocalPath folder) ->
154
152
do cabalFiles <- findCabalFiles (argHpackUse args) (dropFileName (argStackYaml args) </> folder)
@@ -165,11 +163,15 @@ packages2nix args pkgs =
165
163
do hits <- forM subdirs $ \ subdir -> liftIO $ cacheHits (argCacheFile args) url rev subdir
166
164
if any null hits
167
165
then
168
- fetch (return . cabalFromPath url rev subdirs)
166
+ -- If any of the subdirs were missing we need to fetch the files and
167
+ -- generate the bindings.
168
+ fetch (cabalFromPath url rev subdirs)
169
169
(Source url rev UnknownHash ) >>= \ case
170
- (Just (DerivationSource {.. }, genBindings)) -> fromMaybe [] <$> runMaybeT ( genBindings derivHash)
170
+ (Just (DerivationSource {.. }, genBindings)) -> genBindings derivHash
171
171
_ -> return []
172
172
else
173
+ -- If the subdirs are all in the cache then the bindings should already be
174
+ -- generated too.
173
175
forM (concat hits) $ \ ( pkg, nix ) ->
174
176
return (fromString pkg, fromString pkg $= mkPath False nix)
175
177
_ -> return []
@@ -179,13 +181,19 @@ packages2nix args pkgs =
179
181
-> String -- Revision
180
182
-> [FilePath ] -- Subdirs
181
183
-> FilePath -- Local Directory
182
- -> String -- Sha256
183
- -> MaybeT IO [(T. Text , Binding NExpr )]
184
- cabalFromPath url rev subdirs dir sha256 =
185
- fmap concat . forM subdirs $ \ subdir -> do
184
+ -> MaybeT IO (String -> IO [(T. Text , Binding NExpr )])
185
+ cabalFromPath url rev subdirs dir = do
186
+ -- Check that all the subdirs exist if not this
187
+ -- fail the MaybeT so that the next fetcher will be tried
188
+ forM_ subdirs $ \ subdir -> do
186
189
let path = dir </> subdir
187
190
d <- liftIO $ doesDirectoryExist path
188
191
unless d $ fail (" not a directory: " ++ path)
192
+ -- If we got this far we are confident we have downloaded
193
+ -- with the right fetcher. Return an action that will
194
+ -- be used to generate the bindings.
195
+ return $ \ sha256 -> fmap concat . forM subdirs $ \ subdir -> do
196
+ let path = dir </> subdir
189
197
cabalFiles <- liftIO $ findCabalFiles (argHpackUse args) path
190
198
forM cabalFiles $ \ cabalFile -> do
191
199
let pkg = cabalFilePkgName cabalFile
0 commit comments