diff --git a/.github/workflows/generate-readme.yaml b/.github/workflows/generate-readme.yaml new file mode 100644 index 0000000..598df1c --- /dev/null +++ b/.github/workflows/generate-readme.yaml @@ -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" + 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 diff --git a/scripts/generate-readme.js b/scripts/generate-readme.js new file mode 100644 index 0000000..f9e50a7 --- /dev/null +++ b/scripts/generate-readme.js @@ -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();