on master help sign in
Merge pull request #598 from maxsnew/tojson

More JSON Deps functionality + Instances

commitd291190e32aa9003e2f71f7b62e923b7dcce7bfb authorEvan Czaplicki <info@elm-lang.org> dateMon 28 Apr 2014, 22:38 (13 years ago) parent351e2457ad5454
3 files changed+44−5
Elm.cabal+4−1 compiler/Elm/Internal/Dependencies.hs+38−2 compiler/Elm/Internal/Name.hs+2−2
Elm.cabal+4−1
@@ -95,6 +95,7 @@ Library
9595 Paths_Elm
9696
9797 Build-depends: aeson,
98+ aeson-pretty,
9899 base >=4.2 && <5,
99100 binary >= 0.7.0.0,
100101 blaze-html >= 0.5 && < 0.8,
@@ -177,6 +178,7 @@ Executable elm
177178 Paths_Elm
178179
179180 Build-depends: aeson,
181+ aeson-pretty,
180182 base >=4.2 && <5,
181183 binary >= 0.7.0.0,
182184 blaze-html >= 0.5 && < 0.8,
@@ -252,6 +254,7 @@ Test-Suite compiler-tests
252254 HUnit,
253255 QuickCheck >= 2 && < 3,
254256 aeson,
257+ aeson-pretty,
255258 base >=4.2 && <5,
256259 binary >= 0.7.0.0,
257260 blaze-html,
@@ -282,4 +285,4 @@ Test-Suite lib-tests
282285 directory,
283286 Elm,
284287 filepath,
285− process
288+ process
compiler/Elm/Internal/Dependencies.hs+38−2
@@ -5,9 +5,12 @@ import Control.Applicative
55import Control.Monad.Error
66import qualified Control.Exception as E
77import Data.Aeson
8+import Data.Aeson.Encode.Pretty
89import qualified Data.List as List
910import qualified Data.ByteString.Lazy as BS
1011import qualified Data.HashMap.Strict as Map
12+import qualified Data.Text as T
13+
1114import qualified Elm.Internal.Name as N
1215import qualified Elm.Internal.Version as V
1316import qualified Elm.Internal.Paths as Path
@@ -22,14 +25,32 @@ data Deps = Deps
2225 , exposed :: [String]
2326 , elmVersion :: V.Version
2427 , dependencies :: [(N.Name,V.Version)]
25− } deriving Show
28+ } deriving (Show, Eq, Ord)
2629
2730data MiniDeps = Mini [(N.Name,V.Version)]
31+ deriving (Show, Eq, Ord)
32+
33+instance ToJSON MiniDeps where
34+ toJSON (Mini m) = toJSON m
2835
2936instance FromJSON MiniDeps where
3037 parseJSON (Object obj) = Mini <$> getDependencies obj
3138 parseJSON _ = mzero
3239
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+
3354instance FromJSON Deps where
3455 parseJSON (Object obj) =
3556 do version <- get obj "version" "your projects version number"
@@ -114,4 +135,19 @@ withDeps handle path =
114135 "\n You may need to create a dependency file for your project."
115136
116137depsAt :: 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
1010import Data.Typeable
1111
1212data Name = Name { user :: String, project :: String }
13− deriving (Typeable,Eq)
13+ deriving (Typeable,Eq, Ord)
1414
1515instance Binary Name where
1616 get = Name <$> get <*> get
@@ -48,4 +48,4 @@ errorMsg string =
4848 unlines
4949 [ "Dependency file has an invalid name: " ++ string
5050 , "Must have format user/project and match a public github project."
51− ]
51+ ]