Replace magic string with constants

This commit is contained in:
Chris Cheetham 2020-08-17 15:46:33 -04:00
Родитель a0fae304a6
Коммит 1f184fcc17
58 изменённых файлов: 146 добавлений и 80 удалений

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

@ -0,0 +1,11 @@
namespace Steeltoe.Initializr.TemplateEngine
{
public static class Constants
{
public const string Steeltoe24 = "2.4.4";
public const string Steeltoe30 = "3.0.0-rc1";
public const string NetCoreApp21 = "netcoreapp2.1";
public const string NetCoreApp31 = "netcoreapp3.1";
public const string WebApi = "webapi";
}
}

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

@ -27,7 +27,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Models
[ProjectNameValidation]
public string ProjectName
{
get => _projectName ?? "steeltoeProject";
get => _projectName ?? "SteeltoeProject";
set => _projectName = value;
}
@ -35,11 +35,11 @@ namespace Steeltoe.Initializr.TemplateEngine.Models
public string ArchiveName => ProjectName + ".zip";
public string SteeltoeVersion { get; set; }
public string SteeltoeVersion { get; set; } = Constants.Steeltoe24;
public string TargetFramework { get; set; } = "netcoreapp3.1";
public string TargetFramework { get; set; } = Constants.NetCoreApp31;
public string Template { get; set; }
public string Template { get; set; } = Constants.WebApi;
public string Dependencies { get; set; }

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

@ -24,6 +24,6 @@ namespace Steeltoe.Initializr.TemplateEngine.Services
Task<byte[]> GenerateProjectArchiveAsync(GeneratorModel model);
List<ProjectDependency> GetDependencies(string framework, string template);
List<ProjectDependency> GetDependencies(string steeltoe, string framework, string template);
}
}

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

@ -174,15 +174,22 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
private void LoadConfig(string templatePath)
{
var frameworks = new[] {"netcoreapp2.1", "netcoreapp3.1"};
foreach (var framework in frameworks)
var configs = new[]
{
var path = Path.Join(templatePath, "2.4", framework);
new[] {Constants.Steeltoe24, Constants.NetCoreApp21},
new[] {Constants.Steeltoe24, Constants.NetCoreApp31},
new[] {Constants.Steeltoe30, Constants.NetCoreApp31},
};
foreach (var config in configs)
{
var steeltoe = config[0];
var framework = config[1];
var path = Path.Join(templatePath, steeltoe, framework);
foreach (var dir in new DirectoryInfo(path).EnumerateDirectories())
{
var template = dir.Name;
var mustacheTemplateSetting = new MustacheTemplateSettings(_logger, dir.FullName);
_templateSettings.Add(new TemplateKey(framework, template), mustacheTemplateSetting);
_templateSettings.Add(new TemplateKey(steeltoe, framework, template), mustacheTemplateSetting);
}
}
}

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

@ -64,7 +64,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
public async Task<List<KeyValuePair<string, string>>> GenerateProjectFiles(GeneratorModel model)
{
var templateKey = new TemplateKey(model.TargetFramework, model.Template);
var templateKey = new TemplateKey(model.SteeltoeVersion, model.TargetFramework, model.Template);
if (!_mustacheConfig.GetTemplateKeys().Contains(templateKey))
{
throw new InvalidDataException($"Template with Name[{model.Template}] and Framework[{model.TargetFramework}] doesn't exist");
@ -112,7 +112,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
.ToList();
}
public List<ProjectDependency> GetDependencies(string framework, string template)
public List<ProjectDependency> GetDependencies(string steeltoe, string framework, string template)
{
var list = GetAvailableTemplates();
var selectedTemplate = list.FirstOrDefault(x => x.ShortName == template);
@ -123,7 +123,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
}
// var templatePath = _templatePath + Path.DirectorySeparatorChar + selectedTemplate.Name;
var config = _mustacheConfig.GetSchema(new TemplateKey(framework, selectedTemplate.Name));
var config = _mustacheConfig.GetSchema(new TemplateKey(Constants.Steeltoe24, framework, selectedTemplate.Name));
return config.Params
.Where(p => p.Description.ToLower().Contains("steeltoe"))
.Select(p => new ProjectDependency

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

@ -18,24 +18,32 @@ namespace Steeltoe.Initializr.TemplateEngine.Services.Mustache
{
public class TemplateKey
{
public string Steeltoe { get; }
public string Template { get; }
public string Framework { get; }
public TemplateKey(string framework, string template)
public TemplateKey(string steeltoe, string framework, string template)
{
Steeltoe = steeltoe;
Framework = framework;
Template = template;
}
public override int GetHashCode()
{
return Template.GetHashCode() ^ Framework.GetHashCode();
return Steeltoe.GetHashCode() ^ Template.GetHashCode() ^ Framework.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is TemplateKey key && (Template.Equals(key.Template) && Framework.Equals(key.Framework));
return obj is TemplateKey key && Steeltoe.Equals(key.Steeltoe) && Template.Equals(key.Template) && Framework.Equals(key.Framework);
}
public override string ToString()
{
return $"TemplateKey[{Steeltoe},{Framework},{Template}]";
}
}
}

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

@ -74,7 +74,7 @@ Get project:
private string GetDependencies()
{
var result = new StringBuilder();
var dependencies = _templateService.GetDependencies("netcoreapp3.1", string.Empty);
var dependencies = _templateService.GetDependencies("2.4.4", "netcoreapp3.1", string.Empty);
var fieldWidths = new int[] { 40, 100 };
result.Append("\nDependencies: \n");

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

@ -57,7 +57,7 @@ namespace Steeltoe.Initializr.WebApp.Controllers
[FromQuery(Name = "dotNetTemplate")] string template,
[FromQuery(Name = "dotNetFramework")] string framework = "netcoreapp3.1")
{
return Ok(_sttemplateService.GetDependencies(framework, template));
return Ok(_sttemplateService.GetDependencies("2.4.4", framework, template));
}
[Route("templates")]

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

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Authorization;
{{/Auth}}
using Microsoft.AspNetCore.Mvc;
{{#SQLServer}}
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using System.Data;
{{/SQLServer}}
{{#MySql}}
@ -61,7 +61,7 @@ namespace {{ProjectNameSpace}}.Controllers
public ActionResult<IEnumerable<string>> Get()
{
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema("Tables");
_dbConnection.Close();
@ -85,7 +85,7 @@ namespace {{ProjectNameSpace}}.Controllers
public ActionResult<IEnumerable<string>> Get()
{
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema("Tables");
_dbConnection.Close();
@ -164,7 +164,7 @@ namespace {{ProjectNameSpace}}.Controllers
_logger = logger;
_factory = factory;
}
// GET api/values
[HttpGet]
public ActionResult<string> Get()
@ -208,7 +208,7 @@ namespace {{ProjectNameSpace}}.Controllers
{
_config = config;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
@ -272,7 +272,7 @@ namespace {{ProjectNameSpace}}.Controllers
_appOptions = appOptions.Value;
_serviceOptions = serviceOptions.Value;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()

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

@ -8,6 +8,9 @@ using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
{{#AzureSpringCloud}}
using Microsoft.Azure.SpringCloud.Client;
{{/AzureSpringCloud}}
{{#ActuatorsOrDynamicLogger}}
using Steeltoe.Extensions.Logging;
{{/ActuatorsOrDynamicLogger}}
@ -52,7 +55,7 @@ namespace {{ProjectNameSpace}}
{{/ConfigServer}}
{{/CloudFoundry}}
{{#ConfigServer}}
.AddConfigServer()
.AddConfigServer()
{{/ConfigServer}}
{{#PlaceholderConfig}}
.AddPlaceholderResolver()
@ -60,6 +63,9 @@ namespace {{ProjectNameSpace}}
{{#RandomValueConfig}}
.ConfigureAppConfiguration((b) => b.AddRandomValueSource())
{{/RandomValueConfig}}
{{#AzureSpringCloud}}
.UseAzureSpringCloudService()
{{/AzureSpringCloud}}
.UseStartup<Startup>();
{{#ActuatorsOrDynamicLogger}}
builder.ConfigureLogging((hostingContext, loggingBuilder) =>

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

@ -21,6 +21,9 @@
{{#RequiresHttps}}
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="{{AspNetCoreVersion}}" />
{{/RequiresHttps}}
{{#AzureSpringCloud}}
<PackageReference Include="Microsoft.Azure.SpringCloud.Client" Version="1.0.0-alpha.1" />
{{/AzureSpringCloud}}
{{#CloudFoundry}}
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="{{SteeltoeVersion}}" />
{{/CloudFoundry}}
@ -52,6 +55,7 @@
{{/Discovery}}
{{#SQLServer}}
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="{{AspNetCoreVersion}}" />
<PackageReference Include="Steeltoe.CloudFoundry.Connector.EFCore" Version="{{SteeltoeVersion}}" />
{{/SQLServer}}
{{#Redis}}
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.0" />

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

@ -32,53 +32,57 @@
"DefaultValue": false,
"Description": "Steeltoe: Add Dynamic Logger"
},
{
"Name": "AzureSpringCloud",
"DefaultValue": false,
"Description": "Steeltoe: Target Microsoft Azure Spring Cloud Hosting",
"FriendlyName": "Microsoft Azure Spring Cloud"
},
{
"Name": "MongoDB",
"DefaultValue": false,
"Description": "Steeltoe: Add MongoDB connectors"
"Description": "Steeltoe: Add MongoDB connnectors"
},
{
"Name": "MySql",
"DefaultValue": "false",
"Description": "Steeltoe: Add MySql connectors"
"Description": "Steeltoe: Add MySql connnectors"
},
{
"Name": "MySqlEFCore",
"DefaultValue": false,
"Description": "Steeltoe: Add MySQL EFCore connectors",
"friendlyName": "MySql EFCore"
"Description": "Steeltoe: Add MySQL EFCore connnectors"
},
{
"Name": "OAuthConnector",
"DefaultValue": false,
"Description": "Steeltoe: Add OAuth connectors"
"Description": "Steeltoe: Add OAuth connnectors"
},
{
"Name": "Postgres",
"DefaultValue": false,
"Description": "Steeltoe: Add Postgres connectors"
"Description": "Steeltoe: Add Postgres connnectors"
},
{
"Name": "PostgresEFCore",
"DefaultValue": false,
"Description": "Steeltoe: Add Postgres EFCore connectors",
"friendlyName": "Postgres EFCore"
"Description": "Steeltoe: Add PostgresEFCore connnectors"
},
{
"Name": "RabbitMQ",
"DefaultValue": false,
"Description": "Steeltoe: Add RabbitMQ connectors"
"Description": "Steeltoe: Add RabbitMQ connnectors"
},
{
"Name": "Redis",
"DefaultValue": false,
"Description": "Steeltoe: Add Redis connectors"
"Description": "Steeltoe: Add Redis connnectors"
},
{
"Name": "SQLServerEFCore",
"Name": "SQLServer",
"DefaultValue": false,
"Description": "Steeltoe: Add Microsoft SQL Server connectors",
"friendlyName": "SQL Server EFCore"
"Description": "Steeltoe: Add Microsoft SQL Server connnectors",
"friendlyName": "SQL Server"
},
{
"Name": "ProjectNameSpace",
@ -188,10 +192,6 @@
"DefaultValue": "netcoreapp3.1",
"Description": "Target the appropriate Dotnet framework version",
"choices": [
{
"Choice": "netcoreapp3.0",
"Description": "Target Dotnet framework version 3.0"
},
{
"Choice": "netcoreapp3.1",
"Description": "Target Dotnet framework version 3.1"

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

@ -61,7 +61,7 @@ namespace {{ProjectNameSpace}}.Controllers
public ActionResult<IEnumerable<string>> Get()
{
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema("Tables");
_dbConnection.Close();
@ -85,7 +85,7 @@ namespace {{ProjectNameSpace}}.Controllers
public ActionResult<IEnumerable<string>> Get()
{
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema("Tables");
_dbConnection.Close();
@ -164,7 +164,7 @@ namespace {{ProjectNameSpace}}.Controllers
_logger = logger;
_factory = factory;
}
// GET api/values
[HttpGet]
public ActionResult<string> Get()
@ -208,7 +208,7 @@ namespace {{ProjectNameSpace}}.Controllers
{
_config = config;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
@ -272,7 +272,7 @@ namespace {{ProjectNameSpace}}.Controllers
_appOptions = appOptions.Value;
_serviceOptions = serviceOptions.Value;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()

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

@ -0,0 +1,31 @@
using Xunit;
namespace Steeltoe.Initializr.TemplateEngine.Test
{
public class ConstantsTests
{
[Fact]
public void Steeltoe24()
{
Assert.Equal("2.4.4", Constants.Steeltoe24);
}
[Fact]
public void Steeltoe30()
{
Assert.Equal("3.0.0-rc1", Constants.Steeltoe30);
}
[Fact]
public void NetCoreApp21()
{
Assert.Equal("netcoreapp2.1", Constants.NetCoreApp21);
}
[Fact]
public void NetCoreApp31()
{
Assert.Equal("netcoreapp3.1", Constants.NetCoreApp31);
}
}
}

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

@ -42,35 +42,34 @@ namespace Steeltoe.Initializr.TemplateEngine.Test.IntegrationTests
[Fact]
public void Scratch()
{
var argsList = GetArgs(typeof(MustacheTemplateService), "netcoreapp3.1", "Steeltoe-WebApi");
var argsList = GetArgs(typeof(MustacheTemplateService), Constants.Steeltoe30, Constants.NetCoreApp31, "webapi");
foreach (var args in argsList)
{
_testOutputHelper.WriteLine($"dotnet {args[1]}, template {args[2]}, dependency {args[3]}");
_testOutputHelper.WriteLine($"steeltoe {args[1]}, dotnet {args[2]}, template {args[3]}, dependency {args[4]}");
}
}
[Theory]
[MemberData(nameof(GetArgs), typeof(MustacheTemplateService), "netcoreapp2.1", "Steeltoe-WebApi")]
public async Task Steeltoe24Dotnet21(ITemplateService service, string framework, string template, string dependency)
[MemberData(nameof(GetArgs), typeof(MustacheTemplateService), Constants.Steeltoe24, Constants.NetCoreApp21, "webapi")]
public async Task Steeltoe24Dotnet21(ITemplateService service, string steeltoe, string framework, string template, string dependency)
{
await GenerateAndBuildProject(service, framework, template, dependency);
await GenerateAndBuildProject(service, steeltoe, framework, template, dependency);
}
[Theory]
[MemberData(nameof(GetArgs), typeof(MustacheTemplateService), "netcoreapp3.1", "Steeltoe-WebApi")]
public async Task Steeltoe24Dotnet31(ITemplateService service, string framework,
string template, string dependency)
[MemberData(nameof(GetArgs), typeof(MustacheTemplateService), Constants.Steeltoe24, Constants.NetCoreApp31, "webapi")]
public async Task Steeltoe24Dotnet31(ITemplateService service, string steeltoe, string framework, string template, string dependency)
{
await GenerateAndBuildProject(service, framework, template, dependency);
await GenerateAndBuildProject(service, steeltoe, framework, template, dependency);
}
// [Theory]
// [MemberData(nameof(GetArgs), typeof(MustacheTemplateService), "netcoreapp3.1", "Steeltoe-WebApi")]
// public async Task Steeltoe30Dotnet31(ITemplateService service, string framework,
// string template, string dependency)
// {
// await GenerateAndBuildProject(service, framework, template, dependency);
// }
[Theory]
[MemberData(nameof(GetArgs), typeof(MustacheTemplateService), Constants.Steeltoe30, Constants.NetCoreApp31, "webapi")]
public async Task Steeltoe30Dotnet31(ITemplateService service, string steeltoe, string framework, string template, string dependency)
{
await GenerateAndBuildProject(service, steeltoe, framework, template, dependency);
}
// TODO: test Steeltoe 3
@ -78,10 +77,10 @@ namespace Steeltoe.Initializr.TemplateEngine.Test.IntegrationTests
private async Task GenerateAndBuildProject(
ITemplateService service,
string steeltoe,
string framework,
string template,
string dependency,
string steeltoe = "2.4.4")
string dependency)
{
_testOutputHelper.WriteLine(
$"Generating and Building: Steeltoe {steeltoe}, Framework {framework}, Template {template}, dependency {dependency}");
@ -96,8 +95,8 @@ namespace Steeltoe.Initializr.TemplateEngine.Test.IntegrationTests
var zip = new ZipArchive(new MemoryStream(archive));
var dirName = Path.GetTempPath() + Path.DirectorySeparatorChar + Guid.NewGuid();
_testOutputHelper.WriteLine($"Project directory: {dirName}");
zip.ExtractToDirectory(dirName);
var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
@ -114,12 +113,11 @@ namespace Steeltoe.Initializr.TemplateEngine.Test.IntegrationTests
$"Error compiling {dependency}. \n {output}");
}
public static IEnumerable<object[]> GetArgs(Type templateServiceType, string framework, string template)
public static IEnumerable<object[]> GetArgs(Type templateServiceType, string steeltoe, string framework, string template)
{
var service = BuildTemplateService(templateServiceType);
var deps = service.GetDependencies(framework, template).Select(dep => dep.ShortName.ToLower());
return from dep in deps select new object[] {service, framework, template, dep};
var deps = service.GetDependencies(steeltoe, framework, template).Select(dep => dep.ShortName.ToLower());
return from dep in deps select new object[] {service, steeltoe, framework, template, dep};
}
static ITemplateService BuildTemplateService(Type type)

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

@ -39,7 +39,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
[ClassData(typeof(AllImplementationsAndTemplates))]
public void GetDependencies(ITemplateService templateService, string templateName, string framework)
{
var deps = templateService.GetDependencies(framework, templateName);
var deps = templateService.GetDependencies(Constants.Steeltoe24, framework, templateName);
Assert.NotNull(deps);
Assert.NotEmpty(deps);
@ -50,7 +50,7 @@ namespace Steeltoe.Initializr.TemplateEngine.Test
[ClassData(typeof(AllImplementationsAndTemplates))]
public void GetDependencies_WithFriendlyNames(ITemplateService templateService, string templateName, string framework)
{
var deps = templateService.GetDependencies(framework, templateName);
var deps = templateService.GetDependencies(Constants.Steeltoe24, framework, templateName);
Assert.NotNull(deps);
Assert.NotEmpty(deps);
@ -501,7 +501,7 @@ using System.Threading;", valuesController);
[ClassData(typeof(AllImplementationsAndTemplates))]
public async Task CreateTemplate_SqlServer(ITemplateService templateService, string templateName, string framework)
{
var steeltoeVersion = "2.4.4";
var steeltoeVersion = Constants.Steeltoe24;
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
@ -597,7 +597,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
SteeltoeVersion = Constants.Steeltoe24,
Template = templateName,
TargetFramework = framework,
});
@ -614,7 +614,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
SteeltoeVersion = Constants.Steeltoe24,
Template = templateName,
TargetFramework = framework,
});
@ -631,7 +631,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.4",
SteeltoeVersion = Constants.Steeltoe24,
Template = templateName,
TargetFramework = framework,
});
@ -650,7 +650,7 @@ using System.Threading;", valuesController);
var files = await templateService.GenerateProjectFiles(new GeneratorModel()
{
Dependencies = "Actuators",
SteeltoeVersion = "2.4.2",
SteeltoeVersion = Constants.Steeltoe24,
Template = templateName,
TargetFramework = "netcoreapp3.1",
});

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

@ -24,6 +24,7 @@ using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using Steeltoe.Initializr.TemplateEngine;
using Xunit;
using Xunit.Abstractions;
@ -53,7 +54,7 @@ namespace Steeltoe.Initializr.WebApp.Test
[Fact]
public async void GetStarterZipTest()
{
var result = await _client.GetAsync("https://localhost/starter.zip?ProjectName=TestCompany.TestProject&Dependencies=Actuator,MySql&Description=Test%20Description&SteeltoeVersion=2.4.4&TargetFramework=netcoreapp2.1&TemplateShortName=Steeltoe-WebApi");
var result = await _client.GetAsync("https://localhost/starter.zip?ProjectName=TestCompany.TestProject&Dependencies=Actuator,MySql&Description=Test%20Description&SteeltoeVersion=2.4.4&DotNetFramework=netcoreapp2.1&Template=webapi");
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
@ -119,9 +120,9 @@ namespace Steeltoe.Initializr.WebApp.Test
Dependencies = "Actuator,MySql",
Description = "TestDescription",
ProjectName = "TestCompany.TestProject",
SteeltoeVersion = "2.4.4",
TargetFramework = "netcoreapp2.1",
Template = "Steeltoe-WebApi",
SteeltoeVersion = Constants.Steeltoe24,
TargetFramework = Constants.NetCoreApp21,
Template = Constants.WebApi,
};
var props = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);