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

Properly convert release metadata from json #585

Merged
merged 2 commits into from
Apr 20, 2022
Merged
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
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for vscode-haskell

## 2.1.3 (Pre-release)

- Ignore missing entries in Release Metadata
([#585](https://github.com/haskell/vscode-haskell/pull/585)) by @fendor

## 2.1.2 (Pre-release)

- Ignore missing entries in Release Metadata
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "haskell",
"displayName": "Haskell",
"description": "Haskell language support powered by the Haskell Language Server",
"version": "2.1.2",
"version": "2.1.3",
"license": "MIT",
"publisher": "haskell",
"engines": {
Expand Down
24 changes: 20 additions & 4 deletions src/hlsBinaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ async function toolInstalled(
*
* consult [ghcup metadata repo](https://github.com/haskell/ghcup-metadata/) for details.
*/
export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;
export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;

/**
* Compute Map of supported HLS versions for this platform.
Expand Down Expand Up @@ -829,13 +829,29 @@ async function getReleaseMetadata(

const offlineCache = path.join(storagePath, 'ghcupReleases.cache.json');

/**
* Convert a json value to ReleaseMetadata.
* Assumes the json is well-formed and a valid Release-Metadata.
* @param obj Release Metadata without any typing information but well-formed.
* @returns Typed ReleaseMetadata.
*/
const objectToMetadata = (obj: any): ReleaseMetadata => {
const hlsMetaEntries = Object.entries(obj).map(([hlsVersion, archMap]) => {
const archMetaEntries = Object.entries(archMap as any).map(([arch, supportedGhcVersionsPerOs]) => {
return [arch, new Map(Object.entries(supportedGhcVersionsPerOs as any))] as [string, Map<string, string[]>];
});
return [hlsVersion, new Map(archMetaEntries)] as [string, Map<string, Map<string, string[]>>];
});
return new Map(hlsMetaEntries);
};

async function readCachedReleaseData(): Promise<ReleaseMetadata | null> {
try {
logger.info(`Reading cached release data at ${offlineCache}`);
const cachedInfo = await promisify(fs.readFile)(offlineCache, { encoding: 'utf-8' });
// export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;
const value: ReleaseMetadata = JSON.parse(cachedInfo);
return value;
const value: any = JSON.parse(cachedInfo);
return objectToMetadata(value);
} catch (err: any) {
// If file doesn't exist, return null, otherwise consider it a failure
if (err.code === 'ENOENT') {
Expand All @@ -852,7 +868,7 @@ async function getReleaseMetadata(

// Cache the latest successfully fetched release information
await promisify(fs.writeFile)(offlineCache, JSON.stringify(releaseInfoParsed), { encoding: 'utf-8' });
return releaseInfoParsed;
return objectToMetadata(releaseInfoParsed);
} catch (githubError: any) {
// Attempt to read from the latest cached file
try {
Expand Down