Skip to content

Commit 8219c06

Browse files
committed
Get packages from cabal.project and stack.yaml
1 parent 895fc55 commit 8219c06

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

implicit-hie.cabal

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 62b7d90b1ed53933a4c7c61b6392cf2f7ce368607095d9255b80154c6997e755
7+
-- hash: 18f92037a7863d121ac45e847f7dc6177adf0ebc7951dfa1588340f86e64456b
88

99
name: implicit-hie
1010
version: 0.1.0.0
@@ -40,7 +40,9 @@ library
4040
, base >=4.7 && <5
4141
, directory
4242
, filepath
43+
, filepattern
4344
, text
45+
, transformers
4446
, yaml
4547
default-language: Haskell2010
4648

@@ -56,8 +58,10 @@ executable gen-hie
5658
, base >=4.7 && <5
5759
, directory
5860
, filepath
61+
, filepattern
5962
, implicit-hie
6063
, text
64+
, transformers
6165
, yaml
6266
default-language: Haskell2010
6367

@@ -74,9 +78,11 @@ test-suite implicit-hie-test
7478
, base >=4.7 && <5
7579
, directory
7680
, filepath
81+
, filepattern
7782
, hspec
7883
, hspec-attoparsec
7984
, implicit-hie
8085
, text
86+
, transformers
8187
, yaml
8288
default-language: Haskell2010

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ description: "Auto generate a stack or cabal multi component hie.yaml file"
2222
dependencies:
2323
- base >= 4.7 && < 5
2424
- text
25+
- transformers
2526
- attoparsec
2627
- yaml
2728
- directory
29+
- filepattern
2830
- filepath
2931

3032
ghc-options:

src/Hie/Cabal/Parser.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Control.Applicative
66
import Control.Monad
77
import Data.Attoparsec.Text
88
import Data.Char
9+
import Data.Maybe
910
import Data.Text (Text)
1011
import qualified Data.Text as T
1112

@@ -86,7 +87,7 @@ parseString :: Parser Name
8687
parseString = parseQuoted <|> unqualName
8788

8889
unqualName :: Parser Text
89-
unqualName = takeWhile1 (\c -> isAlphaNum c || c `elem` ("-_./" :: String))
90+
unqualName = takeWhile1 (not . (\c -> isSpace c || c == ','))
9091

9192
parseList :: Indent -> Parser [Text]
9293
parseList i = items <|> (emptyOrComLine >> indent i >> items)
@@ -166,3 +167,6 @@ indent :: Indent -> Parser Int
166167
indent i = do
167168
c <- length <$> many' tabOrSpace
168169
if c >= i then pure c else fail "insufficient indent"
170+
171+
extractPkgs :: Parser [T.Text]
172+
extractPkgs = join . catMaybes <$> many' (Just <$> field 0 "packages" parseList <|> (skipToNextLine >> pure Nothing))

src/Hie/Locate.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
14
module Hie.Locate
25
( nestedPkg,
36
nestedCabalFiles,
7+
stackYamlPkgs,
8+
cabalProjectPkgs,
49
)
510
where
611

12+
import Control.Applicative
713
import Control.Monad
14+
import Control.Monad.IO.Class
15+
import Control.Monad.Trans.Maybe
816
import Data.Attoparsec.Text
917
import Data.List
1018
import Data.Maybe
1119
import qualified Data.Text as T
1220
import qualified Data.Text.IO as T
21+
import Data.Yaml
22+
import GHC.Generics
1323
import Hie.Cabal.Parser
1424
import Hie.Yaml
1525
import System.Directory
1626
import System.Directory.Internal
1727
import System.FilePath.Posix
1828

29+
newtype Pkgs = Pkgs [FilePath]
30+
deriving (Eq, Ord)
31+
32+
instance FromJSON Pkgs where
33+
parseJSON (Object v) = Pkgs <$> v .: "packages"
34+
parseJSON _ = fail "could not read packages from stack.yaml"
35+
36+
stackYamlPkgs :: FilePath -> MaybeT IO [FilePath]
37+
stackYamlPkgs p = liftIO $
38+
decodeFileEither (p </> "stack.yaml") >>= \case
39+
Right (Pkgs f) -> pure f
40+
Left e -> fail $ show e
41+
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+
1949
nestedCabalFiles :: FilePath -> IO [FilePath]
2050
nestedCabalFiles f = do
2151
fs <- listDirectory f

0 commit comments

Comments
 (0)