From 5317470f4edba07aace072f21c5c91741c61725c Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Tue, 7 Mar 2017 23:44:24 +0900 Subject: [PATCH 1/8] Add ancestors of packages to packages in setup.py --- src/Nirum/Targets/Python.hs | 10 +++++++++- test/Nirum/Targets/PythonSpec.hs | 12 +++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Nirum/Targets/Python.hs b/src/Nirum/Targets/Python.hs index 49f724c..7a76711 100644 --- a/src/Nirum/Targets/Python.hs +++ b/src/Nirum/Targets/Python.hs @@ -31,6 +31,7 @@ module Nirum.Targets.Python ( Code , insertStandardImport , insertThirdPartyImports , runCodeGen + , spreadModulePaths , stringLiteral , toAttributeName , toClassName @@ -840,6 +841,13 @@ compileModule pythonVersion' source = , ((3, 5), require "typing" "typing" $ standardImports context) ] +spreadModulePaths :: [ModulePath] -> [Code] +spreadModulePaths modulePaths = pathsToPackageNames $ map ancestors modulePaths + where + pathsToPackageNames :: [S.Set ModulePath] -> [Code] + pathsToPackageNames modulePaths' = S.toAscList $ S.map toImportPath $ + S.unions modulePaths' + compilePackageMetadata :: Package' -> InstallRequires -> Code compilePackageMetadata package@Package { metadata = metadata' } (InstallRequires deps optDeps) = @@ -919,7 +927,7 @@ setup( | Author { email = Just e } <- authors metadata' ] pPackages :: Code - pPackages = strings $ map toImportPath $ MS.keys $ modules package + pPackages = strings $ spreadModulePaths $ MS.keys $ modules package pInstallRequires :: Code pInstallRequires = strings $ S.toList deps pPolyfillRequires :: Code diff --git a/test/Nirum/Targets/PythonSpec.hs b/test/Nirum/Targets/PythonSpec.hs index 4a28b64..084936b 100644 --- a/test/Nirum/Targets/PythonSpec.hs +++ b/test/Nirum/Targets/PythonSpec.hs @@ -36,11 +36,12 @@ import Nirum.Constructs.TypeExpression ( TypeExpression ( ListModifier , TypeIdentifier ) ) -import Nirum.Package (Package, resolveBoundModule) +import Nirum.Package (Package (modules), resolveBoundModule) import Nirum.Package.Metadata ( Author (Author, email, name, uri) , Metadata (Metadata, authors, target, version) , Target (compilePackage) ) +import qualified Nirum.Package.ModuleSet as MS import Nirum.PackageSpec (createPackage) import qualified Nirum.Targets.Python as PY import Nirum.Targets.Python ( Source (Source) @@ -349,6 +350,15 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do (3, 4) "ipaddress" (req4 `unionInstallRequires` req5) `shouldBe` req6 (req5 `unionInstallRequires` req4) `shouldBe` req6 + describe "Add ancestors of packages" $ do + let (Source pkg _) = makeDummySource $ Module [] Nothing + modulePaths = MS.keys $ modules pkg + specify "spreadModulePaths" $ + PY.spreadModulePaths modulePaths `shouldBe` [ "foo" + , "foo.bar" + , "qux" + ] + {-# ANN module ("HLint: ignore Functor law" :: String) #-} {-# ANN module ("HLint: ignore Monad law, left identity" :: String) #-} From a34cbfc49fee176559b5082795bc77ad71054117 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 00:32:56 +0900 Subject: [PATCH 2/8] Replace import to qualified import --- src/Nirum/Constructs/ModulePath.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Nirum/Constructs/ModulePath.hs b/src/Nirum/Constructs/ModulePath.hs index f05e7e7..2139030 100644 --- a/src/Nirum/Constructs/ModulePath.hs +++ b/src/Nirum/Constructs/ModulePath.hs @@ -13,7 +13,7 @@ import Data.Char (toLower) import Data.Maybe (fromMaybe, mapMaybe) import GHC.Exts (IsList (Item, fromList, toList)) -import Data.Set (Set, insert, singleton) +import qualified Data.Set as S import Data.Text (intercalate, pack) import System.FilePath (splitDirectories, stripExtension) @@ -55,9 +55,9 @@ fromFilePath filePath = fileIdentifiers :: [Identifier] fileIdentifiers = mapMaybe (fromText . pack) paths -ancestors :: ModulePath -> Set ModulePath -ancestors m@ModuleName {} = singleton m -ancestors m@(ModulePath parent _) = m `insert` ancestors parent +ancestors :: ModulePath -> S.Set ModulePath +ancestors m@ModuleName {} = S.singleton m +ancestors m@(ModulePath parent _) = m `S.insert` ancestors parent instance IsList ModulePath where type Item ModulePath = Identifier From be99431272a7e8152d1dff96484f8625df510eb1 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 00:56:55 +0900 Subject: [PATCH 3/8] Rename ancestors to hierarchy in ModulePath --- src/Nirum/Constructs/ModulePath.hs | 8 ++++---- src/Nirum/Targets/Python.hs | 4 ++-- test/Nirum/Constructs/ModulePathSpec.hs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Nirum/Constructs/ModulePath.hs b/src/Nirum/Constructs/ModulePath.hs index 2139030..2f4a094 100644 --- a/src/Nirum/Constructs/ModulePath.hs +++ b/src/Nirum/Constructs/ModulePath.hs @@ -4,9 +4,9 @@ module Nirum.Constructs.ModulePath ( ModulePath ( ModuleName , moduleName , path ) - , ancestors , fromFilePath , fromIdentifiers + , hierarchy ) where import Data.Char (toLower) @@ -55,9 +55,9 @@ fromFilePath filePath = fileIdentifiers :: [Identifier] fileIdentifiers = mapMaybe (fromText . pack) paths -ancestors :: ModulePath -> S.Set ModulePath -ancestors m@ModuleName {} = S.singleton m -ancestors m@(ModulePath parent _) = m `S.insert` ancestors parent +hierarchy :: ModulePath -> S.Set ModulePath +hierarchy m@ModuleName {} = S.singleton m +hierarchy m@(ModulePath parent _) = m `S.insert` hierarchy parent instance IsList ModulePath where type Item ModulePath = Identifier diff --git a/src/Nirum/Targets/Python.hs b/src/Nirum/Targets/Python.hs index 7a76711..fbc2223 100644 --- a/src/Nirum/Targets/Python.hs +++ b/src/Nirum/Targets/Python.hs @@ -64,7 +64,7 @@ import Nirum.Constructs.Identifier ( Identifier , toSnakeCaseText , toString ) -import Nirum.Constructs.ModulePath (ModulePath, ancestors) +import Nirum.Constructs.ModulePath (ModulePath, hierarchy) import Nirum.Constructs.Name (Name (Name)) import qualified Nirum.Constructs.Name as N import Nirum.Constructs.Service ( Method ( Method @@ -969,7 +969,7 @@ compilePackage' package = initFiles :: [(FilePath, Either CompileError' Code)] initFiles = [ (toFilename (sourceDirectory ver) mp', Right "") | mp <- MS.keys (modules package) - , mp' <- S.elems (ancestors mp) + , mp' <- S.elems (hierarchy mp) , ver <- versions ] modules' :: [(FilePath, Either CompileError' (InstallRequires, Code))] diff --git a/test/Nirum/Constructs/ModulePathSpec.hs b/test/Nirum/Constructs/ModulePathSpec.hs index 8a6b64b..b9f70cf 100644 --- a/test/Nirum/Constructs/ModulePathSpec.hs +++ b/test/Nirum/Constructs/ModulePathSpec.hs @@ -10,7 +10,7 @@ import Test.Hspec.Meta import Nirum.Constructs (Construct (toCode)) import Nirum.Constructs.ModulePath ( ModulePath (ModuleName, ModulePath) - , ancestors + , hierarchy , fromFilePath , fromIdentifiers ) @@ -45,13 +45,13 @@ spec = Just fooBarBaz fromFilePath ("foo" "bar-baz2.nrm") `shouldBe` Just fooBarBaz2 fromFilePath ("foo" "bar_baz2.NRM") `shouldBe` Just fooBarBaz2 - specify "ancestors" $ do - ancestors ["foo", "bar", "baz"] `shouldBe` + specify "hierarchy" $ do + hierarchy ["foo", "bar", "baz"] `shouldBe` [ ["foo"] , ["foo", "bar"] , ["foo", "bar", "baz"] ] - ancestors ["foo"] `shouldBe` [["foo"]] + hierarchy ["foo"] `shouldBe` [["foo"]] context "Construct" $ specify "toCode" $ do toCode foo `shouldBe` "foo" From daf2c10b3bdcd22ed1d73c5e4ba0daeaa8b5d8ca Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 01:00:00 +0900 Subject: [PATCH 4/8] Add ModuleSet.keysSet --- src/Nirum/Package/ModuleSet.hs | 4 ++++ test/Nirum/Package/ModuleSetSpec.hs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/Nirum/Package/ModuleSet.hs b/src/Nirum/Package/ModuleSet.hs index 0a123fa..29b67a8 100644 --- a/src/Nirum/Package/ModuleSet.hs +++ b/src/Nirum/Package/ModuleSet.hs @@ -6,6 +6,7 @@ module Nirum.Package.ModuleSet ( ImportError ( CircularImportError , fromList , fromMap , keys + , keysSet , length , lookup , null @@ -67,6 +68,9 @@ null = M.null . toMap keys :: ModuleSet -> [ModulePath] keys = M.keys . toMap +keysSet :: ModuleSet -> S.Set ModulePath +keysSet = M.keysSet . toMap + lookup :: ModulePath -> ModuleSet -> Maybe Module lookup path = M.lookup path . toMap diff --git a/test/Nirum/Package/ModuleSetSpec.hs b/test/Nirum/Package/ModuleSetSpec.hs index 6bc5d36..b1278ce 100644 --- a/test/Nirum/Package/ModuleSetSpec.hs +++ b/test/Nirum/Package/ModuleSetSpec.hs @@ -23,6 +23,7 @@ import Nirum.Package.ModuleSet ( ImportError ( CircularImportError , fromList , fromMap , keys + , keysSet , length , lookup , null @@ -109,6 +110,8 @@ spec = specify "keys" $ sort (keys validModuleSet) `shouldBe` sort [path | (path, _) <- validModules] + specify "keysSet" $ + keysSet validModuleSet `shouldBe` [p | (p, _) <- validModules] specify "lookup" $ do let Just mod' = lookup ["foo", "bar"] validModuleSet mod' `shouldBe` fooBarModule From d8ead7477bdbf4be59a142700b40c1d7c53b6542 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 01:29:06 +0900 Subject: [PATCH 5/8] Add ModulePath.hierarchies --- src/Nirum/Constructs/ModulePath.hs | 4 ++++ test/Nirum/Constructs/ModulePathSpec.hs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Nirum/Constructs/ModulePath.hs b/src/Nirum/Constructs/ModulePath.hs index 2f4a094..0903e30 100644 --- a/src/Nirum/Constructs/ModulePath.hs +++ b/src/Nirum/Constructs/ModulePath.hs @@ -7,6 +7,7 @@ module Nirum.Constructs.ModulePath ( ModulePath ( ModuleName , fromFilePath , fromIdentifiers , hierarchy + , hierarchies ) where import Data.Char (toLower) @@ -66,3 +67,6 @@ instance IsList ModulePath where (fromIdentifiers identifiers) toList (ModuleName identifier) = [identifier] toList (ModulePath path' identifier) = toList path' ++ [identifier] + +hierarchies :: S.Set ModulePath -> S.Set ModulePath +hierarchies modulePaths = S.unions $ toList $ S.map hierarchy modulePaths diff --git a/test/Nirum/Constructs/ModulePathSpec.hs b/test/Nirum/Constructs/ModulePathSpec.hs index b9f70cf..3430d49 100644 --- a/test/Nirum/Constructs/ModulePathSpec.hs +++ b/test/Nirum/Constructs/ModulePathSpec.hs @@ -11,6 +11,7 @@ import Test.Hspec.Meta import Nirum.Constructs (Construct (toCode)) import Nirum.Constructs.ModulePath ( ModulePath (ModuleName, ModulePath) , hierarchy + , hierarchies , fromFilePath , fromIdentifiers ) @@ -52,6 +53,14 @@ spec = , ["foo", "bar", "baz"] ] hierarchy ["foo"] `shouldBe` [["foo"]] + specify "hierarchies" $ + hierarchies [ ["foo", "bar", "baz"], ["tar", "gz"] ] `shouldBe` + [ ["foo"] + , ["foo", "bar"] + , ["foo", "bar", "baz"] + , ["tar"] + , ["tar", "gz"] + ] context "Construct" $ specify "toCode" $ do toCode foo `shouldBe` "foo" From cc8ba212d2c87e9859f6cc6df3b2a0ff7d3bda68 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 01:30:08 +0900 Subject: [PATCH 6/8] Refactor spreadModulePaths to toImportPaths --- src/Nirum/Targets/Python.hs | 16 ++++++---------- test/Nirum/Package/ModuleSetSpec.hs | 4 +++- test/Nirum/Targets/PythonSpec.hs | 12 ++++++------ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Nirum/Targets/Python.hs b/src/Nirum/Targets/Python.hs index fbc2223..5dd0a0e 100644 --- a/src/Nirum/Targets/Python.hs +++ b/src/Nirum/Targets/Python.hs @@ -31,11 +31,11 @@ module Nirum.Targets.Python ( Code , insertStandardImport , insertThirdPartyImports , runCodeGen - , spreadModulePaths , stringLiteral , toAttributeName , toClassName , toImportPath + , toImportPaths , toNamePair , unionInstallRequires ) where @@ -64,7 +64,7 @@ import Nirum.Constructs.Identifier ( Identifier , toSnakeCaseText , toString ) -import Nirum.Constructs.ModulePath (ModulePath, hierarchy) +import Nirum.Constructs.ModulePath (ModulePath, hierarchy, hierarchies) import Nirum.Constructs.Name (Name (Name)) import qualified Nirum.Constructs.Name as N import Nirum.Constructs.Service ( Method ( Method @@ -228,6 +228,9 @@ toAttributeName' = toAttributeName . N.facialName toImportPath :: ModulePath -> T.Text toImportPath = T.intercalate "." . map toAttributeName . toList +toImportPaths :: S.Set ModulePath -> [T.Text] +toImportPaths paths = S.toAscList $ S.map toImportPath $ hierarchies paths + toNamePair :: Name -> T.Text toNamePair (Name f b) = [qq|('{toAttributeName f}', '{toSnakeCaseText b}')|] @@ -841,13 +844,6 @@ compileModule pythonVersion' source = , ((3, 5), require "typing" "typing" $ standardImports context) ] -spreadModulePaths :: [ModulePath] -> [Code] -spreadModulePaths modulePaths = pathsToPackageNames $ map ancestors modulePaths - where - pathsToPackageNames :: [S.Set ModulePath] -> [Code] - pathsToPackageNames modulePaths' = S.toAscList $ S.map toImportPath $ - S.unions modulePaths' - compilePackageMetadata :: Package' -> InstallRequires -> Code compilePackageMetadata package@Package { metadata = metadata' } (InstallRequires deps optDeps) = @@ -927,7 +923,7 @@ setup( | Author { email = Just e } <- authors metadata' ] pPackages :: Code - pPackages = strings $ spreadModulePaths $ MS.keys $ modules package + pPackages = strings $ toImportPaths $ MS.keysSet $ modules package pInstallRequires :: Code pInstallRequires = strings $ S.toList deps pPolyfillRequires :: Code diff --git a/test/Nirum/Package/ModuleSetSpec.hs b/test/Nirum/Package/ModuleSetSpec.hs index b1278ce..051acb8 100644 --- a/test/Nirum/Package/ModuleSetSpec.hs +++ b/test/Nirum/Package/ModuleSetSpec.hs @@ -6,6 +6,7 @@ import Data.Maybe (isNothing) import Prelude hiding (length, lookup, null) import qualified Data.Map.Strict as M +import qualified Data.Set as S import Test.Hspec.Meta import Nirum.Constructs.Annotation (empty) @@ -111,7 +112,8 @@ spec = sort (keys validModuleSet) `shouldBe` sort [path | (path, _) <- validModules] specify "keysSet" $ - keysSet validModuleSet `shouldBe` [p | (p, _) <- validModules] + keysSet validModuleSet `shouldBe` + S.fromList [p | (p, _) <- validModules] specify "lookup" $ do let Just mod' = lookup ["foo", "bar"] validModuleSet mod' `shouldBe` fooBarModule diff --git a/test/Nirum/Targets/PythonSpec.hs b/test/Nirum/Targets/PythonSpec.hs index 084936b..1fa0d06 100644 --- a/test/Nirum/Targets/PythonSpec.hs +++ b/test/Nirum/Targets/PythonSpec.hs @@ -352,12 +352,12 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do (req5 `unionInstallRequires` req4) `shouldBe` req6 describe "Add ancestors of packages" $ do let (Source pkg _) = makeDummySource $ Module [] Nothing - modulePaths = MS.keys $ modules pkg - specify "spreadModulePaths" $ - PY.spreadModulePaths modulePaths `shouldBe` [ "foo" - , "foo.bar" - , "qux" - ] + modulePaths = MS.keysSet $ modules pkg + specify "toImportPaths" $ + PY.toImportPaths modulePaths `shouldBe` [ "foo" + , "foo.bar" + , "qux" + ] {-# ANN module ("HLint: ignore Functor law" :: String) #-} From b85327107aae92a80009ea4b8e69de87723c25a1 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 01:47:51 +0900 Subject: [PATCH 7/8] Add test for Python.toImportPath --- test/Nirum/Targets/PythonSpec.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Nirum/Targets/PythonSpec.hs b/test/Nirum/Targets/PythonSpec.hs index 1fa0d06..1b97ed2 100644 --- a/test/Nirum/Targets/PythonSpec.hs +++ b/test/Nirum/Targets/PythonSpec.hs @@ -350,6 +350,8 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do (3, 4) "ipaddress" (req4 `unionInstallRequires` req5) `shouldBe` req6 (req5 `unionInstallRequires` req4) `shouldBe` req6 + specify "toImportPath" $ + PY.toImportPath ["foo", "bar"] `shouldBe` "foo.bar" describe "Add ancestors of packages" $ do let (Source pkg _) = makeDummySource $ Module [] Nothing modulePaths = MS.keysSet $ modules pkg From 7f6a8aff52563303713b382cf04fe53ae7127836 Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 8 Mar 2017 09:42:37 +0900 Subject: [PATCH 8/8] Update test_setup_metadata --- test/python/setup_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/setup_test.py b/test/python/setup_test.py index 1cdd45d..552ebba 100644 --- a/test/python/setup_test.py +++ b/test/python/setup_test.py @@ -17,6 +17,6 @@ def test_setup_metadata(): assert ['dev@nirum.org'] == pkg['Author-email'] assert ['nirum'] == pkg['Requires'] assert set(pkg['Provides']) == { - 'fixture.foo', 'fixture.foo.bar', 'fixture.qux', + 'fixture', 'fixture.foo', 'fixture.foo.bar', 'fixture.qux', } assert ['0.3.0'] == pkg['Version']