Skip to content

Commit 02ef704

Browse files
authored
Merge pull request #119 from dahlia/minimum-runtime
Add "minimum_runtime" option to Python target settings
2 parents 05c9a23 + 6aab889 commit 02ef704

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

examples/package.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ version = "0.3.0"
22

33
[targets.python]
44
name = "nirum-examples"
5+
minimum_runtime = "0.3.9"

src/Nirum/Package/Metadata.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Nirum.Package.Metadata ( Author (Author, email, name, uri)
3939
, readMetadata
4040
, stringField
4141
, tableField
42+
, versionField
4243
) where
4344

4445
import Data.Proxy (Proxy (Proxy))

src/Nirum/Targets/Python.hs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Nirum.Targets.Python ( Code
3030
, insertLocalImport
3131
, insertStandardImport
3232
, insertThirdPartyImports
33+
, minimumRuntime
3334
, runCodeGen
3435
, stringLiteral
3536
, toAttributeName
@@ -104,6 +105,7 @@ import Nirum.Package ( BoundModule
104105
)
105106
import Nirum.Package.Metadata ( Author (Author, name, email)
106107
, Metadata (authors, target, version)
108+
, MetadataError (FieldError)
107109
, Target ( CompileError
108110
, CompileResult
109111
, compilePackage
@@ -113,11 +115,16 @@ import Nirum.Package.Metadata ( Author (Author, name, email)
113115
, toByteString
114116
)
115117
, stringField
118+
, versionField
116119
)
117120
import qualified Nirum.Package.ModuleSet as MS
118121

119-
newtype Python = Python { packageName :: T.Text
120-
} deriving (Eq, Ord, Show, Typeable)
122+
minimumRuntime :: SV.Version
123+
minimumRuntime = SV.version 0 3 9 [] []
124+
125+
data Python = Python { packageName :: T.Text
126+
, minimumRuntimeVersion :: SV.Version
127+
} deriving (Eq, Ord, Show, Typeable)
121128

122129
type Package' = Package Python
123130
type CompileError' = T.Text
@@ -900,7 +907,7 @@ setup(
900907
package_dir=\{'': SOURCE_ROOT},
901908
packages=[$pPackages],
902909
provides=[$pPackages],
903-
requires=[$pInstallRequires],
910+
requires=[$pRequires],
904911
setup_requires=setup_requires,
905912
install_requires=install_requires,
906913
extras_require=extras_require,
@@ -924,8 +931,17 @@ setup(
924931
]
925932
pPackages :: Code
926933
pPackages = strings $ toImportPaths $ MS.keysSet $ modules package
934+
runtimeVer :: SV.Version
935+
runtimeVer = minimumRuntimeVersion $ target metadata'
936+
pRequires :: Code
937+
pRequires = strings $ S.toList deps
927938
pInstallRequires :: Code
928-
pInstallRequires = strings $ S.toList deps
939+
pInstallRequires = strings
940+
[ case p of
941+
"nirum" -> [qq|nirum >= {SV.toText runtimeVer}|]
942+
p' -> p'
943+
| p <- S.toList deps
944+
]
929945
pPolyfillRequires :: Code
930946
pPolyfillRequires = T.intercalate ", "
931947
[ [qq|($major, $minor): [{strings $ S.toList deps'}]|]
@@ -988,7 +1004,12 @@ instance Target Python where
9881004
targetName _ = "python"
9891005
parseTarget table = do
9901006
name' <- stringField "name" table
991-
return Python { packageName = name' }
1007+
minRuntime <- case versionField "minimum_runtime" table of
1008+
Left (FieldError _) -> Right minimumRuntime
1009+
otherwise' -> otherwise'
1010+
return Python { packageName = name'
1011+
, minimumRuntimeVersion = max minRuntime minimumRuntime
1012+
}
9921013
compilePackage = compilePackage'
9931014
showCompileError _ e = e
9941015
toByteString _ = encodeUtf8

test/Nirum/Package/MetadataSpec.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Nirum.Package.Metadata ( Metadata (Metadata, version)
3737
, readFromPackage
3838
, readMetadata
3939
, stringField
40+
, versionField
4041
)
4142

4243
stripPrefix :: String -> String
@@ -174,6 +175,21 @@ spec =
174175
stringField "bar" table `shouldBe`
175176
Left (FieldTypeError "bar" "string" "integer (1)")
176177
stringField "qux" table `shouldBe` Left (FieldError "qux")
178+
specify "versionField" $ do
179+
let Right table = parseTomlDoc "<string>"
180+
[q|a = "1.0.0"
181+
b = "1.2.3"
182+
c = "1.0"
183+
d = 1.0
184+
e = "1.2.3.4"|]
185+
versionField "a" table `shouldBe` Right (SV.version 1 0 0 [] [])
186+
versionField "b" table `shouldBe` Right (SV.version 1 2 3 [] [])
187+
versionField "c" table `shouldBe` Left (FieldValueError "c"
188+
"expected a semver string (e.g. \"1.2.3\"), not \"1.0\"")
189+
versionField "d" table `shouldBe`
190+
Left (FieldTypeError "d" "string" "float (1.0)")
191+
versionField "e" table `shouldBe` Left (FieldValueError "e"
192+
"expected a semver string (e.g. \"1.2.3\"), not \"1.2.3.4\"")
177193
where
178194
parse :: Text -> Either MetadataError (Metadata DummyTarget)
179195
parse = parseMetadata "<string>"

test/Nirum/PackageSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Nirum.Package.ModuleSet ( ImportError (MissingModulePathError)
4545
)
4646
import Nirum.Package.ModuleSetSpec (validModules)
4747
import Nirum.Parser (parseFile)
48-
import Nirum.Targets.Python (Python (Python))
48+
import Nirum.Targets.Python (Python (Python), minimumRuntime)
4949

5050
createPackage :: Metadata t -> [(ModulePath, Module)] -> Package t
5151
createPackage metadata' modules' =
@@ -61,7 +61,7 @@ createValidPackage t = createPackage Metadata { version = SV.initial
6161

6262
spec :: Spec
6363
spec = do
64-
testPackage (Python "nirum-examples")
64+
testPackage (Python "nirum-examples" minimumRuntime)
6565
testPackage DummyTarget
6666

6767
testPackage :: forall t . Target t => t -> Spec

test/Nirum/Targets/PythonSpec.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ import Nirum.Targets.Python ( Source (Source)
5959
, addOptionalDependency
6060
, compilePrimitiveType
6161
, compileTypeExpression
62+
, insertLocalImport
63+
, insertStandardImport
64+
, insertThirdPartyImports
65+
, minimumRuntime
66+
, runCodeGen
6267
, stringLiteral
6368
, toAttributeName
6469
, toClassName
6570
, toNamePair
6671
, unionInstallRequires
67-
, insertLocalImport
68-
, insertStandardImport
69-
, insertThirdPartyImports
70-
, runCodeGen
7172
)
7273

7374
codeGen :: a -> CodeGen a
@@ -89,7 +90,7 @@ makeDummySource' pathPrefix m =
8990
, uri = Nothing
9091
}
9192
]
92-
, target = Python "sample-package"
93+
, target = Python "sample-package" minimumRuntime
9394
}
9495
pkg :: Package Python
9596
pkg = createPackage

0 commit comments

Comments
 (0)