Skip to content

Commit 245743b

Browse files
committed
Remove cabal-plan wrt #1092
1 parent 87194ec commit 245743b

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

app/ghcup/Main.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module Main where
1212

13+
import PlanJson
14+
1315
#if defined(BRICK)
1416
import GHCup.BrickMain (brickMain)
1517
#endif
@@ -30,7 +32,6 @@ import GHCup.Prelude.Logger
3032
import GHCup.Prelude.String.QQ
3133
import GHCup.Version
3234

33-
import Cabal.Plan ( findPlanJson, SearchPlanJson(..) )
3435
import Control.Concurrent
3536
import Control.Concurrent.Async
3637
import Control.Exception.Safe
@@ -113,11 +114,10 @@ toSettings options = do
113114
}
114115

115116

116-
117117
plan_json :: String
118118
plan_json = $( do
119119
(fp, c) <- runIO (handleIO (\_ -> pure ("", "")) $ do
120-
fp <- findPlanJson (ProjectRelativeToDir ".")
120+
fp <- findPlanJson "."
121121
c <- B.readFile fp
122122
(Just res) <- pure $ decodeStrict' @Value c
123123
pure (fp, T.unpack $ decUTF8Safe' $ encodePretty res))

app/ghcup/PlanJson.hs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module PlanJson where
2+
3+
import Control.Monad (unless)
4+
import System.FilePath
5+
import System.Directory
6+
7+
findPlanJson
8+
:: FilePath
9+
-> IO FilePath
10+
findPlanJson fp = do
11+
planJsonFn <- do
12+
mRoot <- findProjectRoot fp
13+
case mRoot of
14+
Nothing -> fail ("missing project root relative to: " ++ fp)
15+
Just dir -> fromBuilddir $ dir </> "dist-newstyle"
16+
17+
havePlanJson <- doesFileExist planJsonFn
18+
19+
unless havePlanJson $
20+
fail "missing 'plan.json' file; do you need to run 'cabal new-build'?"
21+
22+
return planJsonFn
23+
where
24+
fromBuilddir distFolder = do
25+
haveDistFolder <- doesDirectoryExist distFolder
26+
27+
unless haveDistFolder $
28+
fail ("missing " ++ show distFolder ++ " folder; do you need to run 'cabal new-build'?")
29+
30+
return $ distFolder </> "cache" </> "plan.json"
31+
32+
33+
-- | Find project root relative to a directory, this emulates cabal's current
34+
-- heuristic, but is slightly more liberal. If no cabal.project is found,
35+
-- cabal-install looks for *.cabal files in the specified directory only. This
36+
-- function also considers *.cabal files in directories higher up in the
37+
-- hierarchy.
38+
findProjectRoot :: FilePath -> IO (Maybe FilePath)
39+
findProjectRoot dir = do
40+
normalisedPath <- canonicalizePath dir
41+
let checkCabalProject d = do
42+
ex <- doesFileExist fn
43+
return $ if ex then Just d else Nothing
44+
where
45+
fn = d </> "cabal.project"
46+
47+
checkCabal d = do
48+
files <- listDirectory' d
49+
return $ if any (isExtensionOf' ".cabal") files
50+
then Just d
51+
else Nothing
52+
53+
result <- walkUpFolders checkCabalProject normalisedPath
54+
case result of
55+
Just rootDir -> pure $ Just rootDir
56+
Nothing -> walkUpFolders checkCabal normalisedPath
57+
where
58+
isExtensionOf' :: String -> FilePath -> Bool
59+
isExtensionOf' ext fp = ext == takeExtension fp
60+
61+
listDirectory' :: FilePath -> IO [FilePath]
62+
listDirectory' fp = filter isSpecialDir <$> getDirectoryContents fp
63+
where
64+
isSpecialDir f = f /= "." && f /= ".."
65+
66+
walkUpFolders :: (FilePath -> IO (Maybe a)) -> FilePath -> IO (Maybe a)
67+
walkUpFolders dtest d0 = do
68+
home <- getHomeDirectory
69+
70+
let go d | d == home = pure Nothing
71+
| isDrive d = pure Nothing
72+
| otherwise = do
73+
t <- dtest d
74+
case t of
75+
Nothing -> go $ takeDirectory d
76+
x@Just{} -> pure x
77+
78+
go d0
79+

ghcup.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ common app-common-depends
7171
, base >=4.12 && <5
7272
, bytestring >=0.10 && <0.12
7373
, cabal-install-parsers >=0.4.5
74-
, cabal-plan ^>=0.7.2
7574
, containers ^>=0.6
7675
, deepseq ^>=1.4
7776
, directory ^>=1.3.6.0
@@ -378,6 +377,7 @@ executable ghcup
378377
main-is: Main.hs
379378

380379
hs-source-dirs: app/ghcup
380+
other-modules: PlanJson
381381
default-language: Haskell2010
382382
default-extensions:
383383
LambdaCase

0 commit comments

Comments
 (0)