Skip to content

Commit d0fa1f5

Browse files
committed
reading git packages from stack.yaml
1 parent bf9d6a3 commit d0fa1f5

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.stack-work/
2+
*.swp

src/Main.hs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module Main where
22

3+
import Control.Monad (forM_)
4+
import System.Environment (getArgs)
5+
6+
import Stack2Yaml
7+
38
main :: IO ()
49
main = do
5-
putStrLn "hello world"
10+
[dir] <- getArgs
11+
putStrLn $ "processing folder '" ++ dir ++ "'"
12+
yaml <- parseStackYaml dir
13+
let gs = gitPackages yaml
14+
forM_ gs print

src/Stack2Yaml.hs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Stack2Yaml
2+
( module Stack2Yaml.StackYaml
3+
) where
4+
5+
import Stack2Yaml.StackYaml

src/Stack2Yaml/StackYaml.hs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Stack2Yaml.StackYaml
4+
( parseStackYaml
5+
, Git (..)
6+
, gitPackages
7+
) where
8+
9+
import Data.Maybe (mapMaybe)
10+
import Data.Text (Text)
11+
import Data.Text.Encoding (decodeUtf8)
12+
import Data.Yaml.Parser (YamlValue (..), readYamlFile)
13+
import System.FilePath ((</>), (<.>))
14+
15+
parseStackYaml :: FilePath -> IO YamlValue
16+
parseStackYaml dir = readYamlFile $ dir </> "stack" <.> "yaml"
17+
18+
data Git = Git
19+
{ gitUrl :: !Text
20+
, gitCommit :: !Text
21+
} deriving (Eq, Ord, Show)
22+
23+
gitPackages :: YamlValue -> [Git]
24+
gitPackages (Mapping m _) = case lookup "packages" m of
25+
Just (Sequence ps _) -> mapMaybe f [p | Mapping (("location", Mapping p _) : _) _ <- ps]
26+
_ -> []
27+
where
28+
--f :: YamlValue ->
29+
f m' = case (lookup "git" m', lookup "commit" m') of
30+
(Just (Scalar g _ _ _), Just (Scalar c _ _ _)) -> Just $ Git { gitUrl = decodeUtf8 g, gitCommit = decodeUtf8 c }
31+
_ -> Nothing
32+
gitPackages _ = []

stack2cabal.cabal

+6
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ extra-source-files: README.md
1616
executable stack2cabal
1717
hs-source-dirs: src
1818
main-is: Main.hs
19+
other-modules: Stack2Yaml
20+
, Stack2Yaml.StackYaml
1921
default-language: Haskell2010
2022
build-depends: base >= 4.7 && < 5
23+
, filepath
24+
, text
25+
, yaml
26+
ghc-options: -Wall -O2

0 commit comments

Comments
 (0)