From 7f8eb6f0f754ed28e7a32aabcd604ccbf2914266 Mon Sep 17 00:00:00 2001 From: Avi Dessauer Date: Mon, 4 May 2020 21:01:29 -0400 Subject: [PATCH] Split formatting from rendering --- src/Hie/Yaml.hs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Hie/Yaml.hs b/src/Hie/Yaml.hs index 27679b6..cef9ae9 100644 --- a/src/Hie/Yaml.hs +++ b/src/Hie/Yaml.hs @@ -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 <> "\""