Skip to content

Commit bef8d6e

Browse files
authored
Merge pull request #133 from Achal1607/javavscode-131
Added JDK 22 GA download option and refactored code a bit
2 parents 32b32c9 + 6dfadc5 commit bef8d6e

File tree

3 files changed

+60
-89
lines changed

3 files changed

+60
-89
lines changed

vscode/src/constants.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

vscode/src/jdkDownloader.ts

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ import * as https from 'https';
2222
import * as child_process from 'child_process';
2323
import * as vscode from 'vscode';
2424
import * as crypto from 'crypto';
25-
import { JDK_RELEASES_TRACK_URL, OPEN_JDK_VERSION_DOWNLOAD_LINKS, ORACLE_JDK_BASE_DOWNLOAD_URL, ORACLE_JDK_VERSION_FALLBACK_DOWNLOAD_VERSIONS } from './constants';
2625
import { handleLog } from './extension';
2726
import { promisify } from 'util';
27+
import * as glob from 'glob';
2828

2929
let customView: vscode.WebviewPanel;
3030
let logger: vscode.OutputChannel;
31+
let jdkConfContent: any;
32+
const JDK_DOWNLOADER_CONF_FILE_PATH = "https://raw.githubusercontent.com/oracle/javavscode/main/vscode/src/jdkDownloaderManagement.json";
33+
3134

