Skip to content

Commit

Permalink
Add support for hpack (see commercialhaskell#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Feb 17, 2016
1 parent efcadef commit 3c0d311
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Stack/Build/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Stack/Ghci.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/Stack/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Stack.Package
,readPackageUnresolved
,readPackageUnresolvedBS
,resolvePackage
,getCabalFileName
,findOrGenerateCabalFile
,Package(..)
,GetPackageFiles(..)
,GetPackageOpts(..)
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/Stack/SDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions stack-7.8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions stack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ nix:
- zlib
extra-deps:
- path-io-0.3.1
- hpack-0.9.0

0 comments on commit 3c0d311

Please sign in to comment.