forked from haskell/haskell-ide-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
98 additions
and
559 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,79 @@ | ||
{-# LANGUAGE TupleSections #-} | ||
module Haskell.Ide.Engine.Cradle (findLocalCradle) where | ||
|
||
import HIE.Bios as BIOS | ||
import HIE.Bios.Types | ||
|
||
import Haskell.Ide.Engine.MonadFunctions | ||
|
||
import Distribution.Helper | ||
import Distribution.Helper.Discover | ||
|
||
import System.FilePath | ||
import System.Directory | ||
|
||
import qualified Data.Map as M | ||
import Data.Foldable (toList) | ||
import Data.List (inits, sortOn) | ||
import Data.Maybe (listToMaybe) | ||
import Data.Ord | ||
import System.Exit | ||
|
||
findLocalCradle :: FilePath -> IO Cradle | ||
findLocalCradle fp = do | ||
-- Get the cabal directory from the cradle | ||
cradleConf <- BIOS.findCradle fp | ||
case cradleConf of | ||
Just yaml -> BIOS.loadCradle yaml | ||
Nothing -> BIOS.loadImplicitCradle fp | ||
Nothing -> cabalHelperCradle fp | ||
|
||
cabalHelperCradle :: FilePath -> IO Cradle | ||
cabalHelperCradle file' = do | ||
-- TODO find cradle | ||
root' <- getCurrentDirectory | ||
root <- canonicalizePath root' | ||
return Cradle | ||
{ cradleRootDir = root | ||
, cradleOptsProg = CradleAction | ||
{ actionName = "Cabal-Helper" | ||
, runCradle = cabalHelperAction root | ||
} | ||
} | ||
|
||
where | ||
cabalHelperAction :: FilePath -> FilePath -> IO (CradleLoadResult ComponentOptions) | ||
cabalHelperAction root fp = do | ||
file <- canonicalizePath fp | ||
let file_dir = makeRelative root $ takeDirectory file | ||
debugm $ "Cabal Helper dirs: " ++ show [root, file, file_dir] | ||
projs <- findProjects root | ||
case projs of | ||
(Ex proj:_) -> do | ||
let [dist_dir] = findDistDirs proj | ||
env <- mkQueryEnv proj dist_dir | ||
units <- runQuery (allUnits id) env | ||
|
||
case getFlags file_dir $ toList units of | ||
Just fs -> do | ||
debugm $ "Flags for \"" ++ fp ++ "\": " ++ show fs | ||
return $ CradleSuccess | ||
ComponentOptions | ||
{ componentOptions = fs ++ [file] | ||
, componentDependencies = [] | ||
} | ||
|
||
Nothing -> return $ CradleFail $ CradleError (ExitFailure 2) ("Could not obtain flags for " ++ fp) | ||
_ -> return $ CradleFail $ CradleError (ExitFailure 1) ("Could not find project from: " ++ fp) | ||
|
||
getFlags :: FilePath -> [UnitInfo] -> Maybe [String] | ||
getFlags dir uis | ||
= listToMaybe | ||
$ map (ciGhcOptions . snd) | ||
$ filter (hasParent dir . fst) | ||
$ sortOn (Down . length . fst) | ||
$ concatMap (\ci -> map (,ci) (ciSourceDirs ci)) | ||
$ concat | ||
$ M.elems . uiComponents <$> uis | ||
|
||
hasParent :: FilePath -> FilePath -> Bool | ||
hasParent child parent = any (equalFilePath parent) (map joinPath $ inits $ splitPath child) |
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
Oops, something went wrong.