Skip to content

Commit 0e4d056

Browse files
committed
Check for resx generator tool type
Checks to see if this is in project as resxgen and use public/internal if so, default public
1 parent c5a6d87 commit 0e4d056

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/utilities/designerGenerator.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22

3-
export function generateDesignerCode(resxPath: string, resources: { [key: string]: { value: string; comment?: string } }): string {
3+
export function generateDesignerCode(resxPath: string, resources: { [key: string]: { value: string; comment?: string } }, accessLevel: 'public' | 'internal' = 'public'): string {
44
const fileName = path.basename(resxPath, '.resx');
55
const namespaceName = fileName.includes('.') ? fileName.split('.')[0] : 'Resources';
66
const className = fileName.includes('.') ? fileName.split('.')[1] : fileName;
@@ -24,7 +24,7 @@ namespace ${namespaceName} {
2424
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("VS Code RESX Editor", "1.0.0.0")]
2525
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
2626
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
27-
public class ${className} {
27+
${accessLevel} class ${className} {
2828
2929
private static global::System.Resources.ResourceManager resourceMan;
3030
@@ -38,7 +38,7 @@ namespace ${namespaceName} {
3838
/// Returns the cached ResourceManager instance used by this class.
3939
/// </summary>
4040
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41-
public static global::System.Resources.ResourceManager ResourceManager {
41+
${accessLevel} static global::System.Resources.ResourceManager ResourceManager {
4242
get {
4343
if (object.ReferenceEquals(resourceMan, null)) {
4444
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("${namespaceName}.${className}", typeof(${className}).Assembly);
@@ -53,7 +53,7 @@ namespace ${namespaceName} {
5353
/// resource lookups using this strongly typed resource class.
5454
/// </summary>
5555
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
56-
public static global::System.Globalization.CultureInfo Culture {
56+
${accessLevel} static global::System.Globalization.CultureInfo Culture {
5757
get {
5858
return resourceCulture;
5959
}
@@ -71,7 +71,7 @@ namespace ${namespaceName} {
7171
code += ` /// ${resource.comment}\n`;
7272
code += ` /// </summary>\n`;
7373
}
74-
code += ` public static string ${key} {
74+
code += ` ${accessLevel} static string ${key} {
7575
get {
7676
return ResourceManager.GetString("${key}", resourceCulture);
7777
}

src/utilities/generateCode.ts

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,72 @@
11
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
24
import { generateDesignerCode } from './designerGenerator';
35
import { printChannelOutput } from '../extension';
46

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+
557
export async function generateAndUpdateDesignerFile(document: vscode.TextDocument, parsedJson: any): Promise<void> {
658
// Check if code generation is enabled
759
const config = vscode.workspace.getConfiguration('resx-editor');
860
const generateCode = config.get<boolean>('generateCode', false);
961

1062
if (generateCode) {
63+
// Determine the access level based on .csproj settings
64+
const accessLevel = await checkResxGeneratorType(document.uri.fsPath);
65+
1166
// Generate and update the Designer.cs file
1267
const designerPath = document.uri.fsPath.replace('.resx', '.Designer.cs');
1368
const designerUri = vscode.Uri.file(designerPath);
14-
const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson);
69+
const designerCode = generateDesignerCode(document.uri.fsPath, parsedJson, accessLevel);
1570

1671
try {
1772
await vscode.workspace.fs.stat(designerUri);

0 commit comments

Comments
 (0)