Skip to content

Commit ea43db6

Browse files
committed
support multi package cabal projects
1 parent 5399602 commit ea43db6

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# implicit-hie
22
```bash
33
cd your-stack-or-cabal-package
4-
gen-hie > hie.yaml
4+
gen-hie
55
```

app/Main.hs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,44 @@ import qualified Data.Text.IO as T
99
import Hie.Cabal.Parser
1010
import Hie.Yaml
1111
import System.Directory
12+
import System.Directory.Internal
1213
import System.FilePath.Posix
1314

1415
main :: IO ()
1516
main = do
16-
files <- listDirectory =<< getCurrentDirectory
17-
let path = filter ((".cabal" ==) . takeExtension) files
18-
sOrC =
17+
pwd <- getCurrentDirectory
18+
files <- listDirectory pwd
19+
cfs <- cabalFiles pwd
20+
let sOrC =
1921
if | any ((".stack-work" ==) . takeFileName) files -> stackHieYaml
2022
| any (("dist-newstyle" ==) . takeFileName) files -> cabalHieYaml
2123
| any (("stack.yaml" ==) . takeFileName) files -> stackHieYaml
2224
| otherwise -> cabalHieYaml
23-
when (null path) $ error "No .cabal file found!\n You may need to run stack build."
24-
file <- T.readFile $ head path
25-
case parseOnly parsePackage file of
26-
Right r -> T.putStr $ sOrC r
27-
_ -> error "Could not parse *.cabal file"
25+
gen f = do
26+
f' <- T.readFile f
27+
case parseOnly parsePackage f' of
28+
Right r -> do
29+
let hiePath = fst (splitFileName f) </> "hie.yaml"
30+
T.writeFile hiePath $ sOrC r
31+
pure ("wrote " <> hiePath)
32+
_ -> pure $ "Could not parse " <> f
33+
when (null cfs) $ error $
34+
"No .cabal files found under"
35+
<> pwd
36+
<> "\n You may need to run stack build."
37+
mapM_ (putStrLn <=< gen) cfs
38+
39+
cabalFiles :: FilePath -> IO [FilePath]
40+
cabalFiles f = do
41+
fs <- listDirectory f
42+
case filter ((".cabal" ==) . takeExtension) fs of
43+
h : _ -> pure [f </> h]
44+
_ ->
45+
fmap concat . mapM cabalFiles
46+
=<< filterM
47+
(fmap (fileTypeIsDirectory . fileTypeFromMetadata) . getFileMetadata)
48+
( map (f </>) $
49+
filter
50+
(`notElem` [".git", "dist", "dist-newstyle", ".stack-work"])
51+
fs
52+
)

0 commit comments

Comments
 (0)