Skip to content

Commit fc1c6ea

Browse files
committed
Generate a single hie.yaml file
Tests are broken and so are benchmark sections
1 parent 06b82bf commit fc1c6ea

File tree

4 files changed

+50
-45
lines changed

4 files changed

+50
-45
lines changed

app/Main.hs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Main where
44

55
import Control.Monad
66
import Data.Attoparsec.Text
7+
import Data.List
8+
import Data.Maybe
79
import qualified Data.Text as T
810
import qualified Data.Text.IO as T
911
import Hie.Cabal.Parser
@@ -17,26 +19,38 @@ main = do
1719
pwd <- getCurrentDirectory
1820
files <- listDirectory pwd
1921
cfs <- cabalFiles pwd
20-
let cabal = (cabalHieYaml, "Cabal ")
21-
stack = (stackHieYaml, "Stack ")
22-
sOrC =
22+
let cabal = (cabalComponent, "cabal")
23+
stack = (stackComponent, "stack")
24+
(fmt, name) =
2325
if | any (("dist-newstyle" ==) . takeFileName) files -> cabal
2426
| any ((".stack-work" ==) . takeFileName) files -> stack
2527
| any (("stack.yaml" ==) . takeFileName) files -> stack
2628
| otherwise -> cabal
2729
gen f = do
2830
f' <- T.readFile f
2931
case parsePackage' f' of
30-
Right r -> do
32+
Right (Package n cs) -> do
3133
let hiePath = fst (splitFileName f) </> "hie.yaml"
32-
T.writeFile hiePath $ fst sOrC r
33-
pure ("wrote " <> snd sOrC <> hiePath)
34-
_ -> pure $ "Could not parse " <> f
34+
dir =
35+
fromJust $ stripPrefix (splitDirectories pwd)
36+
$ splitDirectories
37+
$ fst (splitFileName f)
38+
pkg =
39+
Package n $
40+
map
41+
( \(Comp t n p) ->
42+
Comp t n (T.pack $ joinPath dir </> T.unpack p)
43+
)
44+
cs
45+
pure $ Just pkg
46+
_ -> pure Nothing
3547
when (null cfs) $ error $
3648
"No .cabal files found under"
3749
<> pwd
3850
<> "\n You may need to run stack build."
39-
mapM_ (putStrLn <=< gen) cfs
51+
pkgs <- catMaybes <$> mapM gen cfs
52+
putStr <$> hieYaml name $ unlines $
53+
concatMap (\(Package n cs) -> map ((<> "\n") . fmtComponent . fmt n) cs) pkgs
4054

4155
cabalFiles :: FilePath -> IO [FilePath]
4256
cabalFiles f = do

hie.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ cradle:
22
cabal:
33
- path: "src"
44
component: "lib:implicit-hie"
5+
56
- path: "app/Main.hs"
67
component: "implicit-hie:exe:gen-hie"
8+
79
- path: "test"
810
component: "implicit-hie:test:implicit-hie-test"
11+

src/Hie/Cabal/Parser.hs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Data.Attoparsec.Text
88
import Data.Char (isSpace)
99
import Data.Text (Text)
1010
import qualified Data.Text as T
11-
import Debug.Trace
1211

1312
type Name = Text
1413

@@ -19,11 +18,11 @@ type Indent = Int
1918
data Package = Package Name [Component]
2019
deriving (Show, Eq, Ord)
2120

21+
data CompType = Lib | Exe | Test | Bench
22+
deriving (Show, Eq, Ord)
23+
2224
data Component
23-
= Lib Name Path
24-
| Exe Name Path
25-
| Test Name Path
26-
| Bench Name Path
25+
= Comp CompType Name Path
2726
deriving (Show, Eq, Ord)
2827

2928
parsePackage' :: Text -> Either String Package
@@ -46,11 +45,8 @@ parsePackage =
4645

4746
componentHeader :: Indent -> Text -> Parser Name
4847
componentHeader i t = do
49-
traceM $ "indent" <> show i
5048
indent i
51-
traceM $ "asciiCI" <> T.unpack t
5249
_ <- asciiCI t
53-
traceM "parseString"
5450
skipMany tabOrSpace
5551
n <- parseString <|> pure ""
5652
skipToNextLine
@@ -63,16 +59,16 @@ parseComponent i =
6359
<|> parseTestSuite i
6460

