Skip to content

Commit 2b685f3

Browse files
authored
Merge pull request #142 from commercialhaskell/fix-stack-5305
Re Stack #5305 Add `parseRawPackageLocationImmutables`
2 parents b9f1892 + d0d05e2 commit 2b685f3

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog for pantry
22

3+
## v0.10.1
4+
5+
* Expose new `parseRawPackageLocationImmutables`.
6+
* Add errors S-925 (`RawPackageLocationImmutableParseFail`) and S-775
7+
(`RawPackageLocationImmutableParseWarnings`).
8+
39
## v0.10.0
410

511
* Name of tar file of local cache of package index is not hard coded.

app/test-pretty-exceptions/Main.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Main
88
( main
99
) where
1010

11+
import Data.Aeson.WarningParser ( JSONWarning (..) )
1112
import qualified Data.Conduit.Tar as Tar
1213
import Data.Maybe ( fromJust )
1314
import qualified Data.Text as T
@@ -122,6 +123,8 @@ mainInTerminal terminalWidth Options{..} = do
122123
examples :: [PantryException]
123124
examples = concat
124125
[ [ PackageIdentifierRevisionParseFail hackageMsg ]
126+
, [ RawPackageLocationImmutableParseFail "example text" someExceptionExample ]
127+
, [ RawPackageLocationImmutableParseWarnings "example text" jsonWarningsExample]
125128
, [ InvalidCabalFile loc version pErrorExamples pWarningExamples
126129
| loc <- map Left rawPackageLocationImmutableExamples <> [Right pathAbsFileExample]
127130
, version <- [Nothing, Just versionExample]
@@ -416,3 +419,17 @@ rawSnapNameExample = "<raw-snapshot-name>"
416419

417420
hpackCommandExample :: FilePath
418421
hpackCommandExample = "<path-to-hpack>/hpack"
422+
423+
jsonWarningsExample :: [JSONWarning]
424+
jsonWarningsExample =
425+
[ jsonUnrecognizedFieldsExample
426+
, jsonGeneralWarningExample
427+
]
428+
429+
jsonUnrecognizedFieldsExample :: JSONWarning
430+
jsonUnrecognizedFieldsExample = JSONUnrecognizedFields
431+
"UnresolvedPackageLocationImmutable.UPLIHackage"
432+
["field1", "field2", "field3"]
433+
434+
jsonGeneralWarningExample :: JSONWarning
435+
jsonGeneralWarningExample = JSONGeneralWarning "A general JSON warning."

int/Pantry/Types.hs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module Pantry.Types
8989
, toCabalStringMap
9090
, unCabalStringMap
9191
, parsePackageIdentifierRevision
92+
, parseRawPackageLocationImmutables
9293
, Mismatch (..)
9394
, PantryException (..)
9495
, FuzzyResults (..)
@@ -144,16 +145,18 @@ import Data.Aeson.Types
144145
, toJSONKeyText, withObject, withText
145146
)
146147
import Data.Aeson.WarningParser
147-
( WarningParser, WithJSONWarnings, (..:), (..:?), (..!=)
148-
, (.:), (...:?), jsonSubWarnings, jsonSubWarningsT
149-
, noJSONWarnings, tellJSONField, withObjectWarnings
148+
( JSONWarning (..), WarningParser, WithJSONWarnings (..)
149+
, (..:), (..:?), (..!=), (.:), (...:?), jsonSubWarnings
150+
, jsonSubWarningsT, noJSONWarnings, tellJSONField
151+
, withObjectWarnings
150152
)
151153
import Data.ByteString.Builder
152154
( byteString, toLazyByteString, wordDec )
153155
import qualified Data.Conduit.Tar as Tar
154156
import qualified Data.List.NonEmpty as NE
155157
import qualified Data.Map.Strict as Map ( mapKeysMonotonic )
156158
import Data.Text.Read ( decimal )
159+
import Data.Yaml ( decodeEither' )
157160
import Distribution.CabalSpecVersion ( cabalSpecLatest )
158161
#if MIN_VERSION_Cabal(3,4,0)
159162
import Distribution.CabalSpecVersion ( cabalSpecToVersionDigits )
@@ -482,6 +485,21 @@ instance Pretty RawPackageLocationImmutable where
482485
]
483486
]
484487

488+
-- | Parse, 'Unresolved', one or more 'RawPackageLocationImmutable' from a valid
489+
-- YAML value. Alternatively, yields an exception if the given text cannot be
490+
-- decoded as a YAML value or it is decoded but with warnings.
491+
--
492+
-- @since 0.10.1
493+
parseRawPackageLocationImmutables ::
494+
Text
495+
-- ^ A YAML value.
496+
-> Either PantryException (Unresolved (NonEmpty RawPackageLocationImmutable))
497+
parseRawPackageLocationImmutables t = case decodeEither' $ encodeUtf8 t of
498+
Left err -> Left $ RawPackageLocationImmutableParseFail t (SomeException err)
499+
Right (WithJSONWarnings unresolved warnings) -> case warnings of
500+
[] -> Right unresolved
501+
_ -> Left $ RawPackageLocationImmutableParseWarnings t warnings
502+
485503
-- | Location for remote packages or archives assumed to be immutable.
486504
--
487505
-- @since 0.1.0.0
@@ -1069,6 +1087,8 @@ data Mismatch a = Mismatch
10691087
-- @since 0.1.0.0
10701088
data PantryException
10711089
= PackageIdentifierRevisionParseFail !Text
1090+
| RawPackageLocationImmutableParseFail !Text !SomeException
1091+
| RawPackageLocationImmutableParseWarnings !Text ![JSONWarning]
10721092
| InvalidCabalFile
10731093
!(Either RawPackageLocationImmutable (Path Abs File))
10741094
!(Maybe Version)
@@ -1153,6 +1173,24 @@ instance Display PantryException where
11531173
"Error: [S-360]\n"
11541174
<> "Invalid package identifier (with optional revision): "
11551175
<> display text
1176+
display (RawPackageLocationImmutableParseFail text err) =
1177+
"Error: [S-925]\n"
1178+
<> "Invalid raw immutable package location: "
1179+
<> display text
1180+
<> "\n\n"
1181+
<> "The error encountered was:\n\n"
1182+
<> fromString (displayException err)
1183+
display (RawPackageLocationImmutableParseWarnings text warnings) =
1184+
"Error: [S-775]\n"
1185+
<> "Invalid raw immutable package location: "
1186+
<> display text
1187+
<> "\n\n"
1188+
<> "The warnings encountered were:\n\n"
1189+
<> fold
1190+
( intersperse
1191+
"\n"
1192+
(map (\warning -> "- " <> display warning) warnings)
1193+
)
11561194
display (InvalidCabalFile loc mversion errs warnings) =
11571195
"Error: [S-242]\n"
11581196
<> "Unable to parse cabal file from package "
@@ -1509,6 +1547,26 @@ instance Pretty PantryException where
15091547
[ flow "Invalid package identifier (with optional revision):"
15101548
, fromString $ T.unpack text
15111549
]
1550+
pretty (RawPackageLocationImmutableParseFail text err) =
1551+
"[S-925]"
1552+
<> line
1553+
<> fillSep
1554+
[ flow "Invalid raw immutable package location:"
1555+
, fromString $ T.unpack text <> "."
1556+
, flow "The error encountered was:"
1557+
]
1558+
<> blankLine
1559+
<> string (displayException err)
1560+
pretty (RawPackageLocationImmutableParseWarnings text warnings) =
1561+
"[S-775]"
1562+
<> line
1563+
<> fillSep
1564+
[ flow "Invalid raw immutable package location:"
1565+
, fromString $ T.unpack text <> "."
1566+
, flow "The warnings encountered were:"
1567+
]
1568+
<> line
1569+
<> bulletedList (map (fromString . show) warnings)
15121570
pretty (InvalidCabalFile loc mversion errs warnings) =
15131571
"[S-242]"
15141572
<> line

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pantry
2-
version: 0.10.0
2+
version: 0.10.1
33
synopsis: Content addressable Haskell package management
44
description: Please see the README on GitHub at <https://github.com/commercialhaskell/pantry#readme>
55
category: Development

pantry.cabal

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Pantry.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ module Pantry
141141
, parseSnapName
142142
, parseRawSnapshotLocation
143143
, parsePackageIdentifierRevision
144+
, parseRawPackageLocationImmutables
144145
, parseHackageText
145146

146147
-- ** Cabal values
@@ -284,13 +285,13 @@ import Pantry.Types as P
284285
, packageIdentifierString, packageNameString, parseFlagName
285286
, parseHackageText, parsePackageIdentifier
286287
, parsePackageIdentifierRevision, parsePackageName
287-
, parsePackageNameThrowing, parseRawSnapshotLocation
288-
, parseSnapName, parseTreeM, parseVersion
289-
, parseVersionThrowing, parseWantedCompiler, pirForHash
290-
, resolvePaths, snapshotLocation, toCabalStringMap, toRawPL
291-
, toRawPLI, toRawPM, toRawSL, toRawSnapshotLayer
292-
, unCabalStringMap, unSafeFilePath, versionString
293-
, warnMissingCabalFile
288+
, parsePackageNameThrowing, parseRawPackageLocationImmutables
289+
, parseRawSnapshotLocation, parseSnapName, parseTreeM
290+
, parseVersion, parseVersionThrowing, parseWantedCompiler
291+
, pirForHash, resolvePaths, snapshotLocation
292+
, toCabalStringMap, toRawPL, toRawPLI, toRawPM, toRawSL
293+
, toRawSnapshotLayer, unCabalStringMap, unSafeFilePath
294+
, versionString, warnMissingCabalFile
294295
)
295296
import Path
296297
( Abs, Dir, File, Path, (</>), filename, parent, parseAbsDir

0 commit comments

Comments
 (0)