-
Notifications
You must be signed in to change notification settings - Fork 528
/
build.sh
executable file
·152 lines (125 loc) · 3.9 KB
/
build.sh
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
#!/usr/bin/bash
set -euo pipefail
# Validate JSON
jq . cheat-sheets.json >/dev/null
jq . extra-cheat-sheets.json >/dev/null
# Reset stuff we want to modify
if [ "${GITHUB_RUN_NUMBER-}" = "" ]; then
git checkout README.md cheat-sheets.json
fi
# Update default cheat sheet JSON with document structure
cheat_sheets() {
(
cat <<'EOT'
async function run() {
var cheatsheets = require('./cheat-sheets.json');
for(const name of Object.keys(cheatsheets)) {
let repo = cheatsheets[name];
let regex = new RegExp((repo.filePattern?repo.filePattern:"^(.+).md$"));
// fetch default branch first
let data = await fetch(`https://api.github.com/repos/${repo.github}`)
.then((response) => response.json())
.then((data) => { repoInfo = data; return fetch(`https://api.github.com/repos/${repo.github}/git/trees/${data.default_branch}?recursive=1`)})
.then((response) => response.json());
cheatsheets[name].documents = data.tree
.map((a) => a.path)
.filter((path) => regex.test(path))
.map((path) => {
var tmp = path.match(regex);
return { name: tmp[1], path };
});
}
console.log(JSON.stringify(cheatsheets));
}
run();
EOT
) | node | jq >cheat-sheets.json.new && mv cheat-sheets.json.new cheat-sheets.json
}
# Build lunr search index
build_search_index() {
(
cat <<'EOT'
async function run() {
var cheatsheets = require('./cheat-sheets.json');
var docs = {};
for(const name of Object.keys(cheatsheets)) {
let repo = cheatsheets[name];
// Add entry for section title
const j = Object.keys(docs).length;
docs[j] = {
doc: `${name}`,
id: j,
title: `${name}`,
content: "",
relUrl: `/#/${name}`,
url: `/#/${name}`
}
// Add 1st level child entries
repo?.documents.forEach((d) => {
const j = Object.keys(docs).length;
docs[j] = {
doc: `${name}`,
id: j,
title: `${d.name}`,
content: "",
relUrl: `/#/${name}/${d.name}`,
url: `/#/${name}/${d.name}`
}
});
}
console.log(JSON.stringify(docs));
}
run();
EOT
) | node | jq >search-index.json
}
# Update README with a list of all cheat sheets defined
readme_update() {
sed -i '/<!-- marker -->/,$ d' README.md
(
cat <<'EOT'
var cheatsheets = require('./cheat-sheets.json');
var extra = require('./extra-cheat-sheets.json');
var result = '<!-- marker -->\n\n';
for(const name of ["LZone Cheat Sheets","LZone Examples","Visual Ops"]) {
let repo = cheatsheets[name];
let oldgroup = "";
let docs = {};
result += `\n\n## ${name} Index\n\n`;
for(let d of repo.documents) {
d.path = d.path.replace(/\.md$/, '');
let tmp = d.path.split(/\//);
const dname = tmp[tmp.length-1];
if(dname === 'README')
continue;
docs[dname] = { path: d.path, group: tmp[1] };
}
for(const dname of Object.keys(docs).sort((a,b) => {
return docs[a].path.localeCompare(docs[b].path);
})) {
if (docs[dname].group !== oldgroup) {
result += `\n<br/><b>${docs[dname].group}</b>`;
oldgroup = docs[dname].group;
}
result += ` | <a href='https://lzone.de/#/LZone ${docs[dname].path}'>${dname}</a>`;
}
}
result += '\n\n## Installable External Cheat Sheets\n\n';
for(const dname of Object.keys(extra).sort()) {
result += `- [${dname}](https://github.com/${extra[dname].github})\n`;
}
console.log(result);
EOT
) | node >>README.md
}
cheat_sheets
build_search_index
readme_update
if [ "${GITHUB_RUN_NUMBER-}" != "" ]; then
git config user.email "[email protected]"
git config user.name "Create Index Workflow"
git add -u
git add search-index.json
git commit -m "Update index." README.md cheat-sheets.json search-index.json || exit 0
git push
fi