Skip to content

Commit 421c37a

Browse files
committed
Add ancestors of packages to packages in setup.py
1 parent c724fea commit 421c37a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Nirum/Targets/Python.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Nirum.Targets.Python ( Code
3131
, insertStandardImport
3232
, insertThirdPartyImports
3333
, runCodeGen
34+
, spreadModulePaths
3435
, stringLiteral
3536
, toAttributeName
3637
, toClassName
@@ -840,6 +841,13 @@ compileModule pythonVersion' source =
840841
, ((3, 5), require "typing" "typing" $ standardImports context)
841842
]
842843

844+
spreadModulePaths :: [ModulePath] -> [Code]
845+
spreadModulePaths modulePaths = pathsToPackageNames $ map ancestors modulePaths
846+
where
847+
pathsToPackageNames :: [S.Set ModulePath] -> [Code]
848+
pathsToPackageNames modulePaths' = S.toAscList $ S.map toImportPath $
849+
S.unions modulePaths'
850+
843851
compilePackageMetadata :: Package' -> InstallRequires -> Code
844852
compilePackageMetadata package@Package { metadata = metadata' }
845853
(InstallRequires deps optDeps) =
@@ -919,7 +927,7 @@ setup(
919927
| Author { email = Just e } <- authors metadata'
920928
]
921929
pPackages :: Code
922-
pPackages = strings $ map toImportPath $ MS.keys $ modules package
930+
pPackages = strings $ spreadModulePaths $ MS.keys $ modules package
923931
pInstallRequires :: Code
924932
pInstallRequires = strings $ S.toList deps
925933
pPolyfillRequires :: Code

test/Nirum/Targets/PythonSpec.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ import Nirum.Constructs.TypeExpression ( TypeExpression ( ListModifier
3636
, TypeIdentifier
3737
)
3838
)
39-
import Nirum.Package (Package, resolveBoundModule)
39+
import Nirum.Package (Package(modules), resolveBoundModule)
4040
import Nirum.Package.Metadata ( Author (Author, email, name, uri)
4141
, Metadata (Metadata, authors, target, version)
4242
, Target (compilePackage)
4343
)
44+
import qualified Nirum.Package.ModuleSet as MS
4445
import Nirum.PackageSpec (createPackage)
4546
import qualified Nirum.Targets.Python as PY
4647
import Nirum.Targets.Python ( Source (Source)
@@ -349,6 +350,15 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do
349350
(3, 4) "ipaddress"
350351
(req4 `unionInstallRequires` req5) `shouldBe` req6
351352
(req5 `unionInstallRequires` req4) `shouldBe` req6
353+
describe "Add ancestors of packages" $ do
354+
let (Source pkg _) = makeDummySource $ Module [] Nothing
355+
modulePaths = MS.keys $ modules pkg
356+
specify "spreadModulePaths" $
357+
PY.spreadModulePaths modulePaths `shouldBe` [ "foo"
358+
, "foo.bar"
359+
, "qux"
360+
]
361+
352362

353363
{-# ANN module ("HLint: ignore Functor law" :: String) #-}
354364
{-# ANN module ("HLint: ignore Monad law, left identity" :: String) #-}

0 commit comments

Comments
 (0)