{-# LANGUAGE QuasiQuotes #-}

module Drasil.Generator.ChunkDump (
  -- * Tools for dumping a chunk database
  buildDebuggingFiles
) where

import Control.Lens ((^.))
import Data.Aeson (ToJSON)
import Data.Aeson.Encode.Pretty (encodePretty)

import Drasil.Database (dumpChunkDB, dumpChunkDeps)
import Drasil.FileHandling (FileLayout, PathSegment, file, ps)
import Drasil.System (systemdb, HasSystemMeta)

-- | Internal: For system debugging purposes, dump everything we can to a set of
-- files.
buildDebuggingFiles :: HasSystemMeta sys => sys -> [FileLayout]
buildDebuggingFiles :: forall sys. HasSystemMeta sys => sys -> [FileLayout]
buildDebuggingFiles sys
si =
  [ PathSegment -> DumpedChunkDB -> FileLayout
forall a. ToJSON a => PathSegment -> a -> FileLayout
dumpTo [ps|initial_chunks.json|] (DumpedChunkDB -> FileLayout) -> DumpedChunkDB -> FileLayout
forall a b. (a -> b) -> a -> b
$ ChunkDB -> DumpedChunkDB
dumpChunkDB ChunkDB
db
  , PathSegment -> Dependants -> FileLayout
forall a. ToJSON a => PathSegment -> a -> FileLayout
dumpTo [ps|initial_chunk_dependants.json|] Dependants
dependants
  , PathSegment -> Dependants -> FileLayout
forall a. ToJSON a => PathSegment -> a -> FileLayout
dumpTo [ps|initial_chunk_dependencies.json|] Dependants
dependencies
  -- FIXME: One more file containing the system meta-information.
  ]
  where
    db :: ChunkDB
db = sys
si sys -> Getting ChunkDB sys ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB sys ChunkDB
forall c. HasSystemMeta c => Lens' c ChunkDB
Lens' sys ChunkDB
systemdb
    (Dependants
dependants, Dependants
dependencies) = ChunkDB -> (Dependants, Dependants)
dumpChunkDeps ChunkDB
db

-- | Internal: Build a JSON file from arbitrary data.
dumpTo :: ToJSON a => PathSegment -> a -> FileLayout
dumpTo :: forall a. ToJSON a => PathSegment -> a -> FileLayout
dumpTo PathSegment
targetPath = PathSegment -> ByteString -> FileLayout
forall doc. Writeable doc => PathSegment -> doc -> FileLayout
file PathSegment
targetPath (ByteString -> FileLayout) -> (a -> ByteString) -> a -> FileLayout
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. ToJSON a => a -> ByteString
encodePretty