Skip to content

Commit 939ef94

Browse files
committed
Refactor PowerShell Localization Extension
- Restructured the extension to improve modularity and maintainability. - Introduced an ExtensionManager class to handle initialization and resource management. - Added a Logger class for consistent logging throughout the extension. - Implemented a PowerShellExecutor class for executing PowerShell scripts and parsing localization data. - Created a LocalizationInlineValuesProvider for providing inline values in PowerShell files. - Developed a PowerShellModuleScanner to scan and analyze PowerShell modules for localization. - Enhanced configuration management with a ConfigurationManager class. - Added utility functions and constants in a new Utils module. - Updated tests to reflect changes in file paths and configurations. - Included new images for the extension's assets. - Improved error handling and logging across various components.
1 parent 07942c9 commit 939ef94

22 files changed

Lines changed: 1667 additions & 195 deletions

.vscode/launch.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
{
66
"version": "0.2.0",
7+
"inputs": [
8+
{
9+
"id": "modulePath",
10+
"type": "promptString",
11+
"description": "Enter the path to the module (e.g., Example.psm1)"
12+
}
13+
],
714
"configurations": [
815
{
916
"name": "PowerShell Test LocalizationParser",
1017
"type": "PowerShell",
1118
"request": "launch",
12-
"script": "${workspaceFolder}/src/LocalizationParser.ps1",
19+
"script": "${workspaceFolder}/resources/LocalizationParser.ps1",
1320
"args": [
1421
"${workspaceFolder}/tests/fixtures/Example/Example.psm1",
1522
"-Verbose",
@@ -19,14 +26,12 @@
1926
"createTemporaryIntegratedConsole": true
2027
},
2128
{
22-
"name": "PowerShell Test LocalizationParser (fr-FR)",
29+
"name": "PowerShell Test LocalizationParser (Prompt)",
2330
"type": "PowerShell",
2431
"request": "launch",
25-
"script": "${workspaceFolder}/src/LocalizationParser.ps1",
32+
"script": "${workspaceFolder}/resources/LocalizationParser.ps1",
2633
"args": [
27-
"${workspaceFolder}/tests/fixtures/Example/Example.psm1",
28-
"-UICulture",
29-
"fr-FR",
34+
"${input:modulePath}",
3035
"-Verbose",
3136
"-Debug"
3237
],

ARCHITECTURE.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# PowerShell Localization Extension - Architecture
2+
3+
This document describes the restructured architecture of the PowerShell Localization VS Code extension.
4+
5+
## Architecture Overview
6+
7+
The extension has been restructured following best practices with a modular, maintainable design. The main components are:
8+
9+
### Core Components
10+
11+
#### 1. ExtensionManager (`extensionManager.ts`)
12+
13+
- **Purpose**: Main orchestrator that coordinates all extension components
14+
- **Responsibilities**:
15+
- Extension initialization and cleanup
16+
- Component lifecycle management
17+
- Configuration change handling
18+
- File system watching setup
19+
20+
#### 2. Logger (`logger.ts`)
21+
22+
- **Purpose**: Centralized logging utility
23+
- **Features**:
24+
- Singleton pattern for consistent logging
25+
- Multiple log levels (info, error, warn, debug)
26+
- Timestamped messages
27+
- VS Code output channel integration
28+
29+
#### 3. ConfigurationManager (`configuration.ts`)
30+
31+
- **Purpose**: Manages extension configuration
32+
- **Features**:
33+
- Configuration reading and validation
34+
- Change event handling
35+
- Type-safe configuration access
36+
37+
#### 4. PowerShellModuleScanner (`moduleScanner.ts`)
38+
39+
- **Purpose**: Scans and analyzes PowerShell module files
40+
- **Responsibilities**:
41+
- Workspace scanning for .psm1 files
42+
- Import-LocalizedData detection
43+
- Module information caching
44+
45+
#### 5. PowerShellExecutor (`powershellExecutor.ts`)
46+
47+
- **Purpose**: Executes PowerShell scripts and processes
48+
- **Features**:
49+
- PowerShell process management
50+
- Error handling and logging
51+
- JSON parsing of script output
52+
- PowerShell availability checking
53+
54+
#### 6. LocalizationInlineValuesProvider (`inlineValuesProvider.ts`)
55+
56+
- **Purpose**: Provides inline values for localization variables
57+
- **Features**:
58+
- Variable and property detection
59+
- Localization data caching
60+
- Inline value generation
61+
- Performance optimization
62+
63+
### Supporting Files
64+
65+
#### 7. Types (`types.ts`)
66+
67+
- **Purpose**: TypeScript type definitions
68+
- **Contents**:
69+
- Interface definitions
70+
- Type aliases
71+
- Data structure contracts
72+
73+
#### 8. Utils (`utils.ts`)
74+
75+
- **Purpose**: Constants and utility functions
76+
- **Contents**:
77+
- Extension constants
78+
- File type checking utilities
79+
- Common helper functions
80+
- Regex patterns
81+
82+
#### 9. Extension Entry Point (`extension.ts`)
83+
84+
- **Purpose**: VS Code extension entry point
85+
- **Responsibilities**:
86+
- Extension activation/deactivation
87+
- Error handling
88+
- ExtensionManager initialization
89+
90+
## Design Principles
91+
92+
### 1. Single Responsibility Principle
93+
94+
Each class has a single, well-defined responsibility:
95+
96+
- Logger handles only logging
97+
- ConfigurationManager handles only configuration
98+
- PowerShellExecutor handles only PowerShell execution
99+
100+
### 2. Dependency Injection
101+
102+
Components receive their dependencies through constructors or method parameters, making testing easier and reducing coupling.
103+
104+
### 3. Error Handling
105+
106+
Comprehensive error handling at each layer:
107+
108+
- Try-catch blocks with proper logging
109+
- Graceful degradation when components fail
110+
- User-friendly error messages
111+
112+
### 4. Caching Strategy
113+
114+
- Localization data is cached to improve performance
115+
- Cache invalidation on file changes
116+
- Memory-efficient cache management
117+
118+
### 5. Configuration-Driven
119+
120+
- All behaviors can be controlled through VS Code settings
121+
- Runtime configuration changes are supported
122+
- Type-safe configuration access
123+
124+
## File Structure
125+
126+
```
127+
src/
128+
├── extension.ts # Entry point
129+
├── extensionManager.ts # Main coordinator
130+
├── types.ts # Type definitions
131+
├── utils.ts # Constants and utilities
132+
├── logger.ts # Logging utility
133+
├── configuration.ts # Configuration management
134+
├── moduleScanner.ts # PowerShell module scanning
135+
├── powershellExecutor.ts # PowerShell execution
136+
├── inlineValuesProvider.ts # Inline values provider
137+
└── LocalizationParser.ps1 # PowerShell script
138+
```
139+
140+
## Benefits of This Architecture
141+
142+
### 1. Maintainability
143+
144+
- Clear separation of concerns
145+
- Easy to understand and modify
146+
- Consistent code patterns
147+
148+
### 2. Testability
149+
150+
- Each component can be unit tested independently
151+
- Dependency injection enables mocking
152+
- Clear interfaces for testing
153+
154+
### 3. Extensibility
155+
156+
- Easy to add new features
157+
- Minimal impact when changing existing functionality
158+
- Well-defined extension points
159+
160+
### 4. Performance
161+
162+
- Efficient caching strategies
163+
- Lazy loading where appropriate
164+
- Resource cleanup and disposal
165+
166+
### 5. Robustness
167+
168+
- Comprehensive error handling
169+
- Graceful degradation
170+
- Resource management
171+
172+
## Usage Patterns
173+
174+
### Adding New Features
175+
176+
1. Define types in `types.ts`
177+
2. Add constants to `utils.ts`
178+
3. Create new service classes following existing patterns
179+
4. Register with `ExtensionManager`
180+
5. Add configuration options if needed
181+
182+
### Debugging
183+
184+
- Use the Logger singleton for consistent logging
185+
- Check the "PowerShell Localization" output channel
186+
- Enable debug logging for detailed information
187+
188+
### Configuration
189+
190+
- All settings are in the `powershelllocalization` section
191+
- Changes are automatically detected and applied
192+
- Type-safe access through `ConfigurationManager`

CHANGELOG.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# Change Log
22

3-
All notable changes to the "powershelllocalization" extension will be documented in this file.
3+
All notable changes to the "powershelllocalization" extension will be documented
4+
in this file.
45

5-
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
6+
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
7+
to structure this file.
68

7-
## [Unreleased]
9+
## [0.1.0] Initial Release
810

9-
- Initial release
11+
- Foundational script `LocalizationParser` looks for `psm1` that container
12+
`Import-LocalizedData` and using the AST attempt to execute a similar command.
13+
- Initial configuration options configured:
14+
- `enableInlineValues`: False by default. This renders localization variables
15+
during debugging.
16+
- `enableDecorations`: Shows inline decorators while you write code so you can
17+
see the localized text while you work.
18+
- `searchExclude`: A list of globs of folders to ignore. This is useful if
19+
your module builds output and you don't want to parse it.
20+
- The extension turns on when it detects PowerShell files.
21+
- The localized data is cached for efficiency and is reloaded when the psm1 or
22+
psd1 files are modified.

PACKAGING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Extension Packaging and Installation
22

3-
This document explains how to package and install the PowerShell Localization extension for development and testing purposes.
3+
This document explains how to package and install the PowerShell Localization
4+
extension for development and testing purposes.
45

56
## Quick Start
67

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
# PowerShell Localization
22

3-
A Visual Studio Code extension that displays PowerShell localization variable values inline in your editor, making it easier to develop and debug internationalized PowerShell modules.
3+
A Visual Studio Code extension that displays PowerShell localization variable
4+
values as decorations in your editor, making it easier to develop and debug
5+
internationalized PowerShell modules.
6+
7+
![Example showing the inline decorator](static/image.png)
48

59
## Features
610

7-
- **Inline Value Display**: View localization variable values directly in your PowerShell files
8-
- **Real-time Updates**: Values update automatically when localization files change
9-
- **Multi-language Support**: Works with all localization files (en-US, fr-FR, etc.)
10-
- **PowerShell Integration**: Seamlessly integrates with PowerShell module development workflow
11+
- **Live Decoration Display**: View localization variable values as decorations
12+
directly in your PowerShell files during code writing (Error Lens style)
13+
- **Real-time Updates**: Values update automatically when localization files
14+
change
15+
- **Multi-language Support**: Works with all localization files (en-US, fr-FR,
16+
etc.)
17+
- **PowerShell Integration**: Seamlessly integrates with PowerShell module
18+
development workflow
19+
- **Debug Support**: Optional inline values during debugging sessions
1120

1221
## How It Works
1322

14-
The extension automatically scans for PowerShell modules (`.psm1` files) and their associated localization data files (`.psd1` files in language-specific folders like `en-US/`, `fr-FR/`, etc.). When you reference localization variables in your PowerShell code using `$LocalizedData.VariableName`, the extension will display the actual localized value inline.
23+
The extension automatically scans for PowerShell modules (`.psm1` files) and
24+
their associated localization data files (`.psd1` files in language-specific
25+
folders like `en-US/`, `fr-FR/`, etc.). When you reference localization
26+
variables in your PowerShell code using `$LocalizedData.VariableName`, the
27+
extension will display the actual localized value as a decoration next to your
28+
code.
1529

1630
## Requirements
1731

@@ -22,7 +36,12 @@ The extension automatically scans for PowerShell modules (`.psm1` files) and the
2236

2337
This extension contributes the following settings:
2438

25-
- `powershelllocalization.enableInlineValues`: Enable/disable inline display of localization variable values
39+
- `powershellLocalization.enableDecorations`: Enable/disable decoration display
40+
of localization variable values during code writing (default: true)
41+
- `powershellLocalization.enableInlineValues`: Enable/disable inline display of
42+
localization variable values during debugging (default: false)
43+
- `powershellLocalization.searchExclude`: Configure glob patterns for excluding
44+
directories and files from PowerShell module scanning
2645

2746
## Installation
2847

@@ -38,7 +57,7 @@ To build and install this extension:
3857
# Using PowerShell script
3958
.\package-and-install.ps1
4059

41-
# Using npm scripts
60+
# Using npm scripts
4261
yarn package-install
4362

4463
# Package only
@@ -48,7 +67,8 @@ yarn package-only
4867
## Usage
4968

5069
1. Open a PowerShell module (`.psm1` file) that uses localization
51-
2. Ensure you have localization data files in language folders (e.g., `en-US/ModuleName.psd1`)
70+
2. Ensure you have localization data files in language folders (e.g.,
71+
`en-US/ModuleName.psd1`)
5272
3. Reference localization variables in your code: `$LocalizedData.MessageText`
5373
4. The extension will display the actual localized values inline
5474

@@ -59,5 +79,3 @@ Contributions are welcome! Please feel free to submit issues and pull requests.
5979
## License
6080

6181
MIT
62-
63-
**Enjoy!**

0 commit comments

Comments
 (0)