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

Load plugin readme from file system #14699

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions packages/vsx-registry/src/browser/vsx-extensions-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { OVSXClientProvider } from '../common/ovsx-client-provider';
import { RequestContext, RequestService } from '@theia/core/shared/@theia/request';
import { OVSXApiFilterProvider } from '@theia/ovsx-client';
import { ApplicationServer } from '@theia/core/lib/common/application-protocol';
import { FileService } from '@theia/filesystem/lib/browser/file-service';

@injectable()
export class VSXExtensionsModel {
Expand Down Expand Up @@ -81,6 +82,9 @@ export class VSXExtensionsModel {
@inject(OVSXApiFilterProvider)
protected vsxApiFilter: OVSXApiFilterProvider;

@inject(FileService)
protected readonly fileService: FileService;

@inject(ApplicationServer)
protected readonly applicationServer: ApplicationServer;

Expand Down Expand Up @@ -135,13 +139,24 @@ export class VSXExtensionsModel {
resolve(id: string): Promise<VSXExtension> {
return this.doChange(async () => {
await this.initialized;
const extension = await this.refresh(id);
const extension = await this.refresh(id) ?? this.getExtension(id);
if (!extension) {
throw new Error(`Failed to resolve ${id} extension.`);
}
if (extension.readmeUrl) {
if (extension.readme === undefined) {
try {
const rawReadme = RequestContext.asText(await this.request.request({ url: extension.readmeUrl }));
let rawReadme: string = '';
const installedReadme = await this.findReadmeFile(extension);
// Attempt to read the local readme first
// It saves network resources and is faster
if (installedReadme) {
const readmeContent = await this.fileService.readFile(installedReadme);
rawReadme = readmeContent.value.toString();
} else if (extension.readmeUrl) {
rawReadme = RequestContext.asText(
await this.request.request({ url: extension.readmeUrl })
);
}
const readme = this.compileReadme(rawReadme);
extension.update({ readme });
} catch (e) {
Expand All @@ -154,6 +169,22 @@ export class VSXExtensionsModel {
});
}

protected async findReadmeFile(extension: VSXExtension): Promise<URI | undefined> {
if (!extension.plugin) {
return undefined;
}
// Since we don't know the exact capitalization of the readme file (might be README.md, readme.md, etc.)
// We attempt to find the readme file by searching through the plugin's directories
const packageUri = new URI(extension.plugin.metadata.model.packageUri);
const pluginUri = packageUri.withPath(packageUri.path.join('..'));
const pluginDirStat = await this.fileService.resolve(pluginUri);
const possibleNames = ['readme.md', 'readme.txt', 'readme'];
const readmeFileUri = pluginDirStat.children
?.find(child => possibleNames.includes(child.name.toLowerCase()))
?.resource;
return readmeFileUri;
}

protected async initInstalled(): Promise<void> {
await this.pluginSupport.willStart;
this.pluginSupport.onDidChangePlugins(() => this.updateInstalled());
Expand Down
Loading