@@ -292,8 +292,14 @@ data CommonOptions cSources cxxSources jsSources a = CommonOptions {
292292, commonOptionsBuildTools :: Maybe BuildTools
293293, commonOptionsSystemBuildTools :: Maybe SystemBuildTools
294294, commonOptionsVerbatim :: Maybe (List Verbatim )
295+ , commonOptionsGenerateFile :: Maybe (List GenerateFile )
295296} deriving (Functor , Generic )
296297
298+ data GenerateFile = GenerateFile {
299+ generateFileName :: FilePath
300+ , generateFileContents :: String
301+ } deriving (Generic , FromValue )
302+
297303type ParseCommonOptions = CommonOptions ParseCSources ParseCxxSources ParseJsSources
298304instance FromValue a => FromValue (ParseCommonOptions a )
299305
@@ -325,6 +331,7 @@ instance (Semigroup cSources, Semigroup cxxSources, Semigroup jsSources, Monoid
325331 , commonOptionsBuildTools = Nothing
326332 , commonOptionsSystemBuildTools = Nothing
327333 , commonOptionsVerbatim = Nothing
334+ , commonOptionsGenerateFile = Nothing
328335 }
329336 mappend = (<>)
330337
@@ -356,6 +363,7 @@ instance (Semigroup cSources, Semigroup cxxSources, Semigroup jsSources) => Semi
356363 , commonOptionsBuildTools = commonOptionsBuildTools a <> commonOptionsBuildTools b
357364 , commonOptionsSystemBuildTools = commonOptionsSystemBuildTools b <> commonOptionsSystemBuildTools a
358365 , commonOptionsVerbatim = commonOptionsVerbatim a <> commonOptionsVerbatim b
366+ , commonOptionsGenerateFile = commonOptionsGenerateFile a <> commonOptionsGenerateFile b
359367 }
360368
361369type ParseCSources = Maybe (List FilePath )
@@ -644,6 +652,7 @@ data DecodeResult = DecodeResult {
644652 decodeResultPackage :: Package
645653, decodeResultCabalVersion :: String
646654, decodeResultCabalFile :: FilePath
655+ , decodeResultGenerateFiles :: [(FilePath , String )]
647656, decodeResultWarnings :: [String ]
648657} deriving (Eq , Show )
649658
@@ -656,8 +665,16 @@ readPackageConfig (DecodeOptions programName file mUserDataDir readValue) = runE
656665 userDataDir <- liftIO $ maybe (getAppUserDataDirectory " hpack" ) return mUserDataDir
657666 toPackage programName userDataDir dir config
658667 where
659- addCabalFile :: ((Package , String ), [String ]) -> DecodeResult
660- addCabalFile ((pkg, cabalVersion), warnings) = DecodeResult pkg cabalVersion (takeDirectory_ file </> (packageName pkg ++ " .cabal" )) warnings
668+ addCabalFile :: ((Package , String , [GenerateFile ]), [String ]) -> DecodeResult
669+ addCabalFile ((pkg, cabalVersion, generateFiles), warnings) = DecodeResult {
670+ decodeResultPackage = pkg
671+ , decodeResultCabalVersion = cabalVersion
672+ , decodeResultCabalFile = addPackageDir (packageName pkg ++ " .cabal" )
673+ , decodeResultGenerateFiles = map (first addPackageDir . (generateFileName &&& generateFileContents)) $ nubOn generateFileName $ reverse generateFiles
674+ , decodeResultWarnings = warnings
675+ }
676+
677+ addPackageDir = (takeDirectory_ file </> )
661678
662679 takeDirectory_ :: FilePath -> FilePath
663680 takeDirectory_ p
@@ -997,11 +1014,14 @@ type ConfigWithDefaults = Product
9971014type CommonOptionsWithDefaults a = Product DefaultsConfig (CommonOptions ParseCSources ParseCxxSources ParseJsSources a )
9981015type WithCommonOptionsWithDefaults a = Product DefaultsConfig (WithCommonOptions ParseCSources ParseCxxSources ParseJsSources a )
9991016
1000- toPackage :: ProgramName -> FilePath -> FilePath -> ConfigWithDefaults -> Warnings (Errors IO ) (Package , String )
1017+ toPackage :: ProgramName -> FilePath -> FilePath -> ConfigWithDefaults -> Warnings (Errors IO ) (Package , String , [ GenerateFile ] )
10011018toPackage programName userDataDir dir =
10021019 expandDefaultsInConfig programName userDataDir dir
10031020 >=> traverseConfig (expandForeignSources dir)
1004- >=> toPackage_ dir
1021+ >=> runGenerateFilesWithWarnings . toPackage_ dir
1022+
1023+ runGenerateFilesWithWarnings :: Functor m => GenerateFilesWithWarnings m (a , b ) -> Warnings m (a , b , [GenerateFile ])
1024+ runGenerateFilesWithWarnings = mapWriterT (fmap $ \ ((a, b), c) -> ((a, b, lefts c), rights c))
10051025
10061026expandDefaultsInConfig
10071027 :: ProgramName
@@ -1090,19 +1110,19 @@ toExecutableMap name executables mExecutable = do
10901110
10911111type GlobalOptions = CommonOptions CSources CxxSources JsSources Empty
10921112
1093- toPackage_ :: MonadIO m => FilePath -> Product GlobalOptions (PackageConfig CSources CxxSources JsSources ) -> Warnings m (Package , String )
1113+ toPackage_ :: MonadIO m => FilePath -> Product GlobalOptions (PackageConfig CSources CxxSources JsSources ) -> GenerateFilesWithWarnings m (Package , String )
10941114toPackage_ dir (Product g PackageConfig {.. }) = do
1095- executableMap <- toExecutableMap packageName_ packageConfigExecutables packageConfigExecutable
1115+ executableMap <- liftWarnings $ toExecutableMap packageName_ packageConfigExecutables packageConfigExecutable
10961116 let
10971117 globalVerbatim = commonOptionsVerbatim g
10981118 globalOptions = g {commonOptionsVerbatim = Nothing }
10991119
11001120 executableNames = maybe [] Map. keys executableMap
11011121
1102- toSect :: (Monad m , Monoid a ) => WithCommonOptions CSources CxxSources JsSources a -> Warnings m (Section a )
1122+ toSect :: (Monad m , Monoid a ) => WithCommonOptions CSources CxxSources JsSources a -> GenerateFilesWithWarnings m (Section a )
11031123 toSect = toSection packageName_ executableNames . first ((mempty <$ globalOptions) <> )
11041124
1105- toSections :: (Monad m , Monoid a ) => Maybe (Map String (WithCommonOptions CSources CxxSources JsSources a )) -> Warnings m (Map String (Section a ))
1125+ toSections :: (Monad m , Monoid a ) => Maybe (Map String (WithCommonOptions CSources CxxSources JsSources a )) -> GenerateFilesWithWarnings m (Map String (Section a ))
11061126 toSections = maybe (return mempty ) (traverse toSect)
11071127
11081128 toLib = liftIO . toLibrary dir packageName_
@@ -1125,12 +1145,12 @@ toPackage_ dir (Product g PackageConfig{..}) = do
11251145 ++ concatMap sectionSourceDirs benchmarks
11261146 )
11271147
1128- extraSourceFiles <- expandGlobs " extra-source-files" dir (fromMaybeList packageConfigExtraSourceFiles)
1129- extraDocFiles <- expandGlobs " extra-doc-files" dir (fromMaybeList packageConfigExtraDocFiles)
1148+ extraSourceFiles <- liftWarnings $ expandGlobs " extra-source-files" dir (fromMaybeList packageConfigExtraSourceFiles)
1149+ extraDocFiles <- liftWarnings $ expandGlobs " extra-doc-files" dir (fromMaybeList packageConfigExtraDocFiles)
11301150
11311151 let dataBaseDir = maybe dir (dir </> ) packageConfigDataDir
11321152
1133- dataFiles <- expandGlobs " data-files" dataBaseDir (fromMaybeList packageConfigDataFiles)
1153+ dataFiles <- liftWarnings $ expandGlobs " data-files" dataBaseDir (fromMaybeList packageConfigDataFiles)
11341154
11351155 let
11361156 licenseFiles :: [String ]
@@ -1143,7 +1163,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do
11431163 input <- liftIO (tryReadFile (dir </> file))
11441164 case input >>= inferLicense of
11451165 Nothing -> do
1146- tell [" Inferring license from file " ++ file ++ " failed!" ]
1166+ liftWarnings $ tell [" Inferring license from file " ++ file ++ " failed!" ]
11471167 return Nothing
11481168 license -> return license
11491169 _ -> return Nothing
@@ -1182,8 +1202,8 @@ toPackage_ dir (Product g PackageConfig{..}) = do
11821202 , packageVerbatim = fromMaybeList globalVerbatim
11831203 }
11841204
1185- tell nameWarnings
1186- tell (formatMissingSourceDirs missingSourceDirs)
1205+ liftWarnings $ tell nameWarnings
1206+ liftWarnings $ tell (formatMissingSourceDirs missingSourceDirs)
11871207 return (determineCabalVersion inferredLicense pkg)
11881208 where
11891209 nameWarnings :: [String ]
@@ -1394,13 +1414,20 @@ expandMain = flatten . expand
13941414 , sectionConditionals = map (fmap flatten) sectionConditionals
13951415 }
13961416
1397- toSection :: Monad m => String -> [String ] -> WithCommonOptions CSources CxxSources JsSources a -> Warnings m (Section a )
1417+ type GenerateFilesWithWarnings = WriterT [Either GenerateFile String ]
1418+
1419+ liftWarnings :: Functor m => Warnings m a -> GenerateFilesWithWarnings m a
1420+ liftWarnings = mapWriterT (fmap (fmap $ map Right ))
1421+
1422+ toSection :: Monad m => String -> [String ] -> WithCommonOptions CSources CxxSources JsSources a -> GenerateFilesWithWarnings m (Section a )
13981423toSection packageName_ executableNames = go
13991424 where
1425+ go :: Monad m => WithCommonOptions CSources CxxSources JsSources a -> GenerateFilesWithWarnings m (Section a )
14001426 go (Product CommonOptions {.. } a) = do
14011427 (systemBuildTools, buildTools) <- maybe (return mempty ) toBuildTools commonOptionsBuildTools
14021428
14031429 conditionals <- mapM toConditional (fromMaybeList commonOptionsWhen)
1430+ tell (map Left $ fromMaybeList commonOptionsGenerateFile)
14041431 return Section {
14051432 sectionData = a
14061433 , sectionSourceDirs = nub $ fromMaybeList commonOptionsSourceDirs
@@ -1430,15 +1457,15 @@ toSection packageName_ executableNames = go
14301457 , sectionSystemBuildTools = systemBuildTools <> fromMaybe mempty commonOptionsSystemBuildTools
14311458 , sectionVerbatim = fromMaybeList commonOptionsVerbatim
14321459 }
1433- toBuildTools :: Monad m => BuildTools -> Warnings m (SystemBuildTools , Map BuildTool DependencyVersion )
1434- toBuildTools = fmap (mkSystemBuildTools &&& mkBuildTools) . mapM (toBuildTool packageName_ executableNames). unBuildTools
1460+ toBuildTools :: Monad m => BuildTools -> GenerateFilesWithWarnings m (SystemBuildTools , Map BuildTool DependencyVersion )
1461+ toBuildTools = fmap (mkSystemBuildTools &&& mkBuildTools) . mapM (liftWarnings . toBuildTool packageName_ executableNames). unBuildTools
14351462 where
14361463 mkSystemBuildTools :: [Either (String , VersionConstraint ) b ] -> SystemBuildTools
14371464 mkSystemBuildTools = SystemBuildTools . Map. fromList . lefts
14381465
14391466 mkBuildTools = Map. fromList . rights
14401467
1441- toConditional :: Monad m => ConditionalSection CSources CxxSources JsSources a -> Warnings m (Conditional (Section a ))
1468+ toConditional :: Monad m => ConditionalSection CSources CxxSources JsSources a -> GenerateFilesWithWarnings m (Conditional (Section a ))
14421469 toConditional x = case x of
14431470 ThenElseConditional (Product (ThenElse then_ else_) c) -> conditional c <$> (go then_) <*> (Just <$> go else_)
14441471 FlatConditional (Product sect c) -> conditional c <$> (go sect) <*> pure Nothing
0 commit comments