Skip to content

Commit

Permalink
Merge pull request #16 from sshveta/download_vsix_according_to_os
Browse files Browse the repository at this point in the history
[RFR] Download latest vsix file according to os and date
  • Loading branch information
sshveta authored Oct 22, 2024
2 parents 9255d4a + f5c1813 commit 6e969f1
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# Instead, copy it, remove the .example extension and provide your custom values
# For git password please use your generated git token, not a clear text password


VSCODE_EXECUTABLE_PATH='/usr/share/code/code'
VSIX_FILE_PATH='kai-vscode-plugin-0.0.3.vsix'
VSIX_FILE_PATH="/home/sshveta/Work/kai-ci/"
VSIX_FILE_NAME='konveyor-linux-0.0.1.vsix'
VSIX_DOWNLOAD_URL= 'https://github.com/konveyor/editor-extensions/releases/download/v0.0.1-dev%2B20241017/konveyor-linux-0.0.1.vsix'
PLUGIN_URL= 'https://github.com/konveyor/editor-extensions/releases/download/'
PLUGIN_VERSION= 'v0.0.1-dev%2B'
19 changes: 6 additions & 13 deletions e2e/pages/vscode.pages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { _electron as electron, ElectronApplication, Page } from 'playwright';
import { execSync } from 'child_process';
import * as fs from 'fs';
import { downloadLatestKAIPlugin } from '../utilities/download.utils';
import { getKAIPluginPath } from '../utilities/utils';

class LaunchVSCodePage {
private vscodeApp?: ElectronApplication;
Expand All @@ -15,15 +16,9 @@ class LaunchVSCodePage {
executablePath: string
): Promise<LaunchVSCodePage> {
try {
const vsixFilePath = process.env.VSIX_FILE_PATH;
if (vsixFilePath) {
console.log(`Installing extension from VSIX file: ${vsixFilePath}`);
await LaunchVSCodePage.installExtensionFromVSIX(vsixFilePath);
} else {
console.warn(
'VSIX_FILE_PATH environment variable is not set. Skipping extension installation.'
);
}
const vsixFilePath = getKAIPluginPath();
console.log(`Installing extension from VSIX file: ${vsixFilePath}`);
await LaunchVSCodePage.installExtensionFromVSIX(vsixFilePath);

// Launch VSCode as an Electron app
const vscodeApp = await electron.launch({
Expand All @@ -48,9 +43,7 @@ class LaunchVSCodePage {
private static async installExtensionFromVSIX(
vsixFilePath: string
): Promise<void> {
if (!fs.existsSync(vsixFilePath)) {
throw new Error(`VSIX file not found at path: ${vsixFilePath}`);
}
await downloadLatestKAIPlugin();

try {
// Execute command to install VSIX file using VSCode CLI
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/vscode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.describe('VSCode Tests', () => {

test('should open Extensions tab and verify installed extension', async () => {
const window = vscodeApp.getWindow();
const kaiTab = await window.getByRole('tab', { name: 'KAI', exact: true });
const kaiTab = await window.getByRole('tab', { name: 'Konveyor' });
await kaiTab.click();
// Assert if KAI explorer is opened.
const title = await window.getByRole('heading', {
Expand Down
63 changes: 63 additions & 0 deletions e2e/utilities/download.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import axios from 'axios';
import * as fs from 'fs';
import { getKAIPluginPath, getOSInfo, getKAIPluginName } from './utils';

/**
* Downloads a file from the given URL and saves it to the specified destination.
* @param fileUrl The URL of the file to download.
* @param outputLocationPath The local file path where the file will be saved.
* @returns Promise that resolves when the download is complete.
*/
export async function downloadFile(): Promise<void> {
const outputLocationPath = getKAIPluginPath();
const fileUrl = buildDownloadUrl();

const writer = fs.createWriteStream(outputLocationPath);

const response = await axios({
url: fileUrl,
method: 'GET',
responseType: 'stream',
});

response.data.pipe(writer);

return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}

export async function downloadLatestKAIPlugin() {
try {
await downloadFile();
console.log('File downloaded successfully!');
} catch (err) {
console.error('Error downloading the file:', err);
}
}

/**
* Builds the download URL for the KAI plugin with today's date
* and depending on operating system.
* @returns The generated URL string with the current date and os.
*/
export function buildDownloadUrl(): string {
const pluginUrl =
process.env.PLUGIN_URL ||
'https://github.com/konveyor/editor-extensions/releases/download/';
const version = process.env.PLUGIN_VERSION || 'v0.0.1-dev%2B';
const fileName = getKAIPluginName();

const today = new Date();

// Format date as YYYYMMDD
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0'); // Month is zero-based, so add 1
const day = String(today.getDate()).padStart(2, '0');
const formattedDate = `${year}${month}${day}`;

// Build the full URL
const url = `${pluginUrl}${version}${formattedDate}/${fileName}`;
return url;
}
29 changes: 29 additions & 0 deletions e2e/utilities/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as os from 'os';

// Function to get OS information
export function getOSInfo(): string {
const platform: NodeJS.Platform = os.platform();

switch (platform) {
case 'win32':
return 'windows';
case 'darwin':
return 'macOS';
case 'linux':
return 'linux';
default:
return `Unknown OS: ${platform}`;
}
}

export function getKAIPluginPath(): string {
const vsixFilePath = process.env.VSIX_FILE_PATH;
const pluginFilePath = vsixFilePath + getKAIPluginName();
return pluginFilePath;
}

export function getKAIPluginName(): string {
const vsixFileName =
process.env.VSIX_FILE_NAME || 'konveyor-linux-0.0.1.vsix';
return vsixFileName.replace(/(konveyor-)(\w+)(-.*)/, `$1${getOSInfo()}$3`);
}

0 comments on commit 6e969f1

Please sign in to comment.