Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add new workflow for generating modpack readme #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/generate-readme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# .github/workflows/generate-readme.yml

on:
workflow_call:
inputs:
test-branch-name:
type: string
description: "Clients can use a feature branch with a name matching a branch name from this repository, in order to publish to thunderstore from a feature branch."
required: false

permissions:
contents: write

jobs:
generate-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- uses: tibdex/github-app-token@v1
id: get_installation_token
with:
app_id: ${{ vars.CI_APP_ID }}
private_key: ${{ secrets.CI_TOKEN }}

- uses: actions/checkout@v4
with:
repository: "beeheim/actions-workflows"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be changed to undorn

path: cicd
ref: ${{ (github.ref_name == inputs.test-branch-name) && inputs.test-branch-name || 'main' }}
token: ${{ steps.get_installation_token.outputs.token }}

- name: install dependencies
run: |
cd cicd/scripts
npm install fs path

- name: Generate README
run: node cicd/scripts/generate-readme.js

- name: Commit README
run: |
git config user.name github-actions
git config user.email github-actions[bot]@users.noreply.github.com
git add .
git commit -m "chore: Update README"
git branch update-readme
git push origin update-readme
95 changes: 95 additions & 0 deletions scripts/generate-readme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const fs = require("fs");
const path = require("path");

function parseModString(modString) {
const parts = modString.split("-");
const author = parts[0];
const name = parts[1].split("-")[0];
const version = parts[2];
const mod = {
author: author,
name: name,
version: version,
};
return mod;
}

function getModArrayFromFile(inputFilePath) {
let output = [];
const fileContent = fs.readFileSync(inputFilePath, "utf8");
const lines = fileContent.split("\n");
lines.forEach((line) => {
line = line.trim(); // Remove carriage return characters
if (line) {
const mod = parseModString(line);
output.push(mod);
}
});
output.sort((a, b) => a.name.localeCompare(b.name));
return output;
}

// Currently unused, but a reminder to use this strategy later
function getModArrayFromManifest(manifest) {
const modArray = manifest.dependencies;
let output = [];
modArray.forEach((modString) => {
if (modString) {
const mod = parseModString(modString);
output.push(mod);
}
});
output.sort((a, b) => a.name.localeCompare(b.name));
return output;
}

//
function generateModSection(sectionName, modArray) {
const urlPrefix = "https://valheim.thunderstore.io/package";
let modList = `### ${sectionName}\n\n`;
for (const mod of modArray) {
if (mod) {
const url = `${urlPrefix}/${mod.author}/${mod.name}/${mod.version}/`;
modList += `- [${mod.name}@${mod.version}](${url})\n`;
}
}
modList += "\n";
return modList;
}

function main() {
try {
// Assign arguments to variables
const modpackPath = path.join(__dirname, "../../modpack");
// Convert command-line arguments to variables,
const manifestPath = `${modpackPath}/manifest.json`;
const greylistPath = `${modpackPath}/greylist.txt`;
const dependenciesPath = `${modpackPath}/dependencies.txt`;
const readmePath = `${modpackPath}/README.md`;

const manifest = require(manifestPath);

// Create arrays of mod objects
const greylist = getModArrayFromFile(greylistPath);
const dependencies = getModArrayFromFile(dependenciesPath);

const modpackName = manifest.name;
const description = manifest.description;
const version = manifest.version_number;

// Build the Readme
console.log(`Building Readme for ${modpackName}...`);
let readmeMarkdown = `# ${modpackName.replace(/_/g, " ")} \n\n ## Version ${version}\n\n ${description} \n\n`;
readmeMarkdown += generateModSection("Mandatory Mods", dependencies);
readmeMarkdown += generateModSection("Greylisted Mods", greylist);
readmeMarkdown +=
"\n> Note: When trying to join the official server, do not update mods beyond the version in this modpack.\n";
fs.writeFileSync(readmePath, readmeMarkdown);

// Catch errors just in case
} catch (error) {
console.log(error);
}
}

main();