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 licenseIdMigrationMessages. Resolves #5109. #5113

Merged
merged 1 commit into from
Feb 6, 2018
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
7 changes: 7 additions & 0 deletions Cabal/Distribution/SPDX/License.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ instance Pretty License where
pretty NONE = Disp.text "NONE"
pretty (License l) = pretty l

-- |
-- >>> eitherParsec "BSD-3-Clause AND MIT" :: Either String License
-- Right (License (EAnd (ELicense (ELicenseId BSD_3_Clause) Nothing) (ELicense (ELicenseId MIT) Nothing)))
--
-- >>> eitherParsec "NONE" :: Either String License
-- Right NONE
--
instance Parsec License where
parsec = NONE <$ P.try (P.string "NONE") <|> License <$> parsec
2 changes: 1 addition & 1 deletion Cabal/Distribution/SPDX/LicenseExpression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ instance Parsec SimpleLicenseExpression where
l <- idstring
maybe (fail $ "Incorrect LicenseRef format:" ++ n) (return . ELicenseRef) $ mkLicenseRef (Just d) l
| otherwise = do
l <- maybe (fail $ "Unknown SPDX license identifier: " ++ n) return $ mkLicenseId n
l <- maybe (fail $ "Unknown SPDX license identifier: '" ++ n ++ "' " ++ licenseIdMigrationMessage n) return $ mkLicenseId n
orLater <- isJust <$> P.optional (P.char '+')
if orLater
then return (ELicenseIdPlus l)
Expand Down
43 changes: 42 additions & 1 deletion Cabal/Distribution/SPDX/LicenseId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Distribution.SPDX.LicenseId (
licenseName,
licenseIsOsiApproved,
mkLicenseId,
-- * Helpers
licenseIdMigrationMessage,
) where

import Distribution.Compat.Prelude
Expand Down Expand Up @@ -360,14 +362,53 @@ instance Binary LicenseId
instance Pretty LicenseId where
pretty = Disp.text . licenseId

-- |
-- >>> eitherParsec "BSD-3-Clause" :: Either String LicenseId
-- Right BSD_3_Clause
--
-- >>> eitherParsec "BSD3" :: Either String LicenseId
-- Left "...Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?"
--
instance Parsec LicenseId where
parsec = do
n <- some $ P.satisfy $ \c -> isAsciiAlphaNum c || c == '-' || c == '.'
maybe (fail $ "Unknown SPDX license identifier: " ++ n) return $ mkLicenseId n
maybe (fail $ "Unknown SPDX license identifier: '" ++ n ++ "' " ++ licenseIdMigrationMessage n) return $ mkLicenseId n

instance NFData LicenseId where
rnf l = l `seq` ()

-- | Help message for migrating from non-SDPX license identifiers.
--
-- Old 'License' is almost SDPX, except for 'BSD2', 'BSD3'. This function
-- suggests SPDX variant:
--
-- >>> licenseIdMigrationMessage "BSD3"
-- "Do you mean BSD-3-Clause?"
--
-- Also 'OtherLicense', 'AllRightsReserved', and 'PublicDomain' aren't
-- valid SPDX identifiers
--
-- >>> traverse_ (print . licenseIdMigrationMessage) [ "OtherLicense", "AllRightsReserved", "PublicDomain" ]
-- "SPDX license list contains plenty of licenses. See https://spdx.org/licenses/. Also they can be combined into complex expressions with AND and OR."
-- "You can use NONE as a value of license field."
-- "Public Domain is a complex matter. See https://wiki.spdx.org/view/Legal_Team/Decisions/Dealing_with_Public_Domain_within_SPDX_Files. Consider using a proper license."
--
-- For other common licenses their old license format coincides with the SPDX identifiers:
--
-- >>> traverse eitherParsec ["GPL-2.0", "GPL-3.0", "LGPL-2.1", "MIT", "ISC", "MPL-2.0", "Apache-2.0"] :: Either String [LicenseId]
-- Right [GPL_2_0,GPL_3_0,LGPL_2_1,MIT,ISC,MPL_2_0,Apache_2_0]
--
licenseIdMigrationMessage :: String -> String
licenseIdMigrationMessage = go where
go "BSD3" = "Do you mean BSD-3-Clause?"
go "BSD2" = "Do you mean BSD-2-Clause?"
go "AllRightsReserved" = "You can use NONE as a value of license field."
go "OtherLicense" = "SPDX license list contains plenty of licenses. See https://spdx.org/licenses/. Also they can be combined into complex expressions with AND and OR."
go "PublicDomain" = "Public Domain is a complex matter. See https://wiki.spdx.org/view/Legal_Team/Decisions/Dealing_with_Public_Domain_within_SPDX_Files. Consider using a proper license."

