private bool ValidateStep(PocoWriteStep step) { if (String.IsNullOrWhiteSpace(step.OutputPath)) { Log.Warning("Invalid PocoWrite step: empty OutputPath"); return(false); } return(true); }
public void WritePocos(IEnumerable <Poco> pocos, PocoWriteStep settings) { if (settings == null) { throw new Exception("Cannot write POCOs: no settings found"); } if (pocos == null || !pocos.Any()) { throw new Exception("Cannot write POCOs: no POCOs created"); } if (settings.CleanFolder) { CleanDirectoryContent(settings.OutputPath); } foreach (var poco in pocos .Where(p => settings.PocosExclude == null || !settings.PocosExclude.IsMatch(p.Table.Schema, p.Table.Name))) { string indent = String.Empty; var builder = new StringBuilder(); //writing info builder.AppendLine(InfoText.GeneratedCodeText); builder.AppendLine(); builder.AppendLine("using System;"); builder.AppendLine("using System.Text;"); builder.AppendLine("using System.Collections.Generic;"); if (poco.Usings != null) { foreach (var reference in poco.Usings) { builder.AppendLine(String.Format("using {0};", reference)); } } builder.AppendLine(); builder.AppendLine(String.Format("namespace {0}", poco.Namespace)); builder.AppendLine("{"); indent = indent.AddIndent(); if (poco.Extends != null && poco.Extends.Any()) { builder.AppendLine(String.Format("{0}{1}{2} class {3} : {4}", indent, poco.AccessModifier, poco.IsPartial ? " partial" : String.Empty, poco.Name, String.Join(", ", poco.Extends))); } else { builder.AppendLine(String.Format("{0}{1}{2} class {3}", indent, poco.AccessModifier, poco.IsPartial ? " partial" : String.Empty, poco.Name)); } builder.AppendLine(String.Format("{0}{{", indent)); indent = indent.AddIndent(); //constructor if (poco.InNavigationProperties.Any(p => !p.ForeignKey.IsOne())) { builder.AppendLine(String.Format("{0}public {1}()", indent, poco.Name)); builder.AppendLine(String.Format("{0}{{", indent)); indent = indent.AddIndent(); foreach (var navigation in poco.InNavigationProperties.Where(p => !p.ForeignKey.IsOne())) { builder.AppendLine(String.Format("{0}{1} = new HashSet<{2}>();", indent, navigation.Name /*+ "Navigation"*/, navigation.Poco.Name)); } indent = indent.RemoveIndent(); builder.AppendLine(String.Format("{0}}}", indent)); builder.AppendLine(); } //properties foreach (var property in poco.BaseProperties) { builder.AppendLine(String.Format("{0}public {1} {2} {{ get; set; }}", indent, property.CSharpType, property.Name)); } //navigation properties if (poco.InNavigationProperties.Any()) { builder.AppendLine(); foreach (var property in poco.InNavigationProperties) { if (property.ForeignKey.IsOne()) { builder.AppendLine(String.Format("{0}public {1}{2} {3} {{ get; set; }}", indent, property.IsVirtual ? "virtual " : String.Empty, property.Poco.Name, property.Name)); } else { builder.AppendLine(String.Format("{0}public ICollection<{1}> {2} {{ get; set; }}", indent, property.Poco.Name, property.Name)); } } } if (poco.OutNavigationProperties.Any()) { builder.AppendLine(); foreach (var property in poco.OutNavigationProperties) { builder.AppendLine(String.Format("{0}public {1}{2} {3} {{ get; set; }}", indent, property.IsVirtual ? "virtual " : String.Empty, property.Poco.Name, property.Name)); } } indent = indent.RemoveIndent(); builder.AppendLine(String.Format("{0}}}", indent)); builder.AppendLine("}"); File.WriteAllText(Path.Combine(settings.OutputPath, poco.Name + ".cs"), builder.ToString()); } }
private bool LoadJobs() { if (_settingPaths == null || !_settingPaths.Any()) { Log.Warning("No json settings path specified"); return(false); } Log.Information(String.Format("Parsing {0} setting files", _settingPaths.Count())); foreach (var inputPath in _settingPaths) { var path = Path.GetFullPath(inputPath); Log.Debug(String.Format("Parsing file {0}", path)); var settings = Newtonsoft.Json.JsonConvert.DeserializeObject <JsonSettings>(File.ReadAllText(path)); _jobs = new List <Job>(); int i = 1; Log.Debug(String.Format("Parsing job #{0}", i)); foreach (var jsonJob in settings.Jobs) { int state = 0; bool preDbContext = false; var job = new Job() { SettingsFile = path, WorkingDirectory = Path.GetDirectoryName(path), Provider = jsonJob.Provider, ConnectionString = jsonJob.ConnectionString }; foreach (var step in jsonJob.Steps) { Log.Debug(String.Format("Parsing step: {0}", step.StepType)); if (state == -1) { Log.Warning("Invalid step: no step is allowed after DbContext"); return(false); } switch (step.StepType.ToLowerInvariant()) { case "load": if (state != 0 && state != 1) { Log.Warning("Invalid Load Step: unallowed position in step flow"); return(false); } var loadStep = new LoadStep() { Name = step.Name, EntityTypes = step.EntityTypes.Select(s => s?.ToLowerInvariant()), Schemas = step.Schemas?.Select(s => s?.ToLowerInvariant()), Exclude = step.Exclude.Convert() }; if (!ValidateStep(loadStep)) { return(false); } job.Steps.Add(loadStep); state = 1; break; case "pocogenerate": if (state != 1) { Log.Warning("Invalid PocoGenerate Step: unallowed position in step flow"); return(false); } var pocoGenerateStep = new PocoGenerateStep() { Name = step.Name, Namespace = step.Namespace, ClassAccessModifier = step.ClassAccessModifier, ClassPartial = step.ClassPartial, VirtualNavigationProperties = step.VirtualNavigationProperties, Usings = step.Usings, Extends = step.Extends, ClassNameForcePascalCase = step.ClassNameForcePascalCase, ClassNameReplace = step.ClassNameReplace.Select(c => c.Convert()), PropertyNameForcePascalCase = step.PropertyNameForcePascalCase, PropertyNameReplace = step.PropertyNameReplace.Select(c => c.Convert()), PropertyNullableIfDefaultAndNotPk = step.PropertyNullableIfDefaultAndNotPk }; if (!ValidateStep(pocoGenerateStep)) { return(false); } job.Steps.Add(pocoGenerateStep); state = 2; break; case "pocowrite": if (state != 2) { Log.Warning("Invalid PocoWrite Step: unallowed position in step flow"); return(false); } var pocoWriteStep = new PocoWriteStep() { Name = step.Name, OutputPath = step.OutputPath, CleanFolder = step.CleanFolder, PocosExclude = step.Exclude.Convert() }; if (!ValidateStep(pocoWriteStep)) { return(false); } job.Steps.Add(pocoWriteStep); state = 0; preDbContext = true; break; case "dbcontext": if (state != 0 || !preDbContext) { Log.Warning("Invalid DbContext Step: unallowed position in step flow"); return(false); } var dbContextStep = new DbContextStep() { Name = step.Name, OutputPath = step.OutputPath, StubOutputPath = step.StubOutputPath, Namespace = step.Namespace, ClassName = step.ClassName, ClassAbstract = step.ClassAbstract, Extends = step.Extends, IncludeIndices = step.IncludeIndices, IncludeOnModelCreatingStub = step.IncludeOnModelCreatingStub, IncludeOptionalStubs = step.IncludeOptionalStubs, IncludeViews = step.IncludeViews, IncludeTablesWithoutPK = step.IncludeTablesWithoutPK }; if (!ValidateStep(dbContextStep)) { return(false); } job.Steps.Add(dbContextStep); state = -1; break; } } if (!ValidateJob(job)) { return(false); } _jobs.Add(job); i++; } } Log.Information(String.Format("Loaded {0} jobs", _settingPaths.Count())); return(true); }