-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBlazorBundle.cs
More file actions
277 lines (229 loc) · 10.3 KB
/
AddBlazorBundle.cs
File metadata and controls
277 lines (229 loc) · 10.3 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
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows; // For MessageBox if needed
using Task = System.Threading.Tasks.Task;
namespace BlazorCodeBehindUtil
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class AddBlazorBundle
{
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("9ca5e9f7-883f-40ff-ae70-8d2ce136ad25");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly AsyncPackage package;
/// <summary>
/// Initializes a new instance of the <see cref="AddBlazorBundle"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
/// <param name="commandService">Command service to add command to, not null.</param>
private AddBlazorBundle(AsyncPackage package, OleMenuCommandService commandService)
{
this.package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.Execute, menuCommandID);
commandService.AddCommand(menuItem);
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static AddBlazorBundle Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
private DTE2 _dte;
private ProjectItemsEvents _projectItemsEvents;
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static async Task InitializeAsync(AsyncPackage package)
{
// Switch to the main thread - the call to AddCommand in Command1's constructor requires
// the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService commandService =
await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new AddBlazorBundle(package, commandService);
// Hook rename watcher
Instance._dte = (DTE2)await package.GetServiceAsync(typeof(SDTE));
Events2 events2 = (Events2)Instance._dte.Events;
Instance._projectItemsEvents = events2.ProjectItemsEvents;
Instance._projectItemsEvents.ItemRenamed += Instance.OnItemRenamed;
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
var dte = (DTE2)Package.GetGlobalService(typeof(SDTE));
// 1. Get Settings
var options = (GeneralOptions)package.GetDialogPage(typeof(GeneralOptions));
// 2. Get Selection
UIHierarchyItem selectedItem = ((Array)dte.ToolWindows.SolutionExplorer.SelectedItems).Cast<UIHierarchyItem>().First();
ProjectItem folder = selectedItem.Object as ProjectItem;
Project project = folder.ContainingProject;
string folderPath = folder.Properties.Item("FullPath").Value.ToString();
// 3. Detect Namespace
string projectNamespace = project.Properties.Item("DefaultNamespace").Value.ToString();
string projectFolder = Path.GetDirectoryName(project.FullName);
string relativePath = folderPath.Replace(projectFolder, "").Trim(Path.DirectorySeparatorChar);
string finalNamespace = projectNamespace + (string.IsNullOrEmpty(relativePath) ? "" : "." + relativePath.Replace(Path.DirectorySeparatorChar, '.'));
// 4. Show Dialog
var dialog = new BundleDialog("MyComponent", options.CreateCS, options.CreateCSS, options.CreateJS);
if (dialog.ShowDialog() != true || string.IsNullOrWhiteSpace(dialog.ComponentName)) return;
string name = dialog.ComponentName.Trim();
// --- START OF FIX ---
// 5. Generate the Namespace Content
// We only need this if the user checked the .cs box
string nsContent = "";
if (dialog.CreateCS)
{
if (options.UseFileScopedNamespace)
{
nsContent = $"namespace {finalNamespace};\n\npublic partial class {name}\n{{\n\n}}";
}
else
{
nsContent = $"namespace {finalNamespace}\n{{\n public partial class {name}\n {{\n }}\n}}";
}
}
// 6. Define the files we want to create
var filesToCreate = new Dictionary<string, string>();
filesToCreate.Add($"{name}.razor", $"<h3>{name}</h3>");
if (dialog.CreateCS) filesToCreate.Add($"{name}.razor.cs", nsContent);
if (dialog.CreateCSS) filesToCreate.Add($"{name}.razor.css", "/* Scoped CSS */");
if (dialog.CreateJS) filesToCreate.Add($"{name}.razor.js", "export function init() { }");
// --- END OF FIX ---
// 7. Check for existing files (Safety Check)
var existingFiles = filesToCreate.Keys
.Where(fileName => File.Exists(Path.Combine(folderPath, fileName)))
.ToList();
if (existingFiles.Any())
{
string message = $"The following files already exist:\n\n" +
$"{string.Join("\n", existingFiles)}\n\n" +
"Do you want to overwrite them?";
var result = System.Windows.Forms.MessageBox.Show(
message,
"File Collision",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Warning);
if (result == System.Windows.Forms.DialogResult.No) return;
}
// 8. Create and Add Files
foreach (var file in filesToCreate)
{
string fullPath = Path.Combine(folderPath, file.Key);
File.WriteAllText(fullPath, file.Value);
folder.ProjectItems.AddFromFile(fullPath);
}
}
private void OnDocumentSaved(Document document)
{
ThreadHelper.ThrowIfNotOnUIThread();
string filePath = document.FullName;
if (!filePath.EndsWith(".razor", StringComparison.OrdinalIgnoreCase))
return;
string directory = Path.GetDirectoryName(filePath);
string newBaseName = Path.GetFileNameWithoutExtension(filePath);
string[] extensions =
{
".razor.cs",
".razor.css",
".razor.js"
};
foreach (var ext in extensions)
{
string oldPattern = Directory
.GetFiles(directory, "*" + ext)
.FirstOrDefault(f =>
Path.GetFileName(f).StartsWith(newBaseName) == false);
// Instead, better approach:
string oldFile = Path.Combine(directory, newBaseName + ext);
if (!File.Exists(oldFile))
{
// Try to detect a file with previous name
var match = Directory.GetFiles(directory, "*" + ext)
.FirstOrDefault(f => !f.StartsWith(newBaseName));
if (match != null)
{
string newFilePath = Path.Combine(directory, newBaseName + ext);
File.Move(match, newFilePath);
}
}
}
}
private void OnItemRenamed(ProjectItem projectItem, string oldName)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (projectItem == null)
return;
string newFilePath = projectItem.FileNames[1];
if (!newFilePath.EndsWith(".razor", StringComparison.OrdinalIgnoreCase))
return;
string directory = Path.GetDirectoryName(newFilePath);
string newBaseName = Path.GetFileNameWithoutExtension(newFilePath);
string oldBaseName = Path.GetFileNameWithoutExtension(oldName);
string[] extensions =
{
".razor.cs",
".razor.css",
".razor.js"
};
foreach (var ext in extensions)
{
string oldSibling = Path.Combine(directory, oldBaseName + ext);
string newSibling = Path.Combine(directory, newBaseName + ext);
if (File.Exists(oldSibling))
{
try
{
File.Move(oldSibling, newSibling);
}
catch
{
// swallow silently or log if desired
}
}
}
}
}
}