Skip to content

Commit

Permalink
Change field to packageLibraries, sol#483.
Browse files Browse the repository at this point in the history
  • Loading branch information
philderbeast committed Mar 16, 2022
1 parent be55317 commit 5a16f09
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 46 deletions.
39 changes: 17 additions & 22 deletions src/Hpack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ package name version = Package {
, packageDataDir = Nothing
, packageSourceRepository = Nothing
, packageCustomSetup = Nothing
, packageLibrary = Nothing
, packageInternalLibraries = mempty
, packageLibraries = mempty
, packageExecutables = mempty
, packageTests = mempty
, packageBenchmarks = mempty
Expand Down Expand Up @@ -180,7 +179,7 @@ packageDependencies Package{..} = nub . sortBy (comparing (lexicographically . f
(concatMap deps packageExecutables)
++ (concatMap deps packageTests)
++ (concatMap deps packageBenchmarks)
++ maybe [] deps packageLibrary
++ (concatMap deps packageLibraries)
where
deps xs = [(name, info) | (name, info) <- (Map.toList . unDependencies . sectionDependencies) xs]

Expand Down Expand Up @@ -567,7 +566,7 @@ data PackageConfig_ library executable = PackageConfig {
, packageConfigGit :: Maybe String
, packageConfigCustomSetup :: Maybe CustomSetupSection
, packageConfigLibrary :: Maybe library
, packageConfigInternalLibraries :: Maybe (Map String library)
, packageConfigLibraries :: Maybe (Map String library)
, packageConfigExecutable :: Maybe executable
, packageConfigExecutables :: Maybe (Map String executable)
, packageConfigTests :: Maybe (Map String executable)
Expand Down Expand Up @@ -595,14 +594,14 @@ data DefaultsConfig = DefaultsConfig {
traversePackageConfig :: Traversal PackageConfig
traversePackageConfig t p@PackageConfig{..} = do
library <- traverse (traverseWithCommonOptions t) packageConfigLibrary
internalLibraries <- traverseNamedConfigs t packageConfigInternalLibraries
libraries <- traverseNamedConfigs t packageConfigLibraries
executable <- traverse (traverseWithCommonOptions t) packageConfigExecutable
executables <- traverseNamedConfigs t packageConfigExecutables
tests <- traverseNamedConfigs t packageConfigTests
benchmarks <- traverseNamedConfigs t packageConfigBenchmarks
return p {
packageConfigLibrary = library
, packageConfigInternalLibraries = internalLibraries
, packageConfigLibraries = libraries
, packageConfigExecutable = executable
, packageConfigExecutables = executables
, packageConfigTests = tests
Expand Down Expand Up @@ -680,8 +679,7 @@ addPathsModuleToGeneratedModules :: Package -> Version -> Package
addPathsModuleToGeneratedModules pkg cabalVersion
| cabalVersion < makeVersion [2] = pkg
| otherwise = pkg {
packageLibrary = fmap mapLibrary <$> packageLibrary pkg
, packageInternalLibraries = fmap mapLibrary <$> packageInternalLibraries pkg
packageLibraries = fmap mapLibrary <$> packageLibraries pkg
, packageExecutables = fmap mapExecutable <$> packageExecutables pkg
, packageTests = fmap mapExecutable <$> packageTests pkg
, packageBenchmarks = fmap mapExecutable <$> packageBenchmarks pkg
Expand Down Expand Up @@ -752,8 +750,7 @@ determineCabalVersion inferredLicense pkg@Package{..} = (

version = fromMaybe (makeVersion [1,12]) $ maximum [
packageCabalVersion
, packageLibrary >>= libraryCabalVersion
, internalLibsCabalVersion packageInternalLibraries
, librariesCabalVersion packageLibraries
, executablesCabalVersion packageExecutables
, executablesCabalVersion packageTests
, executablesCabalVersion packageBenchmarks
Expand All @@ -778,12 +775,12 @@ determineCabalVersion inferredLicense pkg@Package{..} = (
where
has field = any (not . null . field) sect

internalLibsCabalVersion :: Map String (Section Library) -> Maybe Version
internalLibsCabalVersion internalLibraries
| Map.null internalLibraries = Nothing
librariesCabalVersion :: Map String (Section Library) -> Maybe Version
librariesCabalVersion libraries
| Map.null libraries = Nothing
| otherwise = foldr max (Just $ makeVersion [2,0]) versions
where
versions = libraryCabalVersion <$> Map.elems internalLibraries
versions = libraryCabalVersion <$> Map.elems libraries

executablesCabalVersion :: Map String (Section Executable) -> Maybe Version
executablesCabalVersion = foldr max Nothing . map executableCabalVersion . Map.elems
Expand Down Expand Up @@ -930,8 +927,7 @@ data Package = Package {
, packageDataDir :: Maybe FilePath
, packageSourceRepository :: Maybe SourceRepository
, packageCustomSetup :: Maybe CustomSetup
, packageLibrary :: Maybe (Section Library)
, packageInternalLibraries :: Map String (Section Library)
, packageLibraries :: Map String (Section Library)
, packageExecutables :: Map String (Section Executable)
, packageTests :: Map String (Section Executable)
, packageBenchmarks :: Map String (Section Executable)
Expand Down Expand Up @@ -1062,14 +1058,14 @@ expandSectionDefaults
-> Warnings (Errors IO) (PackageConfig ParseCSources ParseCxxSources ParseJsSources)
expandSectionDefaults programName userDataDir dir p@PackageConfig{..} = do
library <- traverse (expandDefaults programName userDataDir dir) packageConfigLibrary
internalLibraries <- traverse (traverse (expandDefaults programName userDataDir dir)) packageConfigInternalLibraries
libraries <- traverse (traverse (expandDefaults programName userDataDir dir)) packageConfigLibraries
executable <- traverse (expandDefaults programName userDataDir dir) packageConfigExecutable
executables <- traverse (traverse (expandDefaults programName userDataDir dir)) packageConfigExecutables
tests <- traverse (traverse (expandDefaults programName userDataDir dir)) packageConfigTests
benchmarks <- traverse (traverse (expandDefaults programName userDataDir dir)) packageConfigBenchmarks
return p{
packageConfigLibrary = library
, packageConfigInternalLibraries = internalLibraries
, packageConfigLibraries = libraries
, packageConfigExecutable = executable
, packageConfigExecutables = executables
, packageConfigTests = tests
Expand Down Expand Up @@ -1143,7 +1139,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do
toExecutables = toSections >=> traverse (liftIO . toExecutable dir packageName_)

mLibrary <- traverse (toSect >=> toLib) packageConfigLibrary
internalLibraries <- toSections packageConfigInternalLibraries >>= traverse toLib
libraries <- toSections packageConfigLibraries >>= traverse toLib

executables <- toExecutables executableMap
tests <- toExecutables packageConfigTests
Expand All @@ -1153,7 +1149,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do

missingSourceDirs <- liftIO $ nub . sort <$> filterM (fmap not <$> doesDirectoryExist . (dir </>)) (
maybe [] sectionSourceDirs mLibrary
++ concatMap sectionSourceDirs internalLibraries
++ concatMap sectionSourceDirs libraries
++ concatMap sectionSourceDirs executables
++ concatMap sectionSourceDirs tests
++ concatMap sectionSourceDirs benchmarks
Expand Down Expand Up @@ -1208,8 +1204,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do
, packageDataDir = packageConfigDataDir
, packageSourceRepository = sourceRepository
, packageCustomSetup = mCustomSetup
, packageLibrary = mLibrary
, packageInternalLibraries = internalLibraries
, packageLibraries = libraries
, packageExecutables = executables
, packageTests = tests
, packageBenchmarks = benchmarks
Expand Down
17 changes: 5 additions & 12 deletions src/Hpack/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ renderPackageWith settings headerFieldsAlignment existingFieldOrder sectionsFiel
customSetup :: [Element]
customSetup = maybe [] (return . renderCustomSetup) packageCustomSetup

library :: [Element]
library = maybe [] (return . renderLibrary) packageLibrary

stanzas :: [Element]
stanzas = concat [
sourceRepository
, customSetup
, map renderFlag packageFlags
, library
, renderInternalLibraries packageInternalLibraries
, renderLibraries packageLibraries
, renderExecutables packageExecutables
, renderTests packageTests
, renderBenchmarks packageBenchmarks
Expand Down Expand Up @@ -154,11 +150,11 @@ renderFlag Flag {..} = Stanza ("flag " ++ flagName) $ description ++ [
where
description = maybe [] (return . Field "description" . Literal) flagDescription

renderInternalLibraries :: Map String (Section Library) -> [Element]
renderInternalLibraries = map renderInternalLibrary . Map.toList
renderLibraries :: Map String (Section Library) -> [Element]
renderLibraries = map renderNamedLibrary . Map.toList

renderInternalLibrary :: (String, Section Library) -> Element
renderInternalLibrary (name, sect) =
renderNamedLibrary :: (String, Section Library) -> Element
renderNamedLibrary (name, sect) =
Stanza ("library " ++ name) (renderLibrarySection sect)

renderExecutables :: Map String (Section Executable) -> [Element]
Expand Down Expand Up @@ -198,9 +194,6 @@ renderCustomSetup :: CustomSetup -> Element
renderCustomSetup CustomSetup{..} =
Stanza "custom-setup" $ renderDependencies "setup-depends" customSetupDependencies

renderLibrary :: Section Library -> Element
renderLibrary sect = Stanza "library" $ renderLibrarySection sect

renderLibrarySection :: Section Library -> [Element]
renderLibrarySection = renderSection renderLibraryFields [] [defaultLanguage]

Expand Down
20 changes: 10 additions & 10 deletions test/Hpack/ConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ spec = do
cpp-options: -DTEST
|]
(`shouldBe` package {
packageLibrary = Just (section library) {sectionCppOptions = ["-DFOO", "-DLIB"]}
packageLibraries = [("", (section library) {sectionCppOptions = ["-DFOO", "-DLIB"]})]
, packageExecutables = Map.fromList [("foo", (section $ executable "Main.hs") {sectionCppOptions = ["-DFOO", "-DFOO"]})]
, packageTests = Map.fromList [("spec", (section $ executable "Spec.hs") {sectionCppOptions = ["-DFOO", "-DTEST"]})]
}
Expand All @@ -347,7 +347,7 @@ spec = do
cc-options: -O0
|]
(`shouldBe` package {
packageLibrary = Just (section library) {sectionCcOptions = ["-Wall", "-fLIB"]}
packageLibraries = [("", (section library) {sectionCcOptions = ["-Wall", "-fLIB"]})]
, packageExecutables = Map.fromList [("foo", (section $ executable "Main.hs") {sectionCcOptions = ["-Wall", "-O2"]})]
, packageTests = Map.fromList [("spec", (section $ executable "Spec.hs") {sectionCcOptions = ["-Wall", "-O0"]})]
}
Expand All @@ -371,7 +371,7 @@ spec = do
ghcjs-options: -ghcjs3
|]
(`shouldBe` package {
packageLibrary = Just (section library) {sectionGhcjsOptions = ["-dedupe", "-ghcjs1"]}
packageLibraries = [("", (section library) {sectionGhcjsOptions = ["-dedupe", "-ghcjs1"]})]
, packageExecutables = Map.fromList [("foo", (section $ executable "Main.hs") {sectionGhcjsOptions = ["-dedupe", "-ghcjs2"]})]
, packageTests = Map.fromList [("spec", (section $ executable "Spec.hs") {sectionGhcjsOptions = ["-dedupe", "-ghcjs3"]})]
}
Expand All @@ -383,7 +383,7 @@ spec = do
ld-options: -static
|]
(`shouldBe` package {
packageLibrary = Just (section library) {sectionLdOptions = ["-static"]}
packageLibraries = [("", (section library) {sectionLdOptions = ["-static"]})]
}
)

Expand All @@ -398,7 +398,7 @@ spec = do
main: Main.hs
|]
(`shouldBe` package {
packageLibrary = Just (section library) {sectionBuildable = Just True}
packageLibraries = [("", (section library) {sectionBuildable = Just True})]
, packageExecutables = Map.fromList [("foo", (section $ executable "Main.hs") {sectionBuildable = Just False})]
}
)
Expand All @@ -421,7 +421,7 @@ spec = do
- foo
- bar
|]
(packageLibrary >>> (`shouldBe` Just (section library) {sectionSourceDirs = ["foo", "bar"]}))
(packageLibraries >>> (`shouldBe` [("", (section library) {sectionSourceDirs = ["foo", "bar"]})]))

it "accepts default-extensions" $ do
withPackageConfig_ [i|
Expand All @@ -430,7 +430,7 @@ spec = do
- Foo
- Bar
|]
(packageLibrary >>> (`shouldBe` Just (section library) {sectionDefaultExtensions = ["Foo", "Bar"]}))
(packageLibraries >>> (`shouldBe` [("", (section library) {sectionDefaultExtensions = ["Foo", "Bar"]})]))

it "accepts global default-extensions" $ do
withPackageConfig_ [i|
Expand All @@ -439,7 +439,7 @@ spec = do
- Bar
library: {}
|]
(packageLibrary >>> (`shouldBe` Just (section library) {sectionDefaultExtensions = ["Foo", "Bar"]}))
(packageLibraries >>> (`shouldBe` [("", (section library) {sectionDefaultExtensions = ["Foo", "Bar"]})]))

it "accepts global source-dirs" $ do
withPackageConfig_ [i|
Expand All @@ -448,14 +448,14 @@ spec = do
- bar
library: {}
|]
(packageLibrary >>> (`shouldBe` Just (section library) {sectionSourceDirs = ["foo", "bar"]}))
(packageLibraries >>> (`shouldBe` [("", (section library) {sectionSourceDirs = ["foo", "bar"]})]))

it "allows to specify exposed" $ do
withPackageConfig_ [i|
library:
exposed: no
|]
(packageLibrary >>> (`shouldBe` Just (section library{libraryExposed = Just False})))
(packageLibraries >>> (`shouldBe` [("", section library{libraryExposed = Just False})]))

context "when reading executable section" $ do
it "reads executables section" $ do
Expand Down
4 changes: 2 additions & 2 deletions test/Hpack/RenderSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ spec = do
]

it "includes buildable" $ do
renderPackage_ package {packageLibrary = Just (section library){sectionBuildable = Just False}} `shouldBe` unlines [
renderPackage_ package {packageLibraries = [("", (section library){sectionBuildable = Just False})]} `shouldBe` unlines [
"name: foo"
, "version: 0.0.0"
, "build-type: Simple"
Expand All @@ -122,7 +122,7 @@ spec = do

context "when rendering library section" $ do
it "renders library section" $ do
renderPackage_ package {packageLibrary = Just $ section library} `shouldBe` unlines [
renderPackage_ package {packageLibraries = [("", section library)]} `shouldBe` unlines [
"name: foo"
, "version: 0.0.0"
, "build-type: Simple"
Expand Down

0 comments on commit 5a16f09

Please sign in to comment.