public ProjectRequest BuildProjectRequest(TemplateChoices choices) { if (choices.Category.IsEmpty()) { throw new Exception("Category is required"); } if (choices.ProjectName.IsEmpty()) { throw new Exception("ProjectName is required"); } var category = FindCategory(choices.Category); if (category == null) { throw new Exception("Category '{0}' is unknown".ToFormat(choices.Category)); } var project = category.FindTemplate(choices.ProjectType); if (project == null) { throw new Exception("ProjectTemplate '{0}' for category {1} is unknown".ToFormat(choices.ProjectType, choices.Category)); } return(project.BuildProjectRequest(choices)); }
public ProjectRequest BuildProjectRequest(TemplateChoices choices) { if (choices.Category.IsEmpty()) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "Category is required"); } if (choices.ProjectName.IsEmpty()) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "ProjectName is required"); } var category = FindCategory(choices.Category); if (category == null) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "Category '{0}' is unknown".ToFormat(choices.Category)); } var project = category.FindTemplate(choices.ProjectType); if (project == null) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "ProjectTemplate '{0}' for category {1} is unknown".ToFormat(choices.ProjectType, choices.Category)); } return(project.BuildProjectRequest(choices)); }
public void Configure(TemplateChoices choices, ProjectRequest request) { choices.Options = choices.Options ?? new string[0]; var option = choices.Selections.Has(Name) ? FindOption(choices.Selections[Name]) : Options.FirstOrDefault(x => choices.Options.Any(o => o.EqualsIgnoreCase(x.Name))) ?? Options.First(); request.Alterations.AddRange(option.Alterations); }
public void SetUp() { theSelection = new OptionSelection{Name = "select"}; theSelection.Options.Add(new Option("a", "a1", "a2")); theSelection.Options.Add(new Option("b", "b1", "b2")); theSelection.Options.Add(new Option("c", "c1", "c2")); theChoices = new TemplateChoices(); theRequest = new ProjectRequest("MyFoo", "baseline"); }
public void Configure(TemplateChoices choices, ProjectRequest request) { choices.Options = choices.Options ?? new string[0]; var option = choices.Selections.Has(Name) ? FindOption(choices.Selections[Name]) : Options.FirstOrDefault(x => choices.Options.Any(o => o.EqualsIgnoreCase(x.Name))) ?? Options.First(); request.Alterations.AddRange(option.Alterations); }
// Query/Command separation violation, but hey, it works private bool tryResolveSelection(string optionName, TemplateChoices choices) { var selection = (Selections ?? new OptionSelection[0]) .FirstOrDefault(x => x.FindOption(optionName) != null); if (selection == null) { return(false); } choices.Selections[selection.Name] = optionName; return(true); }
public ProjectRequest BuildProjectRequest(TemplateChoices choices) { if (choices.Category.IsEmpty()) throw new Exception("Category is required"); if (choices.ProjectName.IsEmpty()) throw new Exception("ProjectName is required"); var category = FindCategory(choices.Category); if (category == null) { throw new Exception("Category '{0}' is unknown".ToFormat(choices.Category)); } var project = category.FindTemplate(choices.ProjectType); if (project == null) { throw new Exception("ProjectTemplate '{0}' for category {1} is unknown".ToFormat(choices.ProjectType, choices.Category)); } return project.BuildProjectRequest(choices); }
public ProjectRequest BuildProjectRequest(TemplateChoices choices) { var request = new ProjectRequest(choices.ProjectName, Template); request.Alterations.AddRange(Alterations); if (DotNetVersion.IsNotEmpty()) { request.Version = DotNetVersion; } if (choices.Options != null) { choices.Options.Each(o => { var opt = FindOption(o); if (opt == null) { if (!tryResolveSelection(o, choices)) { if (opt == null) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "Unknown option '{0}' for project type {1}".ToFormat(o, Name)); } } } else { request.Alterations.AddRange(opt.Alterations); } }); } if (Selections != null) { Selections.Each(selection => selection.Configure(choices, request)); } choices.Inputs.Each((key, value) => request.Substitutions.Set(key, value)); return(request); }
public void build_request_copies_inputs_around_substitutions() { var graph = new TemplateGraph(); graph.AddCategory("new").Templates.Add(new ProjectTemplate { Name = "Foo", Template = "FooProj", Alterations = new List<string> { "Foo1", "Foo2" } }); var choices = new TemplateChoices { Category = "new", ProjectType = "Foo", ProjectName = "MyFoo" }; choices.Inputs["Foo1"] = "A"; choices.Inputs["Foo2"] = "B"; choices.Inputs["Foo3"] = "C"; ProjectRequest request = graph.BuildProjectRequest(choices); request.Substitutions.ValueFor("Foo1").ShouldEqual("A"); request.Substitutions.ValueFor("Foo2").ShouldEqual("B"); request.Substitutions.ValueFor("Foo3").ShouldEqual("C"); }
public ProjectRequest BuildProjectRequest(TemplateChoices choices) { var request = new ProjectRequest(choices.ProjectName, Template); request.Alterations.AddRange(Alterations); if (DotNetVersion.IsNotEmpty()) { request.Version = DotNetVersion; } if (choices.Options != null) { choices.Options.Each(o => { var opt = FindOption(o); if (opt == null) { if (!tryResolveSelection(o, choices)) { if (opt == null) throw new Exception("Unknown option '{0}' for project type {1}".ToFormat(o, Name)); } } else { request.Alterations.AddRange(opt.Alterations); } }); } if (Selections != null) { Selections.Each(selection => selection.Configure(choices, request)); } choices.Inputs.Each((key, value) => request.Substitutions.Set(key, value)); return request; }
public void build_request_with_default_value_for_a_selection() { var graph = new TemplateGraph(); var projectTemplate = new ProjectTemplate { Name = "Foo", Template = "FooProj", Alterations = new List<string> {"Foo1", "Foo2"}, DotNetVersion = DotNetVersion.V45 }; graph.AddCategory("new").Templates.Add(projectTemplate); projectTemplate.Selections.Add(new OptionSelection { Name = "FooSelection", Options = new List<Option> { new Option { Name = "FooOpt1", Alterations = new List<string> {"C", "D"} }, new Option { Name = "FooOpt2", Alterations = new List<string> {"E", "F"} } } }); var choices = new TemplateChoices {Category = "new", ProjectType = "Foo", ProjectName = "MyFoo"}; ProjectRequest request = graph.BuildProjectRequest(choices); request.Version.ShouldEqual(DotNetVersion.V45); request.Alterations.ShouldHaveTheSameElementsAs("Foo1", "Foo2", "C", "D"); }
public void build_request_with_named_value_for_a_selection() { var graph = new TemplateGraph(); var templateSet = new ProjectTemplate { Name = "Foo", Template = "FooProj", Alterations = new List<string> { "Foo1", "Foo2" } }; graph.AddCategory("new").Templates.Add(templateSet); templateSet.Selections.Add(new OptionSelection { Name = "FooSelection", Options = new List<Option> { new Option { Name = "FooOpt1", Alterations = new List<string> {"C", "D"} }, new Option { Name = "FooOpt2", Alterations = new List<string> {"E", "F"} } } }); var choices = new TemplateChoices { Category = "new", ProjectType = "Foo", ProjectName = "MyFoo" }; choices.Selections["FooSelection"] = "fooopt2"; ProjectRequest request = graph.BuildProjectRequest(choices); request.Alterations.ShouldHaveTheSameElementsAs("Foo1", "Foo2", "E", "F"); }
public void build_request_with_matching_template_and_options() { var graph = new TemplateGraph(); graph.AddCategory("new").Templates.Add(new ProjectTemplate { Name = "Foo", Template = "FooProj", Alterations = new List<string> {"Foo1", "Foo2"} }); var choices = new TemplateChoices {Category = "new", ProjectType = "Foo", ProjectName = "MyFoo"}; ProjectRequest request = graph.BuildProjectRequest(choices); request.Name.ShouldEqual(choices.ProjectName); request.Template.ShouldEqual("FooProj"); request.Alterations.ShouldHaveTheSameElementsAs("Foo1", "Foo2"); }
public void build_request_with_options() { var graph = new TemplateGraph(); var templateSet = new ProjectTemplate { Name = "Foo", Template = "FooProj", Alterations = new List<string> {"Foo1", "Foo2"} }; graph.AddCategory("new").Templates.Add(templateSet); templateSet.Options.Add(new Option { Name = "FooOpt1", Alterations = new List<string> {"C", "D"} }); templateSet.Options.Add(new Option { Name = "FooOpt2", Alterations = new List<string> {"E", "F"} }); templateSet.Options.Add(new Option { Name = "FooOpt3", Alterations = new List<string> {"G", "H"} }); var choices = new TemplateChoices { Category = "new", ProjectType = "Foo", ProjectName = "MyFoo", Options = new[] {"FooOpt1", "FooOpt3"} }; ProjectRequest request = graph.BuildProjectRequest(choices); request.Alterations.ShouldHaveTheSameElementsAs("Foo1", "Foo2", "C", "D", "G", "H"); }
// Query/Command separation violation, but hey, it works private bool tryResolveSelection(string optionName, TemplateChoices choices) { var selection = (Selections ?? new OptionSelection[0]) .FirstOrDefault(x => x.FindOption(optionName) != null); if (selection == null) return false; choices.Selections[selection.Name] = optionName; return true; }