Skip to content

Commit 88067f9

Browse files
committed
Factor out functions common to readCabal' and fromInstalledPackage
1 parent 393d7f1 commit 88067f9

1 file changed

Lines changed: 48 additions & 45 deletions

File tree

src/Input/Cabal.hs

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import Distribution.Types.LibraryVisibility (LibraryVisibility(..))
4242
import Distribution.Types.PackageDescription (license')
4343
import Distribution.Types.PackageId (pkgVersion)
4444
import Distribution.Types.PackageName (unPackageName)
45-
import Distribution.Types.Version (versionNumbers)
46-
import Distribution.Utils.ShortText (fromShortText)
45+
import Distribution.Types.Version (Version, versionNumbers)
46+
import Distribution.Utils.ShortText (ShortText, fromShortText)
4747
import Hackage.RevDeps (lastVersionsOfPackages)
4848
import qualified Distribution.SPDX as SPDX
4949

@@ -148,7 +148,7 @@ fromInstalledPackage ::
148148
Map UnitId IPI.InstalledPackageInfo ->
149149
IPI.InstalledPackageInfo ->
150150
Package
151-
fromInstalledPackage Settings{..} installedPackages ipi = Package{..}
151+
fromInstalledPackage settings installedPackages ipi = Package{..}
152152
where
153153
pkgId = packageId ipi
154154

@@ -160,35 +160,17 @@ fromInstalledPackage Settings{..} installedPackages ipi = Package{..}
160160
(Map.lookup unitId installedPackages)
161161
)
162162
(IPI.depends ipi)
163-
packageVersion = strPack $ intercalate "." $ map show $ versionNumbers $ pkgVersion pkgId
163+
packageVersion = mkPackageVersion $ pkgVersion pkgId
164164
packageSynopsis = strPack $ fromShortText $ IPI.synopsis ipi
165165
packageLibrary = IPI.libVisibility ipi == LibraryVisibilityPublic
166166
packageDocs = listToMaybe $ IPI.haddockHTMLs ipi
167167

168-
unpackLicenseExpression (SPDX.EOr x y) = unpackLicenseExpression x ++ unpackLicenseExpression y
169-
unpackLicenseExpression x = [x]
170-
171-
packageLicenses = case license' $ IPI.license ipi of
172-
SPDX.NONE -> []
173-
SPDX.License licExpr -> map (show . Distribution.Pretty.pretty) $
174-
unpackLicenseExpression licExpr
175-
packageCategories =
176-
filter (not . null) $ split (`elem` " ,") $
177-
fromShortText $ IPI.category ipi
168+
packageLicenses = mkPackageLicenses . license' $ IPI.license ipi
169+
packageCategories = mkPackageCategories $ IPI.category ipi
178170
packageAuthor = fromShortText $ IPI.author ipi
179171
packageMaintainer = fromShortText $ IPI.maintainer ipi
180172

181-
packageTags = map (both strPack) $ nubOrd $ concat
182-
[ map ("license",) packageLicenses
183-
, map ("category",) packageCategories
184-
, map ("author",) (concatMap cleanup [packageAuthor, packageMaintainer])
185-
]
186-
187-
-- split on things like "," "&" "and", then throw away email addresses, replace spaces with "-" and rename
188-
cleanup =
189-
filter (/= "") .
190-
map (renameTag . intercalate "-" . filter ('@' `notElem`) . words . takeWhile (`notElem` "<(")) .
191-
concatMap (map unwords . split (== "and") . words) . split (`elem` ",&")
173+
packageTags = mkPackageTags settings packageLicenses packageCategories [packageAuthor, packageMaintainer]
192174

193175
-- | Given a tarball of Cabal files, parse the latest version of each package.
194176
parseCabalTarball :: Settings -> FilePath -> IO (Map.Map PkgName Package)
@@ -213,38 +195,59 @@ readCabal settings src = case PD.parseGenericPackageDescriptionMaybe src of
213195
Just gpd -> readCabal' settings gpd
214196

