Ejemplo n.º 1
0
        public async Task ProcessTemplate(ItemTemplate template, Project project, NewItemConfiguration config)
        {
            var itemTemplate = (MicrosoftTemplateEngineItemTemplate)template;
            var parameters   = GetParameters(project, itemTemplate, config);
            var result       = await MicrosoftTemplateEngine.InstantiateAsync(itemTemplate.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);
            }

            foreach (var path in result.ResultInfo.PrimaryOutputs)
            {
                string fullPath = Path.Combine(config.Directory, GetPath(path));

                await MicrosoftTemplateEngine.FormatFile(project?.Policies, fullPath);

                if (project != null)
                {
                    AddFileToProject(project, fullPath);
                }

                IdeApp.Workbench.OpenDocument(fullPath, project).Ignore();

                if (project != null)
                {
                    await InstallNuGetPackages(project, result.ResultInfo);
                }
            }
        }
Ejemplo n.º 2
0
        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 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)
                {
                    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);
        }