Skip to content

Commit f702547

Browse files
Allow 'github' shorthand for extra-deps (fixes commercialhaskell#3873)
1 parent a6ae6a5 commit f702547

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

src/Data/Aeson/Extended.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ instance IsString WarningParserMonoid where
150150

151151
-- Parsed JSON value with its warnings
152152
data WithJSONWarnings a = WithJSONWarnings a [JSONWarning]
153-
deriving Generic
153+
deriving (Eq, Generic, Show)
154154
instance Functor WithJSONWarnings where
155155
fmap f (WithJSONWarnings x w) = WithJSONWarnings (f x) w
156156
instance Monoid a => Monoid (WithJSONWarnings a) where
@@ -160,6 +160,7 @@ instance Monoid a => Monoid (WithJSONWarnings a) where
160160
-- | Warning output from 'WarningParser'.
161161
data JSONWarning = JSONUnrecognizedFields String [Text]
162162
| JSONGeneralWarning !Text
163+
deriving Eq
163164
instance Show JSONWarning where
164165
show (JSONUnrecognizedFields obj [field]) =
165166
"Unrecognized field in " <> obj <> ": " <> T.unpack field

src/Stack/Types/BuildPlan.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ instance subdirs ~ Subdirs => FromJSON (WithJSONWarnings (PackageLocation subdir
237237
= (noJSONWarnings <$> withText "PackageLocation" (\t -> http t <|> file t) v)
238238
<|> repo v
239239
<|> archiveObject v
240+
<|> github v
240241
where
241242
file t = pure $ PLFilePath $ T.unpack t
242243
http t =
@@ -269,6 +270,26 @@ instance subdirs ~ Subdirs => FromJSON (WithJSONWarnings (PackageLocation subdir
269270
, archiveHash = msha'
270271
}
271272

273+
github = withObjectWarnings "PLArchive" $ \o -> do
274+
GitHubRepo ghRepo <- o ..: "github"
275+
commit <- o ..: "commit"
276+
subdirs <- o ..:? "subdirs" ..!= DefaultSubdirs
277+
return $ PLArchive Archive
278+
{ archiveUrl = "https://github.com/" <> ghRepo <> "/archive/" <> commit <> ".tar.gz"
279+
, archiveSubdirs = subdirs
280+
, archiveHash = Nothing
281+
}
282+
283+
-- An unexported newtype wrapper to hang a 'FromJSON' instance off of. Contains
284+
-- a GitHub user and repo name separated by a forward slash, e.g. "foo/bar".
285+
newtype GitHubRepo = GitHubRepo Text
286+
287+
instance FromJSON GitHubRepo where
288+
parseJSON = withText "GitHubRepo" $ \s -> do
289+
case T.split (== '/') s of
290+
[x, y] | not (T.null x || T.null y) -> return (GitHubRepo s)
291+
_ -> fail "expecting \"user/repo\""
292+
272293
-- | Name of an executable.
273294
newtype ExeName = ExeName { unExeName :: Text }
274295
deriving (Show, Eq, Ord, Hashable, IsString, Generic, Store, NFData, Data, Typeable)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Stack.Types.BuildPlanSpec where
4+
5+
import Data.Aeson.Extended (WithJSONWarnings(..))
6+
import Data.ByteString (ByteString)
7+
import qualified Data.ByteString.Char8 as S8
8+
import Data.Yaml (decode)
9+
import Stack.Types.BuildPlan
10+
import Test.Hspec
11+
12+
spec :: Spec
13+
spec =
14+
describe "PackageLocation" $ do
15+
describe "Archive" $ do
16+
describe "github" $ do
17+
let decode' :: ByteString -> Maybe (WithJSONWarnings (PackageLocation Subdirs))
18+
decode' = decode
19+
20+
it "'github' and 'commit' keys" $ do
21+
let contents :: ByteString
22+
contents =
23+
S8.pack
24+
(unlines
25+
[ "github: oink/town"
26+
, "commit: abc123"
27+
])
28+
let expected :: PackageLocation Subdirs
29+
expected =
30+
PLArchive Archive
31+
{ archiveUrl = "https://github.com/oink/town/archive/abc123.tar.gz"
32+
, archiveSubdirs = DefaultSubdirs
33+
, archiveHash = Nothing
34+
}
35+
decode' contents `shouldBe` Just (WithJSONWarnings expected [])
36+
37+
it "'github', 'commit', and 'subdirs' keys" $ do
38+
let contents :: ByteString
39+
contents =
40+
S8.pack
41+
(unlines
42+
[ "github: oink/town"
43+
, "commit: abc123"
44+
, "subdirs:"
45+
, " - foo"
46+
])
47+
let expected :: PackageLocation Subdirs
48+
expected =
49+
PLArchive Archive
50+
{ archiveUrl = "https://github.com/oink/town/archive/abc123.tar.gz"
51+
, archiveSubdirs = ExplicitSubdirs ["foo"]
52+
, archiveHash = Nothing
53+
}
54+
decode' contents `shouldBe` Just (WithJSONWarnings expected [])
55+
56+
it "does not parse GitHub repo with no slash" $ do
57+
let contents :: ByteString
58+
contents =
59+
S8.pack
60+
(unlines
61+
[ "github: oink"
62+
, "commit: abc123"
63+
])
64+
decode' contents `shouldBe` Nothing
65+
66+
it "does not parse GitHub repo with leading slash" $ do
67+
let contents :: ByteString
68+
contents =
69+
S8.pack
70+
(unlines
71+
[ "github: /oink"
72+
, "commit: abc123"
73+
])
74+
decode' contents `shouldBe` Nothing
75+
76+
it "does not parse GitHub repo with trailing slash" $ do
77+
let contents :: ByteString
78+
contents =
79+
S8.pack
80+
(unlines
81+
[ "github: oink/"
82+
, "commit: abc123"
83+
])
84+
decode' contents `shouldBe` Nothing
85+
86+
it "does not parse GitHub repo with more than one slash" $ do
87+
let contents :: ByteString
88+
contents =
89+
S8.pack
90+
(unlines
91+
[ "github: oink/town/here"
92+
, "commit: abc123"
93+
])
94+
decode' contents `shouldBe` Nothing

0 commit comments

Comments
 (0)