-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unreliable dependencies #1093
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module GHCup.CabalConfig (getStoreDir) where | ||
|
||
import Data.ByteString (ByteString) | ||
import Data.List.NonEmpty (NonEmpty) | ||
import Data.Map (Map) | ||
import System.Directory (getAppUserDataDirectory, doesDirectoryExist, getXdgDirectory, XdgDirectory(XdgConfig)) | ||
import System.Environment (lookupEnv) | ||
import System.FilePath ((</>)) | ||
|
||
import qualified Data.ByteString as BS | ||
import qualified Data.Map.Strict as M | ||
import qualified Distribution.CabalSpecVersion as C | ||
import qualified Distribution.FieldGrammar as C | ||
import qualified Distribution.FieldGrammar.Parsec as C | ||
import qualified Distribution.Fields as C | ||
import qualified Distribution.Fields.LexerMonad as C | ||
import qualified Distribution.Parsec as C | ||
import qualified Distribution.Utils.Generic as C | ||
import qualified Text.Parsec as P | ||
|
||
import Data.Foldable (for_) | ||
import Distribution.Parsec.Error | ||
|
||
|
||
|
||
|
||
getStoreDir :: IO FilePath | ||
getStoreDir = do | ||
fp <- findConfig | ||
bs <- BS.readFile fp | ||
either (fail . show . fmap (showPError fp)) resolveConfig (parseConfig bs) | ||
|
||
------------------------------------------------------------------------------- | ||
-- Find config | ||
------------------------------------------------------------------------------- | ||
|
||
-- | Find the @~\/.cabal\/config@ file. | ||
findConfig :: IO FilePath | ||
findConfig = do | ||
env <- lookupEnv "CABAL_CONFIG" | ||
case env of | ||
Just p -> return p | ||
Nothing -> do | ||
cabalDir <- findCabalDir | ||
return (cabalDir </> "config") | ||
|
||
-- | Find the @~\/.cabal@ dir. | ||
findCabalDir :: IO FilePath | ||
findCabalDir = do | ||
cabalDirVar <- lookupEnv "CABAL_DIR" | ||
appDir <- getAppUserDataDirectory "cabal" | ||
isXdg <- not <$> doesDirectoryExist appDir | ||
if | Just dir <- cabalDirVar -> pure dir | ||
| isXdg -> getXdgDirectory XdgConfig "cabal" | ||
| otherwise -> pure appDir | ||
|
||
|
||
------------------------------------------------------------------------------- | ||
-- Parsing | ||
------------------------------------------------------------------------------- | ||
|
||
-- | Parse @~\/.cabal\/config@ file. | ||
parseConfig :: ByteString -> Either (NonEmpty PError) (Maybe FilePath) | ||
parseConfig = parseWith $ \fields0 -> do | ||
let (fields1, _) = C.partitionFields fields0 | ||
let fields2 = M.filterWithKey (\k _ -> k `elem` knownFields) fields1 | ||
parse fields2 | ||
where | ||
knownFields = C.fieldGrammarKnownFieldList grammar | ||
|
||
parse :: Map C.FieldName [C.NamelessField C.Position] | ||
-> C.ParseResult (Maybe FilePath) | ||
parse fields = C.parseFieldGrammar C.cabalSpecLatest fields grammar | ||
|
||
grammar :: C.ParsecFieldGrammar (Maybe FilePath) (Maybe FilePath) | ||
grammar = mempty | ||
<$> C.optionalFieldAla "store-dir" C.FilePathNT id | ||
|
||
parseWith | ||
:: ([C.Field C.Position] -> C.ParseResult a) -- ^ parse | ||
-> ByteString -- ^ contents | ||
-> Either (NonEmpty PError) a | ||
parseWith parser bs = case C.runParseResult result of | ||
(_, Right x) -> Right x | ||
(_, Left (_, es)) -> Left es | ||
where | ||
result = case C.readFields' bs of | ||
Left perr -> C.parseFatalFailure pos (show perr) where | ||
ppos = P.errorPos perr | ||
pos = C.Position (P.sourceLine ppos) (P.sourceColumn ppos) | ||
Right (fields, lexWarnings) -> do | ||
C.parseWarnings (C.toPWarnings lexWarnings) | ||
for_ (C.validateUTF8 bs) $ \pos -> | ||
C.parseWarning C.zeroPos C.PWTUTF $ "UTF8 encoding problem at byte offset " ++ show pos | ||
parser fields | ||
|
||
------------------------------------------------------------------------------- | ||
-- Resolving | ||
------------------------------------------------------------------------------- | ||
|
||
-- | Fill the default in @~\/.cabal\/config@ file. | ||
resolveConfig :: Maybe FilePath -> IO FilePath | ||
resolveConfig (Just fp) = pure fp | ||
resolveConfig Nothing = do | ||
c <- findCabalDir | ||
return (c </> "store") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module GHCup.PlanJson where | ||
|
||
import Control.Monad (unless) | ||
import System.FilePath | ||
import System.Directory | ||
|
||
findPlanJson | ||
:: FilePath | ||
-> IO FilePath | ||
findPlanJson fp = do | ||
planJsonFn <- do | ||
mRoot <- findProjectRoot fp | ||
case mRoot of | ||
Nothing -> fail ("missing project root relative to: " ++ fp) | ||
Just dir -> fromBuilddir $ dir </> "dist-newstyle" | ||
|
||
havePlanJson <- doesFileExist planJsonFn | ||
|
||
unless havePlanJson $ | ||
fail "missing 'plan.json' file; do you need to run 'cabal new-build'?" | ||
|
||
return planJsonFn | ||
where | ||
fromBuilddir distFolder = do | ||
haveDistFolder <- doesDirectoryExist distFolder | ||
|
||
unless haveDistFolder $ | ||
fail ("missing " ++ show distFolder ++ " folder; do you need to run 'cabal new-build'?") | ||
|
||
return $ distFolder </> "cache" </> "plan.json" | ||
|
||
|
||
-- | Find project root relative to a directory, this emulates cabal's current | ||
-- heuristic, but is slightly more liberal. If no cabal.project is found, | ||
-- cabal-install looks for *.cabal files in the specified directory only. This | ||
-- function also considers *.cabal files in directories higher up in the | ||
-- hierarchy. | ||
findProjectRoot :: FilePath -> IO (Maybe FilePath) | ||
findProjectRoot dir = do | ||
normalisedPath <- canonicalizePath dir | ||
let checkCabalProject d = do | ||
ex <- doesFileExist fn | ||
return $ if ex then Just d else Nothing | ||
where | ||
fn = d </> "cabal.project" | ||
|
||
checkCabal d = do | ||
files <- listDirectory' d | ||
return $ if any (isExtensionOf' ".cabal") files | ||
then Just d | ||
else Nothing | ||
|
||
result <- walkUpFolders checkCabalProject normalisedPath | ||
case result of | ||
Just rootDir -> pure $ Just rootDir | ||
Nothing -> walkUpFolders checkCabal normalisedPath | ||
where | ||
isExtensionOf' :: String -> FilePath -> Bool | ||
isExtensionOf' ext fp = ext == takeExtension fp | ||
|
||
listDirectory' :: FilePath -> IO [FilePath] | ||
listDirectory' fp = filter isSpecialDir <$> getDirectoryContents fp | ||
where | ||
isSpecialDir f = f /= "." && f /= ".." | ||
|
||
walkUpFolders :: (FilePath -> IO (Maybe a)) -> FilePath -> IO (Maybe a) | ||
walkUpFolders dtest d0 = do | ||
home <- getHomeDirectory | ||
|
||
let go d | d == home = pure Nothing | ||
| isDrive d = pure Nothing | ||
| otherwise = do | ||
t <- dtest d | ||
case t of | ||
Nothing -> go $ takeDirectory d | ||
x@Just{} -> pure x | ||
|
||
go d0 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
cabal >= 3.12
you can querycabal path | grep store-dir
.