215197
readCabal' :: Settings -> PD.GenericPackageDescription -> Package
216-
readCabal' Settings{..} gpd = Package{..}
198+
readCabal' settings gpd = Package{..}
217199
where
218200
pd = PD.flattenPackageDescription gpd
219201
pkgId = PD.package pd
220202

221203
packageDepends = nubOrd $ foldMap (map (\(PD.Dependency pkg _ _) -> pkg) . PD.targetBuildDepends) $ toListOf Lens.traverseBuildInfos gpd
222-
packageVersion = strPack $ intercalate "." $ map show $ versionNumbers $ PD.pkgVersion pkgId
204+
packageVersion = mkPackageVersion $ PD.pkgVersion pkgId
223205
packageSynopsis = strPack $ fromShortText $ PD.synopsis pd
224206
packageLibrary = PD.hasPublicLib pd
225207
packageDocs = Nothing
226208

227-
unpackLicenseExpression (SPDX.EOr x y) = unpackLicenseExpression x ++ unpackLicenseExpression y
228-
unpackLicenseExpression x = [x]
209+
packageLicenses = mkPackageLicenses $ PD.license pd
210+
packageCategories = mkPackageCategories $ PD.category pd
211+
packageAuthor = fromShortText $ PD.author pd
212+
packageMaintainer = fromShortText $ PD.maintainer pd
213+
214+
packageTags = mkPackageTags settings packageLicenses packageCategories [packageAuthor, packageMaintainer]
215+
216+
mkPackageVersion :: Version -> Str
217+
mkPackageVersion = strPack . intercalate "." . map show . versionNumbers
229218

230-
packageLicenses = case PD.license pd of
219+
mkPackageLicenses :: SPDX.License -> [String]
220+
mkPackageLicenses license =
221+
case license of
231222
SPDX.NONE -> []
232223
SPDX.License licExpr -> map (show . Distribution.Pretty.pretty) $
233224
unpackLicenseExpression licExpr
234-
packageCategories =
235-
filter (not . null) $ split (`elem` " ,") $
236-
fromShortText $ PD.category pd
237-
packageAuthor = fromShortText $ PD.author pd
238-
packageMaintainer = fromShortText $ PD.maintainer pd
225+
where
226+
unpackLicenseExpression (SPDX.EOr x y) = unpackLicenseExpression x ++ unpackLicenseExpression y
227+
unpackLicenseExpression x = [x]
239228

240-
packageTags = map (both strPack) $ nubOrd $ concat
241-
[ map ("license",) packageLicenses
242-
, map ("category",) packageCategories
243-
, map ("author",) (concatMap cleanup [packageAuthor, packageMaintainer])
244-
]
229+
mkPackageCategories :: ShortText -> [String]
230+
mkPackageCategories = filter (not . null) . split (`elem` " ,") . fromShortText
245231

246-
-- split on things like "," "&" "and", then throw away email addresses, replace spaces with "-" and rename
247-
cleanup =
248-
filter (/= "") .
249-
map (renameTag . intercalate "-" . filter ('@' `notElem`) . words . takeWhile (`notElem` "<(")) .
250-
concatMap (map unwords . split (== "and") . words) . split (`elem` ",&")
232+
mkPackageTags ::
233+
Settings ->
234+
-- | Licenses
235+
[String] ->
236+
-- | Categories
237+
[String] ->
238+
-- | Authors
239+
[String] ->
240+
[(Str, Str)]
241+
mkPackageTags settings licenses categories authors =
242+
map (both strPack) $ nubOrd $ concat
243+
[ map ("license",) licenses
244+
, map ("category",) categories
245+
, map ("author",) (concatMap (cleanup settings) authors)
246+
]
247+
248+
-- split on things like "," "&" "and", then throw away email addresses, replace spaces with "-" and rename
249+
cleanup :: Settings -> String -> [String]
250+
cleanup Settings{..} =
251+
filter (/= "") .
252+
map (renameTag . intercalate "-" . filter ('@' `notElem`) . words . takeWhile (`notElem` "<(")) .
253+
concatMap (map unwords . split (== "and") . words) . split (`elem` ",&")

0 commit comments

Comments
 (0)