-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionManager.ts
More file actions
271 lines (229 loc) · 8.75 KB
/
extensionManager.ts
File metadata and controls
271 lines (229 loc) · 8.75 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import * as vscode from 'vscode';
import { Logger } from './logger';
import { ConfigurationManager } from './configuration';
import { PowerShellModuleScanner } from './moduleScanner';
import { PowerShellExecutor } from './powershellExecutor';
import { LocalizationDecorationProvider } from './decorationProvider';
import { POWERSHELL_LANGUAGE_ID, POWERSHELL_MODULE_EXTENSION, POWERSHELL_DATA_EXTENSION } from './utils';
/**
* Main extension manager that coordinates all components
*/
export class ExtensionManager {
private logger: Logger;
private moduleScanner: PowerShellModuleScanner;
private powershellExecutor: PowerShellExecutor;
private decorationProvider: LocalizationDecorationProvider;
private disposables: vscode.Disposable[] = [];
constructor(private context: vscode.ExtensionContext) {
this.logger = Logger.getInstance();
this.moduleScanner = new PowerShellModuleScanner();
this.powershellExecutor = new PowerShellExecutor();
this.decorationProvider = new LocalizationDecorationProvider();
}
/**
* Initializes the extension
*/
public async initialize(): Promise<void> {
this.logger.info('PowerShell Localization extension starting...');
try {
// Check if PowerShell is available
const isPowerShellAvailable = await this.powershellExecutor.isPowerShellAvailable();
if (!isPowerShellAvailable) {
this.logger.error('PowerShell is not available on this system');
vscode.window.showErrorMessage(
'PowerShell Localization: PowerShell (pwsh) is not available. Please install PowerShell Core.'
);
return;
}
// Scan for modules with localization
const hasModulesWithLocalization = await this.moduleScanner.hasAnyModuleWithLocalization();
if (!hasModulesWithLocalization) {
this.logger.info('No PowerShell modules with Import-LocalizedData found');
return;
}
// Register the decoration provider
await this.registerDecorationProvider();
// Set up configuration change listener
this.setupConfigurationListener();
// Set up file system watchers
this.setupFileSystemWatchers();
// Register command palette commands
this.registerCommands();
this.logger.info('PowerShell Localization extension activated successfully');
} catch (error) {
this.logger.error('Failed to initialize extension', error as Error);
vscode.window.showErrorMessage(
`PowerShell Localization: Failed to initialize extension: ${(error as Error).message}`
);
}
}
/**
* Registers the decoration provider for PowerShell files
*/
private async registerDecorationProvider(): Promise<void> {
this.logger.info('Registering PowerShell decoration provider...');
if (!ConfigurationManager.isDecorationEnabled()) {
this.logger.info('Decorations are disabled in configuration');
return;
}
this.decorationProvider.activate();
this.disposables.push(this.decorationProvider);
this.logger.info('Registered PowerShell decoration provider');
}
/**
* Sets up configuration change listener
*/
private setupConfigurationListener(): void {
const disposable = ConfigurationManager.onConfigurationChanged(() => {
this.logger.info('Configuration changed, reinitializing...');
this.handleConfigurationChange();
});
this.disposables.push(disposable);
this.context.subscriptions.push(disposable);
}
/**
* Sets up file system watchers for PowerShell files
*/
private setupFileSystemWatchers(): void {
// Watch for changes to .psm1 files
const psm1Watcher = vscode.workspace.createFileSystemWatcher(`**/*(${POWERSHELL_MODULE_EXTENSION}|${POWERSHELL_LANGUAGE_ID})`);
psm1Watcher.onDidChange((uri) => {
this.logger.debug(`PowerShell module file changed: ${uri.fsPath}`);
this.decorationProvider.clearCacheForFile(uri.fsPath);
});
psm1Watcher.onDidCreate((uri) => {
this.logger.debug(`PowerShell module file created: ${uri.fsPath}`);
this.handleModuleFileChange();
});
psm1Watcher.onDidDelete((uri) => {
this.logger.debug(`PowerShell module file deleted: ${uri.fsPath}`);
this.decorationProvider.clearCacheForFile(uri.fsPath);
});
// Watch for changes to localization files (.psd1)
const psd1Watcher = vscode.workspace.createFileSystemWatcher(`**/*${POWERSHELL_DATA_EXTENSION}`);
psd1Watcher.onDidChange((uri) => {
this.logger.debug(`Localization file changed: ${uri.fsPath}`);
this.decorationProvider.clearCache();
});
this.disposables.push(psm1Watcher, psd1Watcher);
this.context.subscriptions.push(psm1Watcher, psd1Watcher);
}
/**
* Registers command palette commands
*/
private registerCommands(): void {
// Register switch UI culture command
const switchCommand = vscode.commands.registerCommand(
'powershellLocalization.switchUICulture',
async () => {
await this.handleSwitchUICulture();
}
);
// Register set to en-US command
const setEnUsCommand = vscode.commands.registerCommand(
'powershellLocalization.setUICultureToEnUs',
async () => {
await this.handleSetUICulture('en-US');
}
);
// Register set to fr-FR command
const setFrFrCommand = vscode.commands.registerCommand(
'powershellLocalization.setUICultureToFrFr',
async () => {
await this.handleSetUICulture('fr-FR');
}
);
this.disposables.push(switchCommand, setEnUsCommand, setFrFrCommand);
this.context.subscriptions.push(switchCommand, setEnUsCommand, setFrFrCommand);
this.logger.info('Command palette commands registered');
}
/**
* Handles configuration changes
*/
private async handleConfigurationChange(): Promise<void> {
try {
// Clear cache when configuration changes
this.decorationProvider.clearCache();
// Re-register decoration provider if needed
if (ConfigurationManager.isDecorationEnabled()) {
await this.registerDecorationProvider();
}
} catch (error) {
this.logger.error('Failed to handle configuration change', error as Error);
}
}
/**
* Handles module file changes
*/
private async handleModuleFileChange(): Promise<void> {
try {
// Re-scan for modules when files change
const hasModulesWithLocalization = await this.moduleScanner.hasAnyModuleWithLocalization();
if (hasModulesWithLocalization) {
await this.registerDecorationProvider();
}
} catch (error) {
this.logger.error('Failed to handle module file change', error as Error);
}
}
/**
* Handles switching UI culture via input box
*/
private async handleSwitchUICulture(): Promise<void> {
try {
const currentCulture = ConfigurationManager.getUICulture();
const inputCulture = await vscode.window.showInputBox({
prompt: 'Enter UI Culture (e.g., en-US, fr-FR, de-DE)',
value: currentCulture,
validateInput: (value) => {
const culturePattern = /^[a-z]{2}(-[A-Z]{2})?$/;
if (!culturePattern.test(value)) {
return 'Invalid culture format. Use format like "en-US", "fr-FR", "de-DE"';
}
return null;
}
});
if (inputCulture && inputCulture !== currentCulture) {
await this.handleSetUICulture(inputCulture);
}
} catch (error) {
this.logger.error('Failed to switch UI culture', error as Error);
vscode.window.showErrorMessage(`Failed to switch UI culture: ${(error as Error).message}`);
}
}
/**
* Handles setting UI culture to a specific value
*/
private async handleSetUICulture(culture: string): Promise<void> {
try {
const currentCulture = ConfigurationManager.getUICulture();
if (culture === currentCulture) {
vscode.window.showInformationMessage(`UI Culture is already set to ${culture}`);
return;
}
await ConfigurationManager.setUICulture(culture);
// Clear the cache since culture has changed
this.decorationProvider.clearCache();
vscode.window.showInformationMessage(`UI Culture changed to ${culture}`);
this.logger.info(`UI Culture changed from ${currentCulture} to ${culture}`);
} catch (error) {
this.logger.error(`Failed to set UI culture to ${culture}`, error as Error);
vscode.window.showErrorMessage(`Failed to set UI culture: ${(error as Error).message}`);
}
}
/**
* Disposes of all resources
*/
public dispose(): void {
this.logger.info('Disposing extension resources...');
this.disposables.forEach(disposable => {
try {
disposable.dispose();
} catch (error) {
this.logger.error('Error disposing resource', error as Error);
}
});
this.disposables = [];
this.logger.dispose();
}
}