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

✨ Updating toolchain #238

Open
wants to merge 5 commits into
base: develop
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
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@
"adm-zip": "^0.5.10",
"axios": "^1.6.0",
"dotenv": "^16.3.1",
"lzma-native": "^8.0.6",
"node-fetch": "^2.6.7",
"seek-bzip": "^2.0.0",
"semver": "^7.5.2",
Expand Down
117 changes: 38 additions & 79 deletions src/one-click/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as vscode from "vscode";
import { window, ProgressLocation } from "vscode";
var fetch = require("node-fetch");
var admzip = require("adm-zip");
var bunzip = require("seek-bzip");
var tar = require("tar-fs");
var lzma = require("lzma-native");
import * as fs from "fs";
import * as stream from "stream";
import * as path from "path";
Expand All @@ -19,17 +19,18 @@ async function download(
) {
// Check if file type is .tar.bz or .zip

const bz2 = downloadURL.includes(".bz2");
const xz = downloadURL.includes(".xz");
await prosLogger.log("OneClick", `Downloading ${downloadURL}`);
await prosLogger.log("OneClick", `Storage Path: ${storagePath}`);

downloadName =
"Downloading: " + downloadName ??
(storagePath.includes("cli")
? "PROS CLI"
: storagePath.includes("toolchain")
? "PROS Toolchain"
: "VEX Vexcom");
"Downloading: " +
(downloadName ??
(storagePath.includes("cli")
? "PROS CLI"
: storagePath.includes("toolchain")
? "PROS Toolchain"
: "VEX Vexcom"));

await window.withProgress(
{
Expand Down Expand Up @@ -73,23 +74,24 @@ async function download(
out.close();
}
);
return bz2;
return xz;
}

export async function extract(
globalPath: string,
storagePath: string,
bz2: boolean,
xzFile: boolean,
extractName?: string
) {
await prosLogger.log("OneClick", `Extracting ${storagePath}`);
extractName =
"Installing: " + extractName ??
(storagePath.includes("cli")
? "PROS CLI"
: storagePath.includes("toolchain")
? "PROS Toolchain"
: "VEX Vexcom");
"Installing: " +
(extractName ??
(storagePath.includes("cli")
? "PROS CLI"
: storagePath.includes("toolchain")
? "PROS Toolchain"
: "VEX Vexcom"));
await window.withProgress(
{
location: ProgressLocation.Notification,
Expand All @@ -106,97 +108,54 @@ export async function extract(
extract.close();
});

if (bz2) {
await prosLogger.log(
"OneClick",
`Reading compressed data from ${storagePath}`
);
// Read the contents of the bz2 file
var compressedData = await fs.promises.readFile(
path.join(globalPath, "download", storagePath)
);

// Decrypt the bz2 file contents.
let decompressedData;
await prosLogger.log("OneClick", `Decompressing ${storagePath}`);
try {
decompressedData = bunzip.decode(compressedData);
} catch (e: any) {
console.log(e);
await prosLogger.log(
"OneClick",
`Failed to decompress ${storagePath}`
);
vscode.window.showErrorMessage(
"An error occured while decoding the toolchain"
);
}

storagePath = storagePath.replace(".bz2", "");
await prosLogger.log(
"OneClick",
`Writing decompressed data to ${storagePath}`
);
await fs.promises.writeFile(
path.join(globalPath, "download", storagePath),
decompressedData
);
// Write contents of the decrypted bz2 into "sigbots.pros/download/filename.tar"

if (xzFile) {
await new Promise(function (resolve, reject) {
// Create our read stream
prosLogger.log("OneClick", `Creating read stream for ${storagePath}`);
const stats = fs.statSync(
path.join(globalPath, "download", storagePath)
);
const totalSize = stats.size;
read = fs.createReadStream(
path.join(globalPath, "download", storagePath)
);
var decompress = new lzma.createDecompressor();
decompress.on("data", (chunk: Buffer | string | any) => {
_progress.report({ increment: (chunk.length * 100) / totalSize });
});
// Remove tar from the filename
storagePath = storagePath.replace(".tar", "");
storagePath = storagePath.replace(".tar.xz", "");
// create our write stream
prosLogger.log(
"OneClick",
`Extracting ${storagePath} to install folder`
);
extract = tar.extract(path.join(globalPath, "install", storagePath));
extract = tar.extract(path.join(globalPath, "download", storagePath));
// Pipe the read stream into the write stream
read.pipe(extract);
read.pipe(decompress).pipe(extract);
// When the write stream ends, resolve the promise
extract.on("finish", resolve);
// If there's an error, reject the promise and clean up
read.on("error", () => {
prosLogger.log("OneClick", `Error occured for ${storagePath}`);
fs.unlink(
path.join(globalPath, "install", storagePath),
path.join(globalPath, "download", storagePath),
(_) => null
);
reject();
});
});

const files = await fs.promises.readdir(
path.join(globalPath, "install")
path.join(globalPath, "download", storagePath)
);

for (const file of files) {
if (file.includes("toolchain")) {
await prosLogger.log("OneClick", `Finding toolchain files to move`);
const interfiles = await fs.promises.readdir(
path.join(globalPath, "install", file)
if (file.includes("arm-none-eabi")) {
await fs.promises.rename(
path.join(globalPath, "download", storagePath, file),
path.join(globalPath, "install", storagePath)
);
for (const intfile of interfiles) {
if (intfile.includes("gcc-arm-none-eabi")) {
const toBringOut = await fs.promises.readdir(
path.join(globalPath, "install", file, intfile)
);
for (const f of toBringOut) {
await prosLogger.log("OneClick", `Moving ${f} to ${file}`);
await fs.promises.rename(
path.join(globalPath, "install", file, intfile, f),
path.join(globalPath, "install", file, f)
);
}
}
}
console.log(path.join(globalPath, "install", storagePath));
}
}
} // if bz2
Expand Down Expand Up @@ -231,14 +190,14 @@ export async function downloadextract(
name?: string
) {
const globalPath = context.globalStorageUri.fsPath;
const bz2 = await download(
const xz = await download(
globalPath,
downloadURL,
storagePath,
name ?? undefined
);
console.log("download done");
await extract(globalPath, storagePath, bz2, name ?? undefined);
await extract(globalPath, storagePath, xz, name ?? undefined);
console.log(`Finished Installing ${storagePath}`);
window.showInformationMessage(`Finished Installing ${name ?? ""}`);
return true;
Expand Down
Loading
Loading