mirror of
https://github.com/simplex-chat/haskell.nix.git
synced 2026-06-03 09:17:32 +00:00
5b7f7f245c
git-subtree-dir: nix-tools git-subtree-mainline:1110a74a9dgit-subtree-split:555d57e1ea
48 lines
1.5 KiB
Haskell
48 lines
1.5 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
|
|
module Stack2nix.Project
|
|
( findCabalFiles
|
|
) where
|
|
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text.Encoding as T
|
|
import Data.ByteString (ByteString)
|
|
|
|
import System.FilePath ((</>))
|
|
import System.Directory (listDirectory, doesFileExist)
|
|
import Data.List (isSuffixOf)
|
|
|
|
import qualified Hpack.Config as Hpack
|
|
import qualified Hpack.Render as Hpack
|
|
|
|
import Cabal2Nix (CabalFile(..), CabalFileGenerator(..))
|
|
|
|
import Stack2nix.CLI (HpackUse(..))
|
|
|
|
findCabalFiles :: HpackUse -> FilePath -> IO [CabalFile]
|
|
findCabalFiles IgnorePackageYaml path = findOnlyCabalFiles path
|
|
findCabalFiles UsePackageYamlFirst path = doesFileExist (path </> Hpack.packageConfig) >>= \case
|
|
False -> findOnlyCabalFiles path
|
|
True -> do
|
|
mbPkg <- Hpack.readPackageConfig Hpack.defaultDecodeOptions {Hpack.decodeOptionsTarget = path </> Hpack.packageConfig}
|
|
case mbPkg of
|
|
Left e -> error e
|
|
Right r ->
|
|
return $ [InMemory (Just Hpack)
|
|
(Hpack.decodeResultCabalFile r)
|
|
(encodeUtf8 $ render r)]
|
|
|
|
where
|
|
render :: Hpack.DecodeResult -> String
|
|
render r =
|
|
let body = Hpack.renderPackage [] (Hpack.decodeResultPackage r)
|
|
cabalVersion = Hpack.decodeResultCabalVersion r
|
|
in cabalVersion ++ body
|
|
|
|
encodeUtf8 :: String -> ByteString
|
|
encodeUtf8 = T.encodeUtf8 . T.pack
|
|
|
|
|
|
findOnlyCabalFiles :: FilePath -> IO [CabalFile]
|
|
findOnlyCabalFiles path = fmap (OnDisk . (path </>)) . filter (isSuffixOf ".cabal") <$> listDirectory path
|