Skip to content

Commit

Permalink
Clean old autogenerated globalStorage folders (#2597)
Browse files Browse the repository at this point in the history
Signed-off-by: logonoff <git@logonoff.co>
  • Loading branch information
logonoff authored and rgrunber committed Jul 29, 2024
1 parent bf05afb commit a9144a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
28 changes: 26 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ClientErrorHandler } from './clientErrorHandler';
import { Commands, CommandTitle } from './commands';
import { ClientStatus, ExtensionAPI, TraceEvent } from './extension.api';
import * as fileEventHandler from './fileEventHandler';
import { getSharedIndexCache, HEAP_DUMP_LOCATION, prepareExecutable, removeEquinoxFragmentOnDarwinX64 } from './javaServerStarter';
import { getSharedIndexCache, HEAP_DUMP_LOCATION, prepareExecutable, removeEquinoxFragmentOnDarwinX64, startedFromSources } from './javaServerStarter';
import { initializeLogFile, logger } from './log';
import { cleanupLombokCache } from "./lombokSupport";
import { markdownPreviewProvider } from "./markdownPreviewProvider";
Expand All @@ -30,7 +30,7 @@ import { snippetCompletionProvider } from './snippetCompletionProvider';
import { JavaClassEditorProvider } from './javaClassEditor';
import { StandardLanguageClient } from './standardLanguageClient';
import { SyntaxLanguageClient } from './syntaxLanguageClient';
import { convertToGlob, deleteClientLog, deleteDirectory, ensureExists, getBuildFilePatterns, getExclusionGlob, getInclusionPatternsFromNegatedExclusion, getJavaConfig, getJavaConfiguration, hasBuildToolConflicts, resolveActualCause } from './utils';
import { convertToGlob, deleteClientLog, deleteDirectory, ensureExists, getBuildFilePatterns, getExclusionGlob, getInclusionPatternsFromNegatedExclusion, getJavaConfig, getJavaConfiguration, hasBuildToolConflicts, resolveActualCause, getVersion } from './utils';
import glob = require('glob');
import { Telemetry } from './telemetry';
import { getMessage } from './errorUtils';
Expand Down Expand Up @@ -159,6 +159,10 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>

cleanJavaWorkspaceStorage();

if (!startedFromSources()) { // Dev mode: version may not match package.json, deleting the in-use folder
cleanOldGlobalStorage(context);
}

// https://github.com/redhat-developer/vscode-java/issues/3484
if (process.platform === 'darwin' && process.arch === 'x64') {
try {
Expand Down Expand Up @@ -1101,6 +1105,26 @@ async function cleanJavaWorkspaceStorage() {
}
}

async function cleanOldGlobalStorage(context: ExtensionContext) {
const currentVersion = getVersion(context.extensionPath);
const globalStoragePath = context.globalStorageUri?.fsPath; // .../Code/User/globalStorage/redhat.java

ensureExists(globalStoragePath);

// delete folders in .../User/globalStorage/redhat.java that are not named the current version
fs.promises.readdir(globalStoragePath).then(async (files) => {
await Promise.all(files.map(async (file) => {
const currentPath = path.join(globalStoragePath, file);
const stat = await fs.promises.stat(currentPath);

if (stat.isDirectory() && file !== currentVersion) {
logger.info(`Removing old folder in globalStorage : ${file}`);
deleteDirectory(currentPath);
}
}));
});
}

export function registerCodeCompletionTelemetryListener() {
apiManager.getApiInstance().onDidRequestEnd((traceEvent: TraceEvent) => {
if (traceEvent.type === CompletionRequest.method) {
Expand Down
14 changes: 3 additions & 11 deletions src/javaServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { logger } from './log';
import { addLombokParam, isLombokSupportEnabled } from './lombokSupport';
import { RequirementsData } from './requirements';
import { IS_WORKSPACE_VMARGS_ALLOWED, getJavaEncoding, getJavaagentFlag, getKey, isInWorkspaceFolder } from './settings';
import { deleteDirectory, ensureExists, getJavaConfiguration, getTimestamp } from './utils';
import { deleteDirectory, ensureExists, getJavaConfiguration, getTimestamp, getVersion } from './utils';
import { log } from 'console';

// eslint-disable-next-line no-var
Expand Down Expand Up @@ -254,15 +254,7 @@ export function getSharedIndexCache(context: ExtensionContext): string {

function resolveConfiguration(context, configDir) {
ensureExists(context.globalStoragePath);
const extensionPath = path.resolve(context.extensionPath, "package.json");
const packageFile = JSON.parse(fs.readFileSync(extensionPath, 'utf8'));
let version;
if (packageFile) {
version = packageFile.version;
}
else {
version = '0.0.0';
}
const version = getVersion(context.extensionPath);
let configuration = path.resolve(context.globalStoragePath, version);
ensureExists(configuration);
configuration = path.resolve(configuration, configDir);
Expand All @@ -288,7 +280,7 @@ function startedInDebugMode(): boolean {
return hasDebugFlag(args);
}

function startedFromSources(): boolean {
export function startedFromSources(): boolean {
return process.env['DEBUG_VSCODE_JAVA'] === 'true';
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,13 @@ export function resolveActualCause(callstack: any): any {

return callstack;
}

export function getVersion(extensionPath: string): string {
const packagePath = path.resolve(extensionPath, "package.json");
const packageFile = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
if (packageFile) {
return packageFile.version;
}

return '0.0.0';
}

0 comments on commit a9144a7

Please sign in to comment.