uni2work.workflows.visualiser/app/Main.hs
David Mosbach 7ab262cd2b init
2023-03-11 00:53:26 +01:00

52 lines
2.0 KiB
Haskell

module Main where
----------------Imports----------------
import System.Environment (getArgs)
import Data.Yaml (ParseException, decodeEither')
import Data.Aeson (encode, encodeFile)
import qualified Data.ByteString.Char8 as BS
import Workflow (Workflow, buildData)
import Export
import Data.Maybe (fromJust, isNothing)
import Data.Either (isLeft, fromLeft, fromRight)
import Control.Exception (throw)
---------------------------------------
----------------Methods----------------
-- | Required command line arguments:
-- 1. A workflow source file (YAML)
-- 2. A graph data target file (JSON)
main :: IO ()
main = getArgs >>= process >>= finish where
process :: [String] -> IO Bool
process args = if length args /= 2
then print "Please provide (1) a source and (2) a target file" >> return True
else generateJSON args >> return False
finish :: Bool -> IO ()
finish abort = if abort then return () else print "Done."
-- | Imports the YAML document specified in the first command line argument and
-- exports the graph data to the JSON file specified in the second argument.
generateJSON :: [String] -> IO ()
generateJSON args = do
content <- BS.readFile (head args)
let decoded = decodeEither' content :: Either ParseException Workflow
if isLeft decoded then throw (fromLeft undefined decoded) else do
let yaml = fromRight undefined decoded
-- let GData (nodeData, edgeData) = buildData yaml
-- putStrLn $ "\nNode Data:\n\n" ++ show nodeData
-- putStrLn $ "\nEdge Data:\n\n" ++ show edgeData
-- encodeFile (last args) $ GData (nodeData, edgeData)
encodeFile (last args) $ buildData yaml
---------------------------------------
-- https://stackoverflow.com/questions/59903779/how-to-parse-json-with-field-of-optional-and-variant-type-in-haskell
-- https://stackoverflow.com/questions/21292428/reading-yaml-lists-of-objects-in-haskell