Skip to content

Commit

Permalink
Merge pull request #48 from haskell-hvr/findPlanJson
Browse files Browse the repository at this point in the history
Find plan json
  • Loading branch information
phadej authored Nov 21, 2019
2 parents 25389b0 + 6a45e7f commit 53db40c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 10 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Revision history for `cabal-plan`

## 0.6.2.0

### `lib:cabal-plan` Library

* Add `findPlanJson` function

### `exe:cabal-plan` Executable

* Drop `process-extras` dependency

## 0.6.1.0

### `lib:cabal-plan` Library
Expand Down
3 changes: 1 addition & 2 deletions cabal-plan.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cabal-version: 2.2
name: cabal-plan
version: 0.6.1.0
x-revision: 1
version: 0.6.2.0

synopsis: Library and utility for processing cabal's plan.json file
description: {
Expand Down
2 changes: 1 addition & 1 deletion src-exe/Flag.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ data P (def :: Bool) = P

showHide :: HasDefault def t => t -> String -> String -> O.Parser (Flag t)
showHide t n d =
O.flag' (toFlag t True) (O.long ("show-" ++ n) <> O.help d)
O.flag' (toFlag t True) (O.long ("show-" ++ n) Data.Semigroup.<> O.help d)
<|> O.flag' (toFlag t False) (O.long ("hide-" ++ n))
<|> pure (def t)

Expand Down
31 changes: 21 additions & 10 deletions src/Cabal/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Cabal.Plan
-- * Convenience functions
, SearchPlanJson(..)
, findAndDecodePlanJson
, findPlanJson
, findProjectRoot
, decodePlanJson
) where
Expand Down Expand Up @@ -417,14 +418,23 @@ data SearchPlanJson
| ExactPath FilePath -- ^ Exact location of plan.json
deriving (Eq, Show, Read)

-- | Locates the project root for cabal project relative to specified
-- directory.
-- | Find and decode @plan.json@.
--
-- See 'findPlanJson' and 'decodePlanJson'.
--
findAndDecodePlanJson
:: SearchPlanJson
-> IO PlanJson
findAndDecodePlanJson searchLoc = findPlanJson searchLoc >>= decodePlanJson

-- | Find @plan.json@.
--
-- When 'ProjectRelativeToDir' is passed locates the project root for cabal
-- project relative to specified directory.
--
-- @plan.json@ is located from either the optional build dir argument, or in
-- the default directory (@dist-newstyle@) relative to the project root.
--
-- The folder assumed to be the project-root is returned as well.
--
-- This function determines the project root in a slightly more liberal manner
-- than cabal-install. If no cabal.project is found, cabal-install assumes an
-- implicit cabal.project if the current directory contains any *.cabal files.
Expand All @@ -435,25 +445,27 @@ data SearchPlanJson
--
-- Throws 'IO' exceptions on errors.
--
findAndDecodePlanJson
-- @since 0.6.2.0
--
findPlanJson
:: SearchPlanJson
-> IO PlanJson
findAndDecodePlanJson searchLoc = do
-> IO FilePath
findPlanJson searchLoc = do
planJsonFn <- case searchLoc of
ExactPath fp -> pure fp
InBuildDir builddir -> fromBuilddir builddir
ProjectRelativeToDir fp -> do
mRoot <- findProjectRoot fp
case mRoot of
Nothing -> fail ("missing project root relative to: " ++ fp)
Just dir -> fromBuilddir$ dir </> "dist-newstyle"
Just dir -> fromBuilddir $ dir </> "dist-newstyle"

havePlanJson <- Dir.doesFileExist planJsonFn

unless havePlanJson $
fail "missing 'plan.json' file; do you need to run 'cabal new-build'?"

decodePlanJson planJsonFn
return planJsonFn
where
fromBuilddir distFolder = do
haveDistFolder <- Dir.doesDirectoryExist distFolder
Expand All @@ -463,7 +475,6 @@ findAndDecodePlanJson searchLoc = do

return $ distFolder </> "cache" </> "plan.json"


-- | Decodes @plan.json@ file location provided as 'FilePath'
--
-- This is a trivial convenience function so that the caller doesn't
Expand Down

0 comments on commit 53db40c

Please sign in to comment.