From c0ee08c51b7601ce87579e866a825bc2061f8a27 Mon Sep 17 00:00:00 2001 From: Achal Talati Date: Thu, 16 Nov 2023 16:28:11 +0530 Subject: [PATCH 1/3] renamed downloaded jdk binaries so that it is replaced by the already present binaries Signed-off-by: Achal Talati --- vscode/src/jdkDownloader.ts | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/vscode/src/jdkDownloader.ts b/vscode/src/jdkDownloader.ts index 5e54f36..770301c 100644 --- a/vscode/src/jdkDownloader.ts +++ b/vscode/src/jdkDownloader.ts @@ -174,7 +174,7 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s writeStream.on('finish', async () => { vscode.window.showInformationMessage(`${JDKType} ${JDKVersion} for ${osType} download completed!`); - await extractJDK(path.join(targetDirectory, fileName), installationPath, JDKVersion, osType); + await extractJDK(path.join(targetDirectory, fileName), installationPath, JDKVersion, osType, JDKType); }); writeStream.on('error', error => { @@ -189,25 +189,25 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s request.end(); } -export async function extractJDK(jdkTarballPath: string, extractionTarget: string, jdkVersion: string, osType: string): Promise { +export async function extractJDK(jdkTarballPath: string, extractionTarget: string, jdkVersion: string, osType: string, jdkType: string): Promise { const extractCommand = `tar -xzf "${jdkTarballPath}" -C "${extractionTarget}"`; + const oldDirName = `jdk-${jdkVersion}`; + const oldDirectoryPath = await handleJdkPaths(oldDirName, extractionTarget, osType); child_process.exec(extractCommand, async (error) => { - if (error) { vscode.window.showErrorMessage('Error: ' + error); } else { - let installationPath; + const dirName = `${jdkType.split(' ').join('_')}-${jdkVersion}`; + const newDirectoryPath = await handleJdkPaths(dirName, extractionTarget, osType); + await fs.promises.rename(oldDirectoryPath, newDirectoryPath); + + let binPath = newDirectoryPath; if (osType === 'macOS') { - const extractedDirectoryName = `jdk-${jdkVersion}.jdk`; - installationPath = path.join(extractionTarget, extractedDirectoryName, 'Contents', 'Home'); + binPath = path.join(newDirectoryPath, 'Contents', 'Home'); } - else { - const extractedDirectoryName = `jdk-${jdkVersion}`; - installationPath = path.join(extractionTarget, extractedDirectoryName); - } - vscode.workspace.getConfiguration('jdk').update('jdkhome', installationPath, true); + vscode.workspace.getConfiguration('jdk').update('jdkhome', binPath, true); } fs.unlink(jdkTarballPath, async (err) => { @@ -220,6 +220,19 @@ export async function extractJDK(jdkTarballPath: string, extractionTarget: strin }); } +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 }); + } + + return directoryPath; +} + const selectPath = async (installType: string): Promise => { const options: vscode.OpenDialogOptions = { canSelectFiles: false, From fb76445ec09c464d63495974d0da91aa6889bba2 Mon Sep 17 00:00:00 2001 From: Achal Talati Date: Mon, 27 Nov 2023 22:34:37 +0530 Subject: [PATCH 2/3] 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; From 4a876a5582df1cfaa2d50583090d4e17db33cf37 Mon Sep 17 00:00:00 2001 From: Achal Talati Date: Mon, 18 Dec 2023 13:17:50 +0530 Subject: [PATCH 3/3] updated the flow of execution Signed-off-by: Achal Talati --- vscode/src/jdkDownloader.ts | 43 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/vscode/src/jdkDownloader.ts b/vscode/src/jdkDownloader.ts index 651a784..64a852d 100644 --- a/vscode/src/jdkDownloader.ts +++ b/vscode/src/jdkDownloader.ts @@ -190,39 +190,44 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s } export async function extractJDK(jdkTarballPath: string, extractionTarget: string, jdkVersion: string, osType: string, jdkType: string): Promise { + // Extract jdk binaries in a temp folder const downloadedDir = path.join(__dirname, 'jdk_downloads'); - const extractCommand = `tar -xzf "${jdkTarballPath}" -C "${downloadedDir}"`; + const extractCommand = `tar -xzf "${jdkTarballPath}" -C ${downloadedDir}`; + const tempDirName = `jdk-${jdkVersion}${osType === 'macOS' ? '.jdk' : ''}`; + const tempDirectoryPath = path.join(downloadedDir, tempDirName); + let newDirectoryPath: string | null = null; - const oldDirName = `jdk-${jdkVersion}`; - 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 directory with same name is present in the user selected download location then ask user if they want to delete it or not? + const newDirName = `${jdkType.split(' ').join('_')}-${jdkVersion}`; + newDirectoryPath = await handleJdkPaths(newDirName, 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); + vscode.window.showInformationMessage(`Cannot install ${jdkType} ${jdkVersion}. Cannot delete ${newDirName}`); + } else { + // If user agrees for deleting the directory then delete it and move the temp directory to the user selected location + await fs.promises.rename(tempDirectoryPath, newDirectoryPath); - let binPath = newDirectoryPath; - if (osType === 'macOS') { - binPath = path.join(newDirectoryPath, 'Contents', 'Home'); + let binPath = newDirectoryPath; + if (osType === 'macOS') { + binPath = path.join(newDirectoryPath, 'Contents', 'Home'); + } + vscode.workspace.getConfiguration('jdk').update('jdkhome', binPath, true); } - vscode.workspace.getConfiguration('jdk').update('jdkhome', binPath, true); } fs.unlink(jdkTarballPath, async (err) => { if (err) { vscode.window.showErrorMessage("Error: " + err); } else { - await installationCompletion("automatic"); + if (fs.existsSync(tempDirectoryPath)) { + await fs.promises.rmdir(tempDirectoryPath, { recursive: true }); + } + if (newDirectoryPath !== null) { + await installationCompletion("automatic"); + } } }); }); @@ -235,7 +240,7 @@ const handleJdkPaths = async (directoryName: string, parentPath: string, osType: } const directoryPath = path.join(parentPath, name); if (fs.existsSync(directoryPath)) { - const CONFIRMATION_MESSAGE = `${name} is already present. Do you want to delete it and replace with new contents?`; + const CONFIRMATION_MESSAGE = `${name} is already present. Do you want to delete it and create with new contents?`; const selected = await vscode.window.showInformationMessage(CONFIRMATION_MESSAGE, "Yes", "No"); if (selected === "Yes") { await fs.promises.rmdir(directoryPath, { recursive: true });