repo: reorder .github

This commit is contained in:
lowlighter
2022-01-14 02:14:10 -05:00
parent 43dac8a8dc
commit 78eb3e51ec
20 changed files with 157 additions and 162 deletions

130
.github/scripts/build.mjs vendored Normal file
View File

@@ -0,0 +1,130 @@
//Imports
import ejs from "ejs"
import fs from "fs/promises"
import fss from "fs"
import paths from "path"
import url from "url"
import sgit from "simple-git"
import metadata from "../source/app/metrics/metadata.mjs"
import yaml from "js-yaml"
//Mode
const [mode = "dryrun"] = process.argv.slice(2)
console.log(`Mode: ${mode}`)
//Paths
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..")
const __action = paths.join(__metrics, "source/app/action")
const __web = paths.join(__metrics, "source/app/web")
const __readme = paths.join(__metrics, ".github/readme")
const __templates = paths.join(paths.join(__metrics, "source/templates/"))
const __plugins = paths.join(paths.join(__metrics, "source/plugins/"))
const __test_plugins = paths.join(paths.join(__metrics, "tests/plugins"))
const __test_secrets = paths.join(paths.join(__metrics, "tests/secrets.json"))
//Git setup
const git = sgit(__metrics)
const staged = new Set()
//Config and general documentation auto-generation
for (const step of ["config", "documentation"]) {
//Load plugins metadata
const {plugins, templates, packaged, descriptor} = await metadata({log:false})
//Update generated files
async function update({source, output, options = {}}) {
//Regenerate file
console.log(`Generating ${output}`)
const content = await ejs.renderFile(source, {plugins, templates, packaged, descriptor}, {async:true, ...options})
//Save result
const file = paths.join(__metrics, output)
await fs.writeFile(file, content)
//Add to git
staged.add(file)
}
//Templating
switch (step) {
case "config":
await update({source:paths.join(__action, "action.yml"), output:"action.yml"})
await update({source:paths.join(__web, "settings.example.json"), output:"settings.example.json"})
break
case "documentation":
await update({source:paths.join(__readme, "README.md"), output:"README.md", options:{root:__readme}})
await update({source:paths.join(__readme, "partials/documentation/plugins.md"), output:"source/plugins/README.md"})
await update({source:paths.join(__readme, "partials/documentation/templates.md"), output:"source/templates/README.md"})
break
}
}
{
//Load plugins metadata and secrets
const {plugins, templates} = await metadata({log:false, diff:true})
const secrets = Object.assign(JSON.parse(`${await fs.readFile(__test_secrets)}`), {$regex:/\$\{\{\s*secrets\.(?<secret>\w+)\s*\}\}/})
//Get plugin infos
async function plugin(id) {
const path = paths.join(__plugins, id)
const readme = paths.join(path, "README.md")
const examples = paths.join(path, "examples.yml")
const tests = paths.join(__test_plugins, `${id}.yml`)
return {
readme:{
path:readme,
content:`${await fs.readFile(readme)}`
},
tests:{
path:tests
},
examples:fss.existsSync(examples) ? yaml.load(await fs.readFile(examples), "utf8") ?? [] : [],
options:plugins[id].readme.table
}
}
//Plugins
for (const id of Object.keys(plugins)) {
const {examples, options, readme, tests} = await plugin(id)
//Plugin readme
await fs.writeFile(readme.path, readme.content
.replace(/(<!--examples-->)[\s\S]*(<!--\/examples-->)/g, `$1\n${examples.map(({test, prod, ...step}) => ["```yaml", yaml.dump(step), "```"].join("\n")).join("\n")}\n$2`)
.replace(/(<!--options-->)[\s\S]*(<!--\/options-->)/g, `$1\n${options}\n$2`)
)
console.log(`Generating ${readme.path}`)
//Plugin tests
await fs.writeFile(tests.path, yaml.dump(examples.map(({prod, test = {}, name = "", ...step}) => {
if (test.skip)
return null
const result = {name:`${plugins[id].name} - ${name}`, ...step, ...test}
test.with ??= {}
for (const [k, v] of Object.entries(result.with)) {
if (k in test.with)
result.with[k] = test.with[k]
if (secrets.$regex.test(v))
result.with[k] = v.replace(secrets.$regex, secrets[v.match(secrets.$regex)?.groups?.secret])
}
if (!result.with.base)
delete result.with.base
delete result.with.filename
return result
}).filter(t => t)))
console.log(`Generating ${tests.path}`)
}
}
//Commit and push
if (mode === "publish") {
console.log(`Pushing staged changes: \n${[...staged].map(file => ` - ${file}`).join("\n")}`)
const gitted = await git
.addConfig("user.name", "github-actions[bot]")
.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com")
.add([...staged])
.commit("ci: auto-regenerate files")
.push("origin", "master")
console.log(gitted)
}
console.log("Success!")

