-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathmanager.ts
More file actions
135 lines (107 loc) Β· 4.11 KB
/
manager.ts
File metadata and controls
135 lines (107 loc) Β· 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import IBMi from "../IBMi";
import { ComponentIdentification, ComponentInstallState, ComponentState, IBMiComponent } from "./component";
interface ExtensionContextI {
extension: {
id: string
}
}
export class ComponentRegistry {
private readonly components: Map<string, IBMiComponent[]> = new Map;
public registerComponent(context: ExtensionContextI|string, component: IBMiComponent) {
const key = typeof context === `object` ? context.extension.id : context;
if (typeof key !== `string`) {
throw new Error(`Invalid extension context.`);
}
const extensionComponents = this.components.get(key);
if (extensionComponents) {
extensionComponents.push(component);
}
else {
this.components.set(key, [component]);
}
}
public getComponents() {
return this.components;
}
}
export const extensionComponentRegistry = new ComponentRegistry();
export class ComponentManager {
private readonly registered: Map<string, IBMiComponentRuntime> = new Map;
constructor(private readonly connection: IBMi) {}
public getComponentIds(): ComponentIdentification[] {
return Array.from(extensionComponentRegistry.getComponents().values()).flatMap(a => a.flat()).map(c => c.getIdentification());
}
public getInstallState(): ComponentInstallState[] {
return Array.from(this.registered.keys()).map(k => {
const comp = this.registered.get(k)!;
return {
id: comp.component.getIdentification(),
state: comp.getState()
}
});
}
public async startup(lastInstalled: ComponentInstallState[] = []) {
const components = Array.from(extensionComponentRegistry.getComponents().values()).flatMap(a => a.flat());
for (const component of components) {
await component.reset?.();
const newComponent = new IBMiComponentRuntime(this.connection, component);
const installed = lastInstalled.find(i => i.id.name === component.getIdentification().name);
const sameVersion = installed && (installed.id.version === component.getIdentification().version);
if (!installed || !sameVersion || installed.state === `NotChecked`) {
await newComponent.check();
} else {
await newComponent.overrideState(installed.state);
}
this.registered.set(component.getIdentification().name, newComponent);
}
}
get<T extends IBMiComponent>(id: string, ignoreState?: boolean) {
const componentEngine = this.registered.get(id);
if (componentEngine && (ignoreState || componentEngine.getState() === `Installed`)) {
return componentEngine.component as T;
}
}
}
class IBMiComponentRuntime {
public static readonly InstallDirectory = `$HOME/.vscode/`;
private state: ComponentState = `NotChecked`;
private cachedInstallDirectory: string | undefined;
constructor(protected readonly connection: IBMi, readonly component: IBMiComponent) {
}
async getInstallDirectory() {
if (!this.cachedInstallDirectory) {
const result = await this.connection.sendCommand({
command: `echo "${IBMiComponentRuntime.InstallDirectory}"`,
});
this.cachedInstallDirectory = result.stdout.trim() || `/home/${this.connection.currentUser.toLowerCase()}/.vscode/`;
}
return this.cachedInstallDirectory;
}
getState() {
return this.state;
}
async overrideState(newState: ComponentState) {
const installDir = await this.getInstallDirectory();
await this.component.setInstallDirectory?.(installDir);
this.state = newState;
}
async check() {
try {
const installDirectory = await this.getInstallDirectory();
this.state = await this.component.getRemoteState(this.connection, installDirectory);
if (this.state !== `Installed`) {
this.state = await this.component.update(this.connection, installDirectory);
}
}
catch (error) {
console.log(`Error occurred while checking component ${this.toString()}`);
console.log(error);
this.state = `Error`;
}
return this;
}
toString() {
const identification = this.component.getIdentification();
return `${identification.name} (version ${identification.version})`
}
}