Skip to content

Commit 2029382

Browse files
committed
Factor out functions common to readCabal' and fromInstalledPackage
1 parent 1dc2030 commit 2029382

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

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

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

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

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

216198
readCabal' :: Settings -> PD.GenericPackageDescription -> Package
217-
readCabal' Settings{..} gpd = Package{..}
199+
readCabal' settings gpd = Package{..}
218200
where
219201
pd = PD.flattenPackageDescription gpd
220202
pkgId = PD.package pd
221203

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

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

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

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

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

0 commit comments

Comments
 (0)