Skip to content

Commit

Permalink
Use metadata.json for .py versions
Browse files Browse the repository at this point in the history
  • Loading branch information
joedevivo committed Dec 30, 2023
1 parent a28beaf commit 53ca98e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in this file.
- Merged [#122](https://github.com/joedevivo/vscode-circuitpython/pull/122)
- Merged [#29](https://github.com/joedevivo/vscode-circuitpython/pull/29)
- Upgraded dependencies
- Fixed version info for .py files

## [0.1.20]
- Circuit Python 8
Expand Down
67 changes: 58 additions & 9 deletions src/librarymanager/libraryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as globby from "globby";
import * as fs_extra from "fs-extra";
import * as trash from "trash";
import { Container } from "../container";
import { V4MAPPED } from "dns";

class LibraryQP implements vscode.QuickPickItem {
// QuickPickItem impl
Expand Down Expand Up @@ -388,29 +389,60 @@ export class LibraryManager implements vscode.Disposable {
Downloads 6.x. and source bundles. Source are crucial for autocomplete
*/
private async getBundle(tag: string) {
let metdataUrl: string =
LibraryManager.BUNDLE_URL +
"/releases/download/{0}/adafruit-circuitpython-bundle-{0}.json";
let urlRoot: string =
LibraryManager.BUNDLE_URL +
"/releases/download/{0}/adafruit-circuitpython-bundle-{1}-{0}.zip";
this.tag = tag;

let metadataUrl: string = String.Format(metdataUrl, tag);
fs.mkdirSync(path.join(this.storageRootDir, "bundle", tag), {
recursive: true,
});

for await (const suffix of LibraryManager.BUNDLE_SUFFIXES) {
let url: string = String.Format(urlRoot, tag, suffix);
let p: string = path.join(this.storageRootDir, "bundle", tag);

await axios.default
.get(url, { responseType: "stream" })
.then((response) => {
response.data
.pipe(unzip.Extract({ path: p }))
.on("close", (entry) => {
if (suffix === "py") {
Container.loadBundleMetadata();
}
});
response.data.pipe(unzip.Extract({ path: p }));
})
.catch((error) => {
console.log(`Error downloading {suffix} bundle: ${url}`);
});
}

let dest: string = path.join(
this.storageRootDir,
"bundle",
tag,
`adafruit-circuitpython-bundle-${tag}.json`
);

await axios.default
.get(metadataUrl, { responseType: "json" })
.then((response) => {
fs.writeFileSync(dest, JSON.stringify(response.data), {
encoding: "utf8",
});
/*
, (err) => {
if (err) {
console.log(`Error writing file: ${err}`);
} else {
}
});
*/
})
.catch((error) => {
console.log(`Error downloading bundle metadata: ${metadataUrl}`);
});

Container.loadBundleMetadata();
}

public static getMpy(name: string): string {
Expand All @@ -430,14 +462,26 @@ export class LibraryManager implements vscode.Disposable {

public async loadBundleMetadata(): Promise<boolean> {
let bundlePath = this.bundlePath("py");
/*
let bundlePath = path.join(
this.localBundleDir,
`adafruit-circuitpython-bundle-${this.tag}.json`
);
*/
this.libraries = await this.loadLibraryMetadata(bundlePath);
return true;
}

private async loadLibraryMetadata(
rootDir: string
): Promise<Map<string, Library>> {
console.log(rootDir);
let jsonMetadataFile = path.join(
this.localBundleDir,
`adafruit-circuitpython-bundle-${this.tag}.json`
);
let rawData = fs.readFileSync(jsonMetadataFile, "utf8");
let jsonData = JSON.parse(rawData);

let libDirs: string[] = await globby("*", {
absolute: true,
cwd: rootDir,
Expand All @@ -446,7 +490,12 @@ export class LibraryManager implements vscode.Disposable {
});

let libraries: Array<Promise<Library>> = libDirs.map((p, i, a) =>
Library.from(p)
Library.from(p).then((l) => {
if (rootDir.startsWith(this.localBundleDir)) {
l.version = jsonData[l.name].version;
}
return l;
})
);

return new Promise<Map<string, Library>>(async (resolve, reject) => {
Expand Down

0 comments on commit 53ca98e

Please sign in to comment.