From 3c0d311d23071d3c4565137e1c25d5e2dcd88e0e Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Wed, 10 Feb 2016 14:50:11 +0800 Subject: [PATCH] Add support for hpack (see #1679) --- src/Stack/Build/Source.hs | 2 +- src/Stack/Ghci.hs | 2 +- src/Stack/Package.hs | 21 +++++++++++++++++---- src/Stack/SDist.hs | 4 ++-- src/main/Main.hs | 4 ++-- stack-7.8.yaml | 2 ++ stack.cabal | 1 + stack.yaml | 1 + 8 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Stack/Build/Source.hs b/src/Stack/Build/Source.hs index 9bc551bdf4..359dea6946 100644 --- a/src/Stack/Build/Source.hs +++ b/src/Stack/Build/Source.hs @@ -227,7 +227,7 @@ getLocalPackageViews :: (MonadThrow m, MonadIO m, MonadReader env m, HasEnvConfi getLocalPackageViews = do econfig <- asks getEnvConfig locals <- forM (Map.toList $ envConfigPackages econfig) $ \(dir, validWanted) -> do - cabalfp <- getCabalFileName dir + cabalfp <- findOrGenerateCabalFile dir (warnings,gpkg) <- readPackageUnresolved cabalfp mapM_ (printCabalFileWarning cabalfp) warnings let cabalID = package $ packageDescription gpkg diff --git a/src/Stack/Ghci.hs b/src/Stack/Ghci.hs index 88d595ea82..4ee3bbeeb8 100644 --- a/src/Stack/Ghci.hs +++ b/src/Stack/Ghci.hs @@ -299,7 +299,7 @@ ghciSetup GhciOpts{..} = do directlyWanted <- forMaybeM (M.toList (envConfigPackages econfig)) $ \(dir,validWanted) -> - do cabalfp <- getCabalFileName dir + do cabalfp <- findOrGenerateCabalFile dir name <- parsePackageNameFromFilePath cabalfp if validWanted then case M.lookup name targets of diff --git a/src/Stack/Package.hs b/src/Stack/Package.hs index 6b993c08a7..18579e3896 100644 --- a/src/Stack/Package.hs +++ b/src/Stack/Package.hs @@ -20,7 +20,7 @@ module Stack.Package ,readPackageUnresolved ,readPackageUnresolvedBS ,resolvePackage - ,getCabalFileName + ,findOrGenerateCabalFile ,Package(..) ,GetPackageFiles(..) ,GetPackageOpts(..) @@ -93,6 +93,8 @@ import qualified System.Directory as D import System.FilePath (splitExtensions, replaceExtension) import qualified System.FilePath as FilePath import System.IO.Error +import qualified Hpack +import qualified Hpack.Config as Hpack packageIdentifier :: Package -> Stack.Types.PackageIdentifier.PackageIdentifier packageIdentifier pkg = @@ -149,7 +151,7 @@ readPackageDescriptionDir :: (MonadLogger m, MonadIO m, MonadThrow m, MonadCatch -> Path Abs Dir -> m (GenericPackageDescription, PackageDescription) readPackageDescriptionDir config pkgDir = do - cabalfp <- getCabalFileName pkgDir + cabalfp <- findOrGenerateCabalFile pkgDir gdesc <- liftM snd (readPackageUnresolved cabalfp) return (gdesc, resolvePackageDescription config gdesc) @@ -1057,11 +1059,15 @@ logPossibilities dirs mn = do -- -- If no .cabal file is present, or more than one is present, an exception is -- thrown via 'throwM'. -getCabalFileName +-- +-- If the directory contains a file named package.yaml, hpack is used to +-- generate a .cabal file from it. +findOrGenerateCabalFile :: (MonadThrow m, MonadIO m) => Path Abs Dir -- ^ package directory -> m (Path Abs File) -getCabalFileName pkgDir = do +findOrGenerateCabalFile pkgDir = do + liftIO $ hpack pkgDir files <- liftIO $ findFiles pkgDir (flip hasExtension "cabal" . FL.toFilePath) @@ -1072,6 +1078,13 @@ getCabalFileName pkgDir = do _:_ -> throwM $ PackageMultipleCabalFilesFound pkgDir files where hasExtension fp x = FilePath.takeExtension fp == "." ++ x +-- | Generate .cabal file from package.yaml, if necessary. +hpack :: Path Abs Dir -> IO () +hpack pkgDir = do + exists <- doesFileExist (pkgDir $(mkRelFile Hpack.packageConfig)) + when exists $ do + Hpack.hpack (toFilePath pkgDir) True + -- | Path for the package's build log. buildLogPath :: (MonadReader env m, HasBuildConfig env, MonadThrow m) => Package -> Maybe String -> m (Path Abs File) diff --git a/src/Stack/SDist.hs b/src/Stack/SDist.hs index f6fb30bbea..4a84f63421 100644 --- a/src/Stack/SDist.hs +++ b/src/Stack/SDist.hs @@ -174,7 +174,7 @@ gtraverseT f = -- use-cases. readLocalPackage :: M env m => Path Abs Dir -> m LocalPackage readLocalPackage pkgDir = do - cabalfp <- getCabalFileName pkgDir + cabalfp <- findOrGenerateCabalFile pkgDir name <- parsePackageNameFromFilePath cabalfp config <- getPackageConfig defaultBuildOpts name (warnings,package) <- readPackage config cabalfp @@ -267,7 +267,7 @@ checkSDistTarball tarball = withTempTarGzContents tarball $ \pkgDir' -> do pkgDir <- (pkgDir' ) `liftM` (parseRelDir . FP.takeBaseName . FP.takeBaseName . toFilePath $ tarball) -- ^ drop ".tar" ^ drop ".gz" - cabalfp <- getCabalFileName pkgDir + cabalfp <- findOrGenerateCabalFile pkgDir name <- parsePackageNameFromFilePath cabalfp config <- getPackageConfig defaultBuildOpts name (gdesc, pkgDesc) <- readPackageDescriptionDir config pkgDir diff --git a/src/main/Main.hs b/src/main/Main.hs index 27e745b531..b9e327d9f3 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -75,7 +75,7 @@ import qualified Stack.Image as Image import Stack.Init import Stack.New import Stack.Options -import Stack.Package (getCabalFileName) +import Stack.Package (findOrGenerateCabalFile) import qualified Stack.PackageIndex import Stack.SDist (getSDistTarball, checkSDistTarball, checkSDistTarball') import Stack.Setup @@ -1063,7 +1063,7 @@ packagesCmd () go@GlobalOpts{..} = locals <- forM (M.toList (envConfigPackages econfig)) $ \(dir,_) -> - do cabalfp <- getCabalFileName dir + do cabalfp <- findOrGenerateCabalFile dir parsePackageNameFromFilePath cabalfp forM_ locals (liftIO . putStrLn . packageNameString) diff --git a/stack-7.8.yaml b/stack-7.8.yaml index a0ce9e96cb..cdaf56e01b 100644 --- a/stack-7.8.yaml +++ b/stack-7.8.yaml @@ -23,3 +23,5 @@ extra-deps: - hpc-0.6.0.2 - process-1.2.1.0 - time-1.5.0.1 +- base-compat-0.9.0 +- hpack-0.9.0 diff --git a/stack.cabal b/stack.cabal index 16f79c7054..a95946f8a9 100644 --- a/stack.cabal +++ b/stack.cabal @@ -207,6 +207,7 @@ library , project-template >= 0.2 , uuid , zip-archive + , hpack >= 0.9.0 if os(windows) cpp-options: -DWINDOWS build-depends: Win32 diff --git a/stack.yaml b/stack.yaml index e804d2be53..5e7f2a0846 100644 --- a/stack.yaml +++ b/stack.yaml @@ -11,3 +11,4 @@ nix: - zlib extra-deps: - path-io-0.3.1 +- hpack-0.9.0