-- otherwise, we don't know
go _ = ""

-------------------------------------------------------------------------------
-- License Data
-------------------------------------------------------------------------------
Expand Down
43 changes: 42 additions & 1 deletion boot/SPDX.LicenseId.template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Distribution.SPDX.LicenseId (
licenseName,
licenseIsOsiApproved,
mkLicenseId,
-- * Helpers
licenseIdMigrationMessage,
) where

import Distribution.Compat.Prelude
Expand Down Expand Up @@ -33,14 +35,53 @@ instance Binary LicenseId
instance Pretty LicenseId where
pretty = Disp.text . licenseId

-- |
-- >>> eitherParsec "BSD-3-Clause" :: Either String LicenseId
-- Right BSD_3_Clause
--
-- >>> eitherParsec "BSD3" :: Either String LicenseId
-- Left "...Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?"
--
instance Parsec LicenseId where
parsec = do
n <- some $ P.satisfy $ \c -> isAsciiAlphaNum c || c == '-' || c == '.'
maybe (fail $ "Unknown SPDX license identifier: " ++ n) return $ mkLicenseId n
maybe (fail $ "Unknown SPDX license identifier: '" ++ n ++ "' " ++ licenseIdMigrationMessage n) return $ mkLicenseId n

instance NFData LicenseId where
rnf l = l `seq` ()

-- | Help message for migrating from non-SDPX license identifiers.
--
-- Old 'License' is almost SDPX, except for 'BSD2', 'BSD3'. This function
-- suggests SPDX variant:
--
-- >>> licenseIdMigrationMessage "BSD3"
-- "Do you mean BSD-3-Clause?"
--
-- Also 'OtherLicense', 'AllRightsReserved', and 'PublicDomain' aren't
-- valid SPDX identifiers
--
-- >>> traverse_ (print . licenseIdMigrationMessage) [ "OtherLicense", "AllRightsReserved", "PublicDomain" ]
-- "SPDX license list contains plenty of licenses. See https://spdx.org/licenses/. Also they can be combined into complex expressions with AND and OR."
-- "You can use NONE as a value of license field."
-- "Public Domain is a complex matter. See https://wiki.spdx.org/view/Legal_Team/Decisions/Dealing_with_Public_Domain_within_SPDX_Files. Consider using a proper license."
--
-- For other common licenses their old license format coincides with the SPDX identifiers:
--
-- >>> traverse eitherParsec ["GPL-2.0", "GPL-3.0", "LGPL-2.1", "MIT", "ISC", "MPL-2.0", "Apache-2.0"] :: Either String [LicenseId]
-- Right [GPL_2_0,GPL_3_0,LGPL_2_1,MIT,ISC,MPL_2_0,Apache_2_0]
--
licenseIdMigrationMessage :: String -> String
licenseIdMigrationMessage = go where
go "BSD3" = "Do you mean BSD-3-Clause?"
go "BSD2" = "Do you mean BSD-2-Clause?"
go "AllRightsReserved" = "You can use NONE as a value of license field."
go "OtherLicense" = "SPDX license list contains plenty of licenses. See https://spdx.org/licenses/. Also they can be combined into complex expressions with AND and OR."
go "PublicDomain" = "Public Domain is a complex matter. See https://wiki.spdx.org/view/Legal_Team/Decisions/Dealing_with_Public_Domain_within_SPDX_Files. Consider using a proper license."

-- otherwise, we don't know
go _ = ""

-------------------------------------------------------------------------------
-- License Data
-------------------------------------------------------------------------------
Expand Down