Skip to content

Commit

Permalink
Merge pull request #119 from dahlia/minimum-runtime
Browse files Browse the repository at this point in the history
Add "minimum_runtime" option to Python target settings
  • Loading branch information
dahlia authored Mar 16, 2017
2 parents 05c9a23 + 6aab889 commit 02ef704
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/package.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ version = "0.3.0"

[targets.python]
name = "nirum-examples"
minimum_runtime = "0.3.9"
1 change: 1 addition & 0 deletions src/Nirum/Package/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Nirum.Package.Metadata ( Author (Author, email, name, uri)
, readMetadata
, stringField
, tableField
, versionField
) where

import Data.Proxy (Proxy (Proxy))
Expand Down
31 changes: 26 additions & 5 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Nirum.Targets.Python ( Code
, insertLocalImport
, insertStandardImport
, insertThirdPartyImports
, minimumRuntime
, runCodeGen
, stringLiteral
, toAttributeName
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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'}]|]
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions test/Nirum/Package/MetadataSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Nirum.Package.Metadata ( Metadata (Metadata, version)
, readFromPackage
, readMetadata
, stringField
, versionField
)

stripPrefix :: String -> String
Expand Down Expand Up @@ -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 "<string>"
[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 "<string>"
Expand Down
4 changes: 2 additions & 2 deletions test/Nirum/PackageSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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' =
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -89,7 +90,7 @@ makeDummySource' pathPrefix m =
, uri = Nothing
}
]
, target = Python "sample-package"
, target = Python "sample-package" minimumRuntime
}
pkg :: Package Python
pkg = createPackage
Expand Down

0 comments on commit 02ef704

Please sign in to comment.