Skip to content

Commit ebafb8b

Browse files
committed
Read cabal.project and stack.yaml packages
1 parent 8219c06 commit ebafb8b

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

app/Main.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
module Main where
44

55
import Control.Monad
6+
import Control.Monad.Trans.Maybe
67
import Data.Attoparsec.Text
78
import Data.List
89
import Data.Maybe
910
import qualified Data.Text as T
1011
import qualified Data.Text.IO as T
1112
import Hie.Cabal.Parser
12-
import Hie.Yaml
1313
import Hie.Locate
14+
import Hie.Yaml
1415
import System.Directory
1516
import System.Directory.Internal
1617
import System.FilePath.Posix
@@ -19,16 +20,17 @@ main :: IO ()
1920
main = do
2021
pwd <- getCurrentDirectory
2122
files <- listDirectory pwd
22-
cfs <- nestedCabalFiles pwd
2323
let name =
2424
if | any (("dist-newstyle" ==) . takeFileName) files -> "cabal"
2525
| any ((".stack-work" ==) . takeFileName) files -> "stack"
2626
| any (("stack.yaml" ==) . takeFileName) files -> "stack"
2727
| otherwise -> "cabal"
28+
cfs <- runMaybeT $ case name of
29+
"cabal" -> cabalPkgs pwd
30+
_ -> stackYamlPkgs pwd
2831
when (null cfs) $ error $
2932
"No .cabal files found under"
3033
<> pwd
3134
<> "\n You may need to run stack build."
32-
pkgs <- catMaybes <$> mapM (nestedPkg pwd) cfs
35+
pkgs <- catMaybes <$> mapM (nestedPkg pwd) (concat cfs)
3336
putStr <$> hieYaml name $ fmtPkgs name pkgs
34-

src/Hie/Cabal/Parser.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ parseSecMain c s i = do
8181
parseQuoted :: Parser Text
8282
parseQuoted = do
8383
q <- char '"' <|> char '\''
84-
takeTill (== q)
84+
s <- takeTill (== q)
85+
_ <- char q
86+
pure s
8587

8688
parseString :: Parser Name
8789
parseString = parseQuoted <|> unqualName

src/Hie/Locate.hs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
module Hie.Locate
55
( nestedPkg,
6-
nestedCabalFiles,
76
stackYamlPkgs,
8-
cabalProjectPkgs,
7+
cabalPkgs,
98
)
109
where
1110

1211
import Control.Applicative
12+
import Control.Exception
1313
import Control.Monad
1414
import Control.Monad.IO.Class
1515
import Control.Monad.Trans.Maybe
16-
import Data.Attoparsec.Text
16+
import Data.Attoparsec.Text (parseOnly)
1717
import Data.List
1818
import Data.Maybe
1919
import qualified Data.Text as T
@@ -25,6 +25,7 @@ import Hie.Yaml
2525
import System.Directory
2626
import System.Directory.Internal
2727
import System.FilePath.Posix
28+
import System.FilePattern.Directory (getDirectoryFiles)
2829

2930
newtype Pkgs = Pkgs [FilePath]
3031
deriving (Eq, Ord)
@@ -39,27 +40,14 @@ stackYamlPkgs p = liftIO $
3940
Right (Pkgs f) -> pure f
4041
Left e -> fail $ show e
4142

42-
cabalProjectPkgs :: FilePath -> MaybeT IO [FilePath]
43-
cabalProjectPkgs p = do
44-
cp <- liftIO $ T.readFile $ p </> "cabal.project"
45-
case parseOnly extractPkgs cp of
46-
Right f -> pure $ map T.unpack f
47-
_ -> fail "No packages found"
48-
49-
nestedCabalFiles :: FilePath -> IO [FilePath]
50-
nestedCabalFiles f = do
51-
fs <- listDirectory f
52-
nf <-
53-
fmap concat . mapM nestedCabalFiles
54-
=<< filterM
55-
(fmap (fileTypeIsDirectory . fileTypeFromMetadata) . getFileMetadata)
56-
( map (f </>) $
57-
filter
58-
(`notElem` [".git", "dist", "dist-newstyle", ".stack-work"])
59-
fs
60-
)
61-
let cf = filter ((".cabal" ==) . takeExtension) fs
62-
pure $ map (f </>) cf <> nf
43+
cabalPkgs :: FilePath -> MaybeT IO [FilePath]
44+
cabalPkgs p = do
45+
cp <- liftIO (try $ T.readFile $ p </> "cabal.project" :: IO (Either IOException T.Text))
46+
case parseOnly extractPkgs <$> cp of
47+
Right (Right f) -> liftIO $ map (p </>) <$> getDirectoryFiles p (map T.unpack f)
48+
_ -> filter ((".cabal" ==) . takeExtension) <$> liftIO (listDirectory p) >>= \case
49+
[] -> fail "no cabal files found"
50+
h : _ -> pure [p </> h]
6351

6452
nestedPkg :: FilePath -> FilePath -> IO (Maybe Package)
6553
nestedPkg parrent child = do

test/Spec.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ spec = do
7474
$ ("one\n two \n three3" :: Text) ~> parseList 1
7575
`shouldParse` ["one", "two", "three3"]
7676
describe "Should Succeed"
77-
$ it "successfully parses newline list"
77+
$ it "successfully parses newline comma list"
7878
$ ("one\n two, three3" :: Text) ~> parseList 1
7979
`shouldParse` ["one", "two", "three3"]
80+
describe "Should Succeed"
81+
$ it "quoted list"
82+
$ ("\"one\"\n two\n three3" :: Text) ~> parseList 1
83+
`shouldParse` ["one", "two", "three3"]
8084

8185
fullFile :: Text
8286
fullFile = "name: implicit-hie\n" <> libSection <> exeSection <> testSection

0 commit comments

Comments
 (0)