3235
export const calculateChecksum = async (filePath: string): Promise<string> => {
3336
const ALGORITHM = 'sha256';
@@ -73,38 +76,15 @@ export const fetchDropdownOptions = async () => {
7376
// Fetch version of the JDK available
7477
let versions;
7578
try {
76-
const response = await axios.get(JDK_RELEASES_TRACK_URL, { data: { ResponseType: 'document' }, timeout: 3500 });
77-
versions = filterLatestJDKVersionsAvail(response.data.data.releases);
78-
} catch (error) {
79-
versions = ORACLE_JDK_VERSION_FALLBACK_DOWNLOAD_VERSIONS;
80-
handleLog(logger, "Using fallback versions for oracle JDK");
79+
const res = await axios.get(JDK_DOWNLOADER_CONF_FILE_PATH, { timeout: 4000 });
80+
versions = await res.data;
81+
} catch (err: any) {
82+
vscode.window.showErrorMessage("Error fetching JDK download versions");
83+
handleLog(logger, err?.messge);
8184
}
82-
8385
return { machineArch, osType, versions };
8486
}
8587

86-
const filterLatestJDKVersionsAvail = (json: Array<any>) => {
87-
const deliveredVersions = json.filter(release => release.status == "delivered");
88-
89-
const sortedVersionWise = deliveredVersions.sort((a, b) => parseInt(b.family) - parseInt(a.family));
90-
const latestVersion = sortedVersionWise[0];
91-
92-
const ltsVersions = deliveredVersions.filter(release => {
93-
if (release.type === "Feature") {
94-
const dateDiff: number = new Date(release.eosl).getFullYear() - new Date(release.ga).getFullYear();
95-
return dateDiff > 5;
96-
}
97-
return false;
98-
});
99-
100-
let latestLtsVersion = sortedVersionWise.filter(release => release.family === ltsVersions[0].family)[0];
101-
if (latestLtsVersion.family == latestVersion.family) {
102-
latestLtsVersion = sortedVersionWise.filter(release => release.family === ltsVersions[1].family)[0];
103-
}
104-
105-
return { latestVersion, latestLtsVersion }
106-
}
107-
10888
export async function openJDKSelectionView(log: vscode.OutputChannel) {
10989

11090
// Create JDK Downloader view
@@ -118,6 +98,7 @@ export async function openJDKSelectionView(log: vscode.OutputChannel) {
11898
);
11999
logger = log;
120100
const { machineArch, osType, versions } = await fetchDropdownOptions();
101+
jdkConfContent = versions;
121102
customView.webview.html = fetchJDKDownloadView(machineArch, osType, versions);
122103

123104
customView.webview.onDidReceiveMessage(async message => {
@@ -132,7 +113,7 @@ export async function openJDKSelectionView(log: vscode.OutputChannel) {
132113
return;
133114
}
134115

135-
vscode.window.showInformationMessage(`Downloading and completing setup of ${jdkType} ${jdkVersion.split('.')[0]}...`);
116+
vscode.window.showInformationMessage(`Downloading and completing setup of ${jdkType} ${jdkVersion}...`);
136117
JDKDownloader(jdkType, jdkOS, jdkArch, jdkVersion, installationPath);
137118
}
138119
}
@@ -143,22 +124,13 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s
143124
let downloadUrl: string = '';
144125

145126
// Generate download url on the basis of the jdk type chosen
146-
if (JDKType === 'OpenJDK') {
147-
if (osType === 'windows') {
148-
downloadUrl = `${OPEN_JDK_VERSION_DOWNLOAD_LINKS[`${JDKVersion}`]}_${osType.toLowerCase()}-${osArchitecture}_bin.zip`;
149-
}
150-
else {
151-
downloadUrl = `${OPEN_JDK_VERSION_DOWNLOAD_LINKS[`${JDKVersion}`]}_${osType.toLowerCase()}-${osArchitecture}_bin.tar.gz`;
152-
}
127+
const { baseDownloadUrl } = JDKType === 'OpenJDK' ? jdkConfContent.openJdk[`${JDKVersion}`] : jdkConfContent.oracleJdk[`${JDKVersion}`];
128+
129+
if (osType === 'windows') {
130+
downloadUrl = `${baseDownloadUrl}_${osType.toLowerCase()}-${osArchitecture}_bin.zip`;
153131
}
154-
else if (JDKType === 'Oracle JDK') {
155-
const baseVersion = JDKVersion.split('.')[0];
156-
if (osType === 'windows') {
157-
downloadUrl = `${ORACLE_JDK_BASE_DOWNLOAD_URL}/${baseVersion}/latest/jdk-${baseVersion}_${osType.toLowerCase()}-${osArchitecture}_bin.zip`;
158-
}
159-
else {
160-
downloadUrl = `${ORACLE_JDK_BASE_DOWNLOAD_URL}/${baseVersion}/latest/jdk-${baseVersion}_${osType.toLowerCase()}-${osArchitecture}_bin.tar.gz`;
161-
}
132+
else {
133+
downloadUrl = `${baseDownloadUrl}_${osType.toLowerCase()}-${osArchitecture}_bin.tar.gz`;
162134
}
163135

164136
// Define the target directory and file name
@@ -215,17 +187,25 @@ export function JDKDownloader(JDKType: string, osType: string, osArchitecture: s
215187
}
216188

217189
export async function extractJDK(jdkTarballPath: string, extractionTarget: string, jdkVersion: string, osType: string, jdkType: string): Promise<void> {
218-
// Extract jdk binaries in a temp folder
219190
const downloadedDir = path.join(__dirname, 'jdk_downloads');
191+
192+
// Remove already present version of a particular JDK from temp dir
193+
const oldTempExtractedDirs = glob.sync(`jdk-${jdkVersion}*`, { cwd: downloadedDir });
194+
for await (const oldDirName of oldTempExtractedDirs) {
195+
await fs.promises.rmdir(path.join(downloadedDir, oldDirName), { recursive: true });
196+
}
197+
198+
// Extract jdk binaries in a temp folder
220199
const extractCommand = `tar -xzf "${jdkTarballPath}" -C ${downloadedDir}`;
221-
const tempDirName = `jdk-${jdkVersion}${osType === 'macOS' ? '.jdk' : ''}`;
222-
const tempDirectoryPath = path.join(downloadedDir, tempDirName);
200+
let tempDirectoryPath: string | null = null;
223201
let newDirectoryPath: string | null = null;
224202

225203
child_process.exec(extractCommand, async (error) => {
226204
if (error) {
227205
vscode.window.showErrorMessage('Error: ' + error);
228206
} else {
207+
const tempDirName = glob.sync(`jdk-${jdkVersion}*`, { cwd: downloadedDir })?.[0];
208+
tempDirectoryPath = path.join(downloadedDir, tempDirName);
229209
// If directory with same name is present in the user selected download location then ask user if they want to delete it or not?
230210
const newDirName = `${jdkType.split(' ').join('_')}-${jdkVersion}`;
231211
newDirectoryPath = await handleJdkPaths(newDirName, extractionTarget, osType);
@@ -247,7 +227,7 @@ export async function extractJDK(jdkTarballPath: string, extractionTarget: strin
247227
if (err) {
248228
vscode.window.showErrorMessage("Error: " + err);
249229
} else {
250-
if (fs.existsSync(tempDirectoryPath)) {
230+
if (tempDirectoryPath && fs.existsSync(tempDirectoryPath)) {
251231
await fs.promises.rmdir(tempDirectoryPath, { recursive: true });
252232
}
253233
if (newDirectoryPath !== null) {
@@ -421,7 +401,7 @@ export const fetchJDKDownloadView = (machineArch: string, osType: string, versio
421401
</style>
422402
<body>
423403
<h1>JDK Downloader</h1>
424-
<p>This tool enables you to download either the Oracle Java SE JDK with <a href="https://www.java.com/freeuselicense"> Oracle No-Fee Terms and Conditions</a> or the Oracle OpenJDK builds under the <a href="http://openjdk.org/legal/gplv2+ce.html">GNU Public License with ClassPath Exception</a>.
404+
<p>This tool enables you to download either the latest Oracle Java SE JDK with <a href="https://www.java.com/freeuselicense"> Oracle No-Fee Terms and Conditions</a> or the Oracle OpenJDK builds under the <a href="http://openjdk.org/legal/gplv2+ce.html">GNU Public License with ClassPath Exception</a>.
425405
It will then handle the installation and configuration on your behalf.</p>
426406
<p>This enables you to take full advantage of all the features offered by this extension.</p>
427407
<br>
@@ -438,8 +418,12 @@ export const fetchJDKDownloadView = (machineArch: string, osType: string, versio
438418
<br />
439419
<div class="jdk-version-dropdown">
440420
<select id="oracleJDKVersionDropdown">
441-
<option value="${versions.latestVersion.version}" default>JDK ${versions.latestVersion.family}(${versions.latestVersion.version})</option>
442-
<option value="${versions.latestLtsVersion.version}">JDK ${versions.latestLtsVersion.family}(${versions.latestLtsVersion.version})</option>
421+
${Object.keys(versions.oracleJdk).sort((a, b) => parseFloat(b)-parseFloat(a)).map((el, index) => {
422+
if (index === 0) {
423+
return `<option value=${el} default>JDK ${el}</option>`
424+
}
425+
return `<option value=${el}>JDK ${el}</option>`
426+
})}
443427
</select>
444428
</div>
445429
</div>
@@ -474,11 +458,11 @@ export const fetchJDKDownloadView = (machineArch: string, osType: string, versio
474458
<br />
475459
<div class="jdk-version-dropdown">
476460
<select id="openJDKVersionDropdown">
477-
${Object.keys(OPEN_JDK_VERSION_DOWNLOAD_LINKS).map((el, index) => {
461+
${Object.keys(versions.openJdk).sort((a, b) => parseFloat(b)-parseFloat(a)).map((el, index) => {
478462
if (index === 0) {
479-
return `<option value=${el} default>JDK ${el.split('.')[0]}(${el})</option>`
463+
return `<option value=${el} default>JDK ${el}</option>`
480464
}
481-
return `<option value=${el}>JDK ${el.split('.')[0]}(${el})</option>`
465+
return `<option value=${el}>JDK ${el}</option>`
482466
})}
483467
</select>
484468
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"oracleJdk" : {
3+
"22": {
4+
"baseDownloadUrl":"https://download.oracle.com/java/22/latest/jdk-22"
5+
},
6+
"21":{
7+
"baseDownloadUrl":"https://download.oracle.com/java/21/latest/jdk-21"
8+
},
9+
"17":{
10+
"baseDownloadUrl":"https://download.oracle.com/java/17/latest/jdk-17"
11+
}
12+
},
13+
"openJdk" : {
14+
"22" :{
15+
"baseDownloadUrl":"https://download.java.net/java/GA/jdk22/830ec9fcccef480bb3e73fb7ecafe059/36/GPL/openjdk-22"
16+
},
17+
"21" : {
18+
"baseDownloadUrl":"https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)