|
1 | 1 | import * as vscode from 'vscode';
|
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
2 | 4 | import { generateDesignerCode } from './designerGenerator';
|
3 | 5 | import { printChannelOutput } from '../extension';
|
4 | 6 |
|
| 7 | +async function checkResxGeneratorType(resxPath: string): Promise<'public' | 'internal'> { |
| 8 | + // Look for a .csproj file in the same directory or parent directories |
| 9 | + const dir = path.dirname(resxPath); |
| 10 | + const csprojFiles = await vscode.workspace.findFiles( |
| 11 | + new vscode.RelativePattern(dir, '**/*.csproj') |
| 12 | + ); |
| 13 | + |
| 14 | + if (csprojFiles.length === 0) { |
| 15 | + return 'public'; // Default to public if no .csproj is found |
| 16 | + } |
| 17 | + |
| 18 | + const resxFileName = path.basename(resxPath); |
| 19 | + |
| 20 | + for (const csprojFile of csprojFiles) { |
| 21 | + try { |
| 22 | + const content = await vscode.workspace.fs.readFile(csprojFile); |
| 23 | + const csprojContent = content.toString(); |
| 24 | + |
| 25 | + // Use regex to find EmbeddedResource items with our RESX file |
| 26 | + const regex = new RegExp(`<EmbeddedResource\\s+Include=["'](?:[^"']*[\\\\/])?${escapeRegExp(resxFileName)}["'][^>]*>([\\s\\S]*?)</EmbeddedResource>`, 'i'); |
| 27 | + const match = regex.exec(csprojContent); |
| 28 | + |
| 29 | + if (match) { |
| 30 | + const itemContent = match[1]; |
| 31 | + // Look for Generator metadata within the EmbeddedResource element |
| 32 | + const generatorRegex = /<Generator>([^<]+)<\/Generator>/i; |
| 33 | + const generatorMatch = generatorRegex.exec(itemContent); |
| 34 | + |
| 35 | + if (generatorMatch) { |
| 36 | + const generator = generatorMatch[1].trim(); |
| 37 | + if (generator === 'PublicResXFileCodeGenerator') { |
| 38 | + return 'public'; |
| 39 | + } else if (generator === 'ResXFileCodeGenerator') { |
| 40 | + return 'internal'; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } catch (error) { |
| 45 | + printChannelOutput(`Error reading .csproj file: ${error}`, true); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return 'public'; // Default to public if no generator type is specified |
| 50 | +} |
| 51 | + |
| 52 | +// Helper function to escape special regex characters |
| 53 | +function escapeRegExp(string: string): string { |
| 54 | + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 55 | +} |
| 56 | + |
5 | 57 | export async function generateAndUpdateDesignerFile(document: vscode.TextDocument, parsedJson: any): Promise<void> {
|
6 | 58 | // Check if code generation is enabled
|
7 | 59 | const config = vscode.workspace.getConfiguration('resx-editor');
|
8 | 60 | const generateCode = config.get<boolean>('generateCode', false);
|
9 | 61 |
|
10 | 62 | if (generateCode) {
|
| 63 | + // Determine the access level based on .csproj settings |
| 64 | + const accessLevel = await checkResxGeneratorType(document.uri.fsPath); |
| 65 | + |
11 | 66 | // Generate and update the Designer.cs file
|
12 | 67 | const designerPath = document.uri.fsPath.replace('.resx', '.Designer.cs');
|
13 | 68 | const designerUri = vscode.Uri.file(designerPath);
|
14 |
| - const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson); |
| 69 | + const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson, accessLevel); |
15 | 70 |
|
16 | 71 | try {
|
17 | 72 | await vscode.workspace.fs.stat(designerUri);
|
|
0 commit comments