summaryrefslogtreecommitdiff
path: root/Portage/PackageId.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Portage/PackageId.hs')
-rw-r--r--Portage/PackageId.hs39
1 files changed, 37 insertions, 2 deletions
diff --git a/Portage/PackageId.hs b/Portage/PackageId.hs
index 2aa8f0a..ffaa324 100644
--- a/Portage/PackageId.hs
+++ b/Portage/PackageId.hs
@@ -13,6 +13,7 @@ module Portage.PackageId (
parseFriendlyPackage,
normalizeCabalPackageName,
normalizeCabalPackageId,
+ filePathToPackageId,
packageIdToFilePath,
cabal_pn_to_PN
) where
@@ -31,7 +32,7 @@ import Text.PrettyPrint ((<>))
import qualified Data.Char as Char (isAlphaNum, isSpace, toLower)
import Distribution.Text(display)
-import System.FilePath ( (</>) )
+import System.FilePath ((</>), dropExtension)
#if MIN_VERSION_base(4,11,0)
import Prelude hiding ((<>))
@@ -40,7 +41,7 @@ import Prelude hiding ((<>))
newtype Category = Category { unCategory :: String }
deriving (Eq, Ord, Show, Read)
-data PackageName = PackageName Category Cabal.PackageName
+data PackageName = PackageName { category :: Category, cabalPkgName :: Cabal.PackageName }
deriving (Eq, Ord, Show, Read)
data PackageId = PackageId { packageId :: PackageName, pkgVersion :: Portage.Version }
@@ -67,6 +68,26 @@ packageIdToFilePath (PackageId (PackageName cat pn) version) =
a <-> b = a ++ '-':b
a <.> b = a ++ '.':b
+-- | Attempt to generate a PackageId from a FilePath. If not, return
+-- the provided PackageId as-is.
+filePathToPackageId :: PackageId -> FilePath -> PackageId
+filePathToPackageId pkgId fp = do
+ -- take package name from provided FilePath
+ let pn = take (length
+ $ Cabal.unPackageName . cabalPkgName . packageId
+ $ pkgId) fp
+ -- drop .ebuild file extension
+ p = dropExtension fp
+ -- drop package name and the following dash
+ v = drop ((length pn) +1) p
+ c = unCategory . category . packageId $ pkgId
+ -- parse and extract version
+ parsed_v = case parseVersion v of
+ Just (Just my_v) -> my_v
+ _ -> pkgVersion pkgId
+ -- Construct PackageId
+ PackageId (mkPackageName c pn) parsed_v
+
mkPackageName :: String -> String -> PackageName
mkPackageName cat package = PackageName (Category cat) (Cabal.mkPackageName package)
@@ -133,5 +154,19 @@ parseFriendlyPackage str =
return (Just v)
return (mc, p, mv)
+-- | Parse a String in the form of a Portage version
+parseVersion :: FilePath -> Maybe (Maybe Portage.Version)
+parseVersion str =
+ case [ p | (p,s) <- Parse.readP_to_S parser str
+ , all Char.isSpace s ] of
+ [] -> Nothing
+ (x:_) -> Just x
+ where
+ parser = do
+ mv <- Parse.option Nothing $ do
+ v <- parse
+ return (Just v)
+ return mv
+
cabal_pn_to_PN :: Cabal.PackageName -> String
cabal_pn_to_PN = map toLower . display