-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresources.ts
More file actions
601 lines (553 loc) · 27.1 KB
/
Copy pathresources.ts
File metadata and controls
601 lines (553 loc) · 27.1 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// UI Maker — project image resources (the Visual Studio "Select Resource"
// dialog's Import flow).
//
// When the user assigns an Image/BackgroundImage/Icon in the property grid,
// this module reproduces what Visual Studio does for a *project resource*:
//
// 1. copy the picked file into <project>/Resources/
// 2. register it as a ResXFileRef in Properties/Resources.resx
// 3. regenerate Properties/Resources.Designer.cs (ResXFileCodeGenerator
// compatible, so the project still round-trips with Visual Studio)
// 4. classic (non-SDK) csproj: add the file entries VS would add
//
// The Designer.cs then references the image as
// global::<RootNamespace>.Properties.Resources.<name>
//
// Reading goes the other way: the designer webview asks for the images its
// document references, and resolveImages() maps resource names back to files
// (project resources) or data URIs (legacy images embedded base64 in the
// form's own .resx).
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { EXCLUDE_GLOB, getWorkingFolder, pickWorkingFolder } from './workingFolder';
import { isCSharpIdentifier, sanitizeCSharpNamespace } from './csharpText';
import { decodeXmlEntities } from './xmlText';
import { createFilesAtomically, replaceFileAtomically } from './atomicFile';
export interface ImportedImage {
/** Resource name — a valid C# identifier. */
key: string;
/** C# expression for the Designer.cs assignment. */
code: string;
/** Absolute path of the imported image file. */
fsPath: string;
}
/** A reference the webview wants resolved: project resx or local form resx. */
export interface ImageKey {
scope: 'p' | 'l';
key: string;
}
const IMAGE_FILTERS = { 'Images': ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'ico'] };
const ICON_FILTERS = { 'Icons': ['ico'] };
/** Walk up from a file to the directory containing a .csproj/.vbproj (or null).
* Resource IMPORTS additionally require a .csproj (see importResourceFile);
* resolution and webview roots work for both languages. */
export function findProjectDir(startFile: string): string | null {
let dir = path.dirname(startFile);
for (let i = 0; i < 24; i++) {
try {
if (fs.readdirSync(dir).some(f => /\.(cs|vb)proj$/i.test(f))) { return dir; }
} catch { return null; }
const parent = path.dirname(dir);
if (parent === dir) { return null; }
dir = parent;
}
return null;
}
function csprojPath(projDir: string): string | null {
const name = fs.readdirSync(projDir).find(f => /\.csproj$/i.test(f));
return name ? path.join(projDir, name) : null;
}
/** Root namespace: csproj RootNamespace, else the sanitized project name. */
function rootNamespace(csproj: string): string {
try {
const xml = fs.readFileSync(csproj, 'utf8');
const root = /<RootNamespace>\s*([^<]+?)\s*<\/RootNamespace>/.exec(xml)?.[1];
if (root) { return sanitizeCSharpNamespace(decodeXmlEntities(root)); }
} catch { /* fall through to the file name */ }
const base = path.basename(csproj).replace(/\.csproj$/i, '');
return sanitizeCSharpNamespace(base);
}
/** File base name -> valid C# identifier ("refresh icon.png" -> "refresh_icon"). */
function resourceKey(file: string): string {
const base = path.basename(file, path.extname(file)).replace(/[^A-Za-z0-9_]/g, '_');
const candidate = /^[0-9]/.test(base) ? `_${base}` : (base || '_image');
return isCSharpIdentifier(candidate) ? candidate : `_${candidate}`;
}
/**
* Ask the user for an image, import it as a project resource, and return the
* C# expression that references it. Returns undefined when cancelled.
*/
export async function pickAndImportImage(docPath: string, iconOnly: boolean): Promise<ImportedImage | undefined> {
const picked = await vscode.window.showOpenDialog({
canSelectMany: false,
openLabel: 'Use Image',
filters: iconOnly ? ICON_FILTERS : IMAGE_FILTERS
});
if (!picked?.length) { return undefined; }
return importResourceFile(docPath, picked[0].fsPath);
}
/**
* Import one file as a project resource (copy to Resources/, resx entry,
* regenerated accessor class, csproj bookkeeping). Shared by the designer's
* image picker and the side panel's Add Image / Add Resource buttons.
*/
export function importResourceFile(startPath: string, src: string): ImportedImage | undefined {
const projDir = findProjectDir(startPath);
const csproj = projDir ? csprojPath(projDir) : null;
if (!projDir || !csproj) {
vscode.window.showWarningMessage('UI Maker: no .csproj found — resources need a project to import into.');
return undefined;
}
try {
// 1) Copy into <project>/Resources (VS behavior), deduping by content.
const resDir = path.join(projDir, 'Resources');
if (!fs.existsSync(resDir)) { fs.mkdirSync(resDir, { recursive: true }); }
let destName = path.basename(src);
let dest = path.join(resDir, destName);
if (path.resolve(path.dirname(src)) === path.resolve(resDir)) {
dest = src; // picked from Resources/ itself — reuse in place
} else if (fs.existsSync(dest) && !fs.readFileSync(dest).equals(fs.readFileSync(src))) {
const ext = path.extname(destName);
const stem = path.basename(destName, ext);
for (let i = 1; ; i++) {
destName = `${stem}${i}${ext}`;
dest = path.join(resDir, destName);
if (!fs.existsSync(dest) || fs.readFileSync(dest).equals(fs.readFileSync(src))) { break; }
}
if (!fs.existsSync(dest)) { fs.copyFileSync(src, dest); }
} else if (!fs.existsSync(dest)) {
fs.copyFileSync(src, dest);
}
// 2) Register in Properties/Resources.resx with the right CLR type.
const key = addResxFileRef(projDir, dest, resourceTypeFor(dest));
// 3) Regenerate the typed accessor class.
const ns = rootNamespace(csproj);
writeResourcesDesigner(projDir, ns);
// 4) Classic csproj bookkeeping.
registerResourceFiles(csproj, path.relative(projDir, dest));
return { key, code: `global::${ns}.Properties.Resources.${key}`, fsPath: dest };
} catch (err) {
vscode.window.showErrorMessage(`UI Maker: could not import the resource — ${err}`);
return undefined;
}
}
// ------------------------------------------------------------------- resx IO
const RESX_HEADER = `<?xml version="1.0" encoding="utf-8"?>
<root>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</root>
`;
const BITMAP_TYPE = 'System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a4a';
const ICON_TYPE = 'System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a4a';
const STRING_TYPE = 'System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8';
const BYTES_TYPE = 'System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089';
/** ResXFileRef type spec for a file, by extension (VS picks the same way). */
function resourceTypeFor(file: string): string {
const ext = path.extname(file).toLowerCase();
if (ext === '.ico') { return ICON_TYPE; }
if (['.png', '.jpg', '.jpeg', '.gif', '.bmp'].includes(ext)) { return BITMAP_TYPE; }
if (['.txt', '.json', '.xml', '.csv', '.md'].includes(ext)) { return STRING_TYPE; }
return BYTES_TYPE;
}
function resxPath(projDir: string): string {
return path.join(projDir, 'Properties', 'Resources.resx');
}
/** One `<data>` entry of a resx, with everything needed to round-trip it. */
interface ResxEntry {
name: string;
/** ResXFileRef value ("path;Type, Assembly[;encoding]") or null. */
fileRef: string | null;
/** The entry's declared type attribute, or null. */
typeAttr: string | null;
/** The entry's mimetype attribute (binary payloads), or null. */
mimeType: string | null;
/** Plain inline string entry (no type, no mimetype). */
isString: boolean;
}
/** Every <data name=...> entry in a resx, with its fileref value when present. */
function parseResxEntries(xml: string): ResxEntry[] {
const out: ResxEntry[] = [];
const re = /<data\s+name="([^"]+)"([^>]*)>([\s\S]*?)<\/data>/g;
for (let m = re.exec(xml); m; m = re.exec(xml)) {
const attr = (name: string) => {
const value = new RegExp(`\\b${name}="([^"]*)"`).exec(m![2])?.[1];
return value === undefined ? null : decodeXmlEntities(value);
};
const value = decodeXmlEntities(/<value>([\s\S]*?)<\/value>/.exec(m[3])?.[1] ?? '');
const typeAttr = attr('type');
const mimeType = attr('mimetype');
const isFileRef = !!typeAttr && /ResXFileRef/.test(typeAttr);
out.push({
name: decodeXmlEntities(m[1]),
fileRef: isFileRef ? value : null,
typeAttr,
mimeType,
isString: !isFileRef && !typeAttr && !mimeType
});
}
return out;
}
/**
* The C# type an entry's generated accessor should return. Every declared
* type is preserved — an integer, color, or custom resource added by Visual
* Studio must never come back as a Bitmap accessor after we regenerate.
*/
function accessorTypeFor(e: ResxEntry): string {
// File references carry "path;Full.Type, Assembly[;encoding]".
const clr = e.fileRef
? (e.fileRef.split(';')[1] ?? '').split(',')[0].trim()
: e.typeAttr && !/ResXFileRef/.test(e.typeAttr)
? e.typeAttr.split(',')[0].trim()
: '';
if (e.isString || clr === 'System.String') { return 'string'; }
if (clr === 'System.Byte[]') { return 'byte[]'; }
if (/^[A-Za-z_][A-Za-z0-9_.]*(\[\])?$/.test(clr)) { return `global::${clr}`; }
// Unknown payload (e.g. BinaryFormatter blob without a usable type):
// an object accessor is always correct and always compiles.
return 'object';
}
/**
* Add (or reuse) a fileref entry for the file; returns the resource name.
* Creates Properties/Resources.resx when the project has none.
*/
function addResxFileRef(projDir: string, fileAbs: string, type: string): string {
const file = resxPath(projDir);
if (!fs.existsSync(path.dirname(file))) { fs.mkdirSync(path.dirname(file), { recursive: true }); }
let xml = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : RESX_HEADER;
// The fileref path is relative to the resx (Properties/) directory.
const rel = path.relative(path.dirname(file), fileAbs).split('/').join('\\');
const value = `${rel};${type}`;
const entries = parseResxEntries(xml);
// Same file already registered? Reuse its name.
const existing = entries.find(e => e.fileRef?.toLowerCase().startsWith(rel.toLowerCase() + ';'));
if (existing) { return existing.name; }
// Unique name derived from the file name.
let key = resourceKey(fileAbs);
const names = new Set(entries.map(e => e.name));
for (let i = 1; names.has(key); i++) { key = `${resourceKey(fileAbs)}${i}`; }
// VS declares the alias once; older/minimal resx files may lack it.
if (!/assembly alias="System.Windows.Forms"/.test(xml)) {
xml = xml.replace(/<\/root>/,
` <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />\n</root>`);
}
const entry =
` <data name="${key}" type="System.Resources.ResXFileRef, System.Windows.Forms">\n` +
` <value>${value.replace(/&/g, '&').replace(/</g, '<')}</value>\n` +
` </data>\n`;
xml = xml.replace(/<\/root>/, `${entry}</root>`);
replaceFileAtomically(file, xml);
return key;
}
/** Regenerate Properties/Resources.Designer.cs from the resx entries. */
function writeResourcesDesigner(projDir: string, rootNs: string): void {
const file = resxPath(projDir);
const entries = fs.existsSync(file) ? parseResxEntries(fs.readFileSync(file, 'utf8')) : [];
const ns = `${rootNs}.Properties`;
const props = entries.map(e => {
// Names must stay valid identifiers — anything else would corrupt
// the generated class (spaces etc. can appear in hand-edited files).
if (!isCSharpIdentifier(e.name)) { return ''; }
const type = accessorTypeFor(e);
// Text resources (inline strings and string filerefs) use GetString.
if (type === 'string') {
return ` internal static string ${e.name} {\r\n` +
` get {\r\n` +
` return ResourceManager.GetString("${e.name}", resourceCulture);\r\n` +
` }\r\n` +
` }`;
}
return ` internal static ${type} ${e.name} {\r\n` +
` get {\r\n` +
` object obj = ResourceManager.GetObject("${e.name}", resourceCulture);\r\n` +
` return ((${type})(obj));\r\n` +
` }\r\n` +
` }`;
}).filter(Boolean).join('\r\n\r\n');
const code =
`//------------------------------------------------------------------------------\r\n` +
`// <auto-generated>\r\n` +
`// This code was generated by a tool.\r\n` +
`// Changes to this file may cause incorrect behavior and will be lost if\r\n` +
`// the code is regenerated.\r\n` +
`// </auto-generated>\r\n` +
`//------------------------------------------------------------------------------\r\n` +
`\r\n` +
`namespace ${ns} {\r\n` +
` using System;\r\n` +
`\r\n` +
`\r\n` +
` /// <summary>\r\n` +
` /// A strongly-typed resource class, for looking up localized strings, etc.\r\n` +
` /// </summary>\r\n` +
` [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]\r\n` +
` [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\r\n` +
` [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]\r\n` +
` internal class Resources {\r\n` +
`\r\n` +
` private static global::System.Resources.ResourceManager resourceMan;\r\n` +
`\r\n` +
` private static global::System.Globalization.CultureInfo resourceCulture;\r\n` +
`\r\n` +
` [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]\r\n` +
` internal Resources() {\r\n` +
` }\r\n` +
`\r\n` +
` /// <summary>\r\n` +
` /// Returns the cached ResourceManager instance used by this class.\r\n` +
` /// </summary>\r\n` +
` [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]\r\n` +
` internal static global::System.Resources.ResourceManager ResourceManager {\r\n` +
` get {\r\n` +
` if (object.ReferenceEquals(resourceMan, null)) {\r\n` +
` global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("${ns}.Resources", typeof(Resources).Assembly);\r\n` +
` resourceMan = temp;\r\n` +
` }\r\n` +
` return resourceMan;\r\n` +
` }\r\n` +
` }\r\n` +
`\r\n` +
` /// <summary>\r\n` +
` /// Overrides the current thread's CurrentUICulture property for all\r\n` +
` /// resource lookups using this strongly typed resource class.\r\n` +
` /// </summary>\r\n` +
` [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]\r\n` +
` internal static global::System.Globalization.CultureInfo Culture {\r\n` +
` get {\r\n` +
` return resourceCulture;\r\n` +
` }\r\n` +
` set {\r\n` +
` resourceCulture = value;\r\n` +
` }\r\n` +
` }\r\n` +
(props ? `\r\n${props}\r\n` : '') +
` }\r\n` +
`}\r\n`;
replaceFileAtomically(path.join(projDir, 'Properties', 'Resources.Designer.cs'), code);
}
/**
* Classic (non-SDK) projects list every file explicitly — register the resx
* pair once and each imported image. SDK projects glob everything.
*/
function registerResourceFiles(csproj: string, imageRel: string): void {
try {
let xml = fs.readFileSync(csproj, 'utf8');
if (/<Project\s[^>]*\bSdk\s*=/.test(xml)) { return; }
if (!/<\/Project>/.test(xml)) { return; }
const eol = xml.includes('\r\n') ? '\r\n' : '\n';
let block = '';
if (!xml.includes('Properties\\Resources.resx')) {
block +=
` <EmbeddedResource Include="Properties\\Resources.resx">${eol}` +
` <Generator>ResXFileCodeGenerator</Generator>${eol}` +
` <LastGenOutput>Resources.Designer.cs</LastGenOutput>${eol}` +
` </EmbeddedResource>${eol}` +
` <Compile Include="Properties\\Resources.Designer.cs">${eol}` +
` <AutoGen>True</AutoGen>${eol}` +
` <DesignTime>True</DesignTime>${eol}` +
` <DependentUpon>Resources.resx</DependentUpon>${eol}` +
` </Compile>${eol}`;
}
const imgEntry = imageRel.split('/').join('\\')
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
if (!xml.includes(`"${imgEntry}"`)) {
block += ` <None Include="${imgEntry}" />${eol}`;
}
if (!block) { return; }
xml = xml.replace(/<\/Project>/, ` <ItemGroup>${eol}${block} </ItemGroup>${eol}</Project>`);
replaceFileAtomically(csproj, xml);
} catch {
vscode.window.showWarningMessage('UI Maker: could not register the resource files in the project — add them manually.');
}
}
// -------------------------------------------------------- sidebar commands
/**
* The project directory sidebar commands operate on: the WORKING folder when
* one is set (multi-project workspaces are never guessed at), else the
* workspace's single project, else a picker. `languages` limits which project
* types qualify — resource IMPORTS generate C# accessors, so they stay
* C#-only; file creation works for both languages.
*/
async function workspaceProjectDir(languages: 'cs' | 'any' = 'cs'): Promise<string | null> {
const working = getWorkingFolder();
const projRe = languages === 'cs' ? /\.csproj$/i : /\.(cs|vb)proj$/i;
const hasProject = (dir: string): boolean => {
try { return fs.readdirSync(dir).some(f => projRe.test(f)); }
catch { return false; }
};
// A selected working project is authoritative. In particular, never fall
// through from a VB project to some unrelated C# project in the workspace.
if (working) { return hasProject(working) ? working : null; }
const found = await vscode.workspace.findFiles('**/*.{csproj,vbproj}', EXCLUDE_GLOB, 2);
if (found.length === 1) {
return projRe.test(found[0].fsPath) ? path.dirname(found[0].fsPath) : null;
}
if (found.length === 0) { return null; }
// Several projects and no working folder chosen — ask, don't guess.
const picked = await pickWorkingFolder();
return picked && hasProject(picked) ? picked : null;
}
/** Any project file in the folder (used to pick the class-file language). */
function anyProjectPath(projDir: string): string | null {
const name = fs.readdirSync(projDir).find(f => /\.(cs|vb)proj$/i.test(f));
return name ? path.join(projDir, name) : null;
}
/** Side panel "Add Class" on the Code category: create a class-file stub in
* the working project's language (Name.cs or Name.vb). */
export async function addCsFile(): Promise<void> {
const projDir = await workspaceProjectDir('any');
if (!projDir) {
vscode.window.showWarningMessage('UI Maker: this action requires a .NET working project.');
return;
}
const project = anyProjectPath(projDir);
const isVb = !!project && /\.vbproj$/i.test(project);
const ext = isVb ? 'vb' : 'cs';
const name = await vscode.window.showInputBox({
prompt: `Class name for the new .${ext} file`,
placeHolder: 'FileProcessor',
validateInput: v => isCSharpIdentifier(v.trim())
? undefined
: 'Use a non-keyword class name (letters, digits, underscore).'
});
if (!name) { return; }
const cls = name.trim();
const file = path.join(projDir, `${cls}.${ext}`);
if (fs.existsSync(file)) {
vscode.window.showWarningMessage(`UI Maker: ${cls}.${ext} already exists.`);
return;
}
const ns = project && !isVb ? rootNamespace(project) : 'App';
// VB projects apply their root namespace implicitly — no wrapper needed.
const classSource = isVb
? `Public Class ${cls}\r\n` +
`\r\n` +
`End Class\r\n`
: `using System;\r\n` +
`\r\n` +
`namespace ${ns}\r\n` +
`{\r\n` +
` public class ${cls}\r\n` +
` {\r\n` +
` }\r\n` +
`}\r\n`;
try {
createFilesAtomically([{ target: file, contents: classSource }]);
} catch (err) {
vscode.window.showErrorMessage(`UI Maker: could not create ${cls}.${ext} — ${err}`);
return;
}
// Classic projects list every file; SDK projects glob sources automatically.
if (project) {
try {
let xml = fs.readFileSync(project, 'utf8');
if (!/<Project\s[^>]*\bSdk\s*=/.test(xml) && /<\/Project>/.test(xml)) {
const eol = xml.includes('\r\n') ? '\r\n' : '\n';
xml = xml.replace(/<\/Project>/,
` <ItemGroup>${eol} <Compile Include="${cls}.${ext}" />${eol} </ItemGroup>${eol}</Project>`);
replaceFileAtomically(project, xml);
}
} catch { /* non-fatal — the file still exists */ }
}
await vscode.window.showTextDocument(vscode.Uri.file(file));
}
/** Side panel "Add Image" / "Add Resource": import files as project resources. */
export async function addResourceFiles(imagesOnly: boolean): Promise<void> {
const projDir = await workspaceProjectDir();
if (!projDir) {
vscode.window.showWarningMessage(
'UI Maker: resource import currently requires a C# (.csproj) working project — Visual Basic projects manage resources in Visual Studio (My.Resources).');
return;
}
const picked = await vscode.window.showOpenDialog({
canSelectMany: true,
openLabel: imagesOnly ? 'Add Image(s)' : 'Add Resource(s)',
filters: imagesOnly ? IMAGE_FILTERS : { 'All files': ['*'] }
});
if (!picked?.length) { return; }
const keys: string[] = [];
for (const uri of picked) {
const res = importResourceFile(path.join(projDir, 'placeholder'), uri.fsPath);
if (res) { keys.push(res.key); }
}
if (keys.length) {
vscode.window.showInformationMessage(
`UI Maker: added ${keys.length} resource${keys.length === 1 ? '' : 's'} — ` +
`use Properties.Resources.${keys[0]} in your code.`);
}
}
// ------------------------------------------------------------------ resolve
/**
* Resolve the image references a designer document uses so the canvas can
* draw them. Project resources ("p") map to files via Resources.resx
* filerefs; local form resources ("l") are read out of the form's own .resx
* (bytearray-base64 entries) and returned as data: URIs.
*/
export function resolveImages(docPath: string, keys: ImageKey[]): Map<string, string> {
const out = new Map<string, string>();
if (!keys.length) { return out; }
const projKeys = keys.filter(k => k.scope === 'p');
if (projKeys.length) {
const projDir = findProjectDir(docPath);
// C# keeps the project resx in Properties/; VB in "My Project".
const file = projDir
? [resxPath(projDir), path.join(projDir, 'My Project', 'Resources.resx')]
.find(f => fs.existsSync(f)) ?? null
: null;
if (file) {
const entries = parseResxEntries(fs.readFileSync(file, 'utf8'));
for (const k of projKeys) {
const e = entries.find(x => x.name === k.key && x.fileRef);
if (!e?.fileRef) { continue; }
const rel = e.fileRef.split(';')[0].split('\\').join(path.sep);
const abs = path.resolve(path.dirname(file), rel);
if (fs.existsSync(abs)) { out.set(`p:${k.key}`, abs); }
}
}
}
const localKeys = keys.filter(k => k.scope === 'l');
if (localKeys.length) {
// Main.Designer.cs/.Designer.vb -> Main.resx (next to the designer file).
const resx = docPath.replace(/\.designer\.(cs|vb)$/i, '.resx');
if (fs.existsSync(resx)) {
const xml = fs.readFileSync(resx, 'utf8');
for (const k of localKeys) {
const uri = localResxImage(xml, k.key);
if (uri) { out.set(`l:${k.key}`, uri); }
}
}
}
return out;
}
/** Decode one bytearray-base64 image from a form resx into a data URI. */
function localResxImage(xml: string, name: string): string | null {
const esc = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const m = new RegExp(
`<data name="${esc}"[^>]*mimetype="application/x-microsoft\\.net\\.object\\.bytearray\\.base64"[^>]*>\\s*<value>([\\s\\S]*?)</value>`,
'i').exec(xml);
if (!m) { return null; }
const b64 = m[1].replace(/\s+/g, '');
let bytes: Buffer;
try { bytes = Buffer.from(b64, 'base64'); } catch { return null; }
const mime =
bytes.length > 4 && bytes[0] === 0x89 && bytes[1] === 0x50 ? 'image/png'
: bytes.length > 2 && bytes[0] === 0xff && bytes[1] === 0xd8 ? 'image/jpeg'
: bytes.length > 3 && bytes.toString('ascii', 0, 3) === 'GIF' ? 'image/gif'
: bytes.length > 2 && bytes[0] === 0x42 && bytes[1] === 0x4d ? 'image/bmp'
: bytes.length > 4 && bytes[0] === 0 && bytes[1] === 0 && bytes[2] === 1 && bytes[3] === 0 ? 'image/x-icon'
: null;
if (!mime) { return null; } // BinaryFormatter payloads etc. — not renderable
return `data:${mime};base64,${bytes.toString('base64')}`;
}