on master help sign in
Add "native-modules" as an optional field
commit22c6e719ee87498a42eede6c34cb9135df75e162 authorEvan Czaplicki <info@elm-lang.org> dateFri 9 May 2014, 12:52 (12 years ago) parent2fb6d27
1 file changed+35−23
compiler/Elm/Internal/Dependencies.hs+35−23
compiler/Elm/Internal/Dependencies.hs+35−23
@@ -2,10 +2,13 @@
22module Elm.Internal.Dependencies where
33
44import Control.Applicative
5+import Control.Arrow (first)
56import Control.Monad.Error
67import qualified Control.Exception as E
78import Data.Aeson
9+import Data.Aeson.Types (Parser)
810import Data.Aeson.Encode.Pretty
11+import Data.Maybe (fromMaybe)
912import qualified Data.List as List
1013import qualified Data.ByteString.Lazy as BS
1114import qualified Data.HashMap.Strict as Map
@@ -23,6 +26,7 @@ data Deps = Deps
2326 , license :: String
2427 , repo :: String
2528 , exposed :: [String]
29+ , native :: [String]
2630 , elmVersion :: V.Version
2731 , dependencies :: [(N.Name,V.Version)]
2832 } deriving (Show, Eq, Ord)
@@ -38,18 +42,22 @@ instance FromJSON MiniDeps where
3842 parseJSON _ = mzero
3943
4044instance 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)
45+ toJSON d =
46+ object $
47+ [ "version" .= version d
48+ , "summary" .= summary d
49+ , "description" .= description d
50+ , "license" .= license d
51+ , "repository" .= repo d
52+ , "exposed-modules" .= exposed d
53+ , "elm-version" .= elmVersion d
54+ , "dependencies" .= (jsonDeps . dependencies $ d)
55+ ] ++ nativeModules
56+ where
57+ jsonDeps = Map.fromList . map (first (T.pack . show))
58+ nativeModules
59+ | null (native d) = []
60+ | otherwise = [ "native-modules" .= native d ]
5361
5462instance FromJSON Deps where
5563 parseJSON (Object obj) =
@@ -69,15 +77,18 @@ instance FromJSON Deps where
6977 Right nm -> return nm
7078
7179 exposed <- get obj "exposed-modules" "a list of modules exposed to users"
80+
81+ native <- fromMaybe [] <$> (obj .:? "native-modules")
7282
7383 elmVersion <- get obj "elm-version" "the version of the Elm compiler you are using"
7484
7585 deps <- getDependencies obj
7686
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
7888
7989 parseJSON _ = mzero
8090
91+getDependencies :: Object -> Parser [(N.Name, V.Version)]
8192getDependencies obj =
8293 toDeps =<< get obj "dependencies" "a listing of your project's dependencies"
8394 where
@@ -88,7 +99,7 @@ getDependencies obj =
8899 (Nothing, _) -> fail $ N.errorMsg f
89100 (_, Nothing) -> fail $ "invalid version number " ++ v
90101
91−
102+get :: FromJSON a => Object -> T.Text -> String -> Parser a
92103get obj field desc =
93104 do maybe <- obj .:? field
94105 case maybe of
@@ -142,13 +153,14 @@ depsAt = withDeps id
142153prettyJSON :: Deps -> BS.ByteString
143154prettyJSON = encodePretty' config
144155 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"
156+ order = keyOrder [ "name"
157+ , "version"
158+ , "summary"
159+ , "description"
160+ , "license"
161+ , "repo"
162+ , "exposed-modules"
163+ , "native-modules"
164+ , "elm-version"
165+ , "dependencies"
154166 ]