NewProjectConfiguration CreateNewProjectConfig(string baseName, string templateId, string parameters) { FilePath solutionDirectory = Util.CreateTmpDir(baseName); CreateNuGetConfigFile(solutionDirectory); string projectName = GetProjectName(templateId); var config = new NewProjectConfiguration { CreateSolution = true, CreateProjectDirectoryInsideSolutionDirectory = true, CreateGitIgnoreFile = false, UseGit = false, Location = solutionDirectory, ProjectName = projectName, SolutionName = projectName }; foreach (var templateParameter in TemplateParameter.CreateParameters(parameters)) { config.Parameters [templateParameter.Name] = templateParameter.Value; } Directory.CreateDirectory(config.ProjectLocation); return(config); }
public ProjectTemplateTest(string basename, string templateId) { if (templatingService == null) { templatingService = new TemplatingService(); } this.templateId = templateId; SolutionDirectory = Util.CreateTmpDir(basename); CreateNuGetConfigFile(this); ProjectName = GetProjectName(templateId); Config = new NewProjectConfiguration { CreateSolution = true, CreateProjectDirectoryInsideSolutionDirectory = true, CreateGitIgnoreFile = false, UseGit = false, Location = SolutionDirectory, ProjectName = ProjectName, SolutionName = ProjectName }; Directory.CreateDirectory(Config.ProjectLocation); }
public TemplateProcessor( CustomSolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { this.template = template; this.config = config; this.parentFolder = parentFolder; }
public ProcessedTemplateResult ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { IProjectTemplatingProvider provider = GetTemplatingProviderForTemplate(template); if (provider != null) { return(provider.ProcessTemplate(template, config, parentFolder)); } return(null); }
async Task CreateAndBuild( SolutionTemplate template, NewProjectConfiguration config) { var result = await templatingService.ProcessTemplate(template, config, null); var solution = result.WorkspaceItems.FirstOrDefault() as Solution; await solution.SaveAsync(Util.GetMonitor()); RunMSBuild($"/t:Restore {solution.FileName}"); RunMSBuild($"/t:Build {solution.FileName}"); }
public void Run(NewProjectConfiguration config) { if (config.UseGit) { if (config.CreateGitIgnoreFile) { CreateGitIgnoreFile(config.SolutionLocation); } CreateGitRepository(config.SolutionLocation); } }
public Task <ProcessedTemplateResult> ProcessTemplate( SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { var templateProcessor = new TemplateProcessor( (CustomSolutionTemplate)template, config, parentFolder); return(templateProcessor.ProcessTemplate()); }
public void GenerateValidNames(string name, string generatedName) { CreateProjectConfig(@"d:\projects\MySolution"); config.ProjectName = name; config.SolutionName = name; config.CreateProjectDirectoryInsideSolutionDirectory = true; config.CreateSolution = true; Assert.AreEqual(generatedName, NewProjectConfiguration.GenerateValidProjectName(name)); Assert.AreEqual(generatedName, config.GetValidProjectName()); Assert.AreEqual(generatedName, config.GetValidSolutionName()); }
ProjectCreateInformation CreateProjectCreateInformation(NewProjectConfiguration config, SolutionFolder parentFolder) { ProjectCreateInformation cinfo = new ProjectCreateInformation(); cinfo.SolutionPath = FileService.ResolveFullPath(config.SolutionLocation); cinfo.ProjectBasePath = FileService.ResolveFullPath(config.ProjectLocation); cinfo.ProjectName = config.ProjectName; cinfo.SolutionName = config.SolutionName; cinfo.ParentFolder = parentFolder; cinfo.ActiveConfiguration = IdeApp.Workspace.ActiveConfiguration; cinfo.Parameters = config.Parameters; return(cinfo); }
ProjectCreateInformation CreateProjectCreateInformation(NewProjectConfiguration config, SolutionFolder parentFolder) { ProjectCreateInformation cinfo = new ProjectCreateInformation(); cinfo.SolutionPath = new FilePath(config.SolutionLocation).ResolveLinks(); cinfo.ProjectBasePath = new FilePath(config.ProjectLocation).ResolveLinks(); cinfo.ProjectName = config.ProjectName; cinfo.SolutionName = config.SolutionName; cinfo.ParentFolder = parentFolder; cinfo.ActiveConfiguration = IdeApp.Workspace?.ActiveConfiguration ?? ConfigurationSelector.Default; cinfo.Parameters = config.Parameters; return(cinfo); }
async Task CreateAndBuild( SolutionTemplate template, NewProjectConfiguration config) { var result = await templatingService.ProcessTemplate(template, config, null); solution = result.WorkspaceItems.FirstOrDefault() as Solution; await solution.SaveAsync(Util.GetMonitor()); // RestoreDisableParallel prevents parallel restores which sometimes cause // the restore to fail on Mono. RunMSBuild($"/t:Restore /p:RestoreDisableParallel=true \"{solution.FileName}\""); RunMSBuild($"/t:Build \"{solution.FileName}\""); }
public ProcessedTemplateResult ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { IProjectTemplatingProvider provider = GetTemplatingProviderForTemplate(template); if (provider != null) { var result = provider.ProcessTemplate(template, config, parentFolder); if (result.WorkspaceItems.Any()) { RecentTemplates.AddTemplate(template); } return(result); } return(null); }
public static Task <TemplateCreationResult> InstantiateAsync( ITemplateInfo templateInfo, NewProjectConfiguration config, IReadOnlyDictionary <string, string> parameters) { return(templateCreator.InstantiateAsync( templateInfo, config.ProjectName, config.GetValidProjectName(), config.ProjectLocation, parameters, true, false, null )); }
SolutionTemplate FindTemplate(string templateId, NewProjectConfiguration config) { var categories = templatingService .GetProjectTemplateCategories(t => MatchTemplate(t, templateId)) .ToList(); var templates = categories.First() .Categories.First() .Categories.First() .Templates.ToList(); var template = templates.Single(); string language = GetLanguage(templateId); return(template.GetTemplate(language, config.Parameters)); }
ProcessedTemplateResult ProcessTemplate(DefaultSolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { IEnumerable <IWorkspaceFileObject> newItems = null; ProjectCreateInformation cinfo = CreateProjectCreateInformation(config, parentFolder); if (parentFolder == null) { IWorkspaceFileObject newItem = template.Template.CreateWorkspaceItem(cinfo); if (newItem != null) { newItems = new [] { newItem }; } } else { newItems = template.Template.CreateProjects(parentFolder, cinfo); } return(new DefaultProcessedTemplateResult(template.Template, newItems, cinfo.ProjectBasePath)); }
void CreateProjectConfig(string location) { config = new NewProjectConfiguration { Location = ToNativePath(location) }; }
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); 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); } return(new MicrosoftTemplateEngineProcessedTemplateResult(new [] { solution }, solution.FileName, config.ProjectLocation)); } else { return(new MicrosoftTemplateEngineProcessedTemplateResult(workspaceItems.ToArray(), parentFolder.ParentSolution.FileName, config.ProjectLocation)); } }
public Task <ProcessedTemplateResult> ProcessTemplate(SolutionTemplate template, NewProjectConfiguration config, SolutionFolder parentFolder) { return(ProcessTemplate((DefaultSolutionTemplate)template, config, parentFolder)); }
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); }
Dictionary <string, string> GetParameters(MicrosoftTemplateEngineSolutionTemplate template, NewProjectConfiguration config) { var parameters = new Dictionary <string, string> (); if (!string.IsNullOrEmpty(template.DefaultParameters)) { foreach (TemplateParameter parameter in GetValidParameters(template.DefaultParameters)) { parameters [parameter.Name] = parameter.Value; } } // If the template has no wizard then no extra parameters will be set. if (template.HasWizard) { var model = (IStringTagModel)config.Parameters; foreach (ITemplateParameter parameter in template.templateInfo.Parameters) { string parameterValue = (string)model.GetValue(parameter.Name); if (parameterValue != null) { parameters [parameter.Name] = parameterValue; } } } return(parameters); }
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); }