Skip to content

Commit

Permalink
Elm library support
Browse files Browse the repository at this point in the history
  • Loading branch information
turboMaCk committed May 25, 2019
1 parent 02e35d5 commit a24c4e8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions elm2nix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ library
, text
, transformers
, unordered-containers
, vector
exposed-modules:
Elm2Nix
Elm2Nix.FixedOutput
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ pushd test/lib
elm2nix convert > elm-srcs.nix
elm2nix snapshot > versions.dat
nix-build
checkfile ./result/Main.html
checkfile ./result/share/doc/Main.json
popd
25 changes: 18 additions & 7 deletions src/Elm2Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import qualified Data.HashMap.Strict as HM
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Aeson as Json
import qualified Data.Text as Text
import qualified Data.Vector as Vector
import qualified Data.Either as Either

import Elm2Nix.FixedOutput (FixedDerivation(..), prefetch)
import Elm2Nix.PackagesSnapshot (snapshot)
Expand Down Expand Up @@ -51,16 +53,21 @@ parseElmJsonDeps obj =
case obj of
Object hm -> do
deps <- tryLookup hm "dependencies"
case deps of
Object dhm -> do
direct <- tryLookup dhm "direct"
indirect <- tryLookup dhm "indirect"
liftM2 (++) (parseDeps direct) (parseDeps indirect)
v -> Left (UnexpectedValue v)

let libstyleDeps = parseDeps deps
if Either.isRight libstyleDeps then
libstyleDeps
else
case deps of
Object dhm -> do
direct <- tryLookup dhm "direct"
indirect <- tryLookup dhm "indirect"
liftM2 (++) (parseDeps direct) (parseDeps indirect)
v -> Left (UnexpectedValue v)
v -> Left (UnexpectedValue v)
where
parseDep :: Text -> Value -> Either Elm2NixError Dep
parseDep name (String ver) = Right (Text.unpack name, Text.unpack ver)
parseDep name (String ver) = Right (Text.unpack name, sanitizeVersion (Text.unpack ver))
parseDep _ v = Left (UnexpectedValue v)

parseDeps :: Value -> Either Elm2NixError [Dep]
Expand All @@ -71,6 +78,10 @@ parseElmJsonDeps obj =
maybeToRight _ (Just x) = Right x
maybeToRight y Nothing = Left y

sanitizeVersion :: String -> String
sanitizeVersion =
takeWhile (\c -> c /= ' ') -- converts "1.0.0 <= v < 2.0.0" to "1.0.0"

tryLookup :: HashMap Text Value -> Text -> Either Elm2NixError Value
tryLookup hm key =
maybeToRight (KeyNotFound key) (HM.lookup key hm)
Expand Down
4 changes: 2 additions & 2 deletions test/lib/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"summary": "test elm2nix",
"license": "BSD-3-Clause",
"version": "1.0.0",
"exposed-module": ["Main"],
"exposed-modules": ["Main"],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.2",
"elm/core": "1.0.2 <= v < 2.0.0"
},
"test-dependencies": {}
}
3 changes: 3 additions & 0 deletions test/lib/src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Main exposing (f)

{-| @docs f -}

{-| Anwer to universe life and everything -}
f : () -> Int
f () = 42

0 comments on commit a24c4e8

Please sign in to comment.