@@ -7,6 +7,9 @@ module Cabal
77#ifdef ENABLE_CABAL
88import Stack
99import Control.Exception (IOException , catch )
10+ import Control.Monad (when )
11+ import Control.Monad.Trans.Class (lift )
12+ import Control.Monad.Trans.State (execStateT , modify )
1013import Data.Char (isSpace )
1114import Data.List (foldl' , nub , sort , find , isPrefixOf , isSuffixOf )
1215#if __GLASGOW_HASKELL__ < 709
@@ -24,12 +27,13 @@ import Distribution.Simple.LocalBuildInfo (LocalBuildInfo(..), ComponentLocalBui
2427#endif
2528 componentBuildInfo , foldComponent )
2629import Distribution.Simple.Compiler (PackageDB (.. ))
30+ import Distribution.Simple.Command (CommandParse (.. ), commandParseArgs )
2731import Distribution.Simple.GHC (componentGhcOptions )
2832import Distribution.Simple.Program (defaultProgramConfiguration )
2933import Distribution.Simple.Program.Db (lookupProgram )
3034import Distribution.Simple.Program.Types (ConfiguredProgram (programVersion ), simpleProgram )
3135import Distribution.Simple.Program.GHC (GhcOptions (.. ), renderGhcOptions )
32- import Distribution.Simple.Setup (ConfigFlags (.. ), defaultConfigFlags , toFlag )
36+ import Distribution.Simple.Setup (ConfigFlags (.. ), defaultConfigFlags , configureCommand , toFlag )
3337#if __GLASGOW_HASKELL__ >= 709
3438import Distribution.Utils.NubList
3539import qualified Distribution.Simple.GHC as GHC (configure )
@@ -110,30 +114,37 @@ stackifyFlags cfg (Just si) = cfg { configDistPref = toFlag dist
110114-- via: https://groups.google.com/d/msg/haskell-stack/8HJ6DHAinU0/J68U6AXTsasJ
111115-- cabal configure --package-db=clear --package-db=global --package-db=$(stack path --snapshot-pkg-db) --package-db=$(stack path --local-pkg-db)
112116
113- getPackageGhcOpts :: FilePath -> Maybe StackConfig -> IO (Either String [String ])
114- getPackageGhcOpts path mbStack = do
117+ getPackageGhcOpts :: FilePath -> Maybe StackConfig -> [ String ] -> IO (Either String [String ])
118+ getPackageGhcOpts path mbStack opts = do
115119 getPackageGhcOpts' `catch` (\ e -> do
116120 return $ Left $ " Cabal error: " ++ (ioeGetErrorString (e :: IOException )))
117121 where
118122 getPackageGhcOpts' :: IO (Either String [String ])
119123 getPackageGhcOpts' = do
120124 genPkgDescr <- readPackageDescription silent path
121- let cfgFlags'' = (defaultConfigFlags defaultProgramConfiguration)
122- { configDistPref = toFlag $ takeDirectory path </> " dist"
123- -- TODO: figure out how to find out this flag
124- , configUserInstall = toFlag True
125- }
126- let cfgFlags' = stackifyFlags cfgFlags'' mbStack
127- let sandboxConfig = takeDirectory path </> " cabal.sandbox.config"
128- exists <- doesFileExist sandboxConfig
129-
130- cfgFlags <- case exists of
131- False -> return cfgFlags'
132- True -> do
133- sandboxPackageDb <- getSandboxPackageDB sandboxConfig
134- return $ cfgFlags'
135- { configPackageDBs = [Just sandboxPackageDb]
136- }
125+
126+ let programCfg = defaultProgramConfiguration
127+ let initCfgFlags = (defaultConfigFlags programCfg)
128+ { configDistPref = toFlag $ takeDirectory path </> " dist"
129+ -- TODO: figure out how to find out this flag
130+ , configUserInstall = toFlag True
131+ }
132+ let initCfgFlags' = stackifyFlags initCfgFlags mbStack
133+
134+ cfgFlags <- flip execStateT initCfgFlags' $ do
135+ let sandboxConfig = takeDirectory path </> " cabal.sandbox.config"
136+
137+ exists <- lift $ doesFileExist sandboxConfig
138+ when (exists) $ do
139+ sandboxPackageDb <- lift $ getSandboxPackageDB sandboxConfig
140+ modify $ \ x -> x { configPackageDBs = [Just sandboxPackageDb] }
141+
142+ let cmdUI = configureCommand programCfg
143+ case commandParseArgs cmdUI True opts of
144+ CommandReadyToGo (modFlags, _) -> modify modFlags
145+ CommandErrors (e: _) -> error e
146+ _ -> return ()
147+
137148 localBuildInfo <- configure (genPkgDescr, emptyHookedBuildInfo) cfgFlags
138149 let pkgDescr = localPkgDescr localBuildInfo
139150 let baseDir = fst . splitFileName $ path
@@ -217,8 +228,8 @@ findCabalFile dir = do
217228
218229# else
219230
220- getPackageGhcOpts :: FilePath -> IO (Either String [String ])
221- getPackageGhcOpts _ = return $ Right []
231+ getPackageGhcOpts :: FilePath -> [ String ] -> IO (Either String [String ])
232+ getPackageGhcOpts _ _ = return $ Right []
222233
223234findCabalFile :: FilePath -> IO (Maybe FilePath )
224235findCabalFile _ = return Nothing
0 commit comments