Skip to content

Commit e033a56

Browse files
committed
Locate cabal module
1 parent 981a404 commit e033a56

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

app/Main.hs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import qualified Data.Text as T
1010
import qualified Data.Text.IO as T
1111
import Hie.Cabal.Parser
1212
import Hie.Yaml
13+
import Hie.Locate
1314
import System.Directory
1415
import System.Directory.Internal
1516
import System.FilePath.Posix
@@ -18,48 +19,16 @@ main :: IO ()
1819
main = do
1920
pwd <- getCurrentDirectory
2021
files <- listDirectory pwd
21-
cfs <- cabalFiles pwd
22+
cfs <- nestedCabalFiles pwd
2223
let name =
2324
if | any (("dist-newstyle" ==) . takeFileName) files -> "cabal"
2425
| any ((".stack-work" ==) . takeFileName) files -> "stack"
2526
| any (("stack.yaml" ==) . takeFileName) files -> "stack"
2627
| otherwise -> "cabal"
27-
gen f = do
28-
f' <- T.readFile f
29-
case parsePackage' f' of
30-
Right (Package n cs) -> do
31-
let hiePath = fst (splitFileName f) </> "hie.yaml"
32-
dir =
33-
fromJust $ stripPrefix (splitDirectories pwd)
34-
$ splitDirectories
35-
$ fst (splitFileName f)
36-
pkg =
37-
Package n $
38-
map
39-
( \(Comp t n p) ->
40-
Comp t n (T.pack $ joinPath dir </> T.unpack p)
41-
)
42-
cs
43-
pure $ Just pkg
44-
_ -> pure Nothing
4528
when (null cfs) $ error $
4629
"No .cabal files found under"
4730
<> pwd
4831
<> "\n You may need to run stack build."
49-
pkgs <- catMaybes <$> mapM gen cfs
32+
pkgs <- catMaybes <$> mapM (nestedPkg pwd) cfs
5033
putStr <$> hieYaml name $ fmtPkgs name pkgs
5134

52-
cabalFiles :: FilePath -> IO [FilePath]
53-
cabalFiles f = do
54-
fs <- listDirectory f
55-
case filter ((".cabal" ==) . takeExtension) fs of
56-
h : _ -> pure [f </> h]
57-
_ ->
58-
fmap concat . mapM cabalFiles
59-
=<< filterM
60-
(fmap (fileTypeIsDirectory . fileTypeFromMetadata) . getFileMetadata)
61-
( map (f </>) $
62-
filter
63-
(`notElem` [".git", "dist", "dist-newstyle", ".stack-work"])
64-
fs
65-
)

implicit-hie.cabal

Lines changed: 6 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: 19d5dbbfaaca0af267a9130152cdf8d4a25d59ddd71229a803bfa48283606994
7+
-- hash: 62b7d90b1ed53933a4c7c61b6392cf2f7ce368607095d9255b80154c6997e755
88

99
name: implicit-hie
1010
version: 0.1.0.0
@@ -28,6 +28,7 @@ source-repository head
2828
library
2929
exposed-modules:
3030
Hie.Cabal.Parser
31+
Hie.Locate
3132
Hie.Yaml
3233
other-modules:
3334
Paths_implicit_hie
@@ -37,6 +38,8 @@ library
3738
build-depends:
3839
attoparsec
3940
, base >=4.7 && <5
41+
, directory
42+
, filepath
4043
, text
4144
, yaml
4245
default-language: Haskell2010
@@ -69,6 +72,8 @@ test-suite implicit-hie-test
6972
build-depends:
7073
attoparsec
7174
, base >=4.7 && <5
75+
, directory
76+
, filepath
7277
, hspec
7378
, hspec-attoparsec
7479
, implicit-hie

package.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies:
2424
- text
2525
- attoparsec
2626
- yaml
27+
- directory
28+
- filepath
2729

2830
ghc-options:
2931
# - -O2
@@ -51,8 +53,6 @@ executables:
5153
- -with-rtsopts=-N
5254
dependencies:
5355
- implicit-hie
54-
- directory
55-
- filepath
5656

5757
tests:
5858
implicit-hie-test:

src/Hie/Locate.hs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Hie.Locate
2+
( nestedPkg,
3+
nestedCabalFiles,
4+
)
5+
where
6+
7+
import Control.Monad
8+
import Data.Attoparsec.Text
9+
import Data.List
10+
import Data.Maybe
11+
import qualified Data.Text as T
12+
import qualified Data.Text.IO as T
13+
import Hie.Cabal.Parser
14+
import Hie.Yaml
15+
import System.Directory
16+
import System.Directory.Internal
17+
import System.FilePath.Posix
18+
19+
nestedCabalFiles :: FilePath -> IO [FilePath]
20+
nestedCabalFiles f = do
21+
fs <- listDirectory f
22+
case filter ((".cabal" ==) . takeExtension) fs of
23+
h : _ -> pure [f </> h]
24+
_ ->
25+
fmap concat . mapM nestedCabalFiles
26+
=<< filterM
27+
(fmap (fileTypeIsDirectory . fileTypeFromMetadata) . getFileMetadata)
28+
( map (f </>) $
29+
filter
30+
(`notElem` [".git", "dist", "dist-newstyle", ".stack-work"])
31+
fs
32+
)
33+
34+
nestedPkg :: FilePath -> FilePath -> IO (Maybe Package)
35+
nestedPkg parrent child = do
36+
f' <- T.readFile child
37+
case parsePackage' f' of
38+
Right (Package n cs) -> do
39+
let dir =
40+
fromJust $ stripPrefix (splitDirectories parrent)
41+
$ splitDirectories
42+
$ fst (splitFileName child)
43+
pkg =
44+
Package n $
45+
map
46+
( \(Comp t n p) ->
47+
Comp t n (T.pack $ joinPath dir </> T.unpack p)
48+
)
49+
cs
50+
pure $ Just pkg
51+
_ -> pure Nothing

0 commit comments

Comments
 (0)