Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keywords field to package metadata #183

Merged
merged 3 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ is an example:
version = "1.0.0" # (required)
description = "Short description on the package"
license = "MIT"
keywords = "sample example nirum"

[[authors]]
name = "John Doe" # (required)
Expand All @@ -61,6 +62,9 @@ It consists of the following fields (*emphasized fields* are required):
`license` (string)
: An optional license of the package.

`keywords` (string)
: An optional keywords of the package.

`authors` (array of tables)
: The list of authors. Note that it can be empty, but `name` field is
required if any author is provided. Each author table consists of
Expand Down
29 changes: 29 additions & 0 deletions src/Nirum/Package/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Nirum.Package.Metadata ( Author (Author, email, name, uri)
, version
, description
, license
, keywords
)
, MetadataError ( FieldError
, FieldTypeError
Expand Down Expand Up @@ -77,6 +78,7 @@ import Text.Toml.Types (Node ( VArray
)
, Table
, VTArray
, VArray
)
import Text.URI (URI, parseURI)

Expand All @@ -103,6 +105,7 @@ data Metadata t =
Metadata { version :: SV.Version
, description :: Maybe Text
, license :: Maybe Text
, keywords :: [Text]
, authors :: [Author]
, target :: (Eq t, Ord t, Show t, Target t) => t
}
Expand Down Expand Up @@ -184,6 +187,7 @@ parseMetadata metadataPath' tomlText = do
authors' <- authorsField "authors" table
description' <- optional $ stringField "description" table
license' <- optional $ stringField "license" table
keywords' <- textArrayField "keywords" table
targets <- case tableField "targets" table of
Left (FieldError _) -> Right HM.empty
otherwise' -> otherwise'
Expand All @@ -198,6 +202,7 @@ parseMetadata metadataPath' tomlText = do
return Metadata { version = version'
, description = description'
, license = license'
, keywords = keywords'
, authors = authors'
, target = target'
}
Expand Down Expand Up @@ -265,6 +270,20 @@ stringField = typedField "string" $ \ n -> case n of
VString s -> Just s
_ -> Nothing

arrayField :: MetadataField -> Table -> Either MetadataError VArray
arrayField f t =
case arrayF f t of
Right vector -> Right vector
Left (FieldError _) -> Right $ fromList []
Left error' -> Left error'
where
arrayF :: MetadataField -> Table -> Either MetadataError VArray
arrayF = typedField "array" $ \ node ->
case node of
VArray array -> Just array
_ -> Nothing


tableArrayField :: MetadataField -> Table -> Either MetadataError VTArray
tableArrayField f t =
case arrayF f t of
Expand Down Expand Up @@ -302,6 +321,16 @@ versionField field' table = do
Left _ -> Left $ FieldValueError field' $
"expected a semver string (e.g. \"1.2.3\"), not " ++ show s

textArrayField :: MetadataField -> Table -> Either MetadataError [Text]
textArrayField field' table = do
array <- arrayField field' table
textArray' <- mapM parseText array
return $ toList textArray'
where
parseText :: Node -> Either MetadataError Text
parseText (VString s) = Right s
parseText a = Left $ FieldTypeError field' "array" $ fieldType a

authorsField :: MetadataField -> Table -> Either MetadataError [Author]
authorsField field' table = do
array <- tableArrayField field' table
Expand Down
18 changes: 12 additions & 6 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import Nirum.Package.Metadata ( Author (Author, name, email)
, versionField
)
import qualified Nirum.Package.ModuleSet as MS
import qualified Nirum.Package.Metadata as MD

minimumRuntime :: SV.Version
minimumRuntime = SV.version 0 6 0 [] []
Expand Down Expand Up @@ -1187,12 +1188,13 @@ SOURCE_ROOT = '{sourceDirectory Python3}'
if sys.version_info < (3, 0):
SOURCE_ROOT = '{sourceDirectory Python2}'

# TODO: long_description, url, keywords, classifiers
# TODO: long_description, url, classifiers
setup(
name='{pName}',
version='{pVersion}',
description=$pDescription,
license=$pLicense,
keywords=$pKeywords,
author=$author,
author_email=$authorEmail,
package_dir=\{'': SOURCE_ROOT},
Expand All @@ -1207,9 +1209,9 @@ setup(
where
target' :: Python
target' = target metadata'
csStrings :: [T.Text] -> T.Text
csStrings [] = "None"
csStrings s = stringLiteral $ T.intercalate ", " s
csStrings :: T.Text -> [T.Text] -> T.Text
csStrings _ [] = "None"
csStrings d s = stringLiteral $ T.intercalate d s
pName :: Code
pName = packageName $ target metadata'
pVersion :: Code
Expand All @@ -1222,12 +1224,16 @@ setup(
pDescription = fromMaybeToMeta $ description metadata'
pLicense :: Code
pLicense = fromMaybeToMeta $ license metadata'
pKeywords :: Code
pKeywords = csStrings " " $ MD.keywords metadata'
strings :: [Code] -> Code
strings values = T.intercalate ", " $ map stringLiteral (L.sort values)
author :: Code
author = csStrings [aName | Author { name = aName } <- authors metadata']
author = csStrings ", " [aName
| Author { name = aName } <- authors metadata'
]
authorEmail :: Code
authorEmail = csStrings [ decodeUtf8 (E.toByteString e)
authorEmail = csStrings ", " [ decodeUtf8 (E.toByteString e)
| Author { email = Just e } <- authors metadata'
]
pPackages :: Code
Expand Down
1 change: 1 addition & 0 deletions test/Nirum/CodeBuilderSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ package = Package { metadata = Metadata { version = SV.version 0 0 1 [] []
, authors = []
, description = Nothing
, license = Nothing
, keywords = []
, target = DummyTarget
}
, modules = modules'
Expand Down
7 changes: 7 additions & 0 deletions test/Nirum/Package/MetadataSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ spec =
, "string"
, "integer (123)"
)
, ( [q|version = "1.2.3"
keywords = "sample example nirum"
|]
, "keywords"
, "array"
, "string (sample example nirum)"
)
] $ \ (toml, field, expected, actual) -> do
let Left e = parse toml
FieldTypeError field' expected' actual' = e
Expand Down
3 changes: 3 additions & 0 deletions test/Nirum/PackageSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Nirum.Package.Metadata ( Metadata ( Metadata
, version
, description
, license
, keywords
)
, MetadataError (FormatError)
, Target (targetName)
Expand All @@ -64,6 +65,7 @@ createValidPackage t = createPackage Metadata { version = SV.initial
, authors = []
, description = Nothing
, license = Nothing
, keywords = []
, target = t
} validModules

Expand Down Expand Up @@ -113,6 +115,7 @@ testPackage target' = do
, authors = []
, description = Nothing
, license = Nothing
, keywords = []
, target = target'
}
metadata package `shouldBe` metadata'
Expand Down
5 changes: 4 additions & 1 deletion test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import Nirum.Package.Metadata ( Author (Author, email, name, uri)
, target
, version
, description
, license)
, license
, keywords
)
, Target (compilePackage)
)
import qualified Nirum.Package.ModuleSet as MS
Expand Down Expand Up @@ -102,6 +104,7 @@ makeDummySource' pathPrefix m renames =
]
, description = Just "Package description"
, license = Just "MIT"
, keywords = ["sample", "example", "nirum"]
, target = Python "sample-package" minimumRuntime renames
}
pkg :: Package Python
Expand Down
1 change: 1 addition & 0 deletions test/nirum_fixture/package.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version = "0.3.0"
description = "Package description"
license = "MIT"
keywords = ["sample", "example", "nirum"]

[[authors]]
name = "nirum"
Expand Down