private ProjectItem AddArea(Project pj, string AreaName) { ProjectItem AreaItem = null; foreach (ProjectItem pi in pj.ProjectItems) { if (pi.Name == "Areas") { AreaItem = pi; } } if (AreaItem == null) { AreaItem = pj.ProjectItems.AddFolder("Areas"); } ProjectItem AreaNameItem = null; foreach (ProjectItem pi in AreaItem.ProjectItems) { if (pi.Name == AreaName) { AreaNameItem = pi; } } if (AreaNameItem == null) { AreaNameItem = AreaItem.ProjectItems.AddFolder(AreaName); AreaNameItem.ProjectItems.AddFromTemplate("Templates\\MvcAreaItemTemplate\\MvcAreaItemTemplate.cs.vstemplate", AreaName); // Setup the scaffolding item creation parameters to be passed into the T4 template. var parameters = new Dictionary <string, object>() { { "AreaName", AreaName }, { "Namespace", AreaNameItem.GetDefaultNamespace() } }; string areaName = AreaName + "AreaRegistration"; var outputFolderPath = Path.Combine(string.Format("Areas\\{0}", AreaName), areaName); this.AddFileFromTemplate(pj, outputFolderPath, "AreaRegistration", parameters, skipIfExists: _viewModel.SkipIfExists); } return(AreaNameItem); }
/// <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() { Project projectActive = Context.ActiveProject; Solution solution = projectActive.DTE.Solution; List <Project> list = new List <Project>(); foreach (Project projectA in solution.Projects) { if (projectA.Kind == Constants.vsProjectKindSolutionItems) { list.AddRange(GetSolutionFolderProjects(projectA)); } else { list.Add(projectA); } } var dbContextClass = _viewModel.SelectedContextType; var modelTypes = _viewModel.SelectedModelType; //find context projects Project projectContext = null; foreach (Project projectA in list) { if (dbContextClass.CodeType.Namespace.FullName.StartsWith(projectA.Name + ".")) { projectContext = projectA; } } string defaultNamespace = null; string AreaPath = string.Empty; //return; if (_viewModel.SelectedController != "MVC") { AddWebApiConfig(projectActive, dbContextClass, modelTypes.Select(p => p.CodeType).ToList(), projectContext.Name + ".Models"); } #region Area Init if (_viewModel.IsArea) { string AreaName = _viewModel.SelectedArea; ProjectItem AreaItem = AddArea(projectActive, AreaName); AreaPath = string.Format("Areas\\{0}\\", AreaName); defaultNamespace = AreaItem.GetDefaultNamespace(); } #endregion Area Init var selectionRelativePath = AreaPath; AddMvcModels(projectActive, selectionRelativePath + "Models\\", defaultNamespace); foreach (var codeType in modelTypes.Select(p => p.CodeType)) { // Get the Entity Framework Meta Data IEntityFrameworkService efService = (IEntityFrameworkService)Context.ServiceProvider.GetService(typeof(IEntityFrameworkService)); ModelMetadata efMetadata = efService.AddRequiredEntity(Context, dbContextClass.TypeName, codeType.FullName); if (efMetadata.PrimaryKeys.Count() > 1) { continue; } if (_viewModel.SelectedController != "MVC") { AddViewsControllers(projectActive, selectionRelativePath + "Controllers\\", codeType, defaultNamespace); } AddBLL(projectContext, "BLL\\", codeType, dbContextClass, efMetadata); if (_viewModel.SelectedController == "WEBAPI") { AddMvcControllers(projectActive, selectionRelativePath + "Controllers\\API\\", codeType, efMetadata, defaultNamespace); } else if (_viewModel.SelectedController == "ODataV3" || _viewModel.SelectedController == "ODataV4") { AddMvcControllers(projectActive, selectionRelativePath + "Controllers\\OData\\", codeType, efMetadata, defaultNamespace); } else { AddMvcControllers(projectActive, selectionRelativePath + "Controllers\\", codeType, efMetadata, defaultNamespace); } AddMvcViews(projectActive, selectionRelativePath + "Views\\", codeType, dbContextClass, efMetadata, defaultNamespace); } }