-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from JoseVSeb/alpha
Alpha
- Loading branch information
Showing
23 changed files
with
530 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { createHash } from "node:crypto"; | ||
import path = require("node:path"); | ||
import { | ||
ExtensionContext, | ||
LogOutputChannel, | ||
Uri, | ||
commands, | ||
workspace, | ||
} from "vscode"; | ||
|
||
export class Cache { | ||
private uri: Uri; | ||
|
||
private constructor( | ||
private context: ExtensionContext, | ||
private log: LogOutputChannel, | ||
cacheFolder: string, | ||
) { | ||
this.uri = Uri.joinPath(context.extensionUri, cacheFolder); | ||
} | ||
|
||
public static async getInstance( | ||
context: ExtensionContext, | ||
log: LogOutputChannel, | ||
cacheFolder = "cache", | ||
) { | ||
const cache = new Cache(context, log, cacheFolder); | ||
await cache.init(); | ||
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 { | ||
await workspace.fs.createDirectory(this.uri); | ||
this.log.debug(`Cache directory created at ${this.uri.toString()}`); | ||
} catch (error) { | ||
this.log.error(`Failed to create cache directory: ${error}`); | ||
throw error; | ||
} | ||
}; | ||
|
||
get = async (url: string) => { | ||
const basename = path.basename(url); | ||
|
||
const hash = createHash("md5").update(url).digest("hex"); | ||
const dirname = Uri.joinPath(this.uri, hash); | ||
const localPath = Uri.joinPath(dirname, basename); | ||
|
||
try { | ||
// Check if the file is already cached locally | ||
await workspace.fs.stat(localPath); | ||
|
||
this.log.info(`Using cached file at ${localPath.toString()}`); | ||
|
||
return localPath; | ||
} catch (error) { | ||
// Create the cache directory if it doesn't exist | ||
try { | ||
await workspace.fs.createDirectory(dirname); | ||
this.log.debug( | ||
`Cache directory created at ${dirname.toString()}`, | ||
); | ||
} catch (error) { | ||
this.log.error(`Failed to create cache directory: ${error}`); | ||
throw error; | ||
} | ||
|
||
// Download the file and write it to the cache directory | ||
this.log.info(`Downloading file from ${url}`); | ||
|
||
const response = await fetch(url); | ||
if (response.ok) { | ||
const buffer = await response.arrayBuffer(); | ||
await workspace.fs.writeFile(localPath, new Uint8Array(buffer)); | ||
|
||
this.log.info(`File saved to ${localPath.toString()}`); | ||
|
||
return localPath; | ||
} else { | ||
throw new Error( | ||
`Failed to download file from ${url}: ${response.status} ${response.statusText}`, | ||
); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { execSync } from "node:child_process"; | ||
import { | ||
// CancellationToken, | ||
ExtensionContext, | ||
LogOutputChannel, | ||
// Progress, | ||
ProgressLocation, | ||
commands, | ||
window, | ||
} from "vscode"; | ||
import { Cache } from "./Cache"; | ||
import { ExtensionConfiguration } from "./ExtensionConfiguration"; | ||
import { resolveExecutableFileFromConfig } from "./resolveExecutableFileFromConfig"; | ||
import path = require("node:path"); | ||
|
||
export class Executable { | ||
private runner: string = null!; | ||
private cwd: string = null!; | ||
|
||
private constructor( | ||
private context: ExtensionContext, | ||
private config: ExtensionConfiguration, | ||
private cache: Cache, | ||
private log: LogOutputChannel, | ||
) {} | ||
|
||
public static async getInstance( | ||
context: ExtensionContext, | ||
config: ExtensionConfiguration, | ||
cache: Cache, | ||
log: LogOutputChannel, | ||
) { | ||
const instance = new Executable(context, config, cache, log); | ||
await instance.load(); | ||
return instance; | ||
} | ||
|
||
run = async ({ | ||
args, | ||
stdin, | ||
}: { | ||
args: string[]; | ||
stdin: string; | ||
signal?: AbortSignal; | ||
}) => { | ||
return new Promise<string>((resolve, reject) => { | ||
try { | ||
const command = `${this.runner} ${args.join(" ")} -`; | ||
|
||
this.log.debug(`> ${command}`); | ||
|
||
const stdout: string = execSync(command, { | ||
cwd: this.cwd, | ||
encoding: "utf8", | ||
input: stdin, | ||
maxBuffer: Infinity, | ||
windowsHide: true, | ||
}); | ||
|
||
resolve(stdout); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
|
||
subscribe = () => { | ||
this.config.subscriptions.push(this.configurationChangeListener); | ||
this.context.subscriptions.push( | ||
commands.registerCommand( | ||
"googleJavaFormatForVSCode.reloadExecutable", | ||
this.load, | ||
), | ||
); | ||
}; | ||
|
||
private load = async () => | ||
// progress?: Progress<{ | ||
// message?: string | undefined; | ||
// increment?: number | undefined; | ||
// }>, | ||
// token?: CancellationToken, | ||
{ | ||
const uri = await resolveExecutableFileFromConfig( | ||
this.config, | ||
this.log, | ||
); | ||
|
||
const { fsPath } = | ||
uri.scheme === "file" | ||
? uri | ||
: await this.cache.get(uri.toString()); | ||
|
||
const isJar = fsPath.endsWith(".jar"); | ||
const dirname = path.dirname(fsPath); | ||
const basename = path.basename(fsPath); | ||
|
||
if (isJar) { | ||
this.runner = `java -jar ./${basename}`; | ||
} else if (process.platform === "win32") { | ||
this.runner = basename; | ||
} else { | ||
this.runner = `./${basename}`; | ||
} | ||
|
||
this.cwd = dirname; | ||
}; | ||
|
||
private configurationChangeListener = async () => { | ||
this.log.info("Configuration change detected."); | ||
const action = await window.showInformationMessage( | ||
"Configuration change detected. Update executable?", | ||
"Update", | ||
"Ignore", | ||
); | ||
|
||
if (action !== "Update") { | ||
this.log.debug("User ignored updating executable."); | ||
return; | ||
} | ||
|
||
this.log.debug("Updating executable..."); | ||
window.withProgress( | ||
{ | ||
location: ProgressLocation.Notification, | ||
title: "Updating executable...", | ||
cancellable: false, | ||
}, | ||
this.load, | ||
); | ||
}; | ||
} |
Oops, something went wrong.