-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathLanguage.hs
228 lines (166 loc) · 5.78 KB
/
Language.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Glot.Language
( Id
, idToText
, idFromText
, EditorConfig(..)
, RunConfig(..)
, Language(..)
, Book(..)
, readLanguages
, isRunnable
, find
) where
import Prelude hiding (id)
import qualified Dhall
import qualified Dhall.TH
import qualified Yesod.Static as Static
import qualified Yesod.Core.Dispatch as Dispatch
import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.List as List
import qualified Data.Maybe as Maybe
import qualified Data.Aeson as Aeson
import qualified GHC.Generics as GHC
import qualified Database.Persist.Sql as Sql
import qualified Data.Either.Combinators as Either
import qualified Data.Either as Either
import qualified Control.Exception as Exception
Dhall.TH.makeHaskellTypes
[ Dhall.TH.SingleConstructor "EditorConfig" "EditorConfig" "./config/types/EditorConfig.dhall"
, Dhall.TH.SingleConstructor "RunConfig" "RunConfig" "./config/types/RunConfig.dhall"
, Dhall.TH.SingleConstructor "Book" "Book" "./config/types/Book.dhall"
, Dhall.TH.SingleConstructor "LanguageConfig" "LanguageConfig" "./config/types/Language.dhall"
]
deriving instance Show EditorConfig
deriving instance Show RunConfig
deriving instance Show Book
deriving instance Show LanguageConfig
newtype Id = Id Text.Text
deriving (Show, Read, Eq, Ord, GHC.Generic)
deriving newtype (Aeson.ToJSON, Sql.PersistField, Sql.PersistFieldSql)
instance Aeson.FromJSON Id where
parseJSON = Aeson.withText "Language.Id" $ \text ->
case idFromText text of
Just id ->
pure id
Nothing ->
Prelude.fail "Not a valid language id"
instance Dispatch.PathPiece Id where
fromPathPiece text =
idFromText text
toPathPiece id =
idToText id
idToText :: Id -> Text.Text
idToText (Id id) =
id
idFromText :: Text.Text -> Maybe Id
idFromText text =
if isValidId text then
Just (Id text)
else
Nothing
data Language = Language
{ identifier :: Id
, name :: Text.Text
, svgLogoRoute :: Static.StaticRoute
, pngLogoRoute :: Static.StaticRoute
, fileExtension :: Text.Text
, editorConfig :: EditorConfig
, runConfig :: Maybe RunConfig
, books :: [Book]
}
deriving (Show)
readLanguages :: IO [Language]
readLanguages = do
langConfigs <- Dhall.input Dhall.auto "./config/languages.dhall"
eitherLanguages <- mapM fromConfigIO langConfigs
let (errors, languages) = Either.partitionEithers eitherLanguages
mapM_ (\err -> TextIO.putStrLn $ "WARNING: " <> err) (map formatConfigError errors)
pure languages
data ConfigError
= InvalidId LanguageConfig
| FailedToReadLogo LanguageConfig Exception.SomeException
formatConfigError :: ConfigError -> Text.Text
formatConfigError err =
case err of
InvalidId LanguageConfig{..} ->
mconcat
[ "Language "
, id
, " does not have a valid id. The id can only contain lowercase characters and digits."
]
FailedToReadLogo LanguageConfig{..} exception ->
mconcat
[ "Failed to read logos for language "
, id
, ", exception: "
, Text.pack (show exception)
]
fromConfigIO :: LanguageConfig -> IO (Either ConfigError Language)
fromConfigIO langConfig = do
eitherLogoEtags <- Exception.try (readLogoEtags langConfig)
case eitherLogoEtags of
Left exception ->
pure $ Left (FailedToReadLogo langConfig exception)
Right logoEtags ->
pure (fromConfig langConfig logoEtags)
fromConfig :: LanguageConfig -> LogoEtags -> Either ConfigError Language
fromConfig langConfig@LanguageConfig{..} LogoEtags{..} =
let
svgLogoRoute =
Static.StaticRoute ["img", logoName <> ".svg"] [("etag", svgEtag)]
pngLogoRoute =
Static.StaticRoute ["img", logoName <> ".svg.png"] [("etag", pngEtag)]
in do
identifier <- Either.maybeToRight (InvalidId langConfig) (idFromText id)
pure Language{..}
isRunnable :: Language -> Bool
isRunnable Language{..} =
Maybe.isJust runConfig
find :: [Language] -> Id -> Maybe Language
find languages langId =
List.find (\lang -> identifier lang == langId) languages
-- TODO: get static root from arg
svgLogoPath :: LanguageConfig -> Text.Text
svgLogoPath LanguageConfig{..} =
Text.intercalate "/" ["static", "img", logoName <> ".svg"]
-- TODO: get static root from arg
pngLogoPath :: LanguageConfig -> Text.Text
pngLogoPath LanguageConfig{..} =
Text.intercalate "/" ["static", "img", logoName <> ".svg.png"]
isValidId :: Text.Text -> Bool
isValidId text =
let
allowedChars =
['a'..'z'] ++ ['0'..'9']
hasOnlyAllowedChars =
Text.all (`elem` allowedChars) text
hasMinimumLength =
Text.length text > 0
in
hasMinimumLength && hasOnlyAllowedChars
data LogoEtags = LogoEtags
{ svgEtag :: Text.Text
, pngEtag :: Text.Text
}
readLogoEtags :: LanguageConfig -> IO LogoEtags
readLogoEtags langConfig = do
svgEtag <- readEtag (svgLogoPath langConfig)
pngEtag <- readEtag (pngLogoPath langConfig)
pure LogoEtags
{ svgEtag = Text.pack svgEtag
, pngEtag = Text.pack pngEtag
}
readEtag :: Text.Text -> IO String
readEtag path = do
bytes <- BS.readFile (Text.unpack path)
pure $ Static.base64md5 (BSL.fromStrict bytes)