Skip to content

Commit

Permalink
Clean old autogenerated globalStorage folders (redhat-developer#2597)
Browse files Browse the repository at this point in the history
Signed-off-by: logonoff <[email protected]>
  • Loading branch information
logonoff committed Jul 26, 2024
1 parent e3602eb commit 47ea43b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
28 changes: 25 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -114,6 +114,8 @@ export function fixJdtLinksInDocumentation(oldDocumentation: MarkdownString): Ma
}

export async function activate(context: ExtensionContext): Promise<ExtensionAPI> {
window.showErrorMessage('sadfsadfasdfasdfsadf');

await loadSupportedJreNames(context);
context.subscriptions.push(commands.registerCommand(Commands.FILESEXPLORER_ONPASTE, async () => {
const originalClipboard = await env.clipboard.readText();
Expand Down Expand Up @@ -159,6 +161,8 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>

cleanJavaWorkspaceStorage();

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 Expand Up @@ -1162,5 +1186,3 @@ function registerRestartJavaLanguageServerCommand(context: ExtensionContext) {
}
}));
}


12 changes: 2 additions & 10 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 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 47ea43b

Please sign in to comment.