Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launching vscode with some default extensions installed #424

Merged
merged 17 commits into from
Nov 1, 2024
4 changes: 2 additions & 2 deletions .github/workflows/smoke-test-pr-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ jobs:
chectl server:deploy \
--batch \
--platform minikube \
--k8spodwaittimeout=120000 \
--k8spodreadytimeout=120000 \
--k8spodwaittimeout=480000 \
--k8spodreadytimeout=480000 \
--che-operator-cr-patch-yaml "${GITHUB_WORKSPACE}/build/test/github-minikube-checluster-patch.yaml"

#
Expand Down
106 changes: 106 additions & 0 deletions code/extensions/che-commands/src/default-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**********************************************************************
* Copyright (c) 2024 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

/* eslint-disable header/header */

import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import * as path from 'path';

const DEFAULT_EXTENSIONS_FILE = path.join(process.env.PROJECTS_ROOT!, '.default-extensions');

export class DefaultExtensions {

async install(): Promise<void> {
if (!process.env.DEFAULT_EXTENSIONS) {
return;
}

try {
// get list of extesions from DEFAULT_EXTENSIONS environment variable
let extensions: string[] = process.env.DEFAULT_EXTENSIONS!.split(';').filter(value => (value.trim()));

// filter the list, remove the extensions installed before
const installed = await this.readDotDefaultExtensionsFile();
extensions = extensions.filter(value => !installed.includes(value));

if (extensions.length) {
console.log(`Installing default extensions: ${extensions.join('; ')}`);
const result = await this.installExtensions(extensions);
if (result) {
this.writeDotDefaultExtensionsFile(extensions);
}
}
} catch (error) {
console.error(`Failed to install default extensions. ${error}`);
}
}

async readDotDefaultExtensionsFile(): Promise<string[]> {
try {
if (await fs.pathExists(DEFAULT_EXTENSIONS_FILE)) {
return (await fs.readFile(DEFAULT_EXTENSIONS_FILE, 'utf8')).split('\n');
}

} catch (error) {
console.error(`Failed to read .default-extensions file. ${error}`);
}

return [];
}

async writeDotDefaultExtensionsFile(defaultExtensions: string[]): Promise<void> {
try {
let fileContent: string = '';
if (await fs.pathExists(DEFAULT_EXTENSIONS_FILE)) {
fileContent = await fs.readFile(DEFAULT_EXTENSIONS_FILE, 'utf8');
}

let extensions: string[] = fileContent.split('\n').filter(value => (value));
for (const extension of defaultExtensions) {
if (!extensions.includes(extension)) {
extensions.push(extension);
}
}

fileContent = extensions.join('\n');
await fs.writeFile(DEFAULT_EXTENSIONS_FILE, fileContent);

} catch (error) {
console.error(`Failed to write to .default-extensions file. ${error}`);
}
}

async installExtensions(extensions: string[]): Promise<boolean> {
const toInstall: vscode.Uri[] = extensions.map(value => vscode.Uri.file(value));

let installed = false;

await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
cancellable: false,
title: 'Installing default extensions'
}, async (progress) => {
progress.report({ increment: 0 });

try {
await vscode.commands.executeCommand('workbench.extensions.command.installFromVSIX', toInstall);
installed = true;
} catch (error) {
vscode.window.showInformationMessage(`Failed to install default extensions. ${error.message ? error.message : error}`);
}

progress.report({ increment: 100 });
});

return installed;
}

}
3 changes: 3 additions & 0 deletions code/extensions/che-commands/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import * as vscode from 'vscode';
import { DevfileTaskProvider } from './taskProvider';
import { DefaultExtensions } from './default-extensions';

export async function activate(context: vscode.ExtensionContext): Promise<void> {
const channel: vscode.OutputChannel = vscode.window.createOutputChannel('Devfile Commands');
Expand All @@ -23,6 +24,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
const disposable = vscode.tasks.registerTaskProvider('devfile', taskProvider);

context.subscriptions.push(disposable);

await new DefaultExtensions().install();
}

async function getExtensionAPI(extID: string): Promise<any> {
Expand Down
Loading