private async Task AddTemplateFiles(IdentityGeneratorTemplateModel templateModel) { var projectDir = Path.GetDirectoryName(_projectContext.ProjectFullPath); var templates = templateModel.FilesToGenerate.Where(t => t.IsTemplate); foreach (var template in templates) { var outputPath = Path.Combine(projectDir, template.OutputPath); if (template.ShouldOverWrite != OverWriteCondition.Never || !_fileSystem.FileExists(outputPath)) { // We never overwrite some files like _ViewImports.cshtml. _logger.LogMessage($"Adding template: {template.Name}", LogMessageLevel.Trace); await _codegeneratorActionService.AddFileFromTemplateAsync( outputPath, template.SourcePath, TemplateFolders, templateModel); } } if (!templateModel.IsUsingExistingDbContext) { _connectionStringsWriter.AddConnectionString( connectionStringName: $"{templateModel.DbContextClass}Connection", dataBaseName: templateModel.ApplicationName, useSQLite: templateModel.UseSQLite); } }
private async Task AddTemplateFiles(IdentityGeneratorTemplateModel templateModel) { var templates = IdentityGeneratorFilesConfig.GetTemplateFiles(templateModel); var projectDir = Path.GetDirectoryName(_projectContext.ProjectFullPath); foreach (var template in templates) { _logger.LogMessage($"Adding template: {template.Key}", LogMessageLevel.Trace); await _codegeneratorActionService.AddFileFromTemplateAsync( Path.Combine(projectDir, template.Value), template.Key, TemplateFolders, templateModel); } _connectionStringsWriter.AddConnectionString( connectionStringName: $"{templateModel.DbContextClass}Connection", dataBaseName: templateModel.ApplicationName, useSQLite: templateModel.UseSQLite); }
public EditSyntaxTreeResult EditStartupForNewContext(ModelType startUp, string dbContextTypeName, string dbContextNamespace, string dataBaseName, bool useSqlite) { Contract.Assert(startUp != null && startUp.TypeSymbol != null); Contract.Assert(!String.IsNullOrEmpty(dbContextTypeName)); Contract.Assert(!String.IsNullOrEmpty(dataBaseName)); var declarationReference = startUp.TypeSymbol.DeclaringSyntaxReferences.FirstOrDefault(); if (declarationReference != null) { var sourceTree = declarationReference.SyntaxTree; var rootNode = sourceTree.GetRoot(); var startUpClassNode = rootNode.FindNode(declarationReference.Span); var configServicesMethod = startUpClassNode.ChildNodes() .FirstOrDefault(n => n is MethodDeclarationSyntax && ((MethodDeclarationSyntax)n).Identifier.ToString() == "ConfigureServices") as MethodDeclarationSyntax; var configRootProperty = TryGetIConfigurationRootProperty(startUp.TypeSymbol); if (configServicesMethod != null && configRootProperty != null) { var servicesParam = configServicesMethod.ParameterList.Parameters .FirstOrDefault(p => p.Type.ToString() == "IServiceCollection") as ParameterSyntax; string textToAddAtEnd; var statementLeadingTrivia = configServicesMethod.Body.OpenBraceToken.LeadingTrivia.ToString() + " "; if (servicesParam != null) { _connectionStringsWriter.AddConnectionString(dbContextTypeName, dataBaseName, useSqlite: useSqlite); if (useSqlite) { textToAddAtEnd = statementLeadingTrivia + "{0}.AddDbContext<{1}>(options =>" + Environment.NewLine + statementLeadingTrivia + " options.UseSqlite({2}.GetConnectionString(\"{1}\")));" + Environment.NewLine; } else { textToAddAtEnd = statementLeadingTrivia + "{0}.AddDbContext<{1}>(options =>" + Environment.NewLine + statementLeadingTrivia + " options.UseSqlServer({2}.GetConnectionString(\"{1}\")));" + Environment.NewLine; } if (configServicesMethod.Body.Statements.Any()) { textToAddAtEnd = Environment.NewLine + textToAddAtEnd; } var expression = SyntaxFactory.ParseStatement(String.Format(textToAddAtEnd, servicesParam.Identifier, dbContextTypeName, configRootProperty.Name)); MethodDeclarationSyntax newConfigServicesMethod = configServicesMethod.AddBodyStatements(expression); var newRoot = rootNode.ReplaceNode(configServicesMethod, newConfigServicesMethod); var namespacesToAdd = new[] { "Microsoft.EntityFrameworkCore", "Microsoft.Extensions.DependencyInjection", dbContextNamespace }; foreach (var namespaceName in namespacesToAdd) { newRoot = RoslynCodeEditUtilities.AddUsingDirectiveIfNeeded(namespaceName, newRoot as CompilationUnitSyntax); } return(new EditSyntaxTreeResult() { Edited = true, OldTree = sourceTree, NewTree = sourceTree.WithRootAndOptions(newRoot, sourceTree.Options) }); } } } return(new EditSyntaxTreeResult() { Edited = false }); }