Skip to content

Commit 4d7de69

Browse files
committed
Merge branch 'main' into separatePackageStuff
2 parents 081d382 + 092f539 commit 4d7de69

File tree

3 files changed

+39
-48
lines changed

3 files changed

+39
-48
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
{-# LANGUAGE PatternSynonyms #-}
12
module Language.Drasil.Code.PackageData (PackageData(packageProg, packageAux),
2-
package
3+
pattern PackageData, package
34
) where
45

56
import Language.Drasil.Code.FileData (FileAndContents(..))
@@ -9,9 +10,11 @@ import Drasil.GOOL (ProgData, onCodeList)
910
-- | The underlying data type for packages in all renderers.
1011
data PackageData = PackD {packageProg :: ProgData, packageAux :: [FileAndContents]}
1112

12-
-- | Constructor for package data.
13-
packageData :: ProgData -> [FileAndContents] -> PackageData
14-
packageData p as = PackD p (filter (not . isEmpty . fileDoc) as)
13+
pattern PackageData :: ProgData -> [FileAndContents] -> PackageData
14+
pattern PackageData prog aux <- PackD prog aux
15+
where
16+
PackageData prog aux = PackD prog (filter (not . isEmpty . fileDoc) aux)
17+
{-# COMPLETE PackageData #-}
1518

1619
package :: (Monad r) => ProgData -> [r FileAndContents] -> r PackageData
17-
package p = onCodeList (packageData p)
20+
package p = onCodeList (PackageData p)

code/drasil-code/lib/Language/Drasil/GOOL.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
{-# LANGUAGE PatternSynonyms #-}
2+
13
-- | Re-export code-related smart constructors for external code writing and generation.
24
module Language.Drasil.GOOL (
35
AuxiliarySym(..), package,
4-
FileAndContents(..), PackageData(..),
6+
FileAndContents(..), hasPathAndDocToFileAndContents,
7+
PackageData(..), pattern PackageData,
58
unPP, unJP, unCSP, unCPPP, unSP, unJLP
69
) where
710

811
import Language.Drasil.Code.Imperative.GOOL.ClassInterface (AuxiliarySym(..))
9-
import Language.Drasil.Code.FileData (FileAndContents(..))
10-
import Language.Drasil.Code.PackageData (PackageData(..), package)
12+
import Language.Drasil.Code.FileData (FileAndContents(..),
13+
hasPathAndDocToFileAndContents)
14+
import Language.Drasil.Code.PackageData (PackageData(..), pattern PackageData,
15+
package)
1116

1217
import Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.PythonRenderer (unPP)
1318
import Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.JavaRenderer (unJP)

code/drasil-code/test/Main.hs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
{-# LANGUAGE PatternSynonyms, TupleSections #-}
2+
13
-- | Main module to gather all the GOOL tests and generate them.
24
module 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)
78
import qualified Drasil.GOOL as OO (unCI, ProgramSym(..))
89
import Drasil.GProc (ProcProg, unJLC)
910
import qualified Drasil.GProc as Proc (unCI, ProgramSym(..))
1011

1112
import Language.Drasil.Code (ImplementationType(..), makeSds)
1213
import 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)
2021
import Control.Monad.State (evalState, runState)
2122
import Control.Lens ((^.))
23+
import Data.Functor ((<&>))
24+
import Data.Foldable (traverse_)
2225
import System.Directory (setCurrentDirectory, getCurrentDirectory)
23-
import System.FilePath.Posix (takeDirectory)
24-
import System.IO (hClose, hPutStrLn, openFile, IOMode(WriteMode))
26+
import System.FilePath ((</>))
2527
import Prelude hiding (return,print,log,exp,sin,cos,tan)
2628

2729
import HelloWorld (helloWorldOO, helloWorldProc)
@@ -62,14 +64,14 @@ main = do
6264

6365
-- | Gathers all information needed to generate code, sorts it, and calls the renderers.
6466
genCode :: [PackageData] -> 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
7375
classes :: (OOProg r, AuxiliarySym r', Monad r') => (r (OO.Program r) -> ProgData) ->
7476
(r' PackageData -> PackageData) -> [PackageData]
7577
classes 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

Comments
 (0)