6561
parseLib :: Indent -> Parser Component
66-
parseLib i = parseSec i "library" Lib
62+
parseLib i = parseSec i "library" $ Comp Lib
6763

6864
parseTestSuite :: Indent -> Parser Component
69-
parseTestSuite i = parseSec i "test-suite" Test
65+
parseTestSuite i = parseSec i "test-suite" $ Comp Test
7066

7167
parseExe :: Indent -> Parser Component
72-
parseExe = parseSecMain Exe "executable"
68+
parseExe = parseSecMain (Comp Exe) "executable"
7369

7470
parseBench :: Indent -> Parser Component
75-
parseBench = parseSecMain Bench "benchmark"
71+
parseBench = parseSecMain (Comp Bench) "benchmark"
7672

7773
parseSecMain :: (Name -> Path -> Component) -> Text -> Indent -> Parser Component
7874
parseSecMain c s i = do

src/Hie/Yaml.hs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

33
module Hie.Yaml
4-
( cabalHieYaml,
5-
stackHieYaml,
4+
( hieYaml,
5+
fmtComponent,
66
cabalComponent,
77
stackComponent,
88
)
@@ -11,36 +11,28 @@ where
1111
import qualified Data.Text as T
1212
import Hie.Cabal.Parser
1313

14-
cabalHieYaml :: Package -> T.Text
15-
cabalHieYaml (Package n cs) =
16-
T.pack $
17-
"cradle:\n"
18-
<> indent'
19-
("cabal:\n" <> indent' (unlines (map (fmtComponent . cabalComponent n) cs)))
20-
21-
stackHieYaml :: Package -> T.Text
22-
stackHieYaml (Package n cs) =
23-
T.pack $
24-
"cradle:\n"
25-
<> indent'
26-
("stack:\n" <> indent' (unlines (map (fmtComponent . stackComponent n) cs)))
14+
hieYaml :: String -> String -> String
15+
hieYaml sOrC pkgs =
16+
"cradle:\n"
17+
<> indent'
18+
(sOrC <> ":\n" <> indent' pkgs)
2719

2820
indent' :: String -> String
2921
indent' = unlines . map (" " <>) . lines
3022

3123
cabalComponent :: Name -> Component -> (FilePath, String)
32-
cabalComponent n (Lib "" p) = (T.unpack p, T.unpack $ "lib:" <> n)
33-
cabalComponent n (Lib cn p) = (T.unpack p, T.unpack $ "lib:" <> n <> ":" <> cn)
34-
cabalComponent n (Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
35-
cabalComponent n (Bench cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
36-
cabalComponent n (Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
24+
cabalComponent n (Comp Lib "" p) = (T.unpack p, T.unpack $ "lib:" <> n)
25+
cabalComponent n (Comp Lib cn p) = (T.unpack p, T.unpack $ "lib:" <> n <> ":" <> cn)
26+
cabalComponent n (Comp Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
27+
cabalComponent n (Comp Bench cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
28+
cabalComponent n (Comp Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
3729

3830
stackComponent :: Name -> Component -> (FilePath, String)
39-
stackComponent n (Lib "" p) = (T.unpack p, T.unpack $ n <> ":lib")
40-
stackComponent n (Lib cn p) = (T.unpack p, T.unpack $ n <> ":lib:" <> cn)
41-
stackComponent n (Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
42-
stackComponent n (Bench cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
43-
stackComponent n (Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
31+
stackComponent n (Comp Lib "" p) = (T.unpack p, T.unpack $ n <> ":lib")
32+
stackComponent n (Comp Lib cn p) = (T.unpack p, T.unpack $ n <> ":lib:" <> cn)
33+
stackComponent n (Comp Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
34+
stackComponent n (Comp Bench cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
35+
stackComponent n (Comp Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
4436

4537
fmtComponent :: (FilePath, String) -> String
4638
fmtComponent (p, c) =

0 commit comments

Comments
 (0)