Skip to content

Commit

Permalink
Add extra data field to PackageInfo to smuggle executables
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri committed Mar 13, 2024
1 parent 7656d63 commit e2ae9b3
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 36 deletions.
10 changes: 10 additions & 0 deletions assets/css/2-components/9-terminal-icon.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* stylelint-disable selector-class-pattern */
/* stylelint-disable declaration-block-no-redundant-longhand-properties */

.terminal-icon {
height: 1.25rem;
width: 1.25rem;
display: inline;
margin-top: -0.25rem;
}

1 change: 1 addition & 0 deletions assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@import "2-components/6-secondary-search.css";
@import "2-components/7-button.css";
@import "2-components/8-alert.css";
@import "2-components/9-terminal-icon.css";

@import "3-screens/1-package/1-package.css";
@import "3-screens/1-package/2-release-changelog.css";
Expand Down
10 changes: 8 additions & 2 deletions src/core/Flora/Model/Package/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ getPackagesFromCategoryWithLatestVersion categoryId = dbtToEff $ query Select q
, lv.version
, lv.license
, 1
, array[]
from latest_versions as lv
inner join package_categories as p1 on p1.package_id = lv.package_id
inner join categories as c2 on c2.category_id = p1.category_id
Expand All @@ -499,6 +500,7 @@ searchPackage (offset, limit) searchString =
, lv."version"
, lv."license"
, word_similarity(lv.name, ?) as rating
, array[]
FROM latest_versions as lv
WHERE ? <% lv.name
GROUP BY
Expand Down Expand Up @@ -531,6 +533,7 @@ searchPackageByNamespace (offset, limit) namespace searchString =
, lv."version"
, lv."license"
, word_similarity(lv.name, ?) as rating
, array[]
FROM latest_versions as lv
WHERE
? <% lv."name"
Expand Down Expand Up @@ -563,12 +566,13 @@ searchExecutable (offset, limit) searchString =
, l2.synopsis
, l2.version
, l2.license
, word_similarity(l2.name, ?) AS rating
, word_similarity(p0.component_name, ?) AS rating
, array(select p0.component_name)
FROM package_components AS p0
INNER JOIN releases AS r1 ON p0.release_id = r1.release_id
INNER JOIN latest_versions AS l2 ON r1.package_id = l2.package_id
WHERE p0.component_type = 'executable'
AND ? <% l2.name
AND ? <% p0.component_name
ORDER BY rating DESC
, l2.name ASC
OFFSET ?
Expand All @@ -594,6 +598,7 @@ listAllPackages (offset, limit) =
, lv."version"
, lv."license"
, (1.0::real) as rating
, array[]
FROM latest_versions as lv
GROUP BY
lv."namespace"
Expand Down Expand Up @@ -626,6 +631,7 @@ listAllPackagesInNamespace (offset, limit) namespace =
, lv."version"
, lv."license"
, (1.0::real) as rating
, array[]
FROM latest_versions as lv
WHERE lv."namespace" = ?
GROUP BY
Expand Down
1 change: 1 addition & 0 deletions src/core/Flora/Model/Package/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ data PackageInfo = PackageInfo
, version :: Version
, license :: SPDX.License
, rating :: Maybe Double
, extraData :: Vector Text
}
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (FromRow, NFData)
Expand Down
31 changes: 23 additions & 8 deletions src/core/Flora/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,30 @@ search
:: (DB :> es, Log :> es, Time :> es)
=> (Word, Word)
-> Text
-> Eff es (Word, Vector PackageInfo)
-> Eff es (SearchAction, (Word, Vector PackageInfo))
search pagination queryString =
case parseSearchQuery queryString of
Just (ListAllPackagesInNamespace namespace) -> listAllPackagesInNamespace pagination namespace
Just ListAllPackages -> listAllPackages pagination
Just (SearchInNamespace namespace (PackageName packageName)) -> searchPackageByNamespaceAndName pagination namespace packageName
Just (DependentsOf namespace packageName mSearchString) -> searchDependents pagination namespace packageName mSearchString
Just (SearchPackages _) -> searchPackageByName pagination queryString
Just (SearchExecutable executableName) -> searchExecutable pagination executableName
Nothing -> searchPackageByName pagination queryString
Just (ListAllPackagesInNamespace namespace) -> do
result <- listAllPackagesInNamespace pagination namespace
pure (ListAllPackagesInNamespace namespace, result)
Just ListAllPackages -> do
result <- listAllPackages pagination
pure (ListAllPackages, result)
Just (SearchInNamespace namespace p@(PackageName packageName)) -> do
result <- searchPackageByNamespaceAndName pagination namespace packageName
pure (SearchInNamespace namespace p, result)
Just (DependentsOf namespace packageName mSearchString) -> do
result <- searchDependents pagination namespace packageName mSearchString
pure (DependentsOf namespace packageName mSearchString, result)
Just (SearchExecutable executableName) -> do
result <- searchExecutable pagination executableName
pure (SearchExecutable executableName, result)
Just (SearchPackages _) -> do
result <- searchPackageByName pagination queryString
pure (SearchPackages queryString, result)
Nothing -> do
result <- searchPackageByName pagination queryString
pure (SearchPackages queryString, result)

searchPackageByName
:: (DB :> es, Log :> es, Time :> es)
Expand Down Expand Up @@ -175,6 +189,7 @@ dependencyInfoToPackageInfo dep =
dep.latestVersion
dep.latestLicense
Nothing
Vector.empty

listAllPackagesInNamespace
:: (DB :> es, Time :> es, Log :> es)
Expand Down
20 changes: 19 additions & 1 deletion src/web/FloraWeb/Components/Icons.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ module FloraWeb.Components.Icons
, lookingGlass
, information
, exception
, terminal
, license
) where

import Data.Text (Text)
import Lucid
import Lucid.Svg
( d_
( clip_rule_
, d_
, fill_
, fill_rule_
, path_
, stroke_
, stroke_linecap_
Expand Down Expand Up @@ -75,3 +79,17 @@ exception =
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z" />
</svg>
|]

terminal :: FloraHTML
terminal =
toHtmlRaw @Text
[str|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="terminal-icon">
<path stroke-linecap="round" stroke-linejoin="round" d="m6.75 7.5 3 2.25-3 2.25m4.5 0h3m-9 8.25h13.5A2.25 2.25 0 0 0 21 18V6a2.25 2.25 0 0 0-2.25-2.25H5.25A2.25 2.25 0 0 0 3 6v12a2.25 2.25 0 0 0 2.25 2.25Z" />
</svg>
|]

license :: FloraHTML
license =
svg_ [xmlns_ "http://www.w3.org/2000/svg", viewBox_ "0 0 20 20", fill_ "currentColor", class_ "license-icon"] $
path_ [fill_rule_ "evenodd", d_ "M10 2a.75.75 0 01.75.75v.258a33.186 33.186 0 016.668.83.75.75 0 01-.336 1.461 31.28 31.28 0 00-1.103-.232l1.702 7.545a.75.75 0 01-.387.832A4.981 4.981 0 0115 14c-.825 0-1.606-.2-2.294-.556a.75.75 0 01-.387-.832l1.77-7.849a31.743 31.743 0 00-3.339-.254v11.505a20.01 20.01 0 013.78.501.75.75 0 11-.339 1.462A18.558 18.558 0 0010 17.5c-1.442 0-2.845.165-4.191.477a.75.75 0 01-.338-1.462 20.01 20.01 0 013.779-.501V4.509c-1.129.026-2.243.112-3.34.254l1.771 7.85a.75.75 0 01-.387.83A4.98 4.98 0 015 14a4.98 4.98 0 01-2.294-.556.75.75 0 01-.387-.832L4.02 5.067c-.37.07-.738.148-1.103.232a.75.75 0 01-.336-1.462 32.845 32.845 0 016.668-.829V2.75A.75.75 0 0110 2zM5 7.543L3.92 12.33a3.499 3.499 0 002.16 0L5 7.543zm10 0l-1.08 4.787a3.498 3.498 0 002.16 0L15 7.543z", clip_rule_ "evenodd"]
21 changes: 10 additions & 11 deletions src/web/FloraWeb/Components/PackageListItem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module FloraWeb.Components.PackageListItem
( packageListItem
, requirementListItem
, licenseIcon
)
where

Expand All @@ -12,6 +11,7 @@ import Data.List (intersperse, sortOn)
import Data.Map qualified as Map
import Data.Text (Text)
import Data.Text.Display (display)
import Data.Vector (Vector)
import Data.Vector qualified as Vector
import FloraWeb.Pages.Templates (FloraHTML)
import Lucid
Expand All @@ -24,11 +24,11 @@ import Flora.Model.Requirement
( ComponentDependencies
, DependencyInfo (..)
)
import FloraWeb.Components.Icons qualified as Icon
import Lucid.Orphans ()
import Lucid.Svg (clip_rule_, d_, fill_, fill_rule_, path_, viewBox_)

packageListItem :: (Namespace, PackageName, Text, Version, SPDX.License) -> FloraHTML
packageListItem (namespace, packageName, synopsis, version, license) = do
packageListItem :: (Namespace, PackageName, Text, Version, SPDX.License, Vector Text) -> FloraHTML
packageListItem (namespace, packageName, synopsis, version, license, extraData) = do
let href = href_ ("/packages/" <> display namespace <> "/" <> display packageName)
li_ [class_ "package-list-item"] $
a_ [href, class_ ""] $ do
Expand All @@ -38,9 +38,13 @@ packageListItem (namespace, packageName, synopsis, version, license) = do
p_ [class_ "package-list-item__synopsis"] $ toHtml synopsis
div_ [class_ "package-list-item__metadata"] $ do
span_ [class_ "package-list-item__license"] $ do
licenseIcon
Icon.license
toHtml license
span_ [class_ "package-list-item__version"] $ "v" <> toHtml version
Vector.forM_ extraData $ \ed ->
span_ [class_ "package-list-item__extra_data"] $ do
Icon.terminal
toHtml ed

requirementListItem :: ComponentDependencies -> FloraHTML
requirementListItem allComponentDeps =
Expand Down Expand Up @@ -77,7 +81,7 @@ componentListItems DependencyInfo{namespace, name = packageName, latestSynopsis,
p_ [class_ "package-list-item__synopsis"] $ toHtml latestSynopsis
div_ [class_ "package-list-item__metadata"] $ do
span_ [class_ "package-list-item__license"] $ do
licenseIcon
Icon.license
toHtml latestLicense
displayVersionRange requirement

Expand All @@ -86,8 +90,3 @@ displayVersionRange versionRange =
if versionRange == ">=0"
then ""
else span_ [class_ "package-list-item__version-range"] $ toHtml versionRange

licenseIcon :: FloraHTML
licenseIcon =
svg_ [xmlns_ "http://www.w3.org/2000/svg", viewBox_ "0 0 20 20", fill_ "currentColor", class_ "license-icon"] $
path_ [fill_rule_ "evenodd", d_ "M10 2a.75.75 0 01.75.75v.258a33.186 33.186 0 016.668.83.75.75 0 01-.336 1.461 31.28 31.28 0 00-1.103-.232l1.702 7.545a.75.75 0 01-.387.832A4.981 4.981 0 0115 14c-.825 0-1.606-.2-2.294-.556a.75.75 0 01-.387-.832l1.77-7.849a31.743 31.743 0 00-3.339-.254v11.505a20.01 20.01 0 013.78.501.75.75 0 11-.339 1.462A18.558 18.558 0 0010 17.5c-1.442 0-2.845.165-4.191.477a.75.75 0 01-.338-1.462 20.01 20.01 0 013.779-.501V4.509c-1.129.026-2.243.112-3.34.254l1.771 7.85a.75.75 0 01-.387.83A4.98 4.98 0 015 14a4.98 4.98 0 01-2.294-.556.75.75 0 01-.387-.832L4.02 5.067c-.37.07-.738.148-1.103.232a.75.75 0 01-.336-1.462 32.845 32.845 0 016.668-.829V2.75A.75.75 0 0110 2zM5 7.543L3.92 12.33a3.499 3.499 0 002.16 0L5 7.543zm10 0l-1.08 4.787a3.498 3.498 0 002.16 0L15 7.543z", clip_rule_ "evenodd"]
21 changes: 19 additions & 2 deletions src/web/FloraWeb/Pages/Server/Search.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module FloraWeb.Pages.Server.Search where

import Data.List qualified as List
import Data.Positive
import Data.Text (Text)
import Data.Vector (Vector)
import Data.Vector qualified as Vector
import Lucid (Html)
import Servant (Headers (..), ServerT)

import Data.Function ((&))
import Flora.Model.Package.Types
import Flora.Model.User (User)
import Flora.Search qualified as Search
Expand All @@ -31,6 +34,20 @@ searchHandler (Headers session _) (Just searchString) pageParam = do
templateDefaults
{ navbarSearchContent = Just searchString
}
(count, results) <- Search.search (fromPage pageNumber) searchString
(searchAction, (count, results)) <- Search.search (fromPage pageNumber) searchString
let (matchVector, packagesInfo) = Vector.partition (\p -> p.name == PackageName searchString) results
render templateEnv $ Search.showResults searchString count pageNumber matchVector packagesInfo
case searchAction of
Search.SearchExecutable _ ->
let packagesInfo' =
packagesInfo
& Vector.groupBy (\p1 p2 -> p1.name == p2.name && p1.namespace == p2.namespace)
& List.map accumulateExtraData
& Vector.fromList
in render templateEnv $
Search.showExecutableResults searchString count pageNumber matchVector packagesInfo'
_ ->
render templateEnv $
Search.showResults searchString count pageNumber matchVector packagesInfo

accumulateExtraData :: Vector PackageInfo -> PackageInfo
accumulateExtraData = Vector.foldl1' (\acc packageInfo -> acc{extraData = acc.extraData <> packageInfo.extraData})
23 changes: 12 additions & 11 deletions src/web/FloraWeb/Pages/Templates/Packages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import Flora.Model.Package
import Flora.Model.Release.Types
import Flora.Model.Requirement
import Flora.Search (SearchAction (..))
import FloraWeb.Components.Icons
import FloraWeb.Components.PackageListItem (licenseIcon, packageListItem, requirementListItem)
import FloraWeb.Components.Icons qualified as Icon
import FloraWeb.Components.PackageListItem (packageListItem, requirementListItem)
import FloraWeb.Components.PaginationNav (paginationNav)
import FloraWeb.Components.Pill (customBuildType)
import FloraWeb.Components.Utils
Expand Down Expand Up @@ -66,9 +66,9 @@ presentationHeaderForSubpage namespace packageName release target numberOfPackag
div_ [class_ "page-title"] $ h1_ [class_ ""] $ do
span_ [class_ "headline"] $ do
displayNamespace namespace
chevronRightOutline
Icon.chevronRightOutline
linkToPackageWithVersion namespace packageName release.version
chevronRightOutline
Icon.chevronRightOutline
toHtml (display target)
p_ [class_ "synopsis"] $
span_ [class_ "version"] $
Expand All @@ -85,9 +85,9 @@ presentationHeaderForVersions namespace packageName numberOfReleases = div_ [cla
div_ [class_ "page-title"] $ h1_ [class_ ""] $ do
span_ [class_ "headline"] $ do
displayNamespace namespace
chevronRightOutline
Icon.chevronRightOutline
linkToPackage namespace packageName
chevronRightOutline
Icon.chevronRightOutline
toHtml (display Versions)
p_ [class_ "synopsis"] $
span_ [class_ "version"] $
Expand Down Expand Up @@ -116,6 +116,7 @@ showDependents namespace packageName release count packagesInfo currentPage =
, dep.latestSynopsis
, dep.latestVersion
, dep.latestLicense
, Vector.empty
)
)
when (count > 30) $
Expand Down Expand Up @@ -157,7 +158,7 @@ versionListItem namespace packageName release = do
div_ [class_ "package-list-item__metadata"] $
span_ [class_ "package-list-item__license"] $
do
licenseIcon
Icon.license
toHtml release.license

-- | Render a list of package informations
Expand All @@ -172,10 +173,10 @@ packageListing mExactMatchItems packages =
whenJust mExactMatchItems $ \exactMatchItems ->
forM_ exactMatchItems $ \em ->
div_ [class_ "exact-match"] $
packageListItem (em.namespace, em.name, em.synopsis, em.version, em.license)
packageListItem (em.namespace, em.name, em.synopsis, em.version, em.license, em.extraData)
Vector.forM_
packages
( \PackageInfo{..} -> packageListItem (namespace, name, synopsis, version, license)
( \PackageInfo{..} -> packageListItem (namespace, name, synopsis, version, license, extraData)
)

requirementListing :: ComponentDependencies -> FloraHTML
Expand Down Expand Up @@ -295,7 +296,7 @@ displayVersions namespace packageName versions numberOfReleases =
("Revised on " <> display (Time.formatTime defaultTimeLocale "%a, %_d %b %Y, %R %EZ" revisionDate))
, class_ "revised-date"
]
pen
Icon.pen

displayDependencies
:: (Namespace, PackageName, Version)
Expand Down Expand Up @@ -450,7 +451,7 @@ displayPackageFlags (ReleaseFlags packageFlags) =
[ dataText_ "Use the -f option with cabal commands to enable flags"
, class_ "instruction-tooltip"
]
usageInstructionTooltip
Icon.usageInstructionTooltip
ul_ [class_ "package-flags"] $
Vector.forM_ packageFlags displayPackageFlag

Expand Down
24 changes: 23 additions & 1 deletion src/web/FloraWeb/Pages/Templates/Screens/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ showAllPackages count currentPage packagesInfo = do
div_ [class_ ""] $ packageListing Nothing packagesInfo
paginationNav count currentPage ListAllPackages

showAllPackagesInNamespace :: Namespace -> Text -> Word -> Positive Word -> Vector PackageInfo -> FloraHTML
showAllPackagesInNamespace
:: Namespace
-> Text
-> Word
-> Positive Word
-> Vector PackageInfo
-> FloraHTML
showAllPackagesInNamespace namespace description count currentPage packagesInfo = do
div_ [class_ "container"] $ do
presentationHeader (display namespace) description count
Expand All @@ -43,3 +49,19 @@ showResults searchString count currentPage exactMatches results = do
packageListing (Just exactMatches) results
when (count > 30) $
paginationNav count currentPage (SearchPackages searchString)

showExecutableResults
:: Text
-> Word
-> Positive Word
-> Vector PackageInfo
-- ^ Exact matches
-> Vector PackageInfo
-- ^ Results
-> FloraHTML
showExecutableResults executableName count currentPage exactMatches results = do
div_ [class_ "container"] $ do
presentationHeader executableName "" count
packageListing (Just exactMatches) results
when (count > 30) $
paginationNav count currentPage (SearchExecutable executableName)

0 comments on commit e2ae9b3

Please sign in to comment.