@@ -9,19 +9,44 @@ import qualified Data.Text.IO as T
9
9
import Hie.Cabal.Parser
10
10
import Hie.Yaml
11
11
import System.Directory
12
+ import System.Directory.Internal
12
13
import System.FilePath.Posix
13
14
14
15
main :: IO ()
15
16
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 =
19
21
if | any ((" .stack-work" == ) . takeFileName) files -> stackHieYaml
20
22
| any ((" dist-newstyle" == ) . takeFileName) files -> cabalHieYaml
21
23
| any ((" stack.yaml" == ) . takeFileName) files -> stackHieYaml
22
24
| 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