This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathDescription.hs
302 lines (222 loc) · 8.16 KB
/
Description.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Elm.Package.Description
( Description(..)
, defaultDescription
, read, write
)
where
import Prelude hiding (read)
import Control.Applicative ((<|>))
import Control.Arrow (first)
import Control.Monad (forM, when)
import Control.Monad.Error.Class (MonadError, throwError)
import Control.Monad.Trans (MonadIO, liftIO)
import Data.Aeson
import Data.Aeson.Types (Parser, parseEither)
import Data.Aeson.Encode.Pretty (encodePretty', defConfig, confCompare, keyOrder)
import qualified Data.ByteString.Lazy.Char8 as BS
import qualified Data.HashMap.Strict as Map
import qualified Data.List as List
import qualified Data.Text as T
import qualified Elm.Compiler.Module as Module
import qualified Elm.Package as Package
import qualified Elm.Package.Constraint as C
import qualified Elm.Package.Paths as Path
import Elm.Utils ((|>))
-- DESCRIPTION
data Description = Description
{ name :: Package.Name
, repo :: String
, version :: Package.Version
, elmVersion :: C.Constraint
, summary :: String
, license :: String
, sourceDirs :: [FilePath]
, exposed :: [Module.Raw]
, natives :: Bool
, dependencies :: [(Package.Name, C.Constraint)]
}
defaultDescription :: Description
defaultDescription =
Description
{ name = Package.Name "user" "project"
, repo = "https://github.com/user/project.git"
, version = Package.initialVersion
, elmVersion = C.defaultElmVersion
, summary = "helpful summary of your project, less than 80 characters"
, license = "BSD3"
, sourceDirs = [ "." ]
, exposed = []
, natives = False
, dependencies = []
}
-- READ
read :: (MonadIO m, MonadError e m) => (String -> e) -> FilePath -> m Description
read toError path =
do json <- liftIO (BS.readFile path)
either (throwError . toError) return (decodeDescription json)
-- WRITE
write :: Description -> IO ()
write description =
BS.writeFile Path.description (prettyJSON description)
-- TO JSON
prettyJSON :: Description -> BS.ByteString
prettyJSON description =
BS.snoc (prettyAngles (encodePretty' config description)) '\n'
where
config =
defConfig { confCompare = keyOrder (normalKeys ++ dependencyKeys) }
normalKeys =
[ "version"
, "summary"
, "repository"
, "license"
, "source-directories"
, "exposed-modules"
, "native-modules"
, "dependencies"
, "elm-version"
]
dependencyKeys =
dependencies description
|> map fst
|> List.sort
|> map (T.pack . Package.toString)
prettyAngles :: BS.ByteString -> BS.ByteString
prettyAngles string =
BS.concat $ replaceAngles string
replaceAngles :: BS.ByteString -> [BS.ByteString]
replaceAngles str =
let
(before, after) =
BS.break (=='\\') str
in
case BS.take 6 after of
"\\u003e" ->
before : ">" : replaceAngles (BS.drop 6 after)
"\\u003c" ->
before : "<" : replaceAngles (BS.drop 6 after)
"" ->
[before]
_ ->
before : "\\" : replaceAngles (BS.tail after)
instance ToJSON Description where
toJSON d =
object $
[ "repository" .= repo d
, "version" .= version d
, "summary" .= summary d
, "license" .= license d
, "source-directories" .= sourceDirs d
, "exposed-modules" .= map Module.RawForJson (exposed d)
, "dependencies" .= jsonDeps (dependencies d)
, "elm-version" .= elmVersion d
] ++ if natives d then ["native-modules" .= True] else []
where
jsonDeps deps =
Map.fromList $ map (first (T.pack . Package.toString)) deps
-- FROM JSON
decodeDescription :: BS.ByteString -> Either String Description
decodeDescription bytestring =
do value <- eitherDecode bytestring <|> badJson
parseEither getDescription value
badJson :: Either String a
badJson =
Left $
"I cannot parse the JSON. Maybe a comma is missing? Or there is an extra one?\n\
\It could also be because of mismatched brackets or quotes.\n\
\\n\
\You can also check out the following example to see what it should look like:\n\
\<https://raw.githubusercontent.com/elm-lang/html/master/elm-package.json>"
instance FromJSON Description where
parseJSON = getDescription
getDescription :: Value -> Parser Description
getDescription value =
case value of
Object obj ->
do version <- get obj "version" "your project's version number"
elmVersion <- getElmVersion obj
summary <- get obj "summary" "a short summary of your project"
when (length summary >= 80) $
fail "The \"summary\" must be less than 80 characters"
license <- get obj "license" "license information (BSD3 is recommended)"
repo <- get obj "repository" "a link to the project's GitHub repo"
name <- case repoToName repo of
Left err -> fail err
Right nm -> return nm
exposed <- map Module.fromJson <$> get obj "exposed-modules" "a list of modules exposed to users"
sourceDirs <- get obj "source-directories" "a list of directories containing source code"
deps <- getDependencies obj
natives <- maybe False id <$> obj .:? "native-modules"
return $ Description name repo version elmVersion summary license sourceDirs exposed natives deps
_ ->
fail $
"I was expecting a JSON object, like the one here:\n\
\<https://raw.githubusercontent.com/elm-lang/html/master/elm-package.json>"
get :: FromJSON a => Object -> T.Text -> String -> Parser a
get obj field desc =
do maybe <- obj .:? field
case maybe of
Just value ->
return value
Nothing ->
fail $
"Missing field " ++ show field ++ " which should hold " ++ desc ++ ".\n\
\\n\
\Check out an example " ++ Path.description ++ " file here:\n\
\<https://raw.githubusercontent.com/elm-lang/html/master/elm-package.json>"
getDependencies :: Object -> Parser [(Package.Name, C.Constraint)]
getDependencies obj =
do deps <- get obj "dependencies" "a list of the dependencies you need"
forM (Map.toList deps) $ \(rawName, rawConstraint) ->
case Package.fromString rawName of
Left problem ->
fail ("Ran into invalid package name \"" ++ rawName ++ "\" in dependencies.\n\n" ++ problem)
Right name ->
case C.fromString rawConstraint of
Just constraint ->
return (name, constraint)
Nothing ->
fail (C.errorMessage (Just rawName) rawConstraint)
getElmVersion :: Object -> Parser C.Constraint
getElmVersion obj =
do rawConstraint <- get obj "elm-version" elmVersionDescription
case C.fromString rawConstraint of
Just constraint ->
return constraint
Nothing ->
fail (C.errorMessage (Just "the elm-version field") rawConstraint)
elmVersionDescription :: String
elmVersionDescription =
"acceptable versions of the Elm Platform (e.g. \""
++ C.toString C.defaultElmVersion ++ "\")"
repoToName :: String -> Either String Package.Name
repoToName rawRepo =
do rawName <- dropDomain =<< dropExtension rawRepo
case Package.fromString rawName of
Left problem ->
Left (repoProblem problem)
Right name ->
Right name
dropExtension :: String -> Either String String
dropExtension string =
if List.isSuffixOf ".git" string then
Right (take (length string - 4) string)
else
Left (repoProblem "The given URI does not end with .git")
dropDomain :: String -> Either String String
dropDomain string =
let
http = "https://github.com/"
https = "https://github.com/"
in
if List.isPrefixOf http string then
Right (drop (length http) string)
else if List.isPrefixOf https string then
Right (drop (length https) string)
else
Left (repoProblem "The given domain does not start with <https://github.com/>")
repoProblem :: String -> String
repoProblem problem =
"Problem with the \"repository\" field.\n\n" ++ problem