-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathStackageHome.hs
174 lines (151 loc) · 6.61 KB
/
StackageHome.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
module Handler.StackageHome
( getStackageHomeR
, getStackageDiffR
, getStackageCabalConfigR
, getDocsR
, getSnapshotPackagesR
) where
import Data.These
import RIO (textDisplay)
import RIO.Time (FormatTime)
import Import
import Stackage.Database
import Stackage.Snapshot.Diff
getStackageHomeR :: SnapName -> Handler TypedContent
getStackageHomeR name =
track "Handler.StackageHome.getStackageHomeR" $ do
cacheSeconds $ 60 * 60 * 12
Entity sid snapshot <- lookupSnapshot name >>= maybe notFound return
previousSnapName <- fromMaybe name . map snd <$> snapshotBefore (snapshotName snapshot)
let hoogleForm =
let queryText = "" :: Text
exact = False
mPackageName = Nothing :: Maybe Text
in $(widgetFile "hoogle-form")
packages <- getPackagesForSnapshot sid
let packageCount = length packages
selectRep $ do
provideRep $
defaultLayout $ do
setTitle $ toHtml $ snapshotTitle snapshot
$(widgetFile "stackage-home")
provideRep $ pure $ toJSON $ SnapshotInfo snapshot packages
where
strip x = fromMaybe x (stripSuffix "." x)
data SnapshotInfo
= SnapshotInfo { snapshot :: Snapshot
, packages :: [PackageListingInfo]
}
instance ToJSON SnapshotInfo where
toJSON SnapshotInfo{..} = object [ "snapshot" .= snapshot
, "packages" .= packages
]
getStackageDiffR :: SnapName -> SnapName -> Handler TypedContent
getStackageDiffR name1 name2 = track "Handler.StackageHome.getStackageDiffR" $ do
cacheSeconds $ 60 * 60 * 48
Entity sid1 _ <- lookupSnapshot name1 >>= maybe notFound return
Entity sid2 _ <- lookupSnapshot name2 >>= maybe notFound return
snapDiff <- getSnapshotDiff sid1 sid2
selectRep $ do
provideRep $ defaultLayout $ do
setTitle $ "Compare " ++ toHtml (toPathPiece name1) ++ " with "
++ toHtml (toPathPiece name2)
$(widgetFile "stackage-diff")
provideRep $ pure $ toJSON $ WithSnapshotNames name1 name2 snapDiff
getStackageCabalConfigR :: SnapName -> Handler TypedContent
getStackageCabalConfigR name = track "Handler.StackageHome.getStackageCabalConfigR" $ do
cacheSeconds $ 60 * 60 * 48
Entity sid snapshot <- lookupSnapshot name >>= maybe notFound return
render <- getUrlRender
mdownload <- lookupGetParam "download"
when (mdownload == Just "true") $
addHeader "Content-Disposition" "attachment; filename=cabal.config"
mglobal <- lookupGetParam "global"
let isGlobal = mglobal == Just "true"
plis <- getPackagesForSnapshot sid
respondSource typePlain $ yieldMany plis .|
if isGlobal
then conduitGlobal (snapshotCompiler snapshot) render
else conduitLocal (snapshotCompiler snapshot) render
where
-- FIXME move this stuff into stackage-common
conduitGlobal compiler render = do
headerGlobal render
compilerVersion compiler
mapC (Chunk . showPackageGlobal)
conduitLocal compiler render = do
headerLocal render
compilerVersion compiler
goFirst
mapC (Chunk . showPackageLocal)
yield $ Chunk $ toBuilder '\n'
revisionsWarning =
toBuilder (asText "-- NOTE: Due to revisions, this file may not work. See:\n-- https://github.com/fpco/stackage-server/issues/232\n\n")
headerGlobal render = yield $ Chunk $
revisionsWarning ++
toBuilder (asText "-- Stackage snapshot from: ") ++
toBuilder (oldSnapshotUrl render) ++
toBuilder (asText "\n-- Please append these contents to the end of your global cabal config file.\n-- To only use tested packages, uncomment the following line\n-- and comment out other remote-repo lines:\n-- remote-repo: stackage-") ++
toBuilder (toPathPiece name) ++
toBuilder ':' ++
toBuilder (snapshotUrl render) ++
toBuilder '\n'
headerLocal render = yield $ Chunk $
revisionsWarning ++
toBuilder (asText "-- Stackage snapshot from: ") ++
toBuilder (oldSnapshotUrl render) ++
toBuilder (asText "\n-- Please place this file next to your .cabal file as cabal.config\n-- To only use tested packages, uncomment the following line:\n-- remote-repo: stackage-") ++
toBuilder (toPathPiece name) ++
toBuilder ':' ++
toBuilder (snapshotUrl render) ++
toBuilder '\n'
compilerVersion compiler = yield $ Chunk $
toBuilder (asText "with-compiler: ") ++
toBuilder (textDisplay compiler) ++
toBuilder '\n'
oldSnapshotUrl render = asHttp $ render $ OldSnapshotR (toPathPiece name) []
snapshotUrl render = asHttp $ render $ SnapshotR name StackageHomeR
asHttp (stripPrefix "http://" -> Just s) = "http://" <> s
asHttp (stripPrefix "https://" -> Just s) = "http://" <> s
asHttp (stripPrefix "//" -> Just s) = "http://" <> s
asHttp s = error $ "Unexpected url prefix: " <> unpack s
constraint p
| pliOrigin p == Core = toBuilder $ asText " installed"
| otherwise = toBuilder (asText " ==") ++
toBuilder (pliVersion p)
showPackageGlobal p =
toBuilder (asText "constraint: ") ++
toBuilder (pliName p) ++
constraint p ++
toBuilder '\n'
goFirst = do
mx <- await
forM_ mx $ \p -> yield $ Chunk $
toBuilder (asText "constraints: ") ++
toBuilder (pliName p) ++
constraint p
showPackageLocal p =
toBuilder (asText ",\n ") ++
toBuilder (pliName p) ++
constraint p
yearMonthDay :: FormatTime t => t -> String
yearMonthDay = formatTime defaultTimeLocale "%Y-%m-%d"
getSnapshotPackagesR :: SnapName -> Handler () -- FIXME move to OldLinks?
getSnapshotPackagesR name = track "Handler.StackageHome.getSnapshotPackagesR" $
redirect $ SnapshotR name StackageHomeR
getDocsR :: SnapName -> Handler Html
getDocsR name = track "Handler.StackageHome.getDocsR" $ do
cacheSeconds $ 60 * 60 * 48
Entity sid _ <- lookupSnapshot name >>= maybe notFound return
mlis <- getSnapshotModules sid
render <- getUrlRender
let mliUrl mli = render $ haddockUrl name mli
defaultLayout $ do
setTitle $ toHtml $ "Module list for " ++ toPathPiece name
$(widgetFile "doc-list")