Rethink dotnet framework var usage

This commit is contained in:
Chris Cheetham 2020-08-15 08:12:13 -04:00
Родитель 3c2b229c38
Коммит 69cd5896df
11 изменённых файлов: 60 добавлений и 52 удалений

Просмотреть файл

@ -33,7 +33,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Models
set => _projectName = value;
}
public string TemplateShortName { get; set; }
public string Template { get; set; }
public string Description { get; set; }
@ -43,8 +43,6 @@ namespace Steeltoe.Initializr.TemplateEngine.Models
public string TargetFramework { get; set; } = "netcoreapp3.1";
public DotnetFramework TargetFrameworkEnum => Enum.Parse<DotnetFramework>(TargetFramework.Replace(".", ""), true);
public string[] GetDependencies()
{
return string.IsNullOrEmpty(Dependencies) ? null : Dependencies.ToLower().Split(',');

Просмотреть файл

@ -131,9 +131,9 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.DotNetTemplateEngine
iParams.Add("Framework", model.TargetFramework);
}
var templateShortName = string.IsNullOrEmpty(model.TemplateShortName) ? DEFAULT_TEMPLATE : model.TemplateShortName;
TemplateInfo templateInfo = FindTemplateByShortName(templateShortName, model.TargetFrameworkEnum, EnvSettings);
var templateShortName = string.IsNullOrEmpty(model.Template) ? DEFAULT_TEMPLATE : model.Template;
var framework = DotNetFrameworkParser.Parse(model.TargetFramework);
TemplateInfo templateInfo = FindTemplateByShortName(templateShortName, framework, EnvSettings);
if (templateInfo == null)
{
throw new Exception($"Could not find template with shortName: {templateShortName} ");

Просмотреть файл

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
namespace Steeltoe.Initializr.TemplateEngine.Services
{
public enum DotnetFramework
@ -19,4 +21,13 @@ namespace Steeltoe.Initializr.TemplateEngine.Services
NetCoreApp21,
NetCoreApp31,
}
public static class DotNetFrameworkParser
{
public static DotnetFramework Parse(string s)
{
return Enum.Parse<DotnetFramework>(s.Replace(".", string.Empty), true);
}
}
}

Просмотреть файл

@ -174,16 +174,16 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
private void LoadConfig(string templatePath)
{
var frameworks = new[] {"netcoreapp2.1", "netcoreapp3.1"};
foreach (var framework in frameworks)
var frameworkNames = new[] {"netcoreapp2.1", "netcoreapp3.1"};
foreach (var frameworkName in frameworkNames)
{
var path = Path.Join(templatePath, "2.4", framework);
var framework = DotNetFrameworkParser.Parse(frameworkName);
var path = Path.Join(templatePath, "2.4", frameworkName);
foreach (var dir in new DirectoryInfo(path).EnumerateDirectories())
{
var dotnetTemplate = dir.Name;
var mustacheTemplateSetting = new MustacheTemplateSettings(_logger, dir.FullName);
var frameworkEnum = Enum.Parse<DotnetFramework>(framework.Replace(".", ""), true);
_templateSettings.Add(new TemplateKey(dir.Name, frameworkEnum), mustacheTemplateSetting);
_templateSettings.Add(new TemplateKey(dotnetTemplate, framework), mustacheTemplateSetting);
}
}
}

Просмотреть файл

