Skip to content

Commit

Permalink
feat: register command to clear cache
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
JoseVSeb committed Feb 18, 2024
1 parent e7a3576 commit 230405e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ This extension contributes the following settings:

Please refer [Google Java Format repository](https://github.com/google/google-java-format) for available versions and CLI arguments.

## Extension Commands

This extension contributes the following commands:

* `Google Java Format For VS Code: Clear Cache`: Clear cache of [Google Java Format executable](https://github.com/google/google-java-format/releases) downloads by the extension.
* `Google Java Format For VS Code: Reload Executable`: Reload the [Google Java Format executable](https://github.com/google/google-java-format/releases) using the current configuration.

## How to Debug

To debug this extension and see how exactly it invokes the formatter, use *Developer: Set Log Level...* to enable *Debug* for this extension, and then open the *Output* tab and select this extension.
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
}
}
}
],
"commands": [
{
"command": "googleJavaFormatForVSCode.reloadExecutable",
"title": "Google Java Format For VS Code: Reload Executable"
},
{
"command": "googleJavaFormatForVSCode.clearCache",
"title": "Google Java Format For VS Code: Clear Cache"
}
]
},
"scripts": {
Expand Down
37 changes: 30 additions & 7 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { createHash } from "node:crypto";
import path = require("node:path");
import { ExtensionContext, LogOutputChannel, Uri, workspace } from "vscode";
import {
ExtensionContext,
LogOutputChannel,
Uri,
commands,
workspace,
} from "vscode";

export class Cache {
private uri: Uri;

private constructor(
context: ExtensionContext,
private context: ExtensionContext,
private log: LogOutputChannel,
cacheFolder: string,
) {
Expand All @@ -19,10 +26,28 @@ export class Cache {
) {
const cache = new Cache(context, log, cacheFolder);
await cache.init();
// TODO: clear old cache
return cache;
}

subscribe = () => {
this.context.subscriptions.push(
commands.registerCommand(
"googleJavaFormatForVSCode.clearCache",
this.clear,
),
);
};

clear = async () => {
// clear cache
await workspace.fs.delete(this.uri, { recursive: true });
this.log.info("Cache cleared.");
// reload executable after clearing cache
await commands.executeCommand(
"googleJavaFormatForVSCode.reloadExecutable",
);
};

private init = async () => {
// Create the cache directory if it doesn't exist
try {
Expand All @@ -37,10 +62,8 @@ export class Cache {
get = async (url: string) => {
const basename = path.basename(url);

const dirname = Uri.joinPath(
this.uri,
createHash("md5").update(url).digest("hex"),
);
const hash = createHash("md5").update(url).digest("hex");
const dirname = Uri.joinPath(this.uri, hash);
const localPath = Uri.joinPath(dirname, basename);

try {
Expand Down
7 changes: 7 additions & 0 deletions src/Executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LogOutputChannel,
// Progress,
ProgressLocation,
commands,
window,
} from "vscode";
import { Cache } from "./Cache";
Expand Down Expand Up @@ -65,6 +66,12 @@ export class Executable {

subscribe = () => {
this.config.subscriptions.push(this.configurationChangeListener);
this.context.subscriptions.push(
commands.registerCommand(
"googleJavaFormatForVSCode.reloadExecutable",
this.load,
),
);
};

private load = async () =>
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export async function activate(context: ExtensionContext) {
config.subscribe();

const cache = await Cache.getInstance(context, log);
cache.subscribe();

const executable = await Executable.getInstance(
context,
config,
Expand Down

0 comments on commit 230405e

Please sign in to comment.