1
1
mirror of https://github.com/sol/hpack.git synced 2024-10-04 03:38:00 +03:00

Minor refactoring

This commit is contained in:
Simon Hengel 2020-11-12 19:16:41 +07:00
parent 89736238f0
commit aba1e2954a
3 changed files with 111 additions and 98 deletions

View File

@ -837,8 +837,6 @@ determineCabalVersion inferredLicense pkg@Package{..} = (
p sect
, any (any (sectionSatisfies p)) (sectionConditionals sect)
]
sectionAll :: (Semigroup b, Monoid b) => (Section a -> b) -> Section a -> b
sectionAll f sect = f sect <> foldMap (foldMap $ sectionAll f) (sectionConditionals sect)
hasMixins :: DependencyInfo -> Bool
hasMixins (DependencyInfo mixins _) = not (null mixins)
@ -846,6 +844,9 @@ determineCabalVersion inferredLicense pkg@Package{..} = (
hasSubcomponents :: String -> Bool
hasSubcomponents = elem ':'
sectionAll :: (Semigroup b, Monoid b) => (Section a -> b) -> Section a -> b
sectionAll f sect = f sect <> foldMap (foldMap $ sectionAll f) (sectionConditionals sect)
decodeValue :: FromValue a => ProgramName -> FilePath -> Value -> Warnings (Errors IO) a
decodeValue (ProgramName programName) file value = do
(r, unknown) <- lift . ExceptT . return $ first (prefix ++) (Config.decodeValue value)
@ -1107,11 +1108,14 @@ toPackage_ dir (Product g PackageConfig{..}) = do
toSect :: (Monad m, Monoid a) => WithCommonOptions CSources CxxSources JsSources a -> Warnings m (Section a)
toSect = toSection packageName_ executableNames . first ((mempty <$ globalOptions) <>)
toLib = toSect >=> liftIO . toLibrary dir packageName_
toExecutables = maybe (return mempty) (traverse $ toSect >=> liftIO . toExecutable dir packageName_)
toSections :: (Monad m, Monoid a) => Maybe (Map String (WithCommonOptions CSources CxxSources JsSources a)) -> Warnings m (Map String (Section a))
toSections = maybe (return mempty) (traverse toSect)
mLibrary <- traverse toLib packageConfigLibrary
internalLibraries <- maybe (return mempty) (traverse toLib) packageConfigInternalLibraries
toLib = liftIO . toLibrary dir packageName_
toExecutables = toSections >=> traverse (liftIO . toExecutable dir packageName_)
mLibrary <- traverse (toSect >=> toLib) packageConfigLibrary
internalLibraries <- toSections packageConfigInternalLibraries >>= traverse toLib
executables <- toExecutables executableMap
tests <- toExecutables packageConfigTests

View File

@ -41,18 +41,26 @@ toModule path = case reverse $ Path.components path of
file : dirs -> Module . intercalate "." . reverse $ dropExtension file : dirs
getModules :: FilePath -> FilePath -> IO [Module]
getModules dir src_ = sortModules <$> do
exists <- Directory.doesDirectoryExist (dir </> src_)
getModules dir literalSrc = sortModules <$> do
exists <- Directory.doesDirectoryExist (dir </> literalSrc)
if exists
then do
src <- Directory.canonicalizePath (dir </> src_)
removeSetup src . nub . map toModule <$> getModuleFilesRecursive src
canonicalSrc <- Directory.canonicalizePath (dir </> literalSrc)
let
srcIsProjectRoot :: Bool
srcIsProjectRoot = canonicalSrc == dir
toModules :: [Path] -> [Module]
toModules = removeSetup . nub . map toModule
removeSetup :: [Module] -> [Module]
removeSetup
| srcIsProjectRoot = filter (/= "Setup")
| otherwise = id
toModules <$> getModuleFilesRecursive canonicalSrc
else return []
where
removeSetup :: FilePath -> [Module] -> [Module]
removeSetup src
| src == dir = filter (/= "Setup")
| otherwise = id
sortModules :: [Module] -> [Module]
sortModules = map Module . sort . map unModule

View File

@ -986,7 +986,68 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Foo
|]
context "when inferring modules" $ do
context "with mixins" $ do
it "infers cabal-version 2.0" $ do
[i|
library:
dependencies:
foo:
mixin:
- (Blah as Etc)
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
build-depends:
foo
mixins:
foo (Blah as Etc)
|]) {packageCabalVersion = "2.0"}
describe "internal-libraries" $ do
it "accepts internal-libraries" $ do
touch "src/Foo.hs"
[i|
internal-libraries:
bar:
source-dirs: src
|] `shouldRenderTo` internalLibrary "bar" [i|
exposed-modules:
Foo
other-modules:
Paths_foo
hs-source-dirs:
src
|]
it "warns on unknown fields" $ do
[i|
name: foo
internal-libraries:
bar:
baz: 42
|] `shouldWarn` pure "package.yaml: Ignoring unrecognized field $.internal-libraries.bar.baz"
it "warns on missing source-dirs" $ do
[i|
name: foo
internal-libraries:
bar:
source-dirs: src
|] `shouldWarn` pure "Specified source-dir \"src\" does not exist"
it "accepts visibility" $ do
[i|
internal-libraries:
bar:
visibility: public
|] `shouldRenderTo` (internalLibrary "bar" [i|
visibility: public
other-modules:
Paths_foo
|]) {packageCabalVersion = "3.0"}
context "when inferring modules" $ do
context "with a library" $ do
it "ignores duplicate source directories" $ do
touch "src/Foo.hs"
[i|
@ -1217,86 +1278,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Exposed
|]) {packageCabalVersion = "2.0"}
context "with mixins" $ do
it "infers cabal-version 2.0" $ do
[i|
library:
dependencies:
foo:
mixin:
- (Blah as Etc)
|] `shouldRenderTo` (library [i|
other-modules:
Paths_foo
build-depends:
foo
mixins:
foo (Blah as Etc)
|]) {packageCabalVersion = "2.0"}
describe "internal-libraries" $ do
it "accepts internal-libraries" $ do
touch "src/Foo.hs"
[i|
internal-libraries:
bar:
source-dirs: src
|] `shouldRenderTo` internalLibrary "bar" [i|
exposed-modules:
Foo
other-modules:
Paths_foo
hs-source-dirs:
src
|]
it "warns on unknown fields" $ do
[i|
name: foo
internal-libraries:
bar:
baz: 42
|] `shouldWarn` pure "package.yaml: Ignoring unrecognized field $.internal-libraries.bar.baz"
it "warns on missing source-dirs" $ do
[i|
name: foo
internal-libraries:
bar:
source-dirs: src
|] `shouldWarn` pure "Specified source-dir \"src\" does not exist"
it "accepts visibility" $ do
[i|
internal-libraries:
bar:
visibility: public
|] `shouldRenderTo` (internalLibrary "bar" [i|
visibility: public
other-modules:
Paths_foo
|]) {packageCabalVersion = "3.0"}
describe "executables" $ do
it "accepts arbitrary entry points as main" $ do
touch "src/Foo.hs"
touch "src/Bar.hs"
[i|
executables:
foo:
source-dirs: src
main: Foo
|] `shouldRenderTo` executable "foo" [i|
main-is: Foo.hs
ghc-options: -main-is Foo
hs-source-dirs:
src
other-modules:
Bar
Paths_foo
|]
context "when inferring modules" $ do
context "with an executable" $ do
it "infers other-modules" $ do
touch "src/Main.hs"
touch "src/Foo.hs"
@ -1350,7 +1332,7 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
Foo
|]) {packageCabalVersion = "2.0"}
context "with conditional" $ do
context "with a conditional" $ do
it "doesn't infer any modules mentioned in that conditional" $ do
touch "src/Foo.hs"
touch "src/Bar.hs"
@ -1395,7 +1377,26 @@ spec = around_ (inTempDirectoryNamed "foo") $ do
windows
|]
context "with conditional" $ do
describe "executables" $ do
it "accepts arbitrary entry points as main" $ do
touch "src/Foo.hs"
touch "src/Bar.hs"
[i|
executables:
foo:
source-dirs: src
main: Foo
|] `shouldRenderTo` executable "foo" [i|
main-is: Foo.hs
ghc-options: -main-is Foo
hs-source-dirs:
src
other-modules:
Bar
Paths_foo
|]
context "with a conditional" $ do
it "does not apply global options" $ do
-- related bug: https://github.com/sol/hpack/issues/214
[i|