From fb76445ec09c464d63495974d0da91aa6889bba2 Mon Sep 17 00:00:00 2001 From: Achal Talati Date: Mon, 27 Nov 2023 22:34:37 +0530 Subject: [PATCH] added feature to have both oracle and open jdk downloaded via JDK Downloader in same directory Signed-off-by: Achal Talati --- vscode/src/jdkDownloader.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/vscode/src/jdkDownloader.ts b/vscode/src/jdkDownloader.ts index 770301c..651a784 100644 --- a/vscode/src/jdkDownloader.ts +++ b/vscode/src/jdkDownloader.ts @@ -190,17 +190,25 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s } export async function extractJDK(jdkTarballPath: string, extractionTarget: string, jdkVersion: string, osType: string, jdkType: string): Promise { - - const extractCommand = `tar -xzf "${jdkTarballPath}" -C "${extractionTarget}"`; + const downloadedDir = path.join(__dirname, 'jdk_downloads'); + const extractCommand = `tar -xzf "${jdkTarballPath}" -C "${downloadedDir}"`; const oldDirName = `jdk-${jdkVersion}`; - const oldDirectoryPath = await handleJdkPaths(oldDirName, extractionTarget, osType); + const oldDirectoryPath = path.join(downloadedDir, osType === 'macOS' ? oldDirName + '.jdk' : oldDirName); + if (fs.existsSync(oldDirectoryPath)) { + await fs.promises.rmdir(oldDirectoryPath, { recursive: true }); + } child_process.exec(extractCommand, async (error) => { if (error) { vscode.window.showErrorMessage('Error: ' + error); } else { const dirName = `${jdkType.split(' ').join('_')}-${jdkVersion}`; const newDirectoryPath = await handleJdkPaths(dirName, extractionTarget, osType); + if (newDirectoryPath === null) { + await fs.promises.rmdir(oldDirectoryPath, { recursive: true }); + vscode.window.showInformationMessage(`Cannot install ${jdkType} ${jdkVersion}. Cannot delete ${dirName}`); + return; + } await fs.promises.rename(oldDirectoryPath, newDirectoryPath); let binPath = newDirectoryPath; @@ -220,14 +228,21 @@ export async function extractJDK(jdkTarballPath: string, extractionTarget: strin }); } -const handleJdkPaths = async (directoryName: string, parentPath: string, osType: string): Promise => { +const handleJdkPaths = async (directoryName: string, parentPath: string, osType: string): Promise => { let name = directoryName; if (osType === 'macOS') { name = `${directoryName}.jdk`; } const directoryPath = path.join(parentPath, name); if (fs.existsSync(directoryPath)) { - await fs.promises.rmdir(directoryPath, { recursive: true }); + const CONFIRMATION_MESSAGE = `${name} is already present. Do you want to delete it and replace with new contents?`; + const selected = await vscode.window.showInformationMessage(CONFIRMATION_MESSAGE, "Yes", "No"); + if (selected === "Yes") { + await fs.promises.rmdir(directoryPath, { recursive: true }); + } + else if (selected === "No") { + return null; + } } return directoryPath;