Created
July 30, 2022 11:46
-
-
Save neil-morrison44/34fbb18de90cd9a32ca5bdafb2a812b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"repo": "https://api.github.com/repos/spiritualized1997/openFPGA-GBA/releases" | |
}, | |
{ | |
"repo": "https://api.github.com/repos/spiritualized1997/openFPGA-GB-GBC/releases", | |
"name": "Spiritualized.GBC" | |
}, | |
{ | |
"repo": "https://api.github.com/repos/spacemen3/PDP-1/releases" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zx | |
// This might be useful to some people, or provide the start to a more feature-complete version | |
// possibly including .sav file sync etc | |
// a `auto_update.json` file must be present of the form | |
/* | |
[ | |
{ | |
"repo": "https://api.github.com/repos/spiritualized1997/openFPGA-GBA/releases" | |
}, | |
{ | |
"repo": "https://api.github.com/repos/spiritualized1997/openFPGA-GB-GBC/releases", | |
"name": "Spiritualized.GBC" | |
}, | |
{ | |
"repo": "https://api.github.com/repos/spacemen3/PDP-1/releases" | |
} | |
] | |
*/ | |
// then this script gets run via zx (see https://github.com/google/zx) at the root of the SD card | |
const semverRegex = new RegExp("^[0-9]+\.[0-9]+\.[0-9]+$") | |
const isActuallySemver = (potentiallySemver) => semverRegex.test(potentiallySemver) | |
const semverCompare = (semverA, semverB) => { | |
// return true if A is more recent than B | |
const [majorA, minorA, patchA] = semverA.split(".").map((i) => parseInt(i)) | |
const [majorB, minorB, patchB] = semverB.split(".").map((i) => parseInt(i)) | |
if (majorA > majorB) return true | |
if (majorB > majorA) return false | |
if (minorA > minorB) return true | |
if (minorB > minorA) return false | |
if (patchA > patchB) return true | |
if (patchB > patchA) return false | |
return false | |
} | |
const updateCore = async (downloadLink) => { | |
console.log(chalk.green(`Updating core, downloading ${downloadLink}...`)) | |
await $`curl ${downloadLink} --location --output core.zip` | |
await $`unzip -o core.zip` | |
await $`rm core.zip` | |
} | |
const reposList = await fs.readJson("./auto_update.json") | |
for (let index = 0; index < reposList.length; index++) { | |
const {repo, name} = reposList[index] | |
const releases = await (await fetch(repo)).json() | |
const mostRecentRelease = releases.filter(({draft, prerelease}) => !(draft || prerelease))[0] | |
const {tag_name, assets} = mostRecentRelease | |
const releaseSemver = tag_name.replace("v", "") | |
// might need to search for the right zip here if there's more than one | |
const coreAsset = assets[0] | |
const nameGuess = name ?? coreAsset.name.split("_")[0] | |
console.log(chalk.blue(`${tag_name} is the most recent release, checking local core...`)) | |
const fileExists = await fs.exists(`./Cores/${nameGuess}/core.json`) | |
if (fileExists){ | |
const localData = await fs.readJson(`./Cores/${nameGuess}/core.json`) | |
const localSemver = localData.core.metadata.version.replace("v", "") | |
console.log(chalk.yellow(`Local core found: v${localSemver}`)) | |
if (!isActuallySemver(localSemver) || !isActuallySemver(releaseSemver)){ | |
console.log(chalk.red("Code not semver'd, downloading just incase...")) | |
// could compare release dates here but you'd miss any releases made within 1 day | |
updateCore(coreAsset.browser_download_url) | |
continue | |
} | |
if (semverCompare(releaseSemver, localSemver)){ | |
updateCore(coreAsset.browser_download_url) | |
}else{ | |
console.log(chalk.yellow(`Up to date, skipping core.`)) | |
} | |
}else{ | |
updateCore(coreAsset.browser_download_url) | |
} | |
console.log("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment