Skip to content

Commit eb1ccaa

Browse files
committed
Split Lib module into Server and Codegen
1 parent 32c4477 commit eb1ccaa

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

app/Main.hs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Main where
22

3-
import qualified Lib
3+
import qualified Codegen
4+
import qualified Server
45
import qualified System.Environment
56
import qualified System.Exit
67

78
main :: IO ()
89
main = do
910
args <- System.Environment.getArgs
1011
case args of
11-
["--server"] -> Lib.runServer
12-
["--codegen"] -> Lib.runCodegen
12+
["--server"] -> Server.run
13+
["--codegen"] -> Codegen.run
1314
_ -> System.Exit.die "Invalid arguments!"

src/Codegen.hs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
{-# LANGUAGE TypeOperators #-}
4+
5+
module Codegen (run) where
6+
7+
import Elm.Derive (constructorTagModifier, defaultOptions, deriveBoth)
8+
import qualified Game
9+
import Servant.Elm (DefineElm (DefineElm), Proxy (Proxy), defElmImports, defElmOptions, generateElmModuleWith)
10+
import Server (APIRoutes)
11+
import System.Process (createProcess, cwd, proc)
12+
13+
run :: IO ()
14+
run =
15+
let outputDir = "./ui/generated"
16+
in do
17+
putStrLn "Deleting old generated code..."
18+
putStrLn "Generating Elm code from API..."
19+
generateElmModuleWith
20+
defElmOptions
21+
["Api"]
22+
defElmImports
23+
outputDir
24+
[ DefineElm (Proxy :: Proxy Game.State),
25+
DefineElm (Proxy :: Proxy Game.Country)
26+
]
27+
(Proxy :: Proxy APIRoutes)
28+
putStrLn "Formatting generated code using elm-format..."
29+
createProcess (proc "elm-format" ["Api.elm", "--yes"]) {cwd = Just outputDir}
30+
putStrLn "Done!"

src/Lib.hs src/Server.hs

+8-31
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
{-# LANGUAGE TemplateHaskell #-}
33
{-# LANGUAGE TypeOperators #-}
44

5-
module Lib
6-
( runServer,
7-
runCodegen,
8-
app,
5+
module Server
6+
( Server.run,
7+
APIRoutes,
98
)
109
where
1110

@@ -16,20 +15,20 @@ import Network.Wai
1615
import Network.Wai.Handler.Warp
1716
import Network.Wai.Logger (withStdoutLogger)
1817
import Servant
19-
import Servant.Elm (DefineElm (DefineElm), Proxy (Proxy), defElmImports, defElmOptions, generateElmModuleWith)
2018
import System.Environment (lookupEnv)
21-
import qualified System.Process
22-
import System.Process (cwd, proc)
2319

2420
type APIRoutes = "game" :> Get '[JSON] Game.State
2521

22+
deriveBoth defaultOptions {constructorTagModifier = Game.tagToApiLabel} ''Game.Country
23+
deriveBoth defaultOptions ''Game.State
24+
2625
type Routes =
2726
APIRoutes
2827
:<|> "_build" :> Raw
2928
:<|> Raw
3029

31-
runServer :: IO ()
32-
runServer = do
30+
run :: IO ()
31+
run = do
3332
maybePort <- lookupEnv "PORT"
3433
let port = fromMaybe 8080 (fmap read maybePort)
3534
withStdoutLogger $ \logger -> do
@@ -51,25 +50,3 @@ server =
5150
return Game.new
5251
:<|> serveDirectoryWebApp "ui/_build"
5352
:<|> serveDirectoryFileServer "ui/static"
54-
55-
deriveBoth defaultOptions {constructorTagModifier = Game.tagToApiLabel} ''Game.Country
56-
deriveBoth defaultOptions ''Game.State
57-
58-
runCodegen :: IO ()
59-
runCodegen =
60-
let outputDir = "./ui/generated"
61-
in do
62-
putStrLn "Deleting old generated code..."
63-
putStrLn "Generating Elm code from API..."
64-
generateElmModuleWith
65-
defElmOptions
66-
["Api"]
67-
defElmImports
68-
outputDir
69-
[ DefineElm (Proxy :: Proxy Game.State),
70-
DefineElm (Proxy :: Proxy Game.Country)
71-
]
72-
(Proxy :: Proxy APIRoutes)
73-
putStrLn "Formatting generated code using elm-format..."
74-
System.Process.createProcess (proc "elm-format" ["Api.elm", "--yes"]) {cwd = Just outputDir}
75-
putStrLn "Done!"

teg.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ cabal-version: >=1.10
1515

1616
library
1717
hs-source-dirs: src
18-
exposed-modules: Lib,
18+
exposed-modules: Codegen,
19+
Server,
1920
Game
2021
build-depends: base >= 4.7 && < 5
2122
, aeson

0 commit comments

Comments
 (0)