Skip to content

Commit 560b8e4

Browse files
committed
Migrate existing old cache file to new file format
1 parent 7753c4d commit 560b8e4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/hlsBinaries.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ async function getProjectGhcVersion(
205205
return callWrapper(downloadedWrapper);
206206
}
207207

208-
async function getReleaseMetadata(context: ExtensionContext, storagePath: string): Promise<IRelease[] | null> {
208+
async function getReleaseMetadata(
209+
context: ExtensionContext,
210+
storagePath: string,
211+
logger: Logger
212+
): Promise<IRelease[] | null> {
209213
const releasesUrl = workspace.getConfiguration('haskell').releasesURL
210214
? url.parse(workspace.getConfiguration('haskell').releasesURL)
211215
: undefined;
@@ -220,6 +224,25 @@ async function getReleaseMetadata(context: ExtensionContext, storagePath: string
220224
};
221225

222226
const offlineCache = path.join(storagePath, 'approvedReleases.cache.json');
227+
const offlineCacheOldFormat = path.join(storagePath, 'latestApprovedRelease.cache.json');
228+
229+
// Migrate existing old cache file latestApprovedRelease.cache.json to the new cache file
230+
// approvedReleases.cache.json if no such file exists yet.
231+
if (!fs.existsSync(offlineCache)) {
232+
try {
233+
const oldCachedInfo = await promisify(fs.readFile)(offlineCacheOldFormat, { encoding: 'utf-8' });
234+
const oldCachedInfoParsed = validate.parseAndValidate(oldCachedInfo, validate.optional(releaseValidator));
235+
if (oldCachedInfoParsed !== null) {
236+
await promisify(fs.writeFile)(offlineCache, JSON.stringify([oldCachedInfoParsed]), { encoding: 'utf-8' });
237+
}
238+
logger.info(`Successfully migrated ${offlineCacheOldFormat} to ${offlineCache}`);
239+
} catch (err: any) {
240+
// Ignore if old cache file does not exist
241+
if (err.code !== 'ENOENT') {
242+
logger.error(`Failed to migrate ${offlineCacheOldFormat} to ${offlineCache}: ${err}`);
243+
}
244+
}
245+
}
223246

224247
async function readCachedReleaseData(): Promise<IRelease[] | null> {
225248
try {
@@ -317,7 +340,7 @@ export async function downloadHaskellLanguageServer(
317340
}
318341

319342
logger.info('Fetching the latest release from GitHub or from cache');
320-
const releases = await getReleaseMetadata(context, storagePath);
343+
const releases = await getReleaseMetadata(context, storagePath, logger);
321344
if (!releases) {
322345
let message = "Couldn't find any pre-built haskell-language-server binaries";
323346
const updateBehaviour = workspace.getConfiguration('haskell').get('updateBehavior') as UpdateBehaviour;

0 commit comments

Comments
 (0)