21
.github/scripts/markdown_example.mjs vendored Normal file
View File

@@ -0,0 +1,21 @@
//Imports
import puppeteer from "puppeteer"
//Setup browser
const browser = await puppeteer.launch({
headless:true,
executablePath:process.env.PUPPETEER_BROWSER_PATH,
args:["--no-sandbox", "--disable-extensions", "--disable-setuid-sandbox", "--disable-dev-shm-usage"],
ignoreDefaultArgs:["--disable-extensions"],
})
const page = await browser.newPage()
//Select markdown example and take screenshoot
await page.setViewport({width:600, height:600})
await page.goto("https://github.com/lowlighter/lowlighter/blob/master/metrics.markdown.md")
const clip = await page.evaluate(() => {
const {x, y, width, height} = document.querySelector("#readme").getBoundingClientRect()
return {x, y, width, height}
})
await page.screenshot({type:"png", path:"metrics.markdown.png", clip, omitBackground:true})
await browser.close()

79
.github/scripts/preview.mjs vendored Normal file
View File

@@ -0,0 +1,79 @@
//Imports
import fs from "fs/promises"
import paths from "path"
import url from "url"
import setup from "../source/app/metrics/setup.mjs"
//Paths
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..")
const __templates = paths.join(paths.join(__metrics, "source/templates/"))
const __node_modules = paths.join(paths.join(__metrics, "node_modules"))
const __web = paths.join(paths.join(__metrics, "source/app/web/statics"))
const __web_about = paths.join(paths.join(__web, "about"))
const __preview = paths.join(paths.join(__web, "preview"))
const __preview_js = paths.join(__preview, ".js")
const __preview_css = paths.join(__preview, ".css")
const __preview_templates = paths.join(__preview, ".templates")
const __preview_templates_ = paths.join(__preview, ".templates_")
const __preview_about = paths.join(__preview, "about/.statics")
//Extract from web server
const {conf, Templates} = await setup({nosettings:true, log:false})
const templates = Object.entries(Templates).map(([name]) => ({name, enabled:true}))
const metadata = Object.fromEntries(Object.entries(conf.metadata.plugins)
.map(([key, value]) => [key, Object.fromEntries(Object.entries(value).filter(([key]) => ["name", "icon", "category", "web", "supports"].includes(key)))])
.map(([key, value]) => [key, key === "core" ? {...value, web:Object.fromEntries(Object.entries(value.web).filter(([key]) => /^config[.]/.test(key)).map(([key, value]) => [key.replace(/^config[.]/, ""), value]))} : value]))
//Directories
await fs.mkdir(__preview, {recursive:true})
await fs.mkdir(__preview_js, {recursive:true})
await fs.mkdir(__preview_css, {recursive:true})
await fs.mkdir(__preview_templates, {recursive:true})
await fs.mkdir(__preview_templates_, {recursive:true})
await fs.mkdir(__preview_about, {recursive:true})
//Web
fs.copyFile(paths.join(__web, "index.html"), paths.join(__preview, "index.html"))
fs.copyFile(paths.join(__web, "favicon.png"), paths.join(__preview, ".favicon.png"))
fs.copyFile(paths.join(__web, "opengraph.png"), paths.join(__preview, ".opengraph.png"))
//Plugins and templates
fs.writeFile(paths.join(__preview, ".plugins"), JSON.stringify(Object.entries(metadata).filter(([_name, {category}]) => category !== "core").map(([name]) => ({name, enabled:false}))))
fs.writeFile(paths.join(__preview, ".plugins.base"), JSON.stringify(conf.settings.plugins.base.parts))
fs.writeFile(paths.join(__preview, ".plugins.metadata"), JSON.stringify(metadata))
fs.writeFile(paths.join(__preview, ".templates__"), JSON.stringify(templates))
for (const template in conf.templates) {
fs.writeFile(paths.join(__preview_templates_, template), JSON.stringify(conf.templates[template]))
const __partials = paths.join(__templates, template, "partials")
const __preview_partials = paths.join(__preview_templates, template, "partials")
await fs.mkdir(__preview_partials, {recursive:true})
for (const file of await fs.readdir(__partials))
fs.copyFile(paths.join(__partials, file), paths.join(__preview_partials, file))
}
//Styles
fs.copyFile(paths.join(__web, "style.css"), paths.join(__preview_css, "style.css"))
fs.copyFile(paths.join(__web, "style.vars.css"), paths.join(__preview_css, "style.vars.css"))
fs.copyFile(paths.join(__node_modules, "prismjs/themes/prism-tomorrow.css"), paths.join(__preview_css, "style.prism.css"))
//Scripts
fs.writeFile(paths.join(__preview_js, "app.js"), `${await fs.readFile(paths.join(__web, "app.js"))}`)
fs.writeFile(paths.join(__preview_js, "app.placeholder.js"), `${await fs.readFile(paths.join(__web, "app.placeholder.js"))}`)
fs.copyFile(paths.join(__node_modules, "ejs/ejs.min.js"), paths.join(__preview_js, "ejs.min.js"))
fs.copyFile(paths.join(__node_modules, "faker/dist/faker.min.js"), paths.join(__preview_js, "faker.min.js"))
fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.js"), paths.join(__preview_js, "axios.min.js"))
fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.map"), paths.join(__preview_js, "axios.min.map"))
fs.copyFile(paths.join(__node_modules, "vue/dist/vue.min.js"), paths.join(__preview_js, "vue.min.js"))
fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js"), paths.join(__preview_js, "vue.prism.min.js"))
fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js.map"), paths.join(__preview_js, "vue-prism-component.min.js.map"))
fs.copyFile(paths.join(__node_modules, "prismjs/prism.js"), paths.join(__preview_js, "prism.min.js"))
fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-yaml.min.js"), paths.join(__preview_js, "prism.yaml.min.js"))
fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-markdown.min.js"), paths.join(__preview_js, "prism.markdown.min.js"))
fs.copyFile(paths.join(__node_modules, "clipboard/dist/clipboard.min.js"), paths.join(__preview_js, "clipboard.min.js"))
//Meta
fs.writeFile(paths.join(__preview, ".version"), JSON.stringify(`${conf.package.version}-preview`))
fs.writeFile(paths.join(__preview, ".hosted"), JSON.stringify({by:"metrics", link:"https://github.com/lowlighter/metrics"}))
//About
fs.copyFile(paths.join(__web, "about", "index.html"), paths.join(__preview, "about", "index.html"))
for (const file of await fs.readdir(__web_about))
if (file !== ".statics")
fs.copyFile(paths.join(__web_about, file), paths.join(__preview_about, file))

43
.github/scripts/quickstart/index.mjs vendored Normal file
View File

@@ -0,0 +1,43 @@
//Imports
import fs from "fs"
import paths from "path"
import url from "url"
import ejs from "ejs"
//Mode
const [mode, name] = process.argv.slice(2)
//Paths
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../../..")
const __quickstart = paths.join(__metrics, ".github/scripts/quickstart")
//Check arguments
if ((!mode)||(!name))
throw new Error(`Usage is "npm run quickstart -- <mode> <name>"`)
if (!["plugin", "template"].includes(mode))
throw new Error(`Unsupported mode ${mode}`)
//Check if target directory already exists
const target = paths.join(__metrics, `source/${mode}s`, name)
if (fs.existsSync(target))
throw new Error(`A ${mode} named ${name} already exists!`)
//Copy quickstart content
console.log(`quickstart for ${mode}`)
await fs.promises.mkdir(target)
await rcopy(paths.join(__quickstart, mode), target)
//Recursive copy
async function rcopy(from, to) {
for (const file of await fs.promises.readdir(from)) {
const path = paths.join(from, file)
if ((await fs.promises.lstat(path)).isDirectory()) {
await fs.promises.mkdir(paths.join(to, file))
await rcopy(path, paths.join(to, file))
}
else {
console.log(`copying ${path} to ${paths.join(to, file)}`)
await fs.promises.writeFile(paths.join(to, file), await ejs.renderFile(path, {name}, {async:true}))
}
}
}

View File

@@ -0,0 +1,19 @@
### 🧩 <%= `${name.charAt(0).toLocaleUpperCase()}${name.substring(1)}` %>
<table>
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>
#### Examples workflows
[➡️ Available options for this plugin](metadata.yml)
```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_<%= name %>: yes
```

View File

@@ -0,0 +1,7 @@
- name: <%= `${name.charAt(0).toLocaleUpperCase()}${name.substring(1)}` %> plugin (default)
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.<%= name %>.svg
token: ${{ secrets.METRICS_TOKEN }}
base: ""
plugin_<%= name %>: yes

View File

@@ -0,0 +1,15 @@
//Setup
export default async function({login, q, imports, data, computed, rest, graphql, queries, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.<%= name %>))
return null
//Results
return {}
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}

View File

@@ -0,0 +1,14 @@
name: "🧩 <%= `${name.charAt(0).toLocaleUpperCase()}${name.substring(1)}` %>"
category: community # Leave as it
supports:
- user # Support users account
- organization # Support organizations account
- repository # Support repositories metrics
scopes: [] # Required scopes for personal access token
inputs:
# Enable or disable plugin
plugin_<%= name %>:
description: description
type: boolean
default: no

View File

@@ -0,0 +1,18 @@
### 📕 <%= `${name.charAt(0).toLocaleUpperCase()}${name.substring(1)}` %> template
<table>
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>
#### Examples workflows
```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
setup_community_templates: user/metrics@master:<%= name %>
template: "@<%= name %>"
```

View File

@@ -0,0 +1,16 @@
<svg xmlns="http://www.w3.org/2000/svg" width="480" height="99999" class="<%%= !animated ? 'no-animations' : '' %>">
<defs><style><%%= fonts %></style></defs>
<style data-optimizable="true"><%%= style %></style>
<foreignObject x="0" y="0" width="100%" height="100%">
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
<%% for (const partial of [...partials]) { %>
<%%- await include(`partials/${partial}.ejs`) %>
<%% } %>
<div id="metrics-end"></div>
</div>
</foreignObject>
</svg>

After

Width:  |  Height:  |  Size: 562 B

View File

@@ -0,0 +1,14 @@
name: "🖼️ Template name"
extends: classic # Fallback to "classic" template "template.mjs" if not trusted
index: ~ # Leave as it (this is used to order plugins on metrics README.md)
supports:
- user # Support users account
- organization # Support organizations account
- repository # Support repositories metrics
formats:
- svg # Support SVG output
- png # Support PNG output
- jpeg # Support JPEG output
- json # Support JSON output
- markdown # Support markdown output
- markdown-pdf # Support PDF output

View File

@@ -0,0 +1,5 @@
[
"base.header",
"base.activity+community",
"base.repositories"
]

56
.github/scripts/release.mjs vendored Normal file
View File

@@ -0,0 +1,56 @@
//Imports
import github from "@actions/github"
import paths from "path"
import url from "url"
import sgit from "simple-git"
//Git setup
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..")
const git = sgit(__metrics)
//Setup octokit
const token = process.env.GITHUB_TOKEN
const rest = github.getOctokit(token)
//Environment
const maintainer = "lowlighter"
const repository = process.env.GITHUB_REPOSITORY.match(/^(?<owner>[\s\S]+)[/](?<name>[\s\S]+)$/)?.groups ?? null
const version = process.env.GITHUB_COMMIT_MESSAGE.match(/(?<version>v\d+[.]\d+)/)?.groups?.version ?? null
//Check arguments
if ((!repository)||(!repository.name)||(!repository.owner))
throw new Error(`Could not parse repository "${process.env.GITHUB_REPOSITORY}"`)
console.log(`Repository: ${repository.owner}/${repository.name}`)
if (!version)
throw new Error(`Could not parse version from "${process.env.GITHUB_COMMIT_MESSAGE}"`)
console.log(`Version: ${version}`)
//Load related pr
const {data:{items:prs}} = await rest.search.issuesAndPullRequests({
q:`repo:${repository.owner}/${repository.name} is:pr is:merged author:${maintainer} assignee:${maintainer} Release ${version} in:title`
})
//Ensure that there is exactly one pr matching
if (prs.length < 1)
throw new Error(`No matching prs found`)
if (prs.length > 1)
throw new Error(`Multiple prs found (${prs.length} matching)`)
const [patchnote] = prs
console.log(`Using pr#${patchnote.number}: ${patchnote.title}`)
//Check whether release already exists
try {
const {data:{id}} = await rest.repos.getReleaseByTag({owner:repository.owner, repo:repository.name, tag:version})
console.log(`Release ${version} already exists (#${id}), will replace it`)
await rest.repos.deleteRelease({owner:repository.owner, repo:repository.name, release_id:id})
console.log(`Deleting tag ${version}`)
await git.push(["--delete", "origin", version])
await new Promise(solve => setTimeout(solve, 15*1000))
}
catch {
console.log(`Release ${version} does not exists yet, will create it`)
}
//Publish release
await rest.repos.createRelease({owner:repository.owner, repo:repository.name, tag_name:version, name:`Version ${version.replace(/^v/g, "")}`, body:patchnote.body})
console.log(`Successfully published`)