on master help sign in
Merge pull request #598 from maxsnew/tojson
d291190Evan Czaplicki
Elm.cabal+4−1
@@ -95,6 +95,7 @@ Library
95 Paths_Elm
95 Paths_Elm
96
96
97 Build-depends: aeson,
97 Build-depends: aeson,
98+ aeson-pretty,
98 base >=4.2 && <5,
99 base >=4.2 && <5,
99 binary >= 0.7.0.0,
100 binary >= 0.7.0.0,
100 blaze-html >= 0.5 && < 0.8,
101 blaze-html >= 0.5 && < 0.8,
@@ -177,6 +178,7 @@ Executable elm
177 Paths_Elm
178 Paths_Elm
178
179
179 Build-depends: aeson,
180 Build-depends: aeson,
181+ aeson-pretty,
180 base >=4.2 && <5,
182 base >=4.2 && <5,
181 binary >= 0.7.0.0,
183 binary >= 0.7.0.0,
182 blaze-html >= 0.5 && < 0.8,
184 blaze-html >= 0.5 && < 0.8,
@@ -252,6 +254,7 @@ Test-Suite compiler-tests
252 HUnit,
254 HUnit,
253 QuickCheck >= 2 && < 3,
255 QuickCheck >= 2 && < 3,
254 aeson,
256 aeson,
257+ aeson-pretty,
255 base >=4.2 && <5,
258 base >=4.2 && <5,
256 binary >= 0.7.0.0,
259 binary >= 0.7.0.0,
257 blaze-html,
260 blaze-html,
@@ -282,4 +285,4 @@ Test-Suite lib-tests
282 directory,
285 directory,
283 Elm,
286 Elm,
284 filepath,
287 filepath,
285− process
288+ process
compiler/Elm/Internal/Dependencies.hs+38−2
@@ -5,9 +5,12 @@ import Control.Applicative
5import Control.Monad.Error
5import Control.Monad.Error
6import qualified Control.Exception as E
6import qualified Control.Exception as E
7import Data.Aeson
7import Data.Aeson
8+import Data.Aeson.Encode.Pretty
8import qualified Data.List as List
9import qualified Data.List as List
9import qualified Data.ByteString.Lazy as BS
10import qualified Data.ByteString.Lazy as BS
10import qualified Data.HashMap.Strict as Map
11import qualified Data.HashMap.Strict as Map
12+import qualified Data.Text as T
13+
11import qualified Elm.Internal.Name as N
14import qualified Elm.Internal.Name as N
12import qualified Elm.Internal.Version as V
15import qualified Elm.Internal.Version as V
13import qualified Elm.Internal.Paths as Path
16import qualified Elm.Internal.Paths as Path
@@ -22,14 +25,32 @@ data Deps = Deps
22 , exposed :: [String]
25 , exposed :: [String]
23 , elmVersion :: V.Version
26 , elmVersion :: V.Version
24 , dependencies :: [(N.Name,V.Version)]
27 , dependencies :: [(N.Name,V.Version)]
25− } deriving Show
28+ } deriving (Show, Eq, Ord)
26
29
27data MiniDeps = Mini [(N.Name,V.Version)]
30data MiniDeps = Mini [(N.Name,V.Version)]
31+ deriving (Show, Eq, Ord)
32+
33+instance ToJSON MiniDeps where
34+ toJSON (Mini m) = toJSON m
28
35
29instance FromJSON MiniDeps where
36instance FromJSON MiniDeps where
30 parseJSON (Object obj) = Mini <$> getDependencies obj
37 parseJSON (Object obj) = Mini <$> getDependencies obj
31 parseJSON _ = mzero
38 parseJSON _ = mzero
32
39
40+instance ToJSON Deps where
41+ toJSON d = object [
42+ "version" .= version d,
43+ "summary" .= summary d,
44+ "description" .= description d,
45+ "license" .= license d,
46+ "repository" .= repo d,
47+ "exposed-modules" .= exposed d,
48+ "elm-version" .= elmVersion d,
49+ "dependencies" .= (jsonDeps . dependencies $ d)
50+ ]
51+ where jsonDeps = Map.fromList . map (mapFst (T.pack . show))
52+ mapFst f (a,b) = (f a, b)
53+
33instance FromJSON Deps where
54instance FromJSON Deps where
34 parseJSON (Object obj) =
55 parseJSON (Object obj) =
35 do version <- get obj "version" "your projects version number"
56 do version <- get obj "version" "your projects version number"
@@ -114,4 +135,19 @@ withDeps handle path =
114 "\n You may need to create a dependency file for your project."
135 "\n You may need to create a dependency file for your project."
115
136
116depsAt :: FilePath -> ErrorT String IO Deps
137depsAt :: FilePath -> ErrorT String IO Deps
117−depsAt = withDeps id
138+depsAt = withDeps id
139+
140+-- | Encode dependencies in a canonical JSON format
141+prettyJSON :: Deps -> BS.ByteString
142+prettyJSON = encodePretty' config
143+ where config = defConfig { confCompare = order }
144+ order = keyOrder [ "name",
145+ "version",
146+ "summary",
147+ "description",
148+ "license",
149+ "repo",
150+ "exposed-modules",
151+ "elm-version",
152+ "dependencies"
153+ ]
compiler/Elm/Internal/Name.hs+2−2
@@ -10,7 +10,7 @@ import qualified Data.Maybe as Maybe
10import Data.Typeable
10import Data.Typeable
11
11
12data Name = Name { user :: String, project :: String }
12data Name = Name { user :: String, project :: String }
13− deriving (Typeable,Eq)
13+ deriving (Typeable,Eq, Ord)
14
14
15instance Binary Name where
15instance Binary Name where
16 get = Name <$> get <*> get
16 get = Name <$> get <*> get
@@ -48,4 +48,4 @@ errorMsg string =
48 unlines
48 unlines
49 [ "Dependency file has an invalid name: " ++ string
49 [ "Dependency file has an invalid name: " ++ string
50 , "Must have format user/project and match a public github project."
50 , "Must have format user/project and match a public github project."
51− ]
51+ ]