Skip to content

Commit

Permalink
"Components" sidebar section take a while to load redhat-developer#3850
Browse files Browse the repository at this point in the history
Fixes: redhat-developer#3850

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Sep 15, 2024
1 parent 7296a84 commit fdd132f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/devfile-registry/devfileRegistryWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as https from 'https';
import * as YAML from 'js-yaml';
import { ExecutionContext } from '../cli';
import { Registry } from '../odo/componentType';
import { Odo } from '../odo/odoWrapper';
import { OdoPreference } from '../odo/odoPreference';
import { DevfileData, DevfileInfo } from './devfileInfo';

export const DEVFILE_VERSION_LATEST: string = 'latest';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class DevfileRegistry {
let registries: Registry[] = [];
const key = ExecutionContext.key('getRegistries');
if (this.executionContext && !this.executionContext.has(key)) {
registries = await Odo.Instance.getRegistries();
registries = await OdoPreference.Instance.getRegistries();
this.executionContext.set(key, registries);
} else {
registries = this.executionContext.get(key);
Expand Down
92 changes: 92 additions & 0 deletions src/odo/odoPreference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import * as fs from 'fs/promises';
import * as YAML from 'js-yaml';
import * as path from 'path';
import { Platform } from '../util/platform';
import { Registry } from './componentType';


type OdoSettings = {
OdoSettings: {
RegistryList: [{
Name: string;
URL: string;
secure: boolean;
}];
}
};

export class OdoPreference {
private static instance: OdoPreference;

public static get Instance(): OdoPreference {
if (!OdoPreference.instance) {
OdoPreference.instance = new OdoPreference();
}
return OdoPreference.instance;
}

private getOdoPreferenceFile(): string {
// This value can be provided to set a seperate directory for users 'homedir' resolution
// note for mocking purpose ONLY
const customHomeDir = Platform.ENV.CUSTOM_HOMEDIR;
const configFileName = 'preference.yaml';

if (customHomeDir && customHomeDir.length > 0) {
return path.join(customHomeDir, '.odo', configFileName);
}

const userHomeDir = Platform.getUserHomePath();
return path.join(userHomeDir, '.odo', configFileName);
}

public async getRegistries(): Promise<Registry[]> {
const odoPreference = await this.readOdoPreference();
const odoRegistryList = (odoPreference as OdoSettings)?.OdoSettings?.RegistryList;
return odoRegistryList?.map((r) => {
return {
name: r.Name,
url: r.URL,
secure: r.secure } as Registry;
});
}

public async addRegistry(name: string, url: string, token: string): Promise<Registry> {
const odoPreference = await this.readOdoPreference();
odoPreference.OdoSettings.RegistryList.push({
Name: name,
URL: url,
secure: !!token
} as never);
await this.writeOdoPreference(odoPreference);
return {
name,
secure: !!token,
url,
};
}

public async removeRegistry(name: string): Promise<void> {
const odoPreference = await this.readOdoPreference();

odoPreference.OdoSettings.RegistryList.splice(
odoPreference.OdoSettings.RegistryList.findIndex((registry) => registry.name === name), 1
);

await this.writeOdoPreference(odoPreference);
}

public async readOdoPreference(): Promise<any> {
const odoPreferenceFile = await fs.readFile(this.getOdoPreferenceFile(), 'utf8');
return YAML.load(odoPreferenceFile);
}

public async writeOdoPreference(preference: any): Promise<any> {
const preferenceYaml = YAML.dump(preference);
await fs.writeFile(this.getOdoPreferenceFile(), preferenceYaml, 'utf8');
}
}
56 changes: 28 additions & 28 deletions src/odo/odoWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ToolsConfig } from '../tools';
import { ChildProcessUtil, CliExitData } from '../util/childProcessUtil';
import { VsCommandError } from '../vscommand';
import { Command } from './command';
import { Registry } from './componentType';
import { ComponentDescription } from './componentTypeDescription';
import { BindableService } from './odoTypes';

Expand Down Expand Up @@ -259,36 +258,37 @@ export class Odo {
// return data;
// }

private async loadRegistryFromPreferences() {
const cliData = await this.execute(new CommandText('odo', 'preference view -o json'));
const prefs = JSON.parse(cliData.stdout) as { registries: Registry[] };
return prefs.registries;
}
// private async loadRegistryFromPreferences() {
// const cliData = await this.execute(new CommandText('odo', 'preference view -o json'));
// const prefs = JSON.parse(cliData.stdout) as { registries: Registry[] };
// return prefs.registries;
// }

public getRegistries(): Promise<Registry[]> {
return this.loadRegistryFromPreferences();
}
// public getRegistries(): Promise<Registry[]> {
// // return this.loadRegistryFromPreferences();
// return OdoPreference.Instance.getOdoRegistries();
// }

public async addRegistry(name: string, url: string, token: string): Promise<Registry> {
const command = new CommandText('odo', `preference add registry ${name} ${url}`);
if (token) {
command.addOption(new CommandOption('--token', token));
}
await this.execute(command);
return {
name,
secure: !!token,
url,
};
}
// public async addRegistry(name: string, url: string, token: string): Promise<Registry> {
// const command = new CommandText('odo', `preference add registry ${name} ${url}`);
// if (token) {
// command.addOption(new CommandOption('--token', token));
// }
// await this.execute(command);
// return {
// name,
// secure: !!token,
// url,
// };
// }

public async removeRegistry(name: string): Promise<void> {
await this.execute(
new CommandText('odo', `preference remove registry ${name}`, [
new CommandOption('--force'),
]),
);
}
// public async removeRegistry(name: string): Promise<void> {
// await this.execute(
// new CommandText('odo', `preference remove registry ${name}`, [
// new CommandOption('--force'),
// ]),
// );
// }

/**
* Deletes all the odo configuration files associated with the component (`.odo`, `devfile.yaml`) located at the given path.
Expand Down
7 changes: 4 additions & 3 deletions src/registriesView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Registry
} from './odo/componentType';
import { StarterProject } from './odo/componentTypeDescription';
import { OdoPreference } from './odo/odoPreference';
import { Odo } from './odo/odoWrapper';
import { inputValue, quickBtn } from './util/inputValue';
import { Progress } from './util/progress';
Expand Down Expand Up @@ -364,9 +365,9 @@ export class ComponentTypesView implements TreeDataProvider<ComponentType> {

const devfileInfos = await DevfileRegistry.Instance.getDevfileInfoList(regURL);
if (devfileInfos.length > 0) {
const newRegistry = await Odo.Instance.addRegistry(regName, regURL, token);
const newRegistry = await OdoPreference.Instance.addRegistry(regName, regURL, token);
if (registryContext) {
await Odo.Instance.removeRegistry(registryContext.name);
await OdoPreference.Instance.removeRegistry(registryContext.name);
ComponentTypesView.instance.replaceRegistry(registryContext, newRegistry);
} else {
ComponentTypesView.instance.addRegistry(newRegistry);
Expand All @@ -393,7 +394,7 @@ export class ComponentTypesView implements TreeDataProvider<ComponentType> {
'No',
);
if (yesNo === 'Yes') {
await Odo.Instance.removeRegistry(registry.name);
await OdoPreference.Instance.removeRegistry(registry.name);
ComponentTypesView.instance.removeRegistry(registry);
// ComponentTypesView.instance.refresh();
// if (!isEdit) {
Expand Down
18 changes: 10 additions & 8 deletions test/integration/odoWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as tmp from 'tmp';
import { promisify } from 'util';
import { Uri, workspace } from 'vscode';
import { Oc } from '../../src/oc/ocWrapper';
import { OdoPreference } from '../../src/odo/odoPreference';
import { Odo } from '../../src/odo/odoWrapper';
import { LoginUtil } from '../../src/util/loginUtil';

Expand Down Expand Up @@ -102,42 +103,43 @@ suite('./odo/odoWrapper.ts', function () {
});
});

// TODO: Move this out of odoWrapper
suite('registries', function () {
const TEST_REGISTRY = 'TestRegistry';

suiteSetup(async function () {
const registries = await Odo.Instance.getRegistries();
const registries = await OdoPreference.Instance.getRegistries();
if (registries.find((registry) => registry.name === TEST_REGISTRY)) {
await Odo.Instance.removeRegistry(TEST_REGISTRY);
await OdoPreference.Instance.removeRegistry(TEST_REGISTRY);
}
});

suiteTeardown(async function () {
try {
await Odo.Instance.removeRegistry(TEST_REGISTRY);
await OdoPreference.Instance.removeRegistry(TEST_REGISTRY);
} catch {
// do nothing, it's probably already deleted
}
});

test('getRegistries()', async function () {
const registries = await Odo.Instance.getRegistries();
const registries = await OdoPreference.Instance.getRegistries();
expect(registries).to.be.of.length(1);
const registryNames = registries.map((registry) => registry.name);
expect(registryNames).to.contain('DefaultDevfileRegistry');
});

test('addRegistry()', async function () {
await Odo.Instance.addRegistry(TEST_REGISTRY, 'https://example.org', undefined);
const registries = await Odo.Instance.getRegistries();
await OdoPreference.Instance.addRegistry(TEST_REGISTRY, 'https://example.org', undefined);
const registries = await OdoPreference.Instance.getRegistries();
expect(registries).to.be.of.length(2);
const registryNames = registries.map((registry) => registry.name);
expect(registryNames).to.contain(TEST_REGISTRY);
});

test('removeRegistry()', async function () {
await Odo.Instance.removeRegistry(TEST_REGISTRY);
const registries = await Odo.Instance.getRegistries();
await OdoPreference.Instance.removeRegistry(TEST_REGISTRY);
const registries = await OdoPreference.Instance.getRegistries();
expect(registries).to.be.of.length(1);
const registryNames = registries.map((registry) => registry.name);
expect(registryNames).to.not.contain(TEST_REGISTRY);
Expand Down

0 comments on commit fdd132f

Please sign in to comment.