diff --git a/.github/actions/zig-update.js b/.github/actions/zig-update.js new file mode 100644 index 0000000..692ee50 --- /dev/null +++ b/.github/actions/zig-update.js @@ -0,0 +1,63 @@ +const https = require("https"); +const fs = require("fs"); +const { execSync, exec } = require("child_process"); +const { Octokit } = require("@octokit/action"); + +const req = https.get("https://ziglang.org/download/index.json", (res) => { + let data = ""; + + // A chunk of data has been received. + res.on("data", (chunk) => { + data += chunk; + }); + + // The whole response has been received. + res.on("end", () => { + let res = JSON.parse(data); + check(res); + }); +}); + +function check(data) { + if (!data.master.version.startsWith("0.12")) { + console.error("Zig master is no longer on 0.12, please update"); + process.exit(1); + } + const path = "Formula/z/zig@0.12.rb"; + var file = fs.readFileSync(path).toString(); + const verMatch = /"[\d.]+[+-\w]+"/; + const localVer = file.match(verMatch)[0].replaceAll('"', ""); + const remoteVer = (() => { + let digits = data.master.version.split(/\.|-/); + return digits[2] + "." + digits[4]; + })(); + if (localVer == remoteVer) return console.log("Nightly is up to date"); + + const branchName = `zig_0.12.${remoteVer}`; + execSync(`git checkout -b "${branchName}"`); + + file = file.replace(verMatch, `"${remoteVer}"`); + + let replacementIndex = -1; + const newSHA = [ + data.master["aarch64-macos"].shasum, + data.master["x86_64-macos"].shasum, + data.master["aarch64-linux"].shasum, + data.master["x86_64-linux"].shasum, + ]; + file = file.replace(/sha256 "([a-f]|\d)*"/, () => { + replacementIndex++; + return `sha256 "${newSHA[replacementIndex]}"`; + }); + + fs.writeFileSync(path, file); + + const octokit = new Octokit(); + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); + octokit.git.createCommit({ + owner, + repo, + message: `"[Autoupdate]: Sync zig@0.12 to ${remoteVer}"`, + }); + octokit.pulls.create({ owner, repo, base: "main", head: branchName }); +} diff --git a/.github/workflows/zig-update.yml b/.github/workflows/zig-update.yml new file mode 100644 index 0000000..33fe549 --- /dev/null +++ b/.github/workflows/zig-update.yml @@ -0,0 +1,16 @@ +name: Zig nightly update +on: + schedule: + - cron: '16 0 * * *' +jobs: + update-zig: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + version: 12 + - run: npm install @octokit/action + - run: node .github/actions/zig-update.js + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index e43b0f9..9daa824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +node_modules