Skip to content

Commit

Permalink
Accept subcomponents as dependencies (close #382)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jun 2, 2020
1 parent fed5c7f commit 1dc82f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## next
- Accept subcomponents as dependencies (close #382)

## Changes in 0.34.1
- Fix a bug in `github: ...` introduced with `0.34.0`
(f63eb19b956517b4dd8e28dc5785be5889a99298)
Expand Down
6 changes: 3 additions & 3 deletions hpack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ library
src
ghc-options: -Wall
build-depends:
Cabal >=2.2
Cabal >=3.0.0.0
, Glob >=0.9.0
, aeson >=1.4.3.0
, base >=4.9 && <5
Expand Down Expand Up @@ -83,7 +83,7 @@ executable hpack
driver
ghc-options: -Wall
build-depends:
Cabal >=2.2
Cabal >=3.0.0.0
, Glob >=0.9.0
, aeson >=1.4.3.0
, base >=4.9 && <5
Expand Down Expand Up @@ -119,7 +119,7 @@ test-suite spec
ghc-options: -Wall
cpp-options: -DTEST
build-depends:
Cabal >=2.2
Cabal >=3.0.0.0
, Glob >=0.9.0
, HUnit >=1.6.0.0
, QuickCheck
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:
- yaml >= 0.10.0
- aeson >= 1.4.3.0
- scientific
- Cabal >= 2.2
- Cabal >= 3.0.0.0
- pretty
- bifunctors
- cryptonite
Expand Down
11 changes: 10 additions & 1 deletion src/Hpack/Syntax/Dependencies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ module Hpack.Syntax.Dependencies (

import qualified Control.Monad.Fail as Fail
import Data.Text (Text)
import Data.List
import qualified Data.Text as T
import Data.Semigroup (Semigroup(..))
import qualified Distribution.Package as D
import qualified Distribution.Types.LibraryName as D
import Distribution.Pretty (prettyShow)
import Data.Map.Lazy (Map)
import qualified Data.Map.Lazy as Map
import GHC.Exts
Expand Down Expand Up @@ -64,4 +67,10 @@ parseDependency :: Fail.MonadFail m => String -> Text -> m (String, DependencyVe
parseDependency subject = fmap fromCabal . cabalParse subject . T.unpack
where
fromCabal :: D.Dependency -> (String, DependencyVersion)
fromCabal d = (D.unPackageName $ D.depPkgName d, DependencyVersion Nothing . versionConstraintFromCabal $ D.depVerRange d)
fromCabal d = (toName (D.depPkgName d) (toList $ D.depLibraries d), DependencyVersion Nothing . versionConstraintFromCabal $ D.depVerRange d)

toName :: D.PackageName -> [D.LibraryName] -> String
toName package components = prettyShow package <> case components of
[D.LMainLibName] -> ""
[D.LSubLibName lib] -> ":" <> prettyShow lib
xs -> ":{" <> (intercalate "," $ map prettyShow [name | D.LSubLibName name <- xs]) <> "}"
11 changes: 11 additions & 0 deletions test/Hpack/Syntax/DependenciesSpec.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
module Hpack.Syntax.DependenciesSpec (spec) where

import Helper
Expand All @@ -18,6 +19,16 @@ defaultInfo = DependencyInfo [] anyVersion

spec :: Spec
spec = do
describe "parseDependency" $ do
it "accepts dependencies" $ do
parseDependency "dependency" "foo" `shouldReturn` ("foo", DependencyVersion Nothing AnyVersion)

it "accepts dependencies with a subcomponent" $ do
parseDependency "dependency" "foo:bar" `shouldReturn` ("foo:bar", DependencyVersion Nothing AnyVersion)

it "accepts dependencies with multiple subcomponents" $ do
parseDependency "dependency" "foo:{bar,baz}" `shouldReturn` ("foo:{bar,baz}", DependencyVersion Nothing AnyVersion)

describe "fromValue" $ do
context "when parsing Dependencies" $ do
context "with a scalar" $ do
Expand Down

0 comments on commit 1dc82f8

Please sign in to comment.