From 884b143cbaf8849e231192ae21abe5edf5ef9f1a Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Mon, 1 Jul 2024 15:23:32 +0100 Subject: [PATCH] Read API cache data from json Signed-off-by: William Vinnicombe --- .github/workflows/static.yml | 24 +++- .gitignore | 2 + .vscodeignore | 1 + data/0.10.0/github-cache.json | 242 ++++++++++++++++++++++++++++++++ scripts/genCache.py | 99 +++++++++++++ src/utils/examplesUtil.mts | 5 +- src/utils/githubApiCache.mts | 257 ++++++++++++++-------------------- src/utils/githubREST.mts | 2 - 8 files changed, 477 insertions(+), 155 deletions(-) create mode 100644 data/0.10.0/github-cache.json create mode 100644 scripts/genCache.py diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index f0df7c16..8146f633 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -31,12 +31,34 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + - name: Enable corepack + run: corepack enable + - name: Setup node v18 + uses: actions/setup-node@v4 + with: + # the current node version use in vscode + node-version: '18' + cache: 'yarn' + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + - name: Install Dependencies + run: | + yarn + pip install requests js2py + - name: Generate Cache JSON + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + run: | + yarn run compile + python3 scripts/genCache.py - name: Setup Pages uses: actions/configure-pages@v4 - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: - # Upload entire repository + # Upload data directory path: 'data' - name: Deploy to GitHub Pages id: deployment diff --git a/.gitignore b/.gitignore index 1e0cc3e1..c433aa28 100644 --- a/.gitignore +++ b/.gitignore @@ -497,3 +497,5 @@ fabric.properties *.zip !vendor/**/dist + +tmp.py diff --git a/.vscodeignore b/.vscodeignore index 1259eca5..d025a92f 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -16,6 +16,7 @@ rollup.config.mjs **/*.ts .pnp* *.vsix +tmp.py # make sure scripts and data are always included !scripts/** diff --git a/data/0.10.0/github-cache.json b/data/0.10.0/github-cache.json new file mode 100644 index 00000000..04430bbb --- /dev/null +++ b/data/0.10.0/github-cache.json @@ -0,0 +1,242 @@ +{ + "githubApiCache-0-0": [ + "1.5.1" + ], + "githubApiCache-0-1": { + "assets": [ + { + "id": 112935743, + "name": "sdk-1.5.1-html-doc.zip", + "browser_download_url": "https://github.com/raspberrypi/pico-sdk/releases/download/1.5.1/sdk-1.5.1-html-doc.zip" + } + ], + "assetsUrl": "https://api.github.com/repos/raspberrypi/pico-sdk/releases/107707260/assets" + }, + "githubApiCache-1-0": [ + "v3.28.0-rc6" + ], + "githubApiCache-1-1": { + "assets": [ + { + "id": 138286886, + "name": "cmake-3.28.0-rc6-files-v1.json", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-files-v1.json" + }, + { + "id": 138286887, + "name": "cmake-3.28.0-rc6-linux-aarch64.sh", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-aarch64.sh" + }, + { + "id": 138286891, + "name": "cmake-3.28.0-rc6-linux-aarch64.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-aarch64.tar.gz" + }, + { + "id": 138286893, + "name": "cmake-3.28.0-rc6-linux-x86_64.sh", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-x86_64.sh" + }, + { + "id": 138286897, + "name": "cmake-3.28.0-rc6-linux-x86_64.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-x86_64.tar.gz" + }, + { + "id": 138286903, + "name": "cmake-3.28.0-rc6-macos-universal.dmg", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos-universal.dmg" + }, + { + "id": 138286905, + "name": "cmake-3.28.0-rc6-macos-universal.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos-universal.tar.gz" + }, + { + "id": 138286910, + "name": "cmake-3.28.0-rc6-macos10.10-universal.dmg", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos10.10-universal.dmg" + }, + { + "id": 138286923, + "name": "cmake-3.28.0-rc6-macos10.10-universal.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos10.10-universal.tar.gz" + }, + { + "id": 138286925, + "name": "cmake-3.28.0-rc6-SHA-256.txt", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-SHA-256.txt" + }, + { + "id": 138286926, + "name": "cmake-3.28.0-rc6-SHA-256.txt.asc", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-SHA-256.txt.asc" + }, + { + "id": 138286927, + "name": "cmake-3.28.0-rc6-windows-arm64.msi", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-arm64.msi" + }, + { + "id": 138286932, + "name": "cmake-3.28.0-rc6-windows-arm64.zip", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-arm64.zip" + }, + { + "id": 138286933, + "name": "cmake-3.28.0-rc6-windows-i386.msi", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-i386.msi" + }, + { + "id": 138286934, + "name": "cmake-3.28.0-rc6-windows-i386.zip", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-i386.zip" + }, + { + "id": 138286935, + "name": "cmake-3.28.0-rc6-windows-x86_64.msi", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-x86_64.msi" + }, + { + "id": 138286937, + "name": "cmake-3.28.0-rc6-windows-x86_64.zip", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-x86_64.zip" + }, + { + "id": 138286939, + "name": "cmake-3.28.0-rc6.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6.tar.gz" + }, + { + "id": 138286941, + "name": "cmake-3.28.0-rc6.zip", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6.zip" + } + ], + "assetsUrl": "https://api.github.com/repos/Kitware/CMake/releases/132188415/assets" + }, + "githubApiCache-2-0": [ + "v1.12.1" + ], + "githubApiCache-2-1": { + "assets": [ + { + "id": 167333823, + "name": "ninja-linux-aarch64.zip", + "browser_download_url": "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux-aarch64.zip" + }, + { + "id": 167333509, + "name": "ninja-linux.zip", + "browser_download_url": "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip" + }, + { + "id": 167333196, + "name": "ninja-mac.zip", + "browser_download_url": "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-mac.zip" + }, + { + "id": 167333379, + "name": "ninja-win.zip", + "browser_download_url": "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-win.zip" + }, + { + "id": 167333478, + "name": "ninja-winarm64.zip", + "browser_download_url": "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-winarm64.zip" + } + ], + "assetsUrl": "https://api.github.com/repos/ninja-build/ninja/releases/155357494/assets" + }, + "githubApiCache-3-0": [ + "v1.5.1-alpha-1" + ], + "githubApiCache-3-1": { + "assets": [ + { + "id": 141973997, + "name": "openocd-0.12.0-x64-win.zip", + "browser_download_url": "https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/openocd-0.12.0-x64-win.zip" + }, + { + "id": 141974002, + "name": "pico-sdk-tools-1.5.1-x64-win.zip", + "browser_download_url": "https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/pico-sdk-tools-1.5.1-x64-win.zip" + }, + { + "id": 141974003, + "name": "picotool-1.1.2-x64-win.zip", + "browser_download_url": "https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/picotool-1.1.2-x64-win.zip" + } + ], + "assetsUrl": "https://api.github.com/repos/will-v-pi/pico-sdk-tools/releases/134896110/assets" + }, + "githubApiCache-4-0": [ + "v0.12.0-2" + ], + "githubApiCache-4-1": { + "assets": [ + { + "id": 124558743, + "name": "xpack-openocd-0.12.0-2-darwin-arm64.tar.gz", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-arm64.tar.gz" + }, + { + "id": 124558741, + "name": "xpack-openocd-0.12.0-2-darwin-arm64.tar.gz.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-arm64.tar.gz.sha" + }, + { + "id": 124558736, + "name": "xpack-openocd-0.12.0-2-darwin-x64.tar.gz", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-x64.tar.gz" + }, + { + "id": 124558734, + "name": "xpack-openocd-0.12.0-2-darwin-x64.tar.gz.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-x64.tar.gz.sha" + }, + { + "id": 124558733, + "name": "xpack-openocd-0.12.0-2-linux-arm.tar.gz", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm.tar.gz" + }, + { + "id": 124558731, + "name": "xpack-openocd-0.12.0-2-linux-arm.tar.gz.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm.tar.gz.sha" + }, + { + "id": 124558729, + "name": "xpack-openocd-0.12.0-2-linux-arm64.tar.gz", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm64.tar.gz" + }, + { + "id": 124558727, + "name": "xpack-openocd-0.12.0-2-linux-arm64.tar.gz.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm64.tar.gz.sha" + }, + { + "id": 124558723, + "name": "xpack-openocd-0.12.0-2-linux-x64.tar.gz", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-x64.tar.gz" + }, + { + "id": 124558720, + "name": "xpack-openocd-0.12.0-2-linux-x64.tar.gz.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-x64.tar.gz.sha" + }, + { + "id": 124558714, + "name": "xpack-openocd-0.12.0-2-win32-x64.zip", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-win32-x64.zip" + }, + { + "id": 124558710, + "name": "xpack-openocd-0.12.0-2-win32-x64.zip.sha", + "browser_download_url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-win32-x64.zip.sha" + } + ], + "assetsUrl": "https://api.github.com/repos/xpack-dev-tools/openocd-xpack/releases/119866462/assets" + } +} \ No newline at end of file diff --git a/scripts/genCache.py b/scripts/genCache.py new file mode 100644 index 00000000..96e286f5 --- /dev/null +++ b/scripts/genCache.py @@ -0,0 +1,99 @@ +import json +import os +import sys + +import requests +import js2py + + +dir_path = os.path.dirname(os.path.realpath(__file__)) +os.chdir(f"{dir_path}/..") + +# Extract js for variables +with open("dist/extension.cjs", "r") as f: + keep = False + txt = "" + for line in f.readlines(): + if "const EXT_USER_AGENT =" in line: + keep = True + elif "getAuthorizationHeaders" in line: + keep = False + elif "const CURRENT_DATA_VERSION =" in line: + keep = True + + if keep: + txt += line + + if "const CURRENT_DATA_VERSION =" in line: + keep = False + +parsed = js2py.translate_js(txt) +with open("tmp.py", "w") as f: + f.write(parsed) +sys.path.append(os.getcwd()) + +import tmp + +stuff = tmp.var.to_python() + +# stuff.GithubRepository has conversions in both directions, so is twice the size +num_repos = 0 +for _ in stuff.GithubRepository: + num_repos += 0.5 +num_repos = int(num_repos) +assert num_repos == 5 +print("Num repos", num_repos) + + +# Only provide data for these versions +versions = [ + "1.5.1", + "v3.28.0-rc6", + "v1.12.1", + "v1.5.1-alpha-1", + "v0.12.0-2", +] + +headers = { + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": stuff.EXT_USER_AGENT, + "Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}" +} + +ret = {} +for repo in range(num_repos): + ret[f"githubApiCache-{repo}-0"] = versions[repo] + + version = versions[repo] + owner = stuff.ownerOfRepository(repo) + name = stuff.repoNameOfRepository(repo) + x = requests.get( + f"{stuff.GITHUB_API_BASE_URL}/repos/{owner}/{name}/releases/tags/{version}", + headers=headers + ) + data = json.loads(x.content) + assets = [] + for asset in data["assets"]: + assets.append({ + "id": asset["id"], + "name": asset["name"], + "browser_download_url": asset["browser_download_url"] + }) + data = { + "assets": assets, + "assetsUrl": data["assets_url"] + } + + ret[f"githubApiCache-{repo}-1"] = data + + +for k, v in ret.items(): + idx = int(k.split('-')[1]) + print(f"{k} is {stuff.GithubRepository[idx]}") + if isinstance(v, list): + print(v) + else: + print(v["assets"][0]) + +with open(f"data/{stuff.CURRENT_DATA_VERSION}/github-cache.json", "w") as f: + json.dump(ret, f, indent=2) diff --git a/src/utils/examplesUtil.mts b/src/utils/examplesUtil.mts index e2e45f58..7fffd1c4 100644 --- a/src/utils/examplesUtil.mts +++ b/src/utils/examplesUtil.mts @@ -5,7 +5,6 @@ import Logger from "../logger.mjs"; import { existsSync, readFileSync } from "fs"; import { homedir } from "os"; import { getGit, sparseCheckout, sparseCloneRepository } from "./gitUtil.mjs"; -import { EXAMPLES_REPOSITORY_URL } from "./githubREST.mjs"; import Settings from "../settings.mjs"; import { checkForInstallationRequirements } from "./requirementsUtil.mjs"; import { cp } from "fs/promises"; @@ -14,6 +13,8 @@ import { isInternetConnected } from "./downloadHelpers.mjs"; export const CURRENT_DATA_VERSION = "0.10.0"; +const EXAMPLES_REPOSITORY_URL = + "https://github.com/raspberrypi/pico-examples.git"; const EXAMPLES_JSON_URL = "https://raspberrypi.github.io/pico-vscode/" + `${CURRENT_DATA_VERSION}/examples.json`; @@ -64,7 +65,7 @@ export async function loadExamples(): Promise { try { if (!(await isInternetConnected())) { throw new Error( - "Error while downloading supported toolchains list. " + + "Error while downloading examples list. " + "No internet connection" ); } diff --git a/src/utils/githubApiCache.mts b/src/utils/githubApiCache.mts index ccc8a2d3..3a7b8e87 100644 --- a/src/utils/githubApiCache.mts +++ b/src/utils/githubApiCache.mts @@ -1,9 +1,12 @@ // TODO: put defaults into json file, to prevent need for these disables -/* eslint-disable max-len */ -/* eslint-disable @typescript-eslint/naming-convention */ import type { ExtensionContext, Memento } from "vscode"; -import { type GithubReleaseResponse, GithubRepository } from "./githubREST.mjs"; +import type { GithubReleaseResponse, GithubRepository } from "./githubREST.mjs"; import Logger from "../logger.mjs"; +import { CURRENT_DATA_VERSION, getDataRoot } from "./examplesUtil.mjs"; +import { get } from "https"; +import { isInternetConnected } from "./downloadHelpers.mjs"; +import { join as joinPosix } from "path/posix"; +import { readFileSync } from "fs"; /** * Tells if the stored data is a GithubReleaseResponse (data of a specific release) @@ -29,164 +32,118 @@ export interface GithubApiCacheEntry { } -function defaultCacheOfRepository( +const CACHE_JSON_URL = + "https://raspberrypi.github.io/pico-vscode/" + + `${CURRENT_DATA_VERSION}/github-cache.json`; + + +function parseCacheJson( + data: string +): { [id: string] : GithubReleaseResponse | string[] } { + try { + const cache = + JSON.parse( + data.toString() + ) as { [id: string] : GithubReleaseResponse | string[] }; + + return cache; + } catch { + Logger.log("Failed to parse github-cache.json"); + + throw new Error( + "Error while downloading github cache. " + + "Parsing Failed" + ); + } +} + +export async function defaultCacheOfRepository( repository: GithubRepository, dataType: GithubApiCacheEntryDataType -): GithubApiCacheEntry { +): Promise { const ret: GithubApiCacheEntry = { repository: repository, dataType: dataType, data: [], etag: "", }; - switch (repository) { - case GithubRepository.picoSDK: - if (dataType === GithubApiCacheEntryDataType.releases) { - ret.data = ["1.5.1"]; - } - break; - case GithubRepository.cmake: - if (dataType === GithubApiCacheEntryDataType.releases) { - ret.data = ["v3.28.0-rc6"]; - } else { - ret.data = { - assets: [ - { - id: 138286891, - name: 'cmake-3.28.0-rc6-linux-aarch64.tar.gz', - browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-aarch64.tar.gz' - }, - { - id: 138286897, - name: 'cmake-3.28.0-rc6-linux-x86_64.tar.gz', - browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-x86_64.tar.gz' - }, - { - id: 138286905, - name: 'cmake-3.28.0-rc6-macos-universal.tar.gz', - browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos-universal.tar.gz' - }, - { - id: 138286932, - name: 'cmake-3.28.0-rc6-windows-arm64.zip', - browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-arm64.zip' - }, - { - id: 138286937, - name: 'cmake-3.28.0-rc6-windows-x86_64.zip', - browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-x86_64.zip' - }, - ], - assetsUrl: 'https://api.github.com/repos/Kitware/CMake/releases/132188415/assets', - } as GithubReleaseResponse; - } - break; - case GithubRepository.ninja: - if (dataType === GithubApiCacheEntryDataType.releases) { - ret.data = ["v1.12.1"]; - } else { - ret.data = { - assets: [ - { - id: 167333823, - name: 'ninja-linux-aarch64.zip', - browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux-aarch64.zip' - }, - { - id: 167333509, - name: 'ninja-linux.zip', - browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip' - }, - { - id: 167333196, - name: 'ninja-mac.zip', - browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-mac.zip' - }, - { - id: 167333379, - name: 'ninja-win.zip', - browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-win.zip' - }, - { - id: 167333478, - name: 'ninja-winarm64.zip', - browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-winarm64.zip' - } - ], - assetsUrl: 'https://api.github.com/repos/ninja-build/ninja/releases/155357494/assets' - } as GithubReleaseResponse; - } - break; - case GithubRepository.tools: - if (dataType === GithubApiCacheEntryDataType.releases) { - ret.data = []; - } else { - ret.data = { - assets: [ - { - id: 141973997, - name: 'openocd-0.12.0-x64-win.zip', - browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/openocd-0.12.0-x64-win.zip' - }, - { - id: 141974002, - name: 'pico-sdk-tools-1.5.1-x64-win.zip', - browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/pico-sdk-tools-1.5.1-x64-win.zip' - }, - { - id: 141974003, - name: 'picotool-1.1.2-x64-win.zip', - browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/picotool-1.1.2-x64-win.zip' - } - ], - assetsUrl: 'https://api.github.com/repos/will-v-pi/pico-sdk-tools/releases/134896110/assets' - } as GithubReleaseResponse; - } - break; - case GithubRepository.openocd: - if (dataType === GithubApiCacheEntryDataType.releases) { - ret.data = []; - } else { - ret.data = { - assets: [ - { - id: 124558743, - name: 'xpack-openocd-0.12.0-2-darwin-arm64.tar.gz', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-arm64.tar.gz' - }, - { - id: 124558736, - name: 'xpack-openocd-0.12.0-2-darwin-x64.tar.gz', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-x64.tar.gz' - }, - { - id: 124558733, - name: 'xpack-openocd-0.12.0-2-linux-arm.tar.gz', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm.tar.gz' - }, - { - id: 124558729, - name: 'xpack-openocd-0.12.0-2-linux-arm64.tar.gz', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm64.tar.gz' - }, - { - id: 124558723, - name: 'xpack-openocd-0.12.0-2-linux-x64.tar.gz', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-x64.tar.gz' - }, - { - id: 124558714, - name: 'xpack-openocd-0.12.0-2-win32-x64.zip', - browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-win32-x64.zip' + try { + if (!(await isInternetConnected())) { + throw new Error( + "Error while downloading github cache. " + + "No internet connection" + ); + } + const result = await new Promise< + { [id: string] : GithubReleaseResponse | string[] } + >( + (resolve, reject) => { + // Download the JSON file + get(CACHE_JSON_URL, response => { + if (response.statusCode !== 200) { + reject( + new Error( + "Error while downloading github cache list. " + + `Status code: ${response.statusCode}` + ) + ); + } + let data = ""; + + // Append data as it arrives + response.on("data", chunk => { + data += chunk; + }); + + // Parse the JSON data when the download is complete + response.on("end", () => { + // Resolve with the array of SupportedToolchainVersion + const ret = parseCacheJson(data); + if (ret !== undefined) { + resolve(ret); + } else { + reject( + new Error( + "Error while downloading github cache list. " + + "Parsing data failed" + ) + ); } - ], - assetsUrl: 'https://api.github.com/repos/xpack-dev-tools/openocd-xpack/releases/119866462/assets' - } as GithubReleaseResponse; + }); + + // Handle errors + response.on("error", error => { + reject(error); + }); + }); } - break; - } + ); + + // TODO: Logger.debug + Logger.log(`Successfully downloaded github cache from the internet.`); - return ret; + ret.data = result[`githubApiCache-${repository}-${dataType}`]; + + return ret; + } catch (error) { + Logger.log(error instanceof Error ? error.message : (error as string)); + + Logger.log("Failed to load github-cache.json"); + + try { + const cacheFile = readFileSync( + joinPosix(getDataRoot(), "github-cache.json") + ); + const parsed = parseCacheJson(cacheFile.toString("utf-8")); + ret.data = parsed[`githubApiCache-${repository}-${dataType}`]; + + return ret; + } catch (e) { + Logger.log("Failed to load github-cache.json"); + + return undefined; + } + } } /** diff --git a/src/utils/githubREST.mts b/src/utils/githubREST.mts index 304ad6b9..8d51e40b 100644 --- a/src/utils/githubREST.mts +++ b/src/utils/githubREST.mts @@ -35,8 +35,6 @@ export const SDK_REPOSITORY_URL = "https://github.com/raspberrypi/pico-sdk.git"; export const NINJA_REPOSITORY_URL = "https://github.com/ninja-build/ninja.git"; export const CMAKE_REPOSITORY_URL = "https://github.com/Kitware/CMake.git"; export const PYENV_REPOSITORY_URL = "https://github.com/pyenv/pyenv.git"; -export const EXAMPLES_REPOSITORY_URL = - "https://github.com/raspberrypi/pico-examples.git"; export function ownerOfRepository(repository: GithubRepository): string { switch (repository) {