Exemplo n.º 1
0
        public IEnumerable <SolutionItem> CreateProjects(SolutionFolderItem policyParent, ProjectCreateInformation cInfo)
        {
            if (solutionDescriptor.EntryDescriptors.Length == 0)
            {
                throw new InvalidOperationException(GettextCatalog.GetString("Solution template doesn't have any project templates"));
            }

            var solutionEntryItems = new List <SolutionItem> ();

            packageReferencesForCreatedProjects = new List <PackageReferencesForCreatedProject> ();

            foreach (ISolutionItemDescriptor solutionItemDescriptor in GetItemsToCreate(solutionDescriptor, cInfo))
            {
                ProjectCreateInformation itemCreateInfo = GetItemSpecificCreateInfo(solutionItemDescriptor, cInfo);
                itemCreateInfo = new ProjectTemplateCreateInformation(itemCreateInfo, cInfo.ProjectName);
                itemCreateInfo.TemplateInitializationCallback = async p => {
                    try {
                        await solutionItemDescriptor.InitializeItem(policyParent, itemCreateInfo, this.languagename, p);

                        // Handle the case where InitializeItem has to wait for a Task to complete and the project
                        // is saved before all the files are added to the project. Otherwise the project will not contain
                        // the files even though the solution pad shows them.
                        // TODO: Investigate making the InitializeFromTemplate methods Task based.
                        await p.SaveAsync(new ProgressMonitor());
                    } catch (Exception ex) {
                        LoggingService.LogError("TemplateInitializationCallback error.", ex);
                    }
                };

                SolutionItem solutionEntryItem = solutionItemDescriptor.CreateItem(itemCreateInfo, this.languagename);
                if (solutionEntryItem != null)
                {
                    SavePackageReferences(solutionEntryItem, solutionItemDescriptor, itemCreateInfo);
                    solutionEntryItems.Add(solutionEntryItem);
                }
            }

            var pDesc    = this.solutionDescriptor.EntryDescriptors.OfType <ProjectDescriptor> ().FirstOrDefault();
            var metadata = new TemplateMetadata {
                Id       = Id,
                Name     = nonLocalizedName,
                Language = LanguageName,
                Platform = pDesc != null ? pDesc.ProjectType : "Unknown"
            };

            TemplateCounter.Inc(1, null, metadata);

            return(solutionEntryItems);
        }
Exemplo n.º 2
0
        public async Task <WorkspaceItem> CreateWorkspaceItem(ProjectCreateInformation cInfo)
        {
            WorkspaceItemCreatedInformation workspaceItemInfo = await solutionDescriptor.CreateEntry(cInfo, this.languagename);

            this.createdSolutionName = workspaceItemInfo.WorkspaceItem.FileName;
            this.packageReferencesForCreatedProjects = workspaceItemInfo.PackageReferencesForCreatedProjects;

            var pDesc = this.solutionDescriptor.EntryDescriptors.OfType <ProjectDescriptor> ().ToList();

            var metadata = new TemplateMetadata {
                Id       = Id,
                Name     = nonLocalizedName,
                Language = LanguageName ?? string.Empty,
                Platform = pDesc.Count == 1 ? pDesc[0].ProjectType : "Multiple"
            };

            TemplateCounter.Inc(1, null, metadata);

            return(workspaceItemInfo.WorkspaceItem);
        }
Exemplo n.º 3
0
        public IEnumerable <SolutionItem> CreateProjects(SolutionFolderItem policyParent, ProjectCreateInformation cInfo)
        {
            if (solutionDescriptor.EntryDescriptors.Length == 0)
            {
                throw new InvalidOperationException(GettextCatalog.GetString("Solution template doesn't have any project templates"));
            }

            var solutionEntryItems = new List <SolutionItem> ();

            packageReferencesForCreatedProjects = new List <PackageReferencesForCreatedProject> ();

            foreach (ISolutionItemDescriptor solutionItemDescriptor in GetItemsToCreate(solutionDescriptor, cInfo))
            {
                ProjectCreateInformation itemCreateInfo = GetItemSpecificCreateInfo(solutionItemDescriptor, cInfo);
                itemCreateInfo = new ProjectTemplateCreateInformation(itemCreateInfo, cInfo.ProjectName);
                itemCreateInfo.TemplateInitializationCallback = p => solutionItemDescriptor.InitializeItem(policyParent, itemCreateInfo, this.languagename, p);

                SolutionItem solutionEntryItem = solutionItemDescriptor.CreateItem(itemCreateInfo, this.languagename);
                if (solutionEntryItem != null)
                {
                    SavePackageReferences(solutionEntryItem, solutionItemDescriptor, itemCreateInfo);
                    solutionEntryItems.Add(solutionEntryItem);
                }
            }

            var pDesc    = this.solutionDescriptor.EntryDescriptors.OfType <ProjectDescriptor> ().FirstOrDefault();
            var metadata = new TemplateMetadata {
                Id       = Id,
                Name     = nonLocalizedName,
                Language = LanguageName,
                Platform = pDesc != null ? pDesc.ProjectType : "Unknown"
            };

            TemplateCounter.Inc(1, null, metadata);

            return(solutionEntryItems);
        }
Exemplo n.º 4
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 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);
        }