Split formatting from rendering

This commit is contained in:
Avi Dessauer 2020-05-04 21:01:29 -04:00
parent cdccf36665
commit 7f8eb6f0f7

View File

@ -9,38 +9,40 @@ where
import qualified Data.Text as T
import Hie.Cabal.Parser
cabalHieYaml :: Package -> T.Text
cabalHieYaml :: Package -> String
cabalHieYaml (Package n cs) =
"cradle:\n"
<> indentT ("cabal:\n" <> indentT (T.unlines (map (cabalComponent n) cs)))
<> indent'
("cabal:\n" <> indent' (unlines (map (fmtComponent . cabalComponent n) cs)))
stackHieYaml :: Package -> T.Text
stackHieYaml :: Package -> String
stackHieYaml (Package n cs) =
"cradle:\n"
<> indentT ("stack:\n" <> indentT (T.unlines (map (stackComponent n) cs)))
<> indent'
("stack:\n" <> indent' (unlines (map (fmtComponent . stackComponent n) cs)))
indentT :: T.Text -> T.Text
indentT = T.unlines . map (" " <>) . T.lines
indent' :: String -> String
indent' = unlines . map (" " <>) . lines
cabalComponent :: Name -> Component -> T.Text
cabalComponent n (Lib "" p) = comp p $ "lib:" <> n
cabalComponent n (Lib cn p) = comp p $ "lib:" <> n <> ":" <> cn
cabalComponent n (Exe cn p) = comp p $ n <> ":exe:" <> cn
cabalComponent n (Test cn p) = comp p $ n <> ":test:" <> cn
cabalComponent :: Name -> Component -> (FilePath, String)
cabalComponent n (Lib "" p) = (T.unpack p, T.unpack $ "lib:" <> n)
cabalComponent n (Lib cn p) = (T.unpack p, T.unpack $ "lib:" <> n <> ":" <> cn)
cabalComponent n (Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
cabalComponent n (Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
stackComponent :: Name -> Component -> T.Text
stackComponent n (Lib "" p) = comp p $ n <> ":lib"
stackComponent n (Lib cn p) = comp p $ n <> ":lib:" <> cn
stackComponent n (Exe cn p) = comp p $ n <> ":exe:" <> cn
stackComponent n (Test cn p) = comp p $ n <> ":test:" <> cn
stackComponent :: Name -> Component -> (FilePath, String)
stackComponent n (Lib "" p) = (T.unpack p, T.unpack $ n <> ":lib")
stackComponent n (Lib cn p) = (T.unpack p, T.unpack $ n <> ":lib:" <> cn)
stackComponent n (Exe cn p) = (T.unpack p, T.unpack $ n <> ":exe:" <> cn)
stackComponent n (Test cn p) = (T.unpack p, T.unpack $ n <> ":test:" <> cn)
comp :: T.Text -> T.Text -> T.Text
comp p c =
fmtComponent :: (FilePath, String) -> String
fmtComponent (p, c) =
"- path: "
<> dQuote p
<> "\n "
<> "component: "
<> dQuote c
dQuote :: T.Text -> T.Text
dQuote t = T.cons '"' t `T.snoc` '"'
dQuote :: String -> String
dQuote t = '"' : t <> "\""