Skip to content

Commit

Permalink
Rewrite shell code into JS (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzadp authored Dec 15, 2023
1 parent e1cd684 commit ca5211b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 97 deletions.
38 changes: 38 additions & 0 deletions .github/scripts/build-mdbook-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* mdBook relies on the creation of a special SUMMARY.md file
* https://rust-lang.github.io/mdBook/format/summary.html
* This script constructs the summary out of the available source files.
*/

const fs = require('fs');
const summaryPath = "mdbook/src/SUMMARY.md"

module.exports = async ({github, context}) => {
fs.writeFileSync(summaryPath, "# Summary\n\n[Introduction](introduction.md)\n") // Starting point.

const appendRfcsToSummary = (dirPath) => {
for (const filename of fs.readdirSync(dirPath)) {
if (!filename.endsWith(".md")) continue;
const filePath = dirPath + filename
const text = fs.readFileSync(filePath)
const title = text.toString().split(/\n/)
.find(line => line.startsWith("# ") || line.startsWith(" # "))
.replace("# ", "")
// Relative path, without the src prefix (format required by mdbook)
const relativePath = filePath.replace("mdbook/src/", "")
fs.appendFileSync(summaryPath, `- [${title}](${relativePath})\n`)
}
}

fs.appendFileSync(summaryPath, "\n---\n\n# Approved\n\n")
appendRfcsToSummary("mdbook/src/approved/")

fs.appendFileSync(summaryPath, "\n---\n\n# Newly Proposed\n\n")
appendRfcsToSummary("mdbook/src/new/")

fs.appendFileSync(summaryPath, "\n---\n\n# Proposed\n\n")
appendRfcsToSummary("mdbook/src/proposed/")

fs.appendFileSync(summaryPath, "\n---\n\n# Stale\n\n")
appendRfcsToSummary("mdbook/src/stale/")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require('fs');

// The amount of days that an RFC is considered "new".
// Counted from the creation of a given PR.
Expand All @@ -19,6 +19,7 @@ const dateDaysBefore = (daysBefore) => {
"new-rfcs",
"proposed-rfcs",
"stale-rfcs",
"mdbook/src/approved",
"mdbook/src/new",
"mdbook/src/stale",
"mdbook/src/proposed"
Expand Down
28 changes: 28 additions & 0 deletions .github/scripts/gather-markdown-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* This scripts gathers source markdown files of approved and proposed RFCS in mdbook/src directory.
*/

const fs = require('fs');

module.exports = async ({github, context}) => {
// Copy the approved (already-merged) RFCs markdown files, first adding a source link at the top and a TOC.
for (const file of fs.readdirSync("text/")) {
if (!file.endsWith(".md")) continue;
const text = `[(source)](https://github.com/polkadot-fellows/RFCs/blob/main/text/${file})\n\n`
+ "**Table of Contents**\n\n<\!-- toc -->\n"
+ fs.readFileSync(`text/${file}`)
fs.writeFileSync(`mdbook/src/approved/${file}`, text)
}

// Copy the proposed ones (created in previous steps).
for (const status of ["new", "proposed", "stale"]) {
const dirPath = `${status}-rfcs/`
for (const filename of fs.readdirSync(dirPath)) {
if (!filename.endsWith(".md")) continue;
const text = "**Table of Contents**\n\n<\!-- toc -->\n"
+ fs.readFileSync(dirPath + filename)
// Source link is already there (with a link to PR). So we append here.
fs.appendFileSync(`mdbook/src/${status}/${filename}`, text)
}
}
}
41 changes: 20 additions & 21 deletions .github/workflows/mdbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,42 @@ jobs:
uses: actions/github-script@v7
with:
script: |
const script = require('.github/download-rfc-prs.js')
console.log(await script({github, context}))
const script = require('.github/scripts/download-rfc-prs.js')
await script({github, context})
# Create the proposed RFC markdown files from the patches gather in the step above.
# Create the proposed RFC markdown files from the patches gathered in the step above.
- run: |
# We execute the patches, which results in markdown files created in patches/text/*.md
for f in ./patches/*.patch;
do
[ -e "$f" ] || break
git apply $f
done;
# We go over the created markdown files and move (append) them for mdbook to pick up.
for status in {new,proposed,stale}
do
cd ${status}-rfcs/
for f in *.md
do
[ -e "$f" ] || break
# We append the contents - because the links to source already exist there at this point.
cat $f >> "../mdbook/src/$status/$f"
done;
cd -
done;
# Gather source files - approved (merged) files, and files created in the previous steps.
- name: Gather markdown files into mdbook source directory
uses: actions/github-script@v7
with:
script: |
const script = require('.github/scripts/gather-markdown-files.js')
await script({github, context})
- name: Build the mdbook SUMMARY.md
uses: actions/github-script@v7
with:
script: |
const script = require('.github/scripts/build-mdbook-summary.js')
await script({github, context})
- name: Setup mdBook binary
uses: peaceiris/actions-mdbook@adeb05db28a0c0004681db83893d56c0388ea9ea # v1.2.0
with:
mdbook-version: '0.4.36'
- name: Install dependencies
run: |
cargo install [email protected]
# This will:
# - gather the proposed RFCs (constructed in the steps above).
# - gather the approved RFCs (they exist in this repo in text/ directory)
# - generate the mdbook out of it
- name: Generate the mdbook
run: mdbook/book.sh
run: |
cd mdbook
rm -rf ./book/
mdbook build --dest-dir ./book/
- name: Deploy to github pages
uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3.9.3
Expand Down
8 changes: 0 additions & 8 deletions mdbook/SUMMARY_preface.md

This file was deleted.

67 changes: 0 additions & 67 deletions mdbook/book.sh

This file was deleted.

0 comments on commit ca5211b

Please sign in to comment.