Skip to content

Commit 3c0d311

Browse files
committed
Add support for hpack (see #1679)
1 parent efcadef commit 3c0d311

File tree

8 files changed

+27
-10
lines changed

8 files changed

+27
-10
lines changed

src/Stack/Build/Source.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ getLocalPackageViews :: (MonadThrow m, MonadIO m, MonadReader env m, HasEnvConfi
227227
getLocalPackageViews = do
228228
econfig <- asks getEnvConfig
229229
locals <- forM (Map.toList $ envConfigPackages econfig) $ \(dir, validWanted) -> do
230-
cabalfp <- getCabalFileName dir
230+
cabalfp <- findOrGenerateCabalFile dir
231231
(warnings,gpkg) <- readPackageUnresolved cabalfp
232232
mapM_ (printCabalFileWarning cabalfp) warnings
233233
let cabalID = package $ packageDescription gpkg

src/Stack/Ghci.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ ghciSetup GhciOpts{..} = do
299299
directlyWanted <-
300300
forMaybeM (M.toList (envConfigPackages econfig)) $
301301
\(dir,validWanted) ->
302-
do cabalfp <- getCabalFileName dir
302+
do cabalfp <- findOrGenerateCabalFile dir
303303
name <- parsePackageNameFromFilePath cabalfp
304304
if validWanted
305305
then case M.lookup name targets of

src/Stack/Package.hs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Stack.Package
2020
,readPackageUnresolved
2121
,readPackageUnresolvedBS
2222
,resolvePackage
23-
,getCabalFileName
23+
,findOrGenerateCabalFile
2424
,Package(..)
2525
,GetPackageFiles(..)
2626
,GetPackageOpts(..)
@@ -93,6 +93,8 @@ import qualified System.Directory as D
9393
import System.FilePath (splitExtensions, replaceExtension)
9494
import qualified System.FilePath as FilePath
9595
import System.IO.Error
96+
import qualified Hpack
97+
import qualified Hpack.Config as Hpack
9698

9799
packageIdentifier :: Package -> Stack.Types.PackageIdentifier.PackageIdentifier
98100
packageIdentifier pkg =
@@ -149,7 +151,7 @@ readPackageDescriptionDir :: (MonadLogger m, MonadIO m, MonadThrow m, MonadCatch
149151
-> Path Abs Dir
150152
-> m (GenericPackageDescription, PackageDescription)
151153
readPackageDescriptionDir config pkgDir = do
152-
cabalfp <- getCabalFileName pkgDir
154+
cabalfp <- findOrGenerateCabalFile pkgDir
153155
gdesc <- liftM snd (readPackageUnresolved cabalfp)
154156
return (gdesc, resolvePackageDescription config gdesc)
155157

@@ -1057,11 +1059,15 @@ logPossibilities dirs mn = do
10571059
--
10581060
-- If no .cabal file is present, or more than one is present, an exception is
10591061
-- thrown via 'throwM'.
1060-
getCabalFileName
1062+
--
1063+
-- If the directory contains a file named package.yaml, hpack is used to
1064+
-- generate a .cabal file from it.
1065+
findOrGenerateCabalFile
10611066
:: (MonadThrow m, MonadIO m)
10621067
=> Path Abs Dir -- ^ package directory
10631068
-> m (Path Abs File)
1064-
getCabalFileName pkgDir = do
1069+
findOrGenerateCabalFile pkgDir = do
1070+
liftIO $ hpack pkgDir
10651071
files <- liftIO $ findFiles
10661072
pkgDir
10671073
(flip hasExtension "cabal" . FL.toFilePath)
@@ -1072,6 +1078,13 @@ getCabalFileName pkgDir = do
10721078
_:_ -> throwM $ PackageMultipleCabalFilesFound pkgDir files
10731079
where hasExtension fp x = FilePath.takeExtension fp == "." ++ x
10741080

1081+
-- | Generate .cabal file from package.yaml, if necessary.
1082+
hpack :: Path Abs Dir -> IO ()
1083+
hpack pkgDir = do
1084+
exists <- doesFileExist (pkgDir </> $(mkRelFile Hpack.packageConfig))
1085+
when exists $ do
1086+
Hpack.hpack (toFilePath pkgDir) True
1087+
10751088
-- | Path for the package's build log.
10761089
buildLogPath :: (MonadReader env m, HasBuildConfig env, MonadThrow m)
10771090
=> Package -> Maybe String -> m (Path Abs File)

src/Stack/SDist.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ gtraverseT f =
174174
-- use-cases.
175175
readLocalPackage :: M env m => Path Abs Dir -> m LocalPackage
176176
readLocalPackage pkgDir = do
177-
cabalfp <- getCabalFileName pkgDir
177+
cabalfp <- findOrGenerateCabalFile pkgDir
178178
name <- parsePackageNameFromFilePath cabalfp
179179
config <- getPackageConfig defaultBuildOpts name
180180
(warnings,package) <- readPackage config cabalfp
@@ -267,7 +267,7 @@ checkSDistTarball tarball = withTempTarGzContents tarball $ \pkgDir' -> do
267267
pkgDir <- (pkgDir' </>) `liftM`
268268
(parseRelDir . FP.takeBaseName . FP.takeBaseName . toFilePath $ tarball)
269269
-- ^ drop ".tar" ^ drop ".gz"
270-
cabalfp <- getCabalFileName pkgDir
270+
cabalfp <- findOrGenerateCabalFile pkgDir
271271
name <- parsePackageNameFromFilePath cabalfp
272272
config <- getPackageConfig defaultBuildOpts name
273273
(gdesc, pkgDesc) <- readPackageDescriptionDir config pkgDir

src/main/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import qualified Stack.Image as Image
7575
import Stack.Init
7676
import Stack.New
7777
import Stack.Options
78-
import Stack.Package (getCabalFileName)
78+
import Stack.Package (findOrGenerateCabalFile)
7979
import qualified Stack.PackageIndex
8080
import Stack.SDist (getSDistTarball, checkSDistTarball, checkSDistTarball')
8181
import Stack.Setup
@@ -1063,7 +1063,7 @@ packagesCmd () go@GlobalOpts{..} =
10631063
locals <-
10641064
forM (M.toList (envConfigPackages econfig)) $
10651065
\(dir,_) ->
1066-
do cabalfp <- getCabalFileName dir
1066+
do cabalfp <- findOrGenerateCabalFile dir
10671067
parsePackageNameFromFilePath cabalfp
10681068
forM_ locals (liftIO . putStrLn . packageNameString)
10691069

stack-7.8.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ extra-deps:
2323
- hpc-0.6.0.2
2424
- process-1.2.1.0
2525
- time-1.5.0.1
26+
- base-compat-0.9.0
27+
- hpack-0.9.0

stack.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ library
207207
, project-template >= 0.2
208208
, uuid
209209
, zip-archive
210+
, hpack >= 0.9.0
210211
if os(windows)
211212
cpp-options: -DWINDOWS
212213
build-depends: Win32

stack.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ nix:
1111
- zlib
1212
extra-deps:
1313
- path-io-0.3.1
14+
- hpack-0.9.0

0 commit comments

Comments
 (0)