@@ -2,10 +2,13 @@
2module Elm.Internal.Dependencies where
2module Elm.Internal.Dependencies where
4import Control.Applicative
4import Control.Applicative
5+import Control.Arrow (first)
5import Control.Monad.Error
6import Control.Monad.Error
6import qualified Control.Exception as E
7import qualified Control.Exception as E
7import Data.Aeson
8import Data.Aeson
9+import Data.Aeson.Types (Parser)
8import Data.Aeson.Encode.Pretty
10import Data.Aeson.Encode.Pretty
11+import Data.Maybe (fromMaybe)
9import qualified Data.List as List
12import qualified Data.List as List
10import qualified Data.ByteString.Lazy as BS
13import qualified Data.ByteString.Lazy as BS
11import qualified Data.HashMap.Strict as Map
14import qualified Data.HashMap.Strict as Map
@@ -23,6 +26,7 @@ data Deps = Deps
23 , license :: String
26 , license :: String
24 , repo :: String
27 , repo :: String
25 , exposed :: [String]
28 , exposed :: [String]
26 , elmVersion :: V.Version
30 , elmVersion :: V.Version
27 , dependencies :: [(N.Name,V.Version)]
31 , dependencies :: [(N.Name,V.Version)]
28 } deriving (Show, Eq, Ord)
32 } deriving (Show, Eq, Ord)
@@ -38,18 +42,22 @@ instance FromJSON MiniDeps where
38 parseJSON _ = mzero
42 parseJSON _ = mzero
40instance ToJSON Deps where
44instance ToJSON Deps where
41− toJSON d = object [
45+ toJSON d =
42− "version" .= version d,
47+ [ "version" .= version d
43− "summary" .= summary d,
48+ , "summary" .= summary d
44− "description" .= description d,
49+ , "description" .= description d
45− "license" .= license d,
50+ , "license" .= license d
46− "repository" .= repo d,
51+ , "repository" .= repo d
47− "exposed-modules" .= exposed d,
52+ , "exposed-modules" .= exposed d
48− "elm-version" .= elmVersion d,
53+ , "elm-version" .= elmVersion d
49− "dependencies" .= (jsonDeps . dependencies $ d)
54+ , "dependencies" .= (jsonDeps . dependencies $ d)
50− ]
55+ ] ++ nativeModules
51− where jsonDeps = Map.fromList . map (mapFst (T.pack . show))
57+ jsonDeps = Map.fromList . map (first (T.pack . show))
52− mapFst f (a,b) = (f a, b)
58+ nativeModules
59+ | null (native d) = []
60+ | otherwise = [ "native-modules" .= native d ]
54instance FromJSON Deps where
62instance FromJSON Deps where
55 parseJSON (Object obj) =
63 parseJSON (Object obj) =
@@ -69,15 +77,18 @@ instance FromJSON Deps where
69 Right nm -> return nm
77 Right nm -> return nm
71 exposed <- get obj "exposed-modules" "a list of modules exposed to users"
79 exposed <- get obj "exposed-modules" "a list of modules exposed to users"
81+ native <- fromMaybe [] <$> (obj .:? "native-modules")
73 elmVersion <- get obj "elm-version" "the version of the Elm compiler you are using"
83 elmVersion <- get obj "elm-version" "the version of the Elm compiler you are using"
75 deps <- getDependencies obj
85 deps <- getDependencies obj
77− return $ Deps name version summary desc license repo exposed elmVersion deps
87+ return $ Deps name version summary desc license repo exposed native elmVersion deps
79 parseJSON _ = mzero
89 parseJSON _ = mzero
91+getDependencies :: Object -> Parser [(N.Name, V.Version)]
81getDependencies obj =
92getDependencies obj =
82 toDeps =<< get obj "dependencies" "a listing of your project's dependencies"
93 toDeps =<< get obj "dependencies" "a listing of your project's dependencies"
@@ -88,7 +99,7 @@ getDependencies obj =
88 (Nothing, _) -> fail $ N.errorMsg f
99 (Nothing, _) -> fail $ N.errorMsg f
89 (_, Nothing) -> fail $ "invalid version number " ++ v
100 (_, Nothing) -> fail $ "invalid version number " ++ v
91−
102+get :: FromJSON a => Object -> T.Text -> String -> Parser a
92get obj field desc =
103get obj field desc =
93 do maybe <- obj .:? field
104 do maybe <- obj .:? field
94 case maybe of
105 case maybe of
@@ -142,13 +153,14 @@ depsAt = withDeps id
142prettyJSON :: Deps -> BS.ByteString
153prettyJSON :: Deps -> BS.ByteString
143prettyJSON = encodePretty' config
154prettyJSON = encodePretty' config
144 where config = defConfig { confCompare = order }
155 where config = defConfig { confCompare = order }
145− order = keyOrder [ "name",
156+ order = keyOrder [ "name"
146− "version",
157+ , "version"
147− "summary",
158+ , "summary"
148− "description",
159+ , "description"
149− "license",
160+ , "license"
150− "repo",
161+ , "repo"
151− "exposed-modules",
162+ , "exposed-modules"
152− "elm-version",
164+ , "elm-version"
153− "dependencies"
165+ , "dependencies"