Skip to content

Commit

Permalink
Merge pull request #58 from scarf-sh/embed-templates
Browse files Browse the repository at this point in the history
embed templates into the build
  • Loading branch information
justinwoo authored Mar 20, 2024
2 parents f40aca5 + 50008c8 commit 588cade
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/Tie/Codegen/Request.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}

module Tie.Codegen.Request (codegenRequestAuxFile) where

import qualified Data.ByteString as ByteString
import qualified Data.Text as Text
import Paths_tie (getDataFileName)
import Data.FileEmbed (embedStringFile, makeRelativeToProject)
import Prettyprinter (Doc, hsep, vsep)
import qualified Prettyprinter.Util as Prettyprinter
import System.IO.Unsafe (unsafePerformIO)
import Tie.Name (Name)

templateContents :: ByteString
templateContents = $(embedStringFile =<< makeRelativeToProject "Request.template.hs")

auxTemplate :: Text
auxTemplate = unsafePerformIO $ do
file <- getDataFileName "Request.template.hs"
contents <- ByteString.readFile file
pure (decodeUtf8 contents)
auxTemplate = decodeUtf8 templateContents
{-# NOINLINE auxTemplate #-}

codegenRequestAuxFile ::
Expand Down
11 changes: 6 additions & 5 deletions src/Tie/Codegen/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TemplateHaskell #-}

module Tie.Codegen.Response
( codegenResponses,
Expand All @@ -13,7 +14,7 @@ import qualified Data.ByteString as ByteString
import Data.List (lookup)
import qualified Data.Text as Text
import Network.HTTP.Media (renderHeader)
import Paths_tie (getDataFileName)
import Data.FileEmbed (embedStringFile, makeRelativeToProject)
import Prettyprinter (Doc, (<+>))
import qualified Prettyprinter as PP
import qualified Prettyprinter.Render.Text as PP
Expand Down Expand Up @@ -313,11 +314,11 @@ codegenToResponses responseModuleName operationName responses defaultResponse =
)
in decl

templateContents :: ByteString
templateContents = $(embedStringFile =<< makeRelativeToProject "Response.template.hs")

auxTemplate :: Text
auxTemplate = unsafePerformIO $ do
file <- getDataFileName "Response.template.hs"
contents <- ByteString.readFile file
pure (decodeUtf8 contents)
auxTemplate = decodeUtf8 templateContents
{-# NOINLINE auxTemplate #-}

codegenResponseAuxFile ::
Expand Down
8 changes: 4 additions & 4 deletions tie.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ author: Alex Biehl ([email protected])
maintainer: [email protected], [email protected]
copyright: (c) 2022 Scarf Systems
category:
extra-source-files: CHANGELOG.md
data-files:
extra-source-files:
CHANGELOG.md
Response.template.hs
Request.template.hs
test/golden/**/*.out
test/golden/**/*.yaml

source-repository head
type: git
Expand Down Expand Up @@ -48,6 +46,7 @@ library
, containers
, directory
, filepath
, file-embed
, http-media
, insert-ordered-containers
, lens
Expand Down Expand Up @@ -99,6 +98,7 @@ test-suite tie-tests
, bytestring
, containers
, filepath
, file-embed
, hspec
, insert-ordered-containers
, openapi3
Expand Down

0 comments on commit 588cade

Please sign in to comment.