implicit-hie/app/Main.hs

44 lines
1.2 KiB
Haskell
Raw Normal View History

2020-05-01 03:48:32 +03:00
{-# LANGUAGE MultiWayIf #-}
2020-04-24 03:48:49 +03:00
module Main where
2020-05-01 03:48:32 +03:00
import Control.Monad
import Control.Monad.Trans.Maybe
import Data.Maybe
2020-05-09 09:10:48 +03:00
import Hie.Locate
import Hie.Yaml
2020-05-01 03:48:32 +03:00
import System.Directory
import System.Environment
2020-11-10 02:35:45 +03:00
import System.FilePath.Posix
2020-04-24 03:48:49 +03:00
main :: IO ()
2020-04-25 07:28:20 +03:00
main = do
2020-05-02 03:28:14 +03:00
pwd <- getCurrentDirectory
name <- resolveName pwd
cfs <- runMaybeT $ case name of
"cabal" -> cabalPkgs pwd
_ -> stackYamlPkgs pwd
2020-11-10 02:35:45 +03:00
when (null cfs) $
error $
"No .cabal files found under"
<> pwd
<> "\n You may need to run stack build."
pkgs <- catMaybes <$> mapM (nestedPkg pwd) (concat cfs)
2020-05-09 08:26:07 +03:00
putStr <$> hieYaml name $ fmtPkgs name pkgs
resolveName :: FilePath -> IO String
resolveName pwd = do
args <- getArgs
files <- listDirectory pwd
let fileNames = map takeFileName files
name =
2020-11-10 02:35:45 +03:00
if
| "--cabal" `elem` args -> "cabal"
| "--stack" `elem` args -> "stack"
| "dist-newstyle" `elem` fileNames -> "cabal"
| ".stack-work" `elem` fileNames -> "stack"
| "cabal.project" `elem` fileNames -> "cabal"
| "stack.yaml" `elem` fileNames -> "stack"
| otherwise -> "cabal"
return name