public static void SetIdentityOptions(string solutionDirectory, AuthSetup setup) { var classPath = ClassPathHelper.IdentityProjectPath(solutionDirectory, $"ServiceExtensions.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"options.User.RequireUniqueEmail")) { newText = @$ " options.User.RequireUniqueEmail = {setup.IdentityRequirements.RequireUniqueEmail.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequiredLength")) { newText = @$ " options.Password.RequiredLength = {setup.IdentityRequirements.RequiredLength};"; } else if (line.Contains($"options.Password.RequireDigit")) { newText = @$ " options.Password.RequireDigit = {setup.IdentityRequirements.RequireDigit.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireLowercase")) { newText = @$ " options.Password.RequireLowercase = {setup.IdentityRequirements.RequireLowercase.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireUppercase")) { newText = @$ " options.Password.RequireUppercase = {setup.IdentityRequirements.RequireUppercase.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireNonAlphanumeric")) { newText = @$ " options.Password.RequireNonAlphanumeric = {setup.IdentityRequirements.RequireNonAlphanumeric.ToString().ToLower()};"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
private static void UpdateDtoFile(string solutionDirectory, string entityName, List <EntityProperty> props, Dto dto) { var dtoFileName = $"{Utilities.GetDtoName(entityName, dto)}.cs"; var classPath = ClassPathHelper.DtoClassPath(solutionDirectory, dtoFileName, entityName); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; var fkUsingStatements = ""; if (dto == Dto.Read) { foreach (var prop in props) { if (prop.IsForeignKey) { fkUsingStatements += DtoFileTextGenerator.GetForeignKeyUsingStatements(classPath, fkUsingStatements, prop, dto); } } } while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"add-on property marker")) { newText += @$ "{Environment.NewLine}{Environment.NewLine}{DtoFileTextGenerator.DtoPropBuilder(props, dto)}"; } if (line.Contains("using System;")) { newText += fkUsingStatements; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public static void RegisterSwaggerInStartup(string solutionDirectory, ApiEnvironment env) { try { var classPath = ClassPathHelper.StartupClassPath(solutionDirectory, $"{Utilities.GetStartupName(env.EnvironmentName)}.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains("#region Dynamic Services")) { newText += $"{Environment.NewLine} services.AddSwaggerExtension();"; } else if (line.Contains("#region Dynamic App")) { newText += $"{Environment.NewLine} app.UseSwaggerExtension();"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); } catch (FileAlreadyExistsException e) { WriteError(e.Message); throw; } catch (Exception e) { WriteError($"An unhandled exception occurred when running the API command.\nThe error details are: \n{e.Message}"); throw; } }
public static void AddPolicies(string solutionDirectory, List <Policy> policies) { var classPath = ClassPathHelper.InfrastructureIdentityProjectRootClassPath(solutionDirectory, $"ServiceRegistration.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { Directory.CreateDirectory(classPath.ClassDirectory); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var policiesString = ""; foreach (var policy in policies) { policiesString += $@"{Environment.NewLine}{Utilities.PolicyStringBuilder(policy)}"; } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; bool updateNextLine = false; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"AddAuthorization")) { updateNextLine = true; } else if (updateNextLine) { newText += policiesString; updateNextLine = false; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
private static void AddSwaggerServiceExtension(string solutionDirectory, ApiTemplate template) { try { var classPath = ClassPathHelper.WebApiExtensionsClassPath(solutionDirectory, $"ServiceExtensions.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains("#region Swagger Region")) { newText += GetSwaggerServiceExtensionText(template); } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); } catch (FileAlreadyExistsException e) { WriteError(e.Message); throw; } catch (Exception e) { WriteError($"An unhandled exception occurred when running the API command.\nThe error details are: \n{e.Message}"); throw; } }
private static void RegisterAllSeeders(string solutionDirectory, ApiTemplate template) { //TODO move these to a dictionary to lookup and overwrite if I want var repoTopPath = "WebApi"; var entityDir = Path.Combine(solutionDirectory, repoTopPath); if (!Directory.Exists(entityDir)) { throw new DirectoryNotFoundException($"The `{entityDir}` directory could not be found."); } var pathString = Path.Combine(entityDir, $"StartupDevelopment.cs"); if (!File.Exists(pathString)) { throw new FileNotFoundException($"The `{pathString}` file could not be found."); } var tempPath = $"{pathString}temp"; using (var input = File.OpenText(pathString)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains("#region Entity Context Region")) { newText += @$ "{Environment.NewLine}{GetSeederContextText(template)}"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(pathString); File.Move(tempPath, pathString); GlobalSingleton.AddUpdatedFile(pathString.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); //WriteWarning($"TODO Need a message for the update of Startup."); }
public static void UpdateWebApiCsProjSwaggerSettings(string solutionDirectory, string solutionName) { var classPath = ClassPathHelper.WebApiProjectClassPath(solutionDirectory); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"DocumentationFile")) { newText = @$ " <DocumentationFile>{solutionName}.WebApi.xml</DocumentationFile>"; } else if (line.Contains($"NoWarn")) { newText = newText.Replace("</NoWarn>", "1591;</NoWarn>"); } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public static void AddProfile(string solutionDirectory, ApiEnvironment env) { var classPath = ClassPathHelper.LaunchSettingsClassPath(solutionDirectory, $"launchsettings.json"); if (!Directory.Exists(classPath.ClassDirectory)) { Directory.CreateDirectory(classPath.ClassDirectory); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains(@$ "" "profiles" "")) { newText += GetProfileText(env); } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public static void AddEntityProperties(string solutionDirectory, string entityName, List <EntityProperty> props) { var classPath = ClassPathHelper.EntityClassPath(solutionDirectory, $"{entityName}.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"add-on property marker")) { newText += @$ "{Environment.NewLine}{Environment.NewLine}{EntityBuilder.EntityPropBuilder(props)}"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public static void AddDbSet(string solutionDirectory, List <Entity> entities, string dbContextName) { var classPath = ClassPathHelper.DbContextClassPath(solutionDirectory, $"{dbContextName}.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { Directory.CreateDirectory(classPath.ClassDirectory); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"#region DbSet Region")) { newText += @$ "{Environment.NewLine}{DbContextBuilder.GetDbSetText(entities)}"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public static void AddInclude(string solutionDirectory, string entityName, List <EntityProperty> props) { var classPath = ClassPathHelper.RepositoryClassPath(solutionDirectory, $"{Utilities.GetRepositoryName(entityName, false)}.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { Directory.CreateDirectory(classPath.ClassDirectory); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } foreach (var prop in props) { if (prop.IsForeignKey) { var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; var inlcudeText = $@"{Environment.NewLine} .Include({prop.Name.ToLower().Substring(0, 1)} => {prop.Name.ToLower().Substring(0, 1)}.{prop.Name})"; bool updateNextLine = false; // TODO: refactor this. it's... janky. the idea is that I have a comment to mark where // to add the include statements, but it needs to be added two lines after. // this process will continually decrement the lineTracker variable, the number // doesn't matter until you get to the marker, when it will be set to 1, so we can // decrement it until we get to the line that we want to add it to // // with all that said, it's really awful to follow and I need to refactor this with a more elegant method var lineTracker = 0; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains(@$ "var collection")) { newText += inlcudeText; } if (line.Contains(@$ "include marker")) { lineTracker = 2; updateNextLine = true; } if (updateNextLine && lineTracker == 1) { newText += inlcudeText; updateNextLine = false; } lineTracker--; output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); } } }