@ -67,13 +67,12 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
public async Task<List<KeyValuePair<string, string>>> GenerateProjectFiles(GeneratorModel model)
{
var name = string.IsNullOrEmpty(model.TemplateShortName) ? DefaultTemplateName : model.TemplateShortName;
var templateKey = new TemplateKey(name, model.TargetFrameworkEnum);
var template = string.IsNullOrEmpty(model.Template) ? DefaultTemplateName : model.Template;
var framework = DotNetFrameworkParser.Parse(model.TargetFramework);
var templateKey = new TemplateKey(template, framework);
if (!_mustacheConfig.GetTemplateKeys().Contains(templateKey))
{
throw new InvalidDataException($"Template with Name[{name}] and Framework[{model.TargetFramework}] doesn't exist");
throw new InvalidDataException($"Template with Name[{template}] and Framework[{model.TargetFramework}] doesn't exist");
}
Dictionary<string, string> dataView;

Просмотреть файл

@ -22,10 +22,11 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
public DotnetFramework Version { get; }
public TemplateKey(string name, DotnetFramework version)
// public TemplateKey(DotnetFramework framework, string template)
public TemplateKey(string template, DotnetFramework framework)
{
Name = name;
Version = version;
Version = framework;
Name = template;
}
public override int GetHashCode()

Просмотреть файл

@ -27,7 +27,7 @@ export class DependenciesSelector extends Component {
return
}
this.state.currentFramework = this.props.framework
var depsPath = '/api/templates/dependencies?framework=' + this.state.currentFramework
var depsPath = '/api/templates/dependencies?dotNetFramework=' + this.state.currentFramework
fetch(depsPath)
.then(response => response.json())
.then(data => {

Просмотреть файл

@ -54,11 +54,10 @@ namespace Steeltoe.Initializr.WebApp.Controllers
[Route("dependencies")]
public ActionResult GetDependencies(
[FromQuery(Name = "templateShortName")] string templateShortName,
[FromQuery(Name = "framework")] string framework = "netcoreapp3.1")
[FromQuery(Name = "dotNetTemplate")] string template,
[FromQuery(Name = "dotNetFramework")] string framework = "netcoreapp3.1")
{
var frameworkEnum = Enum.Parse<DotnetFramework>(framework.Replace(".", string.Empty), true);
return Ok(_sttemplateService.GetDependencies(templateShortName, frameworkEnum));
return Ok(_sttemplateService.GetDependencies(template, DotNetFrameworkParser.Parse(framework)));
}
[Route("templates")]

Просмотреть файл

@ -192,7 +192,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test.IntegrationTests
var archive = await service.GenerateProjectArchiveAsync(new GeneratorModel()
{
Dependencies = dependency,
TemplateShortName = template,
Template = template,
ProjectName = "Foo.Bar",
TargetFramework = framework,
SteeltoeVersion = steeltoe,

Просмотреть файл

@ -70,7 +70,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
{
Dependencies = "Actuators",
ProjectName = "testProject",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -92,7 +92,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
ProjectName = "testProject",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -107,7 +107,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Discovery",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -125,7 +125,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Actuators,CircuitBreaker",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -149,7 +149,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "MySql",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
string startUpContents = files.Find(x => x.Key == "Startup.cs").Value;
@ -186,7 +186,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "MySqlEFCore",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -210,7 +210,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Postgres",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -243,7 +243,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "ConfigServer",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -270,7 +270,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "RandomValueConfig",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -296,7 +296,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "CloudFoundry",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -329,7 +329,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "PlaceholderConfig",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -364,7 +364,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "PostgresEFCore",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -388,7 +388,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "RabbitMQ",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -425,7 +425,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Redis",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -455,7 +455,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "MongoDB",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -485,7 +485,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "OAuthConnector",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -511,7 +511,7 @@ using System.Threading;", valuesController);
{
Dependencies = "SQLServer",
ProjectName = "testProject",
TemplateShortName = templateName,
Template = templateName,
SteeltoeVersion = steeltoeVersion,
TargetFramework = framework,
});
@ -546,7 +546,7 @@ using System.Threading;", valuesController);
{
Dependencies = "DynamicLogger",
ProjectName = "testProject",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -576,7 +576,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "CloudFoundry",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
string programFileContents = files.Find(x => x.Key == "Program.cs").Value;
@ -602,7 +602,7 @@ using System.Threading;", valuesController);
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -619,7 +619,7 @@ using System.Threading;", valuesController);
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -636,7 +636,7 @@ using System.Threading;", valuesController);
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = framework,
});
@ -655,7 +655,7 @@ using System.Threading;", valuesController);
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.2",
TemplateShortName = templateName,
Template = templateName,
TargetFramework = "netcoreapp3.1",
});
});
@ -669,7 +669,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
TemplateShortName = templateName,
Template = templateName,
ProjectName = "Foo.Bar",
TargetFramework = framework,
});

Просмотреть файл

@ -96,7 +96,7 @@ namespace Steeltoe.Initializr.WebApp.Test
ProjectName = "123.TestProject",
SteeltoeVersion = "2.4.4",
TargetFramework = "netcoreapp2.1",
TemplateShortName = "Steeltoe-WebApi",
Template = "Steeltoe-WebApi",
};
var props = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
@ -121,7 +121,7 @@ namespace Steeltoe.Initializr.WebApp.Test
ProjectName = "TestCompany.TestProject",
SteeltoeVersion = "2.4.4",
TargetFramework = "netcoreapp2.1",
TemplateShortName = "Steeltoe-WebApi",
Template = "Steeltoe-WebApi",
};
var props = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);