-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathElementGenerator.cs
269 lines (230 loc) · 10.1 KB
/
ElementGenerator.cs
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
using Orchardization.UI;
using Microsoft.AspNet.Scaffolding;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System;
using System.Linq;
using EnvDTE;
namespace Orchardization
{
public class ElementGenerator : CodeGenerator
{
ElementViewModel _viewModel;
/// <summary>
/// Constructor for the custom code generator
/// </summary>
/// <param name="context">Context of the current code generation operation based on how scaffolder was invoked(such as selected project/folder) </param>
/// <param name="information">Code generation information that is defined in the factory class.</param>
public ElementGenerator(
CodeGenerationContext context,
CodeGeneratorInformation information)
: base(context, information)
{
_viewModel = new ElementViewModel();
}
/// <summary>
/// Any UI to be displayed after the scaffolder has been selected from the Add Scaffold dialog.
/// Any validation on the input for values in the UI should be completed before returning from this method.
/// </summary>
/// <returns></returns>
public override bool ShowUIAndValidate()
{
// Bring up the selection dialog and allow user to select a model type
ElementWindow window = new ElementWindow(_viewModel);
bool? showDialog = window.ShowDialog();
Validate();
return showDialog ?? false;
}
/// <summary>
/// Validates the users inputs.
/// </summary>
public void Validate()
{
}
/// <summary>
/// This method is executed after the ShowUIAndValidate method, and this is where the actual code generation should occur.
/// In this example, we are generating a new file from t4 template based on the ModelType selected in our UI.
/// </summary>
public override void GenerateCode()
{
var name = _viewModel.ElementName.Trim();
var props = new Dictionary<string, string>();
if (!String.IsNullOrEmpty(_viewModel.Properties) || !String.IsNullOrWhiteSpace(_viewModel.Properties))
{
props = Regex.Split(_viewModel.Properties, "\\n")
.Select(x => x.Split(':'))
.Where(x => x.Length > 1 && !String.IsNullOrEmpty(x[0].Trim()) && !String.IsNullOrEmpty(x[1].Trim()))
.ToDictionary(x => x[0].Trim(), x => x[1].Trim());
}
var firstProperty = props.FirstOrDefault();
var hasProperties = firstProperty.Key != null;
string firstType = "";
string firstKey = "";
if (hasProperties)
{
firstKey = firstProperty.Key;
firstType = firstProperty.Value;
}
bool formsEditor = _viewModel.EditorType == null ? false : _viewModel.EditorType.Contains("Forms API");
var namePlusElement = name.EndsWith("element", StringComparison.InvariantCultureIgnoreCase) ? name : name + "Element"; // the element name with element appended onto it
// Setup the scaffolding item creation parameters to be passed into the T4 template. We'll just use one for everything
var parameters = new Dictionary<string, object>()
{
{ "ElementName", name },
{ "NamePlusElement", namePlusElement },
{ "Module", Context.ActiveProject.Name },
{ "Properties", props },
{ "PropCount", props.Count() },
{ "HasProperty", hasProperties },
{ "FirstKey", firstKey },
{ "FirstType", firstType },
{ "Category", String.IsNullOrWhiteSpace(_viewModel.Category) ? "Content" : _viewModel.Category },
{ "Description", String.IsNullOrWhiteSpace(_viewModel.Description) ? $"Add a {name} to the layout" : _viewModel.Description },
{ "Feature", String.IsNullOrWhiteSpace(_viewModel.Feature) ? "" : _viewModel.Feature },
{ "HasFeature", !String.IsNullOrWhiteSpace(_viewModel.Feature) },
{ "HasEditor", _viewModel.HasEditor },
{ "FormsEditor", formsEditor }
};
// make sure references are there
var vsproject = Context.ActiveProject.Object as VSLangProj.VSProject;
vsproject.References.Add("Orchard.Core");
vsproject.References.Add("Orchard.Framework");
Project orchardForms = null;
Project orchardLayouts = null;
var formProject = Context.ActiveProject.UniqueName;
var projects = vsproject.DTE.Solution.Projects;
var item = projects.GetEnumerator();
while (item.MoveNext())
{
var p = item.Current as Project;
if (p.Name != "Modules")
continue;
for (var i = 1; i <= p.ProjectItems.Count; i++)
{
var subProject = p.ProjectItems.Item(i).SubProject;
if (subProject == null)
{
continue;
}
// If this is another solution folder, do a recursive call, otherwise add
if (subProject.Name == "Orchard.Forms")
orchardForms = subProject;
if (subProject.Name == "Orchard.Layouts")
orchardLayouts = subProject;
// this wont work if those modules are not in the solution
if (orchardLayouts != null && orchardForms != null)
break;
}
break;
}
try
{
if(orchardForms != null)
vsproject.References.AddProject(orchardForms);
if (orchardLayouts != null)
vsproject.References.AddProject(orchardLayouts);
}
catch(Exception ex)
{
}
// in case people are trying to add elements to Orchard.Layouts
//if (!vsproject.Project.Name.Contains("Orchard.Layouts")) vsproject.References.Add("Orchard.Layouts.csproj");
AddFolder(Context.ActiveProject, "Elements");
AddFileFromTemplate(Context.ActiveProject,
"Elements\\" + name,
"Element",
parameters,
skipIfExists: true);
// Add the driver folder and file
AddFolder(Context.ActiveProject, "Drivers");
if (_viewModel.HasEditor && formsEditor)
{
AddFileFromTemplate(Context.ActiveProject,
"Drivers\\" + namePlusElement + "Driver",
"FormsElementDriver",
parameters,
skipIfExists: true);
}
else
{
AddFileFromTemplate(Context.ActiveProject,
"Drivers\\" + name + "Driver",
"ElementDriver",
parameters,
skipIfExists: true);
}
AddFolder(Context.ActiveProject, @"Views\Elements");
AddFileFromTemplate(Context.ActiveProject,
"Views\\Elements\\" + name,
"ElementView",
parameters,
skipIfExists: true);
if(_viewModel.HasEditor && !formsEditor)
{
AddFolder(Context.ActiveProject, @"ViewModels");
AddFileFromTemplate(Context.ActiveProject,
"ViewModels\\" + namePlusElement + "ViewModel",
"FormsElementDriver",
parameters,
skipIfExists: true);
AddFolder(Context.ActiveProject, @"Views\EditorTemplates");
AddFileFromTemplate(Context.ActiveProject,
"Views\\EditorTemplates\\Elements." + name,
"ElementEditorView",
parameters,
skipIfExists: true);
}
}
//public DTE2 GetActiveIDE()
//{
// // Get an instance of currently running Visual Studio IDE.
// DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
// return dte2;
//}
//public IList<Project> Projects()
//{
// Projects projects = GetActiveIDE().Solution.Projects;
// List<Project> list = new List<Project>();
// var item = projects.GetEnumerator();
// while (item.MoveNext())
// {
// var project = item.Current as Project;
// if (project == null)
// {
// continue;
// }
// if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
// {
// list.AddRange(GetSolutionFolderProjects(project));
// }
// else
// {
// list.Add(project);
// }
// }
// return list;
//}
//private IEnumerable<Project> GetSolutionFolderProjects(Project solutionFolder)
//{
// List<Project> list = new List<Project>();
// for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++)
// {
// var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
// if (subProject == null)
// {
// continue;
// }
// // If this is another solution folder, do a recursive call, otherwise add
// if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
// {
// list.AddRange(GetSolutionFolderProjects(subProject));
// }
// else
// {
// list.Add(subProject);
// }
// }
// return list;
//}
}
}