1
- #!/usr/bin/env -S cabal --verbose=1 --index-state=2024 -04-09T14:49:48Z run --
1
+ #!/usr/bin/env -S cabal --verbose=1 --index-state=2025 -04-16T18:30:40Z run --
2
2
{- cabal:
3
3
build-depends:
4
4
base,
14
14
network-uri,
15
15
optparse-applicative ^>= 0.18,
16
16
ansi-wl-pprint >= 1,
17
- pandoc ^>= 3.1,
18
17
prettyprinter,
19
18
req,
20
19
text,
36
35
37
36
module Main (main ) where
38
37
39
- import Cabal.Plan
40
38
import qualified Control.Foldl as Foldl
41
39
import Data.Aeson
42
40
import Data.ByteString.Char8 (ByteString )
@@ -47,11 +45,11 @@ import Data.Map.Strict (Map)
47
45
import qualified Data.Map.Strict as Map
48
46
import Data.Maybe
49
47
import qualified Data.Text as Text
50
- import qualified Data.Text.IO as Text
51
48
import qualified Data.Text.Encoding as Text
49
+ import qualified Data.Text.IO as Text
52
50
import Data.Version
53
- import qualified GitHub
54
- import Network.HTTP.Client ( HttpExceptionContent ( .. ), HttpException ( .. ), responseStatus , responseHeaders )
51
+ import Network.HTTP.Client ( HttpException ( .. ), HttpExceptionContent ( .. ),
52
+ responseHeaders , responseStatus )
55
53
import Network.HTTP.Req
56
54
import Network.HTTP.Types.Header (hLocation )
57
55
import Network.HTTP.Types.Status (found302 )
@@ -60,7 +58,9 @@ import qualified Network.URI.Encode as URIE
60
58
import Options.Applicative
61
59
import Prettyprinter
62
60
import qualified Prettyprinter.Util as PP
63
- import qualified Text.Pandoc as Pandoc
61
+
62
+ import Cabal.Plan
63
+ import qualified GitHub
64
64
import Turtle
65
65
66
66
main :: IO ()
@@ -91,15 +91,8 @@ main = sh do
91
91
pure (n, v, changelogLocation)
92
92
93
93
-- generate a massive markdown table
94
- let writerOptions =
95
- Pandoc. def { Pandoc. writerExtensions = Pandoc. githubMarkdownExtensions }
96
- pandocOutput = Pandoc. runPure do
97
- Pandoc. writeMarkdown writerOptions (generatePandoc changelogPaths)
98
-
99
- case pandocOutput of
100
- Left pandocError -> die $
101
- " Failed to render markdown with error " <> Pandoc. renderError pandocError
102
- Right res -> liftIO . Text. writeFile outputPath $ format (s% " \n " ) res
94
+ let res = generateMarkdown changelogPaths
95
+ liftIO . Text. writeFile outputPath $ format (s% " \n " ) res
103
96
104
97
generateReleaseChangelogLinksDescription :: Description
105
98
generateReleaseChangelogLinksDescription = Description $
@@ -212,38 +205,38 @@ findChangelogFromGitHub :: MonadIO m => GitHubAccessToken -> CHaPEntry -> m (May
212
205
findChangelogFromGitHub accessToken c@ CHaPEntry {.. } = do
213
206
liftIO $ print c
214
207
let query = changelogLookupGitHub entryGitHubOwner entryGitHubRepo entrySubdir entryGitHubRevision
215
- liftIO $ print query
208
+ liftIO $ print query
216
209
contentDir <- liftIO (runGitHub accessToken query) >>= \ case
217
- Left (GitHub. HTTPError originalError@ (HttpExceptionRequest _originalReq (StatusCodeException resp _))) -> do
210
+ Left (GitHub. HTTPError originalError@ (HttpExceptionRequest _originalReq (StatusCodeException resp _))) -> do
218
211
if responseStatus resp == found302
219
212
then do
220
213
let responseHeaders' = responseHeaders resp
221
- case List. lookup hLocation responseHeaders' of
214
+ case List. lookup hLocation responseHeaders' of
222
215
Nothing -> die " findChangelogFromGitHub: Got HTTP 302 redirect but no location header found"
223
216
Just redirectLocation -> do
224
217
225
218
-- We must construct the redirect URL
226
219
-- We drop 2 characters at the end because the location appears to be malformed
227
220
let responseLocation = URIE. decodeText $ Text. dropEnd 2 $ Text. decodeUtf8 redirectLocation
228
- finalResponseQueryURl = responseLocation
221
+ finalResponseQueryURl = responseLocation
229
222
230
- newLocationQuery <- case query of
223
+ newLocationQuery <- case query of
231
224
GitHub. Query _ queryString -> do
232
225
redirectPathSegments <- generateRedirectPathSegments finalResponseQueryURl
233
- pure $ GitHub. query redirectPathSegments queryString
226
+ pure $ GitHub. query redirectPathSegments queryString
234
227
unexpected -> die $ " findChangelogFromGitHub: Expected a Query type but got: " <> repr unexpected
235
-
236
- r <- liftIO (runGitHub accessToken newLocationQuery)
237
- case r of
228
+
229
+ r <- liftIO (runGitHub accessToken newLocationQuery)
230
+ case r of
238
231
Left e' -> die $ Text. unlines [ " Redirect failed: " <> repr e'
239
232
, " Original http error: " <> repr originalError
240
233
]
241
234
Right (GitHub. ContentFile _) -> die
242
235
" Redirect result: Expected changelogLookupGitHub to return a directory, but got a single file"
243
236
Right (GitHub. ContentDirectory dir) -> pure dir
244
-
237
+
245
238
else die $
246
- " GitHub lookup failed with HTTP exception: " <> Text. pack (show resp)
239
+ " GitHub lookup failed with HTTP exception: " <> Text. pack (show resp)
247
240
Left gitHubError -> die $
248
241
" GitHub lookup failed with error " <> repr gitHubError
249
242
Right (GitHub. ContentFile _) -> die
@@ -258,9 +251,9 @@ findChangelogFromGitHub accessToken c@CHaPEntry{..} = do
258
251
Just (name, constructGitHubPath entryGitHubOwner entryGitHubRepo entryGitHubRevision path)
259
252
260
253
generateRedirectPathSegments :: MonadIO m => Text -> m [Text ]
261
- generateRedirectPathSegments url =
254
+ generateRedirectPathSegments url =
262
255
case URI. parseURI (Text. unpack url) of
263
- Just uri ->
256
+ Just uri ->
264
257
let segments = map Text. pack $ URI. pathSegments uri
265
258
in if null segments
266
259
then die $ " generateRedirectPathSegments: No path segments found in URL: " <> url
@@ -298,25 +291,33 @@ runGitHub :: GitHub.GitHubRW req res => GitHubAccessToken -> req -> res
298
291
runGitHub (GitHubAccessToken tok) =
299
292
GitHub. github (GitHub. OAuth tok)
300
293
301
- generatePandoc :: [(PkgName , Ver , Maybe (Text , Text ))] -> Pandoc. Pandoc
302
- generatePandoc ps =
303
- Pandoc. Pandoc mempty
304
- [ Pandoc. Plain [ Pandoc. Str " Package changelogs " ]
305
- , Pandoc. Table mempty ( Pandoc. Caption Nothing [] ) colSpec tableHead [tableBody] ( Pandoc. TableFoot mempty mempty )
306
- ]
294
+ generateMarkdown :: [(PkgName , Ver , Maybe (Text , Text ))] -> Text
295
+ generateMarkdown changelogPaths =
296
+ let
297
+ rows = mkHeader : map mkRow changelogPaths
298
+ table = render rows
299
+ in Text. unlines $ " Package changelogs " : " " : table
307
300
where
308
- colSpec = replicate 3 (Pandoc. AlignDefault , Pandoc. ColWidthDefault )
309
- tableHead = Pandoc. TableHead mempty [Pandoc. Row mempty tableHeadCells]
310
- tableHeadCells =
311
- [ mkCell [Pandoc. Str " Package" ]
312
- , mkCell [Pandoc. Str " Version" ]
313
- , mkCell [Pandoc. Str " Changelog" ]
314
- ]
315
- tableBody = Pandoc. TableBody mempty 0 [] (fmap mkTableRow ps)
316
- mkTableRow (PkgName n, v, linkMaybe) =
317
- Pandoc. Row mempty
318
- [ mkCell [Pandoc. Str n]
319
- , mkCell [Pandoc. Str (dispVer v)]
320
- , mkCell (foldMap (\ (fn, link) -> [Pandoc. Link mempty [Pandoc. Str fn] (link, fn)]) linkMaybe)
321
- ]
322
- mkCell t = Pandoc. Cell mempty Pandoc. AlignDefault 1 1 [Pandoc. Plain t]
301
+ mkHeader = [" Package" , " Version" , " Changelog" ]
302
+ mkRow (PkgName n, v, linkMaybe) = [n , dispVer v, dispLink linkMaybe]
303
+
304
+ -- example result: [CHANGELOG.md](https://github.com/IntersectMBO/cardano-base/blob/f11ddc7f/cardano-slotting/CHANGELOG.md "CHANGELOG.md")
305
+ dispLink (Just (file, link)) = format (" [" % s% " ](" % s% " \" " % s% " \" )" ) file link file
306
+ dispLink Nothing = " "
307
+
308
+ render :: [[Text ]] -> [Text ]
309
+ render = map renderRow . List. transpose . map (separator . innerMargins . alignLeft) . List. transpose
310
+ where
311
+ renderRow = surroundWith ' |' . Text. intercalate " |"
312
+
313
+ alignLeft ts =
314
+ let maxLen = maximum (Text. length <$> ts)
315
+ in map (Text. justifyLeft maxLen ' ' ) ts
316
+
317
+ surroundWith c = Text. cons c . flip Text. snoc c
318
+
319
+ innerMargins = map (surroundWith ' ' )
320
+
321
+ -- insert separator line after the first entry (assumed to be the header in its final width)
322
+ separator (h: rs) = h : Text. replicate (Text. length h) " -" : rs
323
+ separator [] = []
0 commit comments