Skip to content

Commit a3c3360

Browse files
committed
it works
1 parent f5706f0 commit a3c3360

File tree

6 files changed

+55
-23
lines changed

6 files changed

+55
-23
lines changed

app/Main.hs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
{-# LANGUAGE MultiWayIf #-}
2+
13
module Main where
24

5+
import Control.Monad
36
import Data.Attoparsec.Text
7+
import qualified Data.Text as T
48
import qualified Data.Text.IO as T
59
import Hie.Cabal.Parser
610
import Hie.Yaml
7-
import System.Environment
11+
import System.Directory
12+
import System.FilePath.Posix
813

914
main :: IO ()
1015
main = do
11-
args <- getArgs
12-
file <- T.readFile $ head args
16+
files <- listDirectory =<< getCurrentDirectory
17+
let path = filter ((".cabal" ==) . takeExtension) files
18+
sOrC =
19+
if | any ((".stack-work" ==) . takeFileName) files -> stackHieYaml
20+
| any (("dist-newstyle" ==) . takeFileName) files -> cabalHieYaml
21+
| otherwise -> stackHieYaml
22+
when (null path) $ error "No .cabal file found!\n You may need to run stack build."
23+
file <- T.readFile $ head path
1324
case parseOnly parseSec file of
14-
Right r -> do
15-
T.putStr $ cabalHieYaml r
16-
T.putStr $ stackHieYaml r
25+
Right r -> T.writeFile "hie.yaml" $ sOrC r
1726
_ -> error "Could not parse *.cabal file"

hie.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cradle:
22
stack:
3-
- path: ./src
4-
component: "implicit-hie:lib"
5-
- path: ./app
6-
component: "implicit-hie:exe:implicit-hie-exe"
7-
- path: ./test
8-
component: "implicit-hie:test:implicit-hie-test"
3+
- path: "src"
4+
component: "implicit-hie:lib"
5+
- path: "app"
6+
component: "implicit-hie:exe:implicit-hie-exe"
7+
- path: "test"
8+
component: "implicit-hie:test:implicit-hie-test"

implicit-hie.cabal

Lines changed: 3 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: bc348cef928e75313b689d7626632633e11398ad278358aea4933e83a9564985
7+
-- hash: f941332727b491b78a5ecc8465f43d3597766e4d3363a1d76d459e4a84515b5f
88

99
name: implicit-hie
1010
version: 0.1.0.0
@@ -51,6 +51,8 @@ executable implicit-hie-exe
5151
build-depends:
5252
attoparsec
5353
, base >=4.7 && <5
54+
, directory
55+
, filepath
5456
, implicit-hie
5557
, text
5658
, yaml

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ executables:
5151
- -with-rtsopts=-N
5252
dependencies:
5353
- implicit-hie
54+
- directory
55+
- filepath
5456

5557
tests:
5658
implicit-hie-test:

src/Hie/Yaml.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ cabalComponent _ (Test p cn) = component p $ "test:" <> cn
2929

3030
stackComponent :: Name -> Component -> T.Text
3131
stackComponent n (Lib p) = component p $ n <> ":lib"
32-
stackComponent n (Exe p cn) = component p $ n <> ":exe:" <> cn
33-
stackComponent n (Test p cn) = component p $ n <> ":test:" <> cn
32+
stackComponent n (Exe cn p) = component p $ n <> ":exe:" <> cn
33+
stackComponent n (Test cn p) = component p $ n <> ":test:" <> cn
3434

3535
component :: T.Text -> T.Text -> T.Text
3636
component p c =

test/Spec.hs

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

3+
import Data.Attoparsec.Text
34
import qualified Data.Text as T
45
import Data.Text (Text)
56
import Hie.Cabal.Parser
7+
import Hie.Yaml
68
import Test.Hspec
79
import Test.Hspec.Attoparsec
810

@@ -25,20 +27,25 @@ spec = do
2527
`shouldParse` Lib "src"
2628
describe "Should Succeed"
2729
$ it "successfully parses library section"
28-
$ ("name: implicit-hie\n" <> exeSection <> testSection <> libSection)
29-
~> parseSec
30-
`shouldParse` Package
31-
"implicit-hie"
32-
[ Exe "implicit-hie-exe" "app",
33-
Test "implicit-hie-test" "test",
34-
Lib "src"
35-
]
30+
$ fullFile ~> parseSec
31+
`shouldParse` Package
32+
"implicit-hie"
33+
[ Lib "src",
34+
Exe "implicit-hie-exe" "app",
35+
Test "implicit-hie-test" "test"
36+
]
3637
describe "Should Succeed"
3738
$ it
3839
"successfully parses library section"
3940
$ let r = "test\n"
4041
in (libSection <> r) ~?> parseLib 0
4142
`leavesUnconsumed` r
43+
describe "Should Succeed"
44+
$ it "successfully generates stack hie.yaml"
45+
$ (stackHieYaml <$> parseOnly parseSec fullFile) `shouldBe` Right stackHie
46+
47+
fullFile :: Text
48+
fullFile = "name: implicit-hie\n" <> libSection <> exeSection <> testSection
4249

4350
exeSection :: Text
4451
exeSection =
@@ -85,3 +92,15 @@ libSection =
8592
\ , text\n\
8693
\ default-language: Haskell2010\n\
8794
\"
95+
96+
stackHie :: Text
97+
stackHie =
98+
"cradle:\n\
99+
\ stack:\n\
100+
\ - path: \"src\"\n\
101+
\ component: \"implicit-hie:lib\"\n\
102+
\ - path: \"app\"\n\
103+
\ component: \"implicit-hie:exe:implicit-hie-exe\"\n\
104+
\ - path: \"test\"\n\
105+
\ component: \"implicit-hie:test:implicit-hie-test\"\n\
106+
\"

0 commit comments

Comments
 (0)