Skip to content

Commit b1efc35

Browse files
yorickvPangerman
authored andcommitted
cabal-to-nix: add fallback to hpack when cabal file does not exist (input-output-hk#71)
Also improve some errors. Fixes input-output-hk#308.
1 parent cee8245 commit b1efc35

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cabal2nix/Main.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Main where
44

55
import System.Environment (getArgs)
66

7+
import System.Exit
78
import System.Directory
89
import System.FilePath
910
import Control.Monad
@@ -26,7 +27,6 @@ import Data.ByteString (ByteString)
2627

2728
import Data.List (isPrefixOf, isSuffixOf)
2829

29-
import qualified Hpack
3030
import qualified Hpack.Config as Hpack
3131
import qualified Hpack.Render as Hpack
3232
import qualified Data.Text as T
@@ -48,12 +48,20 @@ main = getArgs >>= \case
4848
(Just (DerivationSource{..}, genBindings)) -> genBindings derivHash
4949
_ -> return ()
5050
[path,file] -> doesDirectoryExist file >>= \case
51-
False -> print . prettyNix =<< cabal2nix False MinimalDetails (Just (Path path)) (OnDisk file)
51+
False -> doesFileExist file >>= \case
52+
True -> nixFromCabal path (OnDisk file)
53+
False -> (findCabalFiles $ takeDirectory file) >>= \case
54+
[] -> die "Cabal file does not exit, no package.yaml found either"
55+
[cabal] -> nixFromCabal path cabal
56+
_ -> die "Cabal file does not exist, but multiple other cabal files found"
5257
True -> print . prettyNix =<< cabalexprs file
5358
[file] -> doesDirectoryExist file >>= \case
54-
False -> print . prettyNix =<< cabal2nix False MinimalDetails (Just (Path ".")) (OnDisk file)
59+
False -> nixFromCabal "." (OnDisk file)
5560
True -> print . prettyNix =<< cabalexprs file
56-
_ -> putStrLn "call with cabalfile (Cabal2Nix file.cabal)."
61+
_ -> die "call with cabalfile (Cabal2Nix file.cabal)."
62+
where
63+
nixFromCabal path cabal =
64+
print . prettyNix =<< cabal2nix False MinimalDetails (Just (Path path)) cabal
5765

5866
cabalFromPath
5967
:: String -- URL

0 commit comments

Comments
 (0)