|
| 1 | +/* |
| 2 | + Copyright (c) 2024 Alan de Freitas ([email protected]) |
| 3 | +
|
| 4 | + Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +
|
| 7 | + Official repository: https://github.com/cppalliance/mrdocs |
| 8 | +*/ |
| 9 | + |
| 10 | +const https = require('https'); |
| 11 | +const request = require('sync-request'); |
| 12 | +const fs = require("fs"); |
| 13 | + |
| 14 | +function humanizeBytes(bytes) { |
| 15 | + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; |
| 16 | + if (bytes == 0) return '0 Byte'; |
| 17 | + const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); |
| 18 | + return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; |
| 19 | +} |
| 20 | + |
| 21 | +function humanizeDate(date) { |
| 22 | + const options = { year: 'numeric', month: 'long', day: 'numeric' }; |
| 23 | + return new Date(date).toLocaleDateString('en-US', options); |
| 24 | +} |
| 25 | + |
| 26 | +module.exports = function (registry) { |
| 27 | + registry.blockMacro('mrdocs-releases', function () { |
| 28 | + const self = this |
| 29 | + self.process(function (parent, target, attrs) { |
| 30 | + // Collect all release URLs |
| 31 | + let cacheFilenamePath = 'releasesResponse.json'; |
| 32 | + let cachePath = `${__dirname}/../build/requests/${cacheFilenamePath}`; |
| 33 | + fs.mkdirSync(`${__dirname}/../build/requests/`, { recursive: true }); |
| 34 | + const readFromCacheFile = fs.existsSync(cachePath) && fs.statSync(cachePath).mtime > new Date(Date.now() - 1000 * 60 * 60 * 24); |
| 35 | + const releasesResponse = |
| 36 | + readFromCacheFile ? |
| 37 | + fs.readFileSync(cachePath, 'utf-8') : |
| 38 | + request('GET', 'https://api.github.com/repos/cppalliance/mrdocs/releases', { |
| 39 | + headers: { |
| 40 | + 'User-Agent': 'request' |
| 41 | + } |
| 42 | + }).getBody('utf-8') |
| 43 | + if (!readFromCacheFile) { |
| 44 | + fs.writeFileSync(cachePath, releasesResponse); |
| 45 | + } |
| 46 | + const releases = JSON.parse(releasesResponse) |
| 47 | + |
| 48 | + // Create table |
| 49 | + let text = '|===\n' |
| 50 | + text += '| 3+| 🪟 Windows 2+| 🐧 Linux \n' |
| 51 | + text += '| 📃 Release | 📦 7z | 📦 msi | 📦 zip | 📦 tar.xz | 📦 tar.gz \n' |
| 52 | + for (const release of releases) { |
| 53 | + if (release.name === 'llvm-package') continue |
| 54 | + text += `| ${release.html_url}[${release.name},window=_blank]\n\n${humanizeDate(release.published_at)} ` |
| 55 | + const assetSuffixes = ['win64.7z', 'win64.msi', 'win64.zip', 'Linux.tar.xz', 'Linux.tar.gz'] |
| 56 | + for (const suffix of assetSuffixes) { |
| 57 | + const asset = release.assets.find(asset => asset.name.endsWith(suffix)) |
| 58 | + if (asset) { |
| 59 | + text += `| ${asset.browser_download_url}[🔗 ${asset.name}]\n\n(${humanizeBytes(asset.size)}) ` |
| 60 | + } else { |
| 61 | + text += '| - ' |
| 62 | + } |
| 63 | + } |
| 64 | + text += '\n' |
| 65 | + } |
| 66 | + text += '|===\n' |
| 67 | + return self.parseContent(parent, text) |
| 68 | + }) |
| 69 | + }) |
| 70 | +} |
0 commit comments