Skip to content

Commit

Permalink
Add version select to Blabber buildscript sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jan 1, 2024
1 parent e02ca3c commit a874c40
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
23 changes: 18 additions & 5 deletions _includes/tabbed_builscript.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@
{%- if include.mods %}
{%- assign mods = include.mods %}
{%- else %}
{%- assign mods = "" | split: "," | push: include.mod1 %}
{%- assign mod1 = include.mod1 | split: ":" %}
{%- assign mods = "" | split: "," | push: mod1 %}
{%- if include.mod2 %}
{%- assign mods = mods | push: include.mod2 %}
{%- assign mod2 = include.mod2 | split: ":" %}
{%- assign mods = mods | push: mod2 %}
{%- endif %}
{%- if include.mod3 %}
{%- assign mods = mods | push: include.mod3 %}
{%- assign mod3 = include.mod3 | split: ":" %}
{%- assign mods = mods | push: mod3 %}
{%- endif %}
{%- endif %}
{%- capture result %}
{%- include tabbed.liquid key="buildscript" tab_names=tab_names tabs=tabs %}
{%- endcapture %}
{% for mod in mods %}
{%- capture result %}
{%- capture replacement_key %}<{% if mods.size > 1 %}{{ mod | upcase }}_{% endif %}VERSION>{% endcapture %}
{%- capture replacement %}<span class="mod-version-{{ mod }}">{{ replacement_key }}</span>{% endcapture %}
{%- capture replacement_key %}&lt;{% if mods.size > 1 %}{{ mod[0] | upcase }}_{% endif %}VERSION&gt;{% endcapture %}
{%- capture replacement %}<span class="mod-version-{{ mod[0] }}">{{ replacement_key }}</span>{% endcapture %}
{{ result | replace: replacement_key, replacement }}
{%- endcapture %}
{% endfor %}
{% if mods.size > 0 and mods[0].size > 1 %}
<label for="select-mcversion">Select a Minecraft Version:</label>
<select id="select-mcversion" class="mc-version-select" name="select-mcversion" disabled>
<option>Loading...</option>
</select>
<script type="module">
import {setUpSmartBuildscript} from "/scripts/smart-buildscript.js";
setUpSmartBuildscript({ {% for mod in mods %}{{ mod[0] }}: '{{ mod[1] }}'{% unless forloop.last %}, {% endunless %}{% endfor %} });
</script>
{% endif %}
{{ result }}
6 changes: 5 additions & 1 deletion scripts/modrinth-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function handleRequestTask(req) {
});
}

export const compareMcVersions = (a, b) => {
return b[1].mcVersion.releaseTime - a[1].mcVersion.releaseTime;
};

/**
* @typedef {Object} ModrinthVersion
* @property {string} name
Expand Down Expand Up @@ -84,5 +88,5 @@ export async function getVersions(modrinthProjectId) {
isSnapshot: pistonMeta.get(id)?.type !== 'release',
},
modVersions: versions,
}])).sort((a, b) => b[1].mcVersion.releaseTime - a[1].mcVersion.releaseTime));
}])).sort(compareMcVersions));
}
35 changes: 14 additions & 21 deletions scripts/smart-buildscript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVersions } from "./modrinth-api.js";
import {compareMcVersions, getVersions} from "./modrinth-api.js";

/**
* @typedef {Object} McIndexedVersion
Expand All @@ -16,7 +16,7 @@ function pickDefaultVersion(versions) {
return versions.get(pinned);
}

return versions[0];
return versions.values().next().value;
}

/**
Expand All @@ -36,15 +36,7 @@ function indexVersions(projectVersions) {
result.get(mcVersionId).modVersions.set(mod, modVersions);
}
}
if (projectVersions.length > 1) {
// iterate again to prune every version that is lacking one of the mods
for (const [mcVersion, mods] of result) {
if (mods.size < projectVersions.length) { // missing one or more mods
result.delete(mcVersion);
}
}
}
return result;
return new Map([...result].sort(compareMcVersions));
}

function doReplace(modName, version = 'VERSION') {
Expand All @@ -55,35 +47,35 @@ function doReplace(modName, version = 'VERSION') {

/**
* @param {McIndexedVersion} version
* @param {string[]} mods
* @param {boolean?} pin
*/
function selectVersion(version, pin) {
function selectVersion(version, mods, pin) {
// update the window URL to include the selected version
const state = history.state;
const title = document.title;
if (pin) {
const url = new URL(window.location.href);
url.searchParams.set('version', version.mcVersion.id);
history.replaceState(state, title, url);
history.replaceState(history.state, document.title, url);
}

// replace the version in the buildscript
for (const [modName, modVersions] of version.modVersions) {
doReplace(modName, modVersions[0]?.name);
for (const modName of mods) {
doReplace(modName, version.modVersions.get(modName)?.[0]?.name ?? `<${modName.toLocaleUpperCase()}_VERSION>`);
}
}

/**
* @param {Map<string, McIndexedVersion>} projectVersions
* @param {string[]} mods
*/
function updateVersionSelects(projectVersions) {
function updateVersionSelects(projectVersions, mods) {
const showPreReleases = document.getElementById('include-prereleases')?.checked || false;
/** @type {Map<string, McIndexedVersion>} */
const validVersions = new Map([...projectVersions].filter(([_, { mcVersion }]) => showPreReleases || !mcVersion.isSnapshot));

const selectedVersion = pickDefaultVersion(validVersions);
if (selectedVersion) {
selectVersion(selectedVersion);
selectVersion(selectedVersion, mods);
}
for (const versionSelect of document.getElementsByClassName('mc-version-select')) {
versionSelect.value = selectedVersion?.id;
Expand All @@ -103,6 +95,7 @@ function updateVersionSelects(projectVersions) {
* @returns {Promise<void>}
*/
export async function setUpSmartBuildscript(modrinthProjectIds) {
const mods = Object.keys(modrinthProjectIds);
const projectVersions = indexVersions(await Promise.all(
Object.entries(modrinthProjectIds).map(
([mod, modrinthProjectId]) =>
Expand All @@ -113,10 +106,10 @@ export async function setUpSmartBuildscript(modrinthProjectIds) {
for (const versionSelect of document.getElementsByClassName('mc-version-select')) {
versionSelect.addEventListener('change', () => {
if (projectVersions.has(versionSelect.value)) {
selectVersion(projectVersions.get(versionSelect.value), true);
selectVersion(projectVersions.get(versionSelect.value), mods, true);
}
});
}

updateVersionSelects(projectVersions);
updateVersionSelects(projectVersions, mods);
}
2 changes: 1 addition & 1 deletion wiki/blabber/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ dependencies {
}
```
{% endcapture %}
{%- include tabbed_builscript.liquid groovy=groovy kts=kts catalogue=catalogue %}
{%- include tabbed_builscript.liquid mod1="blabber:2oRMVFgd" mod2="cca:K01OU20C" groovy=groovy kts=kts catalogue=catalogue %}


You can find the current version of Blabber in the [releases](https://github.com/Ladysnake/Blabber/releases) tab of the repository on Github,
Expand Down
11 changes: 1 addition & 10 deletions wiki/cardinal-components-api/dev-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ mechanism provided by the Fabric toolchain to include Cardinal Components in you

Unless specified otherwise, the following block must be added to your `build.gradle` **after** the relevant `repositories` block:

<label for="select-mcversion">Select a Minecraft Version:</label>
<select id="select-mcversion" class="mc-version-select" name="select-mcversion" disabled>
<option>Loading...</option>
</select>
<script type="module">
import {setUpSmartBuildscript} from "/scripts/smart-buildscript.js";
setUpSmartBuildscript({cca: "K01OU20C"});
</script>

{% capture groovy %}
`gradle.properties`:
```properties
Expand Down Expand Up @@ -77,7 +68,7 @@ dependencies {
}
```
{% endcapture %}
{%- include tabbed_builscript.liquid mod1="cca" groovy=groovy kts=kts catalogue=catalogue %}
{%- include tabbed_builscript.liquid mod1="cca:K01OU20C" groovy=groovy kts=kts catalogue=catalogue %}


## Ladysnake Reposilite
Expand Down

0 comments on commit a874c40

Please sign in to comment.