diff --git a/examples/package.toml b/examples/package.toml index 8aaeb1d..c6dc540 100644 --- a/examples/package.toml +++ b/examples/package.toml @@ -2,3 +2,4 @@ version = "0.3.0" [targets.python] name = "nirum-examples" +minimum_runtime = "0.3.9" diff --git a/src/Nirum/Package/Metadata.hs b/src/Nirum/Package/Metadata.hs index 1b5e6ca..0c4eed2 100644 --- a/src/Nirum/Package/Metadata.hs +++ b/src/Nirum/Package/Metadata.hs @@ -39,6 +39,7 @@ module Nirum.Package.Metadata ( Author (Author, email, name, uri) , readMetadata , stringField , tableField + , versionField ) where import Data.Proxy (Proxy (Proxy)) diff --git a/src/Nirum/Targets/Python.hs b/src/Nirum/Targets/Python.hs index 918e07c..c795ab3 100644 --- a/src/Nirum/Targets/Python.hs +++ b/src/Nirum/Targets/Python.hs @@ -30,6 +30,7 @@ module Nirum.Targets.Python ( Code , insertLocalImport , insertStandardImport , insertThirdPartyImports + , minimumRuntime , runCodeGen , stringLiteral , toAttributeName @@ -104,6 +105,7 @@ import Nirum.Package ( BoundModule ) import Nirum.Package.Metadata ( Author (Author, name, email) , Metadata (authors, target, version) + , MetadataError (FieldError) , Target ( CompileError , CompileResult , compilePackage @@ -113,11 +115,16 @@ import Nirum.Package.Metadata ( Author (Author, name, email) , toByteString ) , stringField + , versionField ) import qualified Nirum.Package.ModuleSet as MS -newtype Python = Python { packageName :: T.Text - } deriving (Eq, Ord, Show, Typeable) +minimumRuntime :: SV.Version +minimumRuntime = SV.version 0 3 9 [] [] + +data Python = Python { packageName :: T.Text + , minimumRuntimeVersion :: SV.Version + } deriving (Eq, Ord, Show, Typeable) type Package' = Package Python type CompileError' = T.Text @@ -900,7 +907,7 @@ setup( package_dir=\{'': SOURCE_ROOT}, packages=[$pPackages], provides=[$pPackages], - requires=[$pInstallRequires], + requires=[$pRequires], setup_requires=setup_requires, install_requires=install_requires, extras_require=extras_require, @@ -924,8 +931,17 @@ setup( ] pPackages :: Code pPackages = strings $ toImportPaths $ MS.keysSet $ modules package + runtimeVer :: SV.Version + runtimeVer = minimumRuntimeVersion $ target metadata' + pRequires :: Code + pRequires = strings $ S.toList deps pInstallRequires :: Code - pInstallRequires = strings $ S.toList deps + pInstallRequires = strings + [ case p of + "nirum" -> [qq|nirum >= {SV.toText runtimeVer}|] + p' -> p' + | p <- S.toList deps + ] pPolyfillRequires :: Code pPolyfillRequires = T.intercalate ", " [ [qq|($major, $minor): [{strings $ S.toList deps'}]|] @@ -988,7 +1004,12 @@ instance Target Python where targetName _ = "python" parseTarget table = do name' <- stringField "name" table - return Python { packageName = name' } + minRuntime <- case versionField "minimum_runtime" table of + Left (FieldError _) -> Right minimumRuntime + otherwise' -> otherwise' + return Python { packageName = name' + , minimumRuntimeVersion = max minRuntime minimumRuntime + } compilePackage = compilePackage' showCompileError _ e = e toByteString _ = encodeUtf8 diff --git a/test/Nirum/Package/MetadataSpec.hs b/test/Nirum/Package/MetadataSpec.hs index 60fc00b..1cbaa69 100644 --- a/test/Nirum/Package/MetadataSpec.hs +++ b/test/Nirum/Package/MetadataSpec.hs @@ -37,6 +37,7 @@ import Nirum.Package.Metadata ( Metadata (Metadata, version) , readFromPackage , readMetadata , stringField + , versionField ) stripPrefix :: String -> String @@ -174,6 +175,21 @@ spec = stringField "bar" table `shouldBe` Left (FieldTypeError "bar" "string" "integer (1)") stringField "qux" table `shouldBe` Left (FieldError "qux") + specify "versionField" $ do + let Right table = parseTomlDoc "" + [q|a = "1.0.0" + b = "1.2.3" + c = "1.0" + d = 1.0 + e = "1.2.3.4"|] + versionField "a" table `shouldBe` Right (SV.version 1 0 0 [] []) + versionField "b" table `shouldBe` Right (SV.version 1 2 3 [] []) + versionField "c" table `shouldBe` Left (FieldValueError "c" + "expected a semver string (e.g. \"1.2.3\"), not \"1.0\"") + versionField "d" table `shouldBe` + Left (FieldTypeError "d" "string" "float (1.0)") + versionField "e" table `shouldBe` Left (FieldValueError "e" + "expected a semver string (e.g. \"1.2.3\"), not \"1.2.3.4\"") where parse :: Text -> Either MetadataError (Metadata DummyTarget) parse = parseMetadata "" diff --git a/test/Nirum/PackageSpec.hs b/test/Nirum/PackageSpec.hs index 51c56ff..9f1d06a 100644 --- a/test/Nirum/PackageSpec.hs +++ b/test/Nirum/PackageSpec.hs @@ -45,7 +45,7 @@ import Nirum.Package.ModuleSet ( ImportError (MissingModulePathError) ) import Nirum.Package.ModuleSetSpec (validModules) import Nirum.Parser (parseFile) -import Nirum.Targets.Python (Python (Python)) +import Nirum.Targets.Python (Python (Python), minimumRuntime) createPackage :: Metadata t -> [(ModulePath, Module)] -> Package t createPackage metadata' modules' = @@ -61,7 +61,7 @@ createValidPackage t = createPackage Metadata { version = SV.initial spec :: Spec spec = do - testPackage (Python "nirum-examples") + testPackage (Python "nirum-examples" minimumRuntime) testPackage DummyTarget testPackage :: forall t . Target t => t -> Spec diff --git a/test/Nirum/Targets/PythonSpec.hs b/test/Nirum/Targets/PythonSpec.hs index 225e238..00e7f19 100644 --- a/test/Nirum/Targets/PythonSpec.hs +++ b/test/Nirum/Targets/PythonSpec.hs @@ -59,15 +59,16 @@ import Nirum.Targets.Python ( Source (Source) , addOptionalDependency , compilePrimitiveType , compileTypeExpression + , insertLocalImport + , insertStandardImport + , insertThirdPartyImports + , minimumRuntime + , runCodeGen , stringLiteral , toAttributeName , toClassName , toNamePair , unionInstallRequires - , insertLocalImport - , insertStandardImport - , insertThirdPartyImports - , runCodeGen ) codeGen :: a -> CodeGen a @@ -89,7 +90,7 @@ makeDummySource' pathPrefix m = , uri = Nothing } ] - , target = Python "sample-package" + , target = Python "sample-package" minimumRuntime } pkg :: Package Python pkg = createPackage