on master help sign in
Add pretty JSON function and Ord instance
commit5a18f9aaeec4951d5cf192c49b6f056403897a31 authorMax New <maxsnew@gmail.com> dateSun 27 Apr 2014, 17:34 (13 years ago) parent729da57
3 files changed+44−4
Elm.cabal+3 compiler/Elm/Internal/Dependencies.hs+39−2 compiler/Elm/Internal/Name.hs+2−2
Elm.cabal+3
@@ -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 == 0.5.* || == 0.6.*,
compiler/Elm/Internal/Dependencies.hs+39−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,33 @@ 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+ "name" .= name d,
43+ "version" .= version d,
44+ "summary" .= summary d,
45+ "description" .= description d,
46+ "license" .= license d,
47+ "repo" .= repo d,
48+ "exposed-modules" .= exposed d,
49+ "elm-version" .= elmVersion d,
50+ "dependencies" .= (jsonDeps . dependencies $ d)
51+ ]
52+ where jsonDeps = Map.fromList . map (mapFst (T.pack . show))
53+ mapFst f (a,b) = (f a, b)
54+
3355instance FromJSON Deps where
3456 parseJSON (Object obj) =
3557 do version <- get obj "version" "your projects version number"
@@ -114,4 +136,19 @@ withDeps handle path =
114136 "\n You may need to create a dependency file for your project."
115137
116138depsAt :: FilePath -> ErrorT String IO Deps
117−depsAt = withDeps id
139+depsAt = withDeps id
140+
141+-- | Encode dependencies in a canonical JSON format
142+prettyJSON :: Deps -> BS.ByteString
143+prettyJSON = encodePretty' config
144+ where config = defConfig { confCompare = order }
145+ order = keyOrder [ "name",
146+ "version",
147+ "summary",
148+ "description",
149+ "license",
150+ "repo",
151+ "exposed-modules",
152+ "elm-version",
153+ "dependencies"
154+ ]
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+ ]