1+ {-# LANGUAGE PatternSynonyms, TupleSections #-}
2+
13-- | Main module to gather all the GOOL tests and generate them.
24module Main (main ) where
35
4- import Drasil.GOOL (Label , OOProg , unJC , unPC , unCSC ,
5- unCPPC , unSC , initialState , FileData (.. ), ProgData (.. ), ModData (.. ),
6- headers , sources , mainMod )
6+ import Drasil.GOOL (Label , OOProg , unJC , unPC , unCSC , unCPPC , unSC ,
7+ initialState , ProgData (.. ), headers , sources , mainMod )
78import qualified Drasil.GOOL as OO (unCI , ProgramSym (.. ))
89import Drasil.GProc (ProcProg , unJLC )
910import qualified Drasil.GProc as Proc (unCI , ProgramSym (.. ))
1011
1112import Language.Drasil.Code (ImplementationType (.. ), makeSds )
1213import Language.Drasil.GOOL (AuxiliarySym (.. ), package ,
13- FileAndContents ( fileDoc ) , PackageData (.. ), unPP , unJP , unCSP , unCPPP , unSP ,
14- unJLP )
15- import qualified Language.Drasil.GOOL as D (filePath )
14+ hasPathAndDocToFileAndContents , PackageData (.. ), pattern PackageData ,
15+ unPP , unJP , unCSP , unCPPP , unSP , unJLP )
16+ import qualified Language.Drasil.GOOL as D (filePath , FileAndContents ( .. ) )
1617
17- import Utils.Drasil (createDirIfMissing )
18+ import Utils.Drasil (createDirIfMissing , createFile )
1819
19- import Text.PrettyPrint.HughesPJ (Doc , render )
20+ import Text.PrettyPrint.HughesPJ (render )
2021import Control.Monad.State (evalState , runState )
2122import Control.Lens ((^.) )
23+ import Data.Functor ((<&>) )
24+ import Data.Foldable (traverse_ )
2225import System.Directory (setCurrentDirectory , getCurrentDirectory )
23- import System.FilePath.Posix (takeDirectory )
24- import System.IO (hClose , hPutStrLn , openFile , IOMode (WriteMode ))
26+ import System.FilePath ((</>) )
2527import Prelude hiding (return ,print ,log ,exp ,sin ,cos ,tan )
2628
2729import HelloWorld (helloWorldOO , helloWorldProc )
@@ -62,14 +64,14 @@ main = do
6264
6365-- | Gathers all information needed to generate code, sorts it, and calls the renderers.
6466genCode :: [PackageData ProgData ] -> IO ()
65- genCode files = createCodeFiles (concatMap (\ p -> replicate (length (progMods
66- (packageProg p)) + length (packageAux p)) (progName $ packageProg p)) files) $
67- makeCode (map (progMods . packageProg) files) (map packageAux files)
67+ genCode files =
68+ createCodeFiles $ files >>= \ (PackageData prog aux) ->
69+ let label = progName prog
70+ modCode = progMods prog <&> \ modFileData ->
71+ (label, hasPathAndDocToFileAndContents modFileData)
72+ auxCode = aux <&> (label,)
73+ in modCode ++ auxCode
6874
69- -- Cannot assign the list of tests in a where clause and re-use it because the
70- -- "r" type variable needs to be instantiated to two different types
71- -- (CodeInfo and a renderer) each time this function is called
72- -- | Gathers the GOOL file tests and prepares them for rendering
7375classes :: (OOProg r , AuxiliarySym r' , Monad r' ) => (r (OO. Program r ) -> ProgData ) ->
7476 (r' (PackageData ProgData ) -> PackageData ProgData ) -> [PackageData ProgData ]
7577classes unRepr unRepr' = zipWith
@@ -95,31 +97,12 @@ jlClasses unRepr unRepr' = zipWith
9597 (map (Proc. unCI . (`evalState` initialState)) [helloWorldProc,
9698 fileTestsProc, vectorTestProc, nameGenTestProc])
9799
98- -- | Formats code to be rendered.
99- makeCode :: [[FileData ]] -> [[FileAndContents ]] -> [(FilePath , Doc )]
100- makeCode files auxs = concat $ zipWith (++)
101- (map (map (\ fd -> (filePath fd, modDoc $ fileMod fd))) files)
102- (map (map (\ fileAndContents ->
103- (D. filePath fileAndContents, fileDoc fileAndContents))) auxs)
104-
105- -- zip (map filePath files) (map (modDoc . fileMod) files)
106- -- ++ zip (map D.filePath auxs) (map fileDoc auxs)
107-
108100------------------
109101-- IO Functions --
110102------------------
111103
112104-- | Creates the requested 'Code' by producing files.
113- createCodeFiles :: [Label ] -> [(FilePath , Doc )] -> IO () -- [(FilePath, Doc)] -> IO ()
114- createCodeFiles ns cs = mapM_ createCodeFile (zip ns cs)
115-
116- -- | Helper that creates the file and renders code.
117- createCodeFile :: (Label , (FilePath , Doc )) -> IO ()
118- createCodeFile (n, (path, code)) = do
119- createDirIfMissing False n
120- setCurrentDirectory n
121- createDirIfMissing True (takeDirectory path)
122- h <- openFile path WriteMode
123- hPutStrLn h (render code)
124- hClose h
125- setCurrentDirectory " .."
105+ createCodeFiles :: [(Label , D. FileAndContents )] -> IO ()
106+ createCodeFiles = traverse_ $ \ (name, file) -> do
107+ let path = name </> D. filePath file -- FIXME [Brandon Bosman, Feb. 10, 2026]: make GOOL allow us to add name to path internally
108+ createFile path (render $ D. fileDoc file)
0 commit comments