Skip to content

Commit d1d50ce

Browse files
committed
Add Macro script data extraction
1 parent 2f95eac commit d1d50ce

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

src/_data/depctrl.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const limitedWebRequest = async (url, type) => {
1515
}), url, type);
1616
}
1717

18+
// check hashes of all files in a feed
1819
const checkFileIntegrity = async (feeds) => {
1920
for (var feed of feeds) {
2021
var feedHashesValid = true;
@@ -44,6 +45,46 @@ const checkFileIntegrity = async (feeds) => {
4445
return feeds;
4546
}
4647

48+
const extractProperty = (script, property) => {
49+
var match = Array.from(script.matchAll(new RegExp(`\\s*${property}\\s*=\\s*(?:tr)?(?:"([^"]*)"|'([^']*)')`, 'g')));
50+
if (match.length === 1) {
51+
return (match[0][1] || "") + (match[0][2] || "");
52+
}
53+
return null;
54+
}
55+
56+
// extracts data from lua or moon macros and saves it alongside feed data
57+
const extractScriptData = async (feeds) => {
58+
for (var feed of feeds) {
59+
for (var [macroID, macro] of Object.entries(feed.macros || {})) {
60+
var defaultChannel = Object.values(macro.channels).filter((c) => c.default)[0];
61+
macro["_defaultVersion"] = defaultChannel.version;
62+
63+
var urls = Object.values(defaultChannel.files)
64+
.filter((f) => f.type !== "test" && !f.delete)
65+
.map((f) => f.url);
66+
if (urls.length !== 1) {
67+
continue;
68+
}
69+
70+
var url = urls[0];
71+
if (url.endsWith(".lua") || url.endsWith(".moon")) {
72+
var script = (await limitedWebRequest(url, "buffer")).toString();
73+
var scriptData = {
74+
"name": extractProperty(script, "script_name"),
75+
"description": extractProperty(script, "script_description"),
76+
"author": extractProperty(script, "script_author"),
77+
"version": extractProperty(script, "script_version"),
78+
"namespace": extractProperty(script, "script_namespace")
79+
}
80+
macro["_scriptData"] = scriptData;
81+
} else {
82+
continue;
83+
}
84+
}
85+
}
86+
return feeds;
87+
}
4788

4889
// fetch single feed
4990
const fetchFeed = (url) => {
@@ -153,6 +194,7 @@ function fillTemplateVar(data, repDict = {}, parentKey = "", depth = 0) {
153194
// store data locally to not compute feeds multiple times
154195
const data = fetchAllFeeds(seedFeed)
155196
.then(fillTemplateVar)
156-
.then(checkFileIntegrity);
197+
.then(checkFileIntegrity)
198+
.then(extractScriptData);
157199

158200
module.exports.getData = () => data;

src/pages/macros-pages.njk

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,56 @@ eleventyComputed:
102102
</div>
103103
{% endfor %}
104104
</p>
105+
106+
{% if macro._scriptData %}
107+
<h2>Script Data:</h2>
108+
<p>
109+
<table class="table table-hover">
110+
<thead>
111+
<tr>
112+
<th scope="col">Property</th>
113+
<th scope="col">Feed Data</th>
114+
<th scope="col">Script Data</th>
115+
<th scope="col">Matches</th>
116+
</tr>
117+
</thead>
118+
<tbody>
119+
<tr>
120+
<th>Name</th>
121+
<td>{{ macro.name }}</td>
122+
<td>{{ macro._scriptData.name }}</td>
123+
<td>{% if macro._scriptData.name %}{% if macro.name === macro._scriptData.name %}{% else %}{% endif %}{% else %}{% endif %}</td>
124+
</tr>
125+
<tr>
126+
<th>Description</th>
127+
<td>{{ macro.description }}</td>
128+
<td>{{ macro._scriptData.description }}</td>
129+
<td>{% if macro._scriptData.description %}{% if macro.description === macro._scriptData.description %}{% else %}{% endif %}{% else %}{% endif %}</td>
130+
</tr>
131+
<tr>
132+
<th>Author</th>
133+
<td>{{ macro.author }}</td>
134+
<td>{{ macro._scriptData.author }}</td>
135+
<td>{% if macro._scriptData.author %}{% if macro.author === macro._scriptData.author %}{% else %}{% endif %}{% else %}{% endif %}</td>
136+
</tr>
137+
<tr>
138+
<th>Version</th>
139+
<td>{{ macro._defaultVersion }}</td>
140+
<td>{{ macro._scriptData.version }}</td>
141+
<td>{% if macro._scriptData.version %}{% if macro._defaultVersion === macro._scriptData.version %}{% else %}{% endif %}{% else %}{% endif %}</td>
142+
</tr>
143+
<tr>
144+
<th>Namespace</th>
145+
<td>{{ macro._namespace }}</td>
146+
<td>{{ macro._scriptData.namespace }}</td>
147+
<td>{% if macro._scriptData.namespace %}{% if macro._namespace === macro._scriptData.namespace %}{% else %}{% endif %}{% else %}{% endif %}</td>
148+
</tr>
149+
</tbody>
150+
</table>
151+
</p>
152+
153+
{% endif %}
154+
105155
{% if not loop.last %}<hr>{% endif %}
106156

107157
{% endfor %}

0 commit comments

Comments
 (0)