forked from jlandersen/vscode-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes jlandersen#123 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
bf47c3d
commit e2eefef
Showing
7 changed files
with
302 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Kafka } from "kafkajs"; | ||
import { Cluster, ConnectionOptions } from "../client/client"; | ||
import { ClusterSettings } from "../settings/clusters"; | ||
|
||
export interface ClusterProviderProcessor { | ||
collectClusters(clusterSettings: ClusterSettings): Promise<Cluster[] | undefined>; | ||
createKafka?(connectionOptions: ConnectionOptions): Kafka; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import * as vscode from "vscode"; | ||
import { Kafka } from "kafkajs"; | ||
import { Cluster, ConnectionOptions, createDefaultKafka } from "../client/client"; | ||
import { ClusterSettings } from "../settings/clusters"; | ||
import { collectDefaultClusters } from "../wizards/clusters"; | ||
import { ClusterProviderProcessor } from "./api"; | ||
|
||
export class ClusterProvider { | ||
|
||
private processor: ClusterProviderProcessor | undefined; | ||
|
||
constructor(private definition: ClusterProviderDefinition, private extensionId: string) { | ||
|
||
} | ||
|
||
public get id(): string { | ||
return this.definition.id; | ||
} | ||
|
||
public get label(): string { | ||
return this.definition.label || this.definition.id; | ||
} | ||
|
||
async collectClusters(clusterSettings: ClusterSettings): Promise<Cluster[] | undefined> { | ||
const processor = await this.getProcessor(); | ||
return processor.collectClusters(clusterSettings); | ||
} | ||
|
||
async createKafka(connectionOptions: ConnectionOptions): Promise<Kafka | undefined> { | ||
const processor = await this.getProcessor(); | ||
if (processor.createKafka) { | ||
return processor.createKafka(connectionOptions); | ||
} | ||
} | ||
|
||
private async getProcessor(): Promise<ClusterProviderProcessor> { | ||
if (this.processor) { | ||
return this.processor; | ||
} | ||
const extension = vscode.extensions.getExtension(this.extensionId); | ||
if (!extension) { | ||
throw new Error('Extension is not started'); | ||
} | ||
|
||
// Wait for extension is activated in order to register the commands. | ||
await extension.activate(); | ||
|
||
const processor = await vscode.commands.executeCommand(this.definition.commandId); | ||
if (!processor) { | ||
throw new Error('Cannot find cluster provider processor'); | ||
} | ||
return this.processor = <ClusterProviderProcessor>processor; | ||
} | ||
|
||
} | ||
|
||
const defaultClusterProviderId = 'vscode-kafka.manual'; | ||
|
||
let providers: Map<string, ClusterProvider> = new Map(); | ||
|
||
export function getClusterProvider(clusterProviderId?: string): ClusterProvider | undefined { | ||
intializeIfNeeded(); | ||
return providers.get(clusterProviderId || defaultClusterProviderId); | ||
} | ||
|
||
export function getClusterProviders(): ClusterProvider[] { | ||
intializeIfNeeded(); | ||
return [...providers.values()]; | ||
} | ||
|
||
function intializeIfNeeded() { | ||
if (providers.size === 0) { | ||
providers = collectClusterProviderDefinitions(vscode.extensions.all); | ||
} | ||
} | ||
|
||
export interface ClusterProviderDefinition { | ||
id: string; | ||
label?: string; | ||
commandId: string; | ||
} | ||
|
||
let existingExtensions: Array<ClusterProviderDefinition>; | ||
|
||
export function collectClusterProviderDefinitions(extensions: readonly vscode.Extension<any>[]): Map<string, ClusterProvider> { | ||
const result: Map<string, ClusterProvider> = new Map(); | ||
if (extensions && extensions.length) { | ||
for (const extension of extensions) { | ||
const contributesSection = extension.packageJSON['contributes']; | ||
if (contributesSection) { | ||
const kafkaExtension = contributesSection['kafka']; | ||
if (kafkaExtension) { | ||
const clusterProviders = kafkaExtension['clusterProviders']; | ||
if (Array.isArray(clusterProviders) && clusterProviders.length) { | ||
for (const item of clusterProviders) { | ||
const definition = item as ClusterProviderDefinition; | ||
result.set(definition.id, new ClusterProvider(definition, extension.id)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function onExtensionChange(extensions: readonly vscode.Extension<any>[]) { | ||
if (!existingExtensions) { | ||
return; | ||
} | ||
//const oldExtensions = new Set(existingExtensions.slice()); | ||
//const newExtensions = collectClusterProviderDefinitions(extensions); | ||
/*let hasChanged = (oldExtensions.size !== newExtensions.length); | ||
if (!hasChanged) { | ||
for (const newExtension of newExtensions) { | ||
if (!oldExtensions.has(newExtension)) { | ||
hasChanged = true; | ||
break; | ||
} | ||
} | ||
}*/ | ||
} | ||
|
||
export function registerClusterProcessorCommand(context: vscode.ExtensionContext) { | ||
context.subscriptions.push(vscode.commands.registerCommand('vscode-kafka.cluster.providers', () => { | ||
return { | ||
collectClusters: (clusterSettings: ClusterSettings): Promise<Cluster[] | undefined> => collectDefaultClusters(clusterSettings), | ||
createKafka: (connectionOptions: ConnectionOptions): Kafka => createDefaultKafka(connectionOptions) | ||
} as ClusterProviderProcessor; | ||
})); | ||
} |
Oops, something went wrong.