@@ -99,6 +99,8 @@ import Data.Text.Encoding (decodeUtf8)
99
99
import Data.Scientific (Scientific )
100
100
import System.Directory
101
101
import System.FilePath
102
+ import Control.Monad.State (MonadState , StateT , evalStateT )
103
+ import qualified Control.Monad.State as State
102
104
import Control.Monad.Writer (MonadWriter , WriterT , runWriterT , tell )
103
105
import Control.Monad.Except
104
106
import Data.Version (Version , makeVersion , showVersion )
@@ -639,7 +641,7 @@ liftIOEither action = liftIO action >>= liftEither
639
641
640
642
type FormatYamlParseError = FilePath -> Yaml. ParseException -> String
641
643
642
- decodeYaml :: (FromValue a , MonadIO m , Warnings m , Errors m ) => FormatYamlParseError -> FilePath -> m a
644
+ decodeYaml :: (FromValue a , MonadIO m , Warnings m , Errors m , State m ) => FormatYamlParseError -> FilePath -> m a
643
645
decodeYaml formatYamlParseError file = do
644
646
(warnings, a) <- liftIOEither $ first (ParseError . formatYamlParseError file) <$> Yaml. decodeYamlWithParseError file
645
647
tell warnings
@@ -668,11 +670,12 @@ readPackageConfig options = first (formatHpackError $ decodeOptionsProgramName o
668
670
669
671
type Errors = MonadError HpackError
670
672
type Warnings = MonadWriter [String ]
673
+ type State = MonadState SpecVersion
671
674
672
- type ConfigM m = WriterT [String ] (ExceptT HpackError m )
675
+ type ConfigM m = StateT SpecVersion ( WriterT [String ] (ExceptT HpackError m ) )
673
676
674
- runConfigM :: ConfigM m a -> m (Either HpackError (a , [String ]))
675
- runConfigM = runExceptT . runWriterT
677
+ runConfigM :: Monad m => ConfigM m a -> m (Either HpackError (a , [String ]))
678
+ runConfigM = runExceptT . runWriterT . ( `evalStateT` NoSpecVersion )
676
679
677
680
readPackageConfigWithError :: DecodeOptions -> IO (Either HpackError DecodeResult )
678
681
readPackageConfigWithError (DecodeOptions _ file mUserDataDir readValue formatYamlParseError) = fmap (fmap addCabalFile) . runConfigM $ do
@@ -902,15 +905,16 @@ determineCabalVersion inferredLicense pkg@Package{..} = (
902
905
sectionAll :: Monoid b => (Section a -> b ) -> Section a -> b
903
906
sectionAll f sect = f sect <> foldMap (foldMap $ sectionAll f) (sectionConditionals sect)
904
907
905
- decodeValue :: (FromValue a , Warnings m , Errors m ) => FilePath -> Value -> m a
908
+ decodeValue :: (FromValue a , State m , Warnings m , Errors m ) => FilePath -> Value -> m a
906
909
decodeValue file value = do
907
910
(r, unknown, deprecated) <- liftEither $ first (DecodeValueError file) (Config. decodeValue value)
908
911
case r of
909
912
UnsupportedSpecVersion v -> do
910
913
throwError $ HpackVersionNotSupported file v Hpack. version
911
- SupportedSpecVersion a -> do
914
+ SupportedSpecVersion v a -> do
912
915
tell (map formatUnknownField unknown)
913
916
tell (map formatDeprecatedField deprecated)
917
+ State. modify $ max v
914
918
return a
915
919
where
916
920
prefix :: String
@@ -922,14 +926,20 @@ decodeValue file value = do
922
926
formatDeprecatedField :: (String , String ) -> String
923
927
formatDeprecatedField (name, substitute) = prefix <> name <> " is deprecated, use " <> substitute <> " instead"
924
928
925
- data CheckSpecVersion a = SupportedSpecVersion a | UnsupportedSpecVersion Version
929
+ data SpecVersion = NoSpecVersion | SpecVersion Version
930
+ deriving (Eq , Show , Ord )
931
+
932
+ toSpecVersion :: Maybe ParseSpecVersion -> SpecVersion
933
+ toSpecVersion = maybe NoSpecVersion (SpecVersion . unParseSpecVersion)
934
+
935
+ data CheckSpecVersion a = SupportedSpecVersion SpecVersion a | UnsupportedSpecVersion Version
926
936
927
937
instance FromValue a => FromValue (CheckSpecVersion a ) where
928
938
fromValue = withObject $ \ o -> o .:? " spec-version" >>= \ case
929
939
Just (ParseSpecVersion v) | Hpack. version < v -> return $ UnsupportedSpecVersion v
930
- _ -> SupportedSpecVersion <$> fromValue (Object o)
940
+ v -> SupportedSpecVersion (toSpecVersion v) <$> fromValue (Object o)
931
941
932
- newtype ParseSpecVersion = ParseSpecVersion Version
942
+ newtype ParseSpecVersion = ParseSpecVersion { unParseSpecVersion :: Version }
933
943
934
944
instance FromValue ParseSpecVersion where
935
945
fromValue value = do
@@ -1079,7 +1089,7 @@ toPackage formatYamlParseError userDataDir dir =
1079
1089
setLanguage = (mempty { commonOptionsLanguage = Alias . Last $ Just (Just language) } <> )
1080
1090
1081
1091
expandDefaultsInConfig
1082
- :: (MonadIO m , Warnings m , Errors m ) =>
1092
+ :: (MonadIO m , Warnings m , Errors m , State m ) =>
1083
1093
FormatYamlParseError
1084
1094
-> FilePath
1085
1095
-> FilePath
@@ -1088,7 +1098,7 @@ expandDefaultsInConfig
1088
1098
expandDefaultsInConfig formatYamlParseError userDataDir dir = bitraverse (expandGlobalDefaults formatYamlParseError userDataDir dir) (expandSectionDefaults formatYamlParseError userDataDir dir)
1089
1099
1090
1100
expandGlobalDefaults
1091
- :: (MonadIO m , Warnings m , Errors m ) =>
1101
+ :: (MonadIO m , Warnings m , Errors m , State m ) =>
1092
1102
FormatYamlParseError
1093
1103
-> FilePath
1094
1104
-> FilePath
@@ -1098,7 +1108,7 @@ expandGlobalDefaults formatYamlParseError userDataDir dir = do
1098
1108
fmap (`Product ` Empty ) >>> expandDefaults formatYamlParseError userDataDir dir >=> \ (Product c Empty ) -> return c
1099
1109
1100
1110
expandSectionDefaults
1101
- :: (MonadIO m , Warnings m , Errors m ) =>
1111
+ :: (MonadIO m , Warnings m , Errors m , State m ) =>
1102
1112
FormatYamlParseError
1103
1113
-> FilePath
1104
1114
-> FilePath
@@ -1121,7 +1131,7 @@ expandSectionDefaults formatYamlParseError userDataDir dir p@PackageConfig{..} =
1121
1131
}
1122
1132
1123
1133
expandDefaults
1124
- :: forall a m . (MonadIO m , Warnings m , Errors m ) =>
1134
+ :: forall a m . (MonadIO m , Warnings m , Errors m , State m ) =>
1125
1135
(FromValue a , Monoid a )
1126
1136
=> FormatYamlParseError
1127
1137
-> FilePath
@@ -1169,7 +1179,7 @@ toExecutableMap name executables mExecutable = do
1169
1179
1170
1180
type GlobalOptions = CommonOptions CSources CxxSources JsSources Empty
1171
1181
1172
- toPackage_ :: (MonadIO m , Warnings m ) => FilePath -> Product GlobalOptions (PackageConfig CSources CxxSources JsSources ) -> m (Package , String )
1182
+ toPackage_ :: (MonadIO m , Warnings m , State m ) => FilePath -> Product GlobalOptions (PackageConfig CSources CxxSources JsSources ) -> m (Package , String )
1173
1183
toPackage_ dir (Product g PackageConfig {.. }) = do
1174
1184
executableMap <- toExecutableMap packageName_ packageConfigExecutables packageConfigExecutable
1175
1185
let
@@ -1377,7 +1387,7 @@ removeConditionalsThatAreAlwaysFalse sect = sect {
1377
1387
where
1378
1388
p = (/= CondBool False ) . conditionalCondition
1379
1389
1380
- inferModules :: MonadIO m =>
1390
+ inferModules :: ( MonadIO m , State m ) =>
1381
1391
FilePath
1382
1392
-> String
1383
1393
-> (a -> [Module ])
@@ -1386,10 +1396,19 @@ inferModules :: MonadIO m =>
1386
1396
-> ([Module ] -> a -> b )
1387
1397
-> Section a
1388
1398
-> m (Section b )
1389
- inferModules dir packageName_ getMentionedModules getInferredModules fromData fromConditionals = fmap removeConditionalsThatAreAlwaysFalse . traverseSectionAndConditionals
1390
- (fromConfigSection fromData [pathsModuleFromPackageName packageName_])
1391
- (fromConfigSection (\ [] -> fromConditionals) [] )
1392
- []
1399
+ inferModules dir packageName_ getMentionedModules getInferredModules fromData fromConditionals sect_ = do
1400
+ specVersion <- State. get
1401
+ let
1402
+ pathsModule :: [Module ]
1403
+ pathsModule = case specVersion of
1404
+ SpecVersion v | v >= makeVersion [0 ,36 ,0 ] -> []
1405
+ _ -> [pathsModuleFromPackageName packageName_]
1406
+
1407
+ removeConditionalsThatAreAlwaysFalse <$> traverseSectionAndConditionals
1408
+ (fromConfigSection fromData pathsModule)
1409
+ (fromConfigSection (\ [] -> fromConditionals) [] )
1410
+ []
1411
+ sect_
1393
1412
where
1394
1413
fromConfigSection fromConfig pathsModule_ outerModules sect@ Section {sectionData = conf} = do
1395
1414
modules <- liftIO $ listModules dir sect
@@ -1400,7 +1419,7 @@ inferModules dir packageName_ getMentionedModules getInferredModules fromData fr
1400
1419
r = fromConfig pathsModule inferableModules conf
1401
1420
return (outerModules ++ getInferredModules r, r)
1402
1421
1403
- toLibrary :: MonadIO m => FilePath -> String -> Section LibrarySection -> m (Section Library )
1422
+ toLibrary :: ( MonadIO m , State m ) => FilePath -> String -> Section LibrarySection -> m (Section Library )
1404
1423
toLibrary dir name =
1405
1424
inferModules dir name getMentionedLibraryModules getLibraryModules fromLibrarySectionTopLevel fromLibrarySectionInConditional
1406
1425
where
@@ -1444,7 +1463,7 @@ getMentionedExecutableModules :: ExecutableSection -> [Module]
1444
1463
getMentionedExecutableModules (ExecutableSection (Alias (Last main)) otherModules generatedModules)=
1445
1464
maybe id (:) (toModule . Path. fromFilePath <$> main) $ fromMaybeList (otherModules <> generatedModules)
1446
1465
1447
- toExecutable :: MonadIO m => FilePath -> String -> Section ExecutableSection -> m (Section Executable )
1466
+ toExecutable :: ( MonadIO m , State m ) => FilePath -> String -> Section ExecutableSection -> m (Section Executable )
1448
1467
toExecutable dir packageName_ =
1449
1468
inferModules dir packageName_ getMentionedExecutableModules getExecutableModules fromExecutableSection (fromExecutableSection [] )
1450
1469
. expandMain
0 commit comments