public async Task <ProcessedTemplateResult> ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { var solutionTemplate = (MicrosoftTemplateEngineSolutionTemplate)template; var parameters = GetParameters(solutionTemplate, config); var templateInfo = solutionTemplate.templateInfo; var workspaceItems = new List <IWorkspaceFileObject> (); var filesBeforeCreation = Directory.GetFiles(config.ProjectLocation, "*", SearchOption.AllDirectories); var result = await MicrosoftTemplateEngine.InstantiateAsync(templateInfo, config, parameters); if (result.Status != CreationResultStatus.Success) { string message = string.Format("Could not create template. Id='{0}' {1} {2}", template.Id, result.Status, result.Message); throw new InvalidOperationException(message); } var filesToOpen = new List <string> (); foreach (var postAction in result.ResultInfo.PostActions) { switch (postAction.ActionId.ToString().ToUpper()) { case "84C0DA21-51C8-4541-9940-6CA19AF04EE6": if (postAction.Args.TryGetValue("files", out var files)) { foreach (var fi in files.Split(';')) { if (int.TryParse(fi.Trim(), out var i)) { filesToOpen.Add(Path.Combine(config.ProjectLocation, GetPath(result.ResultInfo.PrimaryOutputs [i]))); } } } break; case "D396686C-DE0E-4DE6-906D-291CD29FC5DE": //TODO: Load project files break; } } //TODO: Once templates support "D396686C-DE0E-4DE6-906D-291CD29FC5DE" use that to load projects foreach (var path in result.ResultInfo.PrimaryOutputs) { var fullPath = Path.Combine(config.ProjectLocation, GetPath(path)); if (Services.ProjectService.IsSolutionItemFile(fullPath)) { workspaceItems.Add(await MonoDevelop.Projects.Services.ProjectService.ReadSolutionItem(new Core.ProgressMonitor(), fullPath)); } } var metadata = new TemplateMetadata { Id = templateInfo.Identity, Name = templateInfo.Name, Language = template.Language, Platform = string.Join(";", templateInfo.Classifications) }; TemplateCounter.Inc(1, null, metadata); MicrosoftTemplateEngineProcessedTemplateResult processResult; if (parentFolder == null) { var solution = new Solution(); solution.SetLocation(config.SolutionLocation, config.SolutionName); foreach (var item in workspaceItems.Cast <SolutionFolderItem> ()) { IConfigurationTarget configurationTarget = item as IConfigurationTarget; if (configurationTarget != null) { foreach (ItemConfiguration configuration in configurationTarget.Configurations) { bool flag = false; foreach (SolutionConfiguration solutionCollection in solution.Configurations) { if (solutionCollection.Id == configuration.Id) { flag = true; } } if (!flag) { solution.AddConfiguration(configuration.Id, true); } } } solution.RootFolder.AddItem(item); } processResult = new MicrosoftTemplateEngineProcessedTemplateResult(new [] { solution }, solution.FileName, config.ProjectLocation); } else { processResult = new MicrosoftTemplateEngineProcessedTemplateResult(workspaceItems.ToArray(), parentFolder.ParentSolution.FileName, config.ProjectLocation); } // Format all source files generated during the project creation foreach (var p in workspaceItems.OfType <Project> ()) { foreach (var file in p.Files) { if (!filesBeforeCreation.Contains((string)file.FilePath, FilePath.PathComparer)) //Format only newly created files { if (solutionTemplate.ShouldFormatFile(file.FilePath)) { await MicrosoftTemplateEngine.FormatFile(parentFolder?.Policies ?? p.Policies, file.FilePath); } } } } processResult.SetFilesToOpen(filesToOpen); return(processResult); }
public async Task <ProcessedTemplateResult> ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { var solutionTemplate = (MicrosoftTemplateEngineSolutionTemplate)template; var parameters = GetParameters(solutionTemplate, config); var templateInfo = solutionTemplate.templateInfo; var workspaceItems = new List <IWorkspaceFileObject> (); var result = await templateCreator.InstantiateAsync( templateInfo, config.ProjectName, config.GetValidProjectName(), config.ProjectLocation, parameters, true, false, null ); var filesToOpen = new List <string> (); foreach (var postAction in result.ResultInfo.PostActions) { switch (postAction.ActionId.ToString().ToUpper()) { case "84C0DA21-51C8-4541-9940-6CA19AF04EE6": if (postAction.Args.TryGetValue("files", out var files)) { foreach (var fi in files.Split(';')) { if (int.TryParse(fi.Trim(), out var i)) { filesToOpen.Add(Path.Combine(config.ProjectLocation, result.ResultInfo.PrimaryOutputs [i].Path)); } } } break; case "D396686C-DE0E-4DE6-906D-291CD29FC5DE": //TODO: Load project files break; } } //TODO: Once templates support "D396686C-DE0E-4DE6-906D-291CD29FC5DE" use that to load projects foreach (var path in Directory.GetFiles(config.ProjectLocation, "*.*proj", SearchOption.AllDirectories)) { if (path.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase)) { workspaceItems.Add(await MonoDevelop.Projects.Services.ProjectService.ReadSolutionItem(new Core.ProgressMonitor(), path)); } } var metadata = new Dictionary <string, string> (); metadata ["Id"] = templateInfo.Identity; metadata ["Name"] = templateInfo.Name; metadata ["Language"] = template.Language; metadata ["Platform"] = string.Join(";", templateInfo.Classifications); TemplateCounter.Inc(1, null, metadata); MicrosoftTemplateEngineProcessedTemplateResult processResult; if (parentFolder == null) { var solution = new Solution(); solution.SetLocation(config.SolutionLocation, config.SolutionName); foreach (var item in workspaceItems.Cast <SolutionFolderItem> ()) { IConfigurationTarget configurationTarget = item as IConfigurationTarget; if (configurationTarget != null) { foreach (ItemConfiguration configuration in configurationTarget.Configurations) { bool flag = false; foreach (SolutionConfiguration solutionCollection in solution.Configurations) { if (solutionCollection.Id == configuration.Id) { flag = true; } } if (!flag) { solution.AddConfiguration(configuration.Id, true); } } } solution.RootFolder.AddItem(item); } processResult = new MicrosoftTemplateEngineProcessedTemplateResult(new [] { solution }, solution.FileName, config.ProjectLocation); } else { processResult = new MicrosoftTemplateEngineProcessedTemplateResult(workspaceItems.ToArray(), parentFolder.ParentSolution.FileName, config.ProjectLocation); } // Format all source files generated during the project creation foreach (var p in workspaceItems.OfType <Project> ()) { foreach (var file in p.Files) { await FormatFile(p, file.FilePath); } } processResult.SetFilesToOpen(filesToOpen); return(processResult); }
public async Task <ProcessedTemplateResult> ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { var templateInfo = ((MicrosoftTemplateEngineSolutionTemplate)template).templateInfo; var workspaceItems = new List <IWorkspaceFileObject> (); var result = await templateCreator.InstantiateAsync( templateInfo, config.ProjectName, config.GetValidProjectName(), config.ProjectLocation, new Dictionary <string, string> (), true, false); if (result.ResultInfo.PrimaryOutputs.Any()) { foreach (var res in result.ResultInfo.PrimaryOutputs) { var fullPath = Path.Combine(config.ProjectLocation, res.Path); //This happens if some project is excluded by modifiers, e.g. Test project disabled in wizard settings by user if (!File.Exists(fullPath)) { continue; } workspaceItems.Add(await MonoDevelop.Projects.Services.ProjectService.ReadSolutionItem(new Core.ProgressMonitor(), fullPath)); } } else { //TODO: Remove this code once https://github.com/dotnet/templating/pull/342 is released in NuGet feed and we bump NuGet version of templating engine foreach (var path in Directory.GetFiles(config.ProjectLocation, "*.*proj", SearchOption.AllDirectories)) { if (path.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase)) { workspaceItems.Add(await MonoDevelop.Projects.Services.ProjectService.ReadSolutionItem(new Core.ProgressMonitor(), path)); } } } var metadata = new Dictionary <string, string> (); metadata ["Id"] = templateInfo.Identity; metadata ["Name"] = templateInfo.Name; metadata ["Language"] = template.Language; metadata ["Platform"] = string.Join(";", templateInfo.Classifications); TemplateCounter.Inc(1, null, metadata); MicrosoftTemplateEngineProcessedTemplateResult processResult; if (parentFolder == null) { var solution = new Solution(); solution.SetLocation(config.SolutionLocation, config.SolutionName); foreach (var item in workspaceItems.Cast <SolutionFolderItem> ()) { IConfigurationTarget configurationTarget = item as IConfigurationTarget; if (configurationTarget != null) { foreach (ItemConfiguration configuration in configurationTarget.Configurations) { bool flag = false; foreach (SolutionConfiguration solutionCollection in solution.Configurations) { if (solutionCollection.Id == configuration.Id) { flag = true; } } if (!flag) { solution.AddConfiguration(configuration.Id, true); } } } solution.RootFolder.AddItem(item); } processResult = new MicrosoftTemplateEngineProcessedTemplateResult(new [] { solution }, solution.FileName, config.ProjectLocation); } else { processResult = new MicrosoftTemplateEngineProcessedTemplateResult(workspaceItems.ToArray(), parentFolder.ParentSolution.FileName, config.ProjectLocation); } // Format all source files generated during the project creation foreach (var p in workspaceItems.OfType <Project> ()) { foreach (var file in p.Files) { await FormatFile(p, file.FilePath); } } return(processResult); }