Ejemplo n.º 1
0
        private string UpdateFileData(IEntityAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            string usingStatement = $"{options.ProjectName}.Persistence.Modules.{options.Domain}.{options.EntityNamePlural}";

            fileData = UsingStatements.Add(fileData, usingStatement);

            StringEditor stringEditor = new StringEditor(fileData);

            // ----------- DbSet -----------
            stringEditor.NextThatContains("CustomInstantiate");

            stringEditor.InsertLine(GetDbSetLine(options));
            stringEditor.InsertNewLine();

            // ----------- OnModelCreating -----------

            stringEditor.NextThatContains("this.OnModelCreatingPartial(modelBuilder);");

            stringEditor.InsertLine(GetEmptyModelBuilderEntityLine(options));
            stringEditor.InsertNewLine();

            return(stringEditor.GetText());
        }
        private string UpdateFileData(IEntityAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Persistence.Modules.{options.Domain}.{options.EntityNamePlural}");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Persistence.Tests.Modules.{options.Domain}.{options.EntityNamePlural}");

            // ----------- DbSet -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.NextThatContains("persistenceDbContext.SaveChanges();");

            if (options.HasRequestScope)
            {
                stringEditor.InsertLine($"            persistenceDbContext.{options.EntityNamePlural}.Add(Db{options.EntityName}.ToEf{options.EntityName}(Db{options.EntityName}Test.DbDefault(), {options.EntityName}TestValues.{options.RequestScopeName}IdDbDefault));");
                stringEditor.InsertLine($"            persistenceDbContext.{options.EntityNamePlural}.Add(Db{options.EntityName}.ToEf{options.EntityName}(Db{options.EntityName}Test.DbDefault2(), {options.EntityName}TestValues.{options.RequestScopeName}IdDbDefault));");
            }
            else
            {
                stringEditor.InsertLine($"            persistenceDbContext.{options.EntityNamePlural}.Add(Db{options.EntityName}.ToEf{options.EntityName}(Db{options.EntityName}Test.DbDefault()));");
                stringEditor.InsertLine($"            persistenceDbContext.{options.EntityNamePlural}.Add(Db{options.EntityName}.ToEf{options.EntityName}(Db{options.EntityName}Test.DbDefault2()));");
            }

            stringEditor.InsertNewLine();

            return(stringEditor.GetText());
        }
Ejemplo n.º 3
0
        public void Add(IRelationAdditionOptions options, string domainFolder, string templateFileName)
        {
            string filePath = GetFilePath(options, domainFolder, templateFileName);
            string fileData = UpdateFileData(options, filePath);

            fileData = UsingStatements.Add(fileData, "Microsoft.EntityFrameworkCore");

            CsharpClassWriter.Write(filePath, fileData);
        }
        public void Add(IRelationAdditionOptions options, string domainFolder, string templateFileName, string namespaceToAdd)
        {
            string filePath = GetFilePath(options, domainFolder, templateFileName);
            string fileData = UpdateFileData(options, filePath);

            fileData = UsingStatements.Add(fileData, namespaceToAdd);

            CsharpClassWriter.Write(filePath, fileData);
        }
 public NameSpaceModel AddUsingStatement(string statement)
 {
     if (UsingStatements == null)
     {
         UsingStatements = new List <string>();
     }
     UsingStatements.Append(statement);
     return(this);
 }
        private string UpdateFileData(IRelationAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Contract.Persistence.Modules.{options.DomainFrom}.{options.EntityNamePluralFrom}");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Logic.Tests.Modules.{options.DomainFrom}.{options.EntityNamePluralFrom}");

            // ----------- Repository Generation -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.MoveToEnd();
            stringEditor.Next();
            stringEditor.PrevThatContains("}");
            stringEditor.PrevThatContains("}");
            stringEditor.InsertLine("\n" +
                                    $"        private Mock<I{options.EntityNamePluralFrom}CrudRepository> Setup{options.EntityNamePluralFrom}RepositoryDefault()\n" +
                                    "        {\n" +
                                    $"            var {options.EntityNamePluralLowerFrom}CrudRepository = new Mock<I{options.EntityNamePluralFrom}CrudRepository>(MockBehavior.Strict);\n" +
                                    $"            {options.EntityNamePluralLowerFrom}CrudRepository.Setup(repository => repository.Does{options.EntityNameFrom}Exist({options.EntityNameFrom}TestValues.IdDefault)).Returns(true);\n" +
                                    $"            {options.EntityNamePluralLowerFrom}CrudRepository.Setup(repository => repository.Does{options.EntityNameFrom}Exist({options.EntityNameFrom}TestValues.IdDefault2)).Returns(true);\n" +
                                    $"            {options.EntityNamePluralLowerFrom}CrudRepository.Setup(repository => repository.Does{options.EntityNameFrom}Exist({options.EntityNameFrom}TestValues.IdForCreate)).Returns(false);\n" +
                                    $"            return {options.EntityNamePluralLowerFrom}CrudRepository;\n" +
                                    "        }");

            fileData = stringEditor.GetText();

            // ----------- TestMethods -----------
            stringEditor = new StringEditor(fileData);
            stringEditor.NextThatContains("[TestMethod]");
            while (stringEditor.GetLineNumber() < stringEditor.GetLineCount())
            {
                stringEditor.Next();
                if (stringEditor.GetLine().Contains("Create" + options.EntityNameTo) ||
                    stringEditor.GetLine().Contains("Update" + options.EntityNameTo))
                {
                    stringEditor.NextThatContains($"Mock<I{options.EntityNamePluralTo}CrudRepository>");
                    stringEditor.Next(line => !line.Contains("CrudRepository>") && line.Trim().Length > 0);
                    stringEditor.InsertLine($"            Mock<I{options.EntityNamePluralFrom}CrudRepository> {options.EntityNamePluralLowerFrom}CrudRepository = this.Setup{options.EntityNamePluralFrom}RepositoryDefault();");

                    stringEditor.NextThatContains($"{options.EntityNamePluralTo}CrudLogic {options.EntityNamePluralLowerTo}CrudLogic = new {options.EntityNamePluralTo}CrudLogic");
                    stringEditor.Next(line => !line.Contains("CrudRepository.Object"));
                    stringEditor.InsertLine($"                {options.EntityNamePluralLowerFrom}CrudRepository.Object,");
                }
                else
                {
                    stringEditor.NextThatContains($"{options.EntityNamePluralTo}CrudLogic {options.EntityNamePluralLowerTo}CrudLogic = new {options.EntityNamePluralTo}CrudLogic");
                    stringEditor.Next(line => !line.Contains("CrudRepository.Object"));
                    stringEditor.InsertLine("                null,");
                }
                stringEditor.NextThatContains("[TestMethod]");
            }

            return(stringEditor.GetText());
        }
        private string UpdateFileData(IRelationAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Contract.Persistence.Modules.{options.DomainFrom}.{options.EntityNamePluralFrom}");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Persistence.Tests.Modules.{options.DomainFrom}.{options.EntityNamePluralFrom}");

            // ----------- AssertDbDefault -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.NextThatContains("AssertDbDefault(");
            stringEditor.Next(line => line.Trim().Equals("}"));

            stringEditor.InsertLine($"            Db{options.EntityNameFrom}Test.AssertDbDefault(db{options.EntityNameTo}Detail.{options.PropertyNameFrom});");

            return(stringEditor.GetText());
        }
        /// <summary>
        /// Validates the context for anything that might lead to a compile or runtime error
        /// </summary>
        public void Validate(string[] allowedNamespaces, string[] excludedNamespaces)
        {
            foreach (var client in HttpClients)
            {
                client.Validate();
            }

            foreach (var hub in HubClients)
            {
                hub.Validate();
            }

            foreach (var function in Functions)
            {
                function.Validate();
            }

            Regex allowedUsings;
            Regex unallowedUsings;

            if (allowedNamespaces?.Any() ?? false)
            {
                allowedUsings = new Regex($"({string.Join("|", allowedNamespaces)})");
            }
            else
            {
                allowedUsings = new Regex($"(.+)");
            }

            if (excludedNamespaces?.Any() ?? false)
            {
                unallowedUsings = new Regex($"({string.Join("|", excludedNamespaces)})");
            }
            else
            {
                unallowedUsings = new Regex($"(^[.]+)");
            }

            this.UsingStatements = UsingStatements.Where(x => allowedUsings.IsMatch(x) &&
                                                         !unallowedUsings.IsMatch(x))
                                   .ToList();
        }
        private string UpdateFileData(IRelationAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Persistence.Tests.Modules.{options.DomainFrom}.{options.EntityNamePluralFrom}");

            // ----------- Asserts -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.MoveToEnd();
            stringEditor.Next();
            stringEditor.PrevThatContains("}");
            stringEditor.PrevThatContains("}");

            stringEditor.InsertNewLine();
            stringEditor.InsertLine($"        public static readonly Guid {options.PropertyNameFrom}IdDbDefault = {options.EntityNameFrom}TestValues.IdDbDefault;");
            stringEditor.InsertLine($"        public static readonly Guid {options.PropertyNameFrom}IdDbDefault2 = {options.EntityNameFrom}TestValues.IdDbDefault2;");
            stringEditor.InsertLine($"        public static readonly Guid {options.PropertyNameFrom}IdForCreate = {options.EntityNameFrom}TestValues.IdDbDefault;");
            stringEditor.InsertLine($"        public static readonly Guid {options.PropertyNameFrom}IdForUpdate = {options.EntityNameFrom}TestValues.IdDbDefault2;");

            return(stringEditor.GetText());
        }
Ejemplo n.º 10
0
        private string UpdateFileData(IRelationAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, "System.Linq");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Contract.Persistence.Modules.{options.DomainTo}.{options.EntityNamePluralTo}");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Logic.Tests.Modules.{options.DomainTo}.{options.EntityNamePluralTo}");

            // ----------- Creators -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.NextThatContains($"public static IDb{options.EntityNameFrom}Detail Default()");
            stringEditor.Next(line => line.Trim().Equals("};"));
            stringEditor.InsertLine($"                {options.PropertyNameTo} = new List<IDb{options.EntityNameTo}> " + "{" + $" Db{options.EntityNameTo}Test.Default() " + "},");
            fileData = stringEditor.GetText();

            stringEditor = new StringEditor(fileData);
            stringEditor.NextThatContains($"public static IDb{options.EntityNameFrom}Detail Default2()");
            stringEditor.Next(line => line.Trim().Equals("};"));
            stringEditor.InsertLine($"                {options.PropertyNameTo} = new List<IDb{options.EntityNameTo}> " + "{" + $" Db{options.EntityNameTo}Test.Default2() " + "},");
            fileData = stringEditor.GetText();

            // ----------- Asserts -----------
            stringEditor = new StringEditor(fileData);
            stringEditor.NextThatContains("AssertDefault(");
            stringEditor.Next(line => line.Trim().Equals("}"));
            stringEditor.InsertLine($"            Db{options.EntityNameTo}Test.AssertDefault(db{options.EntityNameFrom}Detail.{options.PropertyNameTo}.ToArray()[0]);");
            fileData = stringEditor.GetText();

            stringEditor = new StringEditor(fileData);
            stringEditor.NextThatContains("AssertDefault2(");
            stringEditor.Next(line => line.Trim().Equals("}"));
            stringEditor.InsertLine($"            Db{options.EntityNameTo}Test.AssertDefault2(db{options.EntityNameFrom}Detail.{options.PropertyNameTo}.ToArray()[0]);");
            fileData = stringEditor.GetText();

            return(stringEditor.GetText());
        }
        private string UpdateFileData(IRelationAdditionOptions options, string filePath)
        {
            string fileData = File.ReadAllText(filePath);

            fileData = UsingStatements.Add(fileData, "System.Linq");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Contract.Logic.Modules.{options.DomainTo}.{options.EntityNamePluralTo}");
            fileData = UsingStatements.Add(fileData, $"{options.ProjectName}.Logic.Tests.Modules.{options.DomainTo}.{options.EntityNamePluralTo}");

            // ----------- AssertDbDefault -----------
            StringEditor stringEditor = new StringEditor(fileData);

            stringEditor.NextThatContains("AssertDefault(");
            stringEditor.Next(line => line.Trim().Equals("}"));
            stringEditor.InsertLine($"            {options.EntityNameTo}Test.AssertDefault({options.EntityNameLowerFrom}Detail.{options.PropertyNameTo}.ToArray()[0]);");
            fileData = stringEditor.GetText();

            stringEditor = new StringEditor(fileData);
            stringEditor.NextThatContains("AssertDefault2(");
            stringEditor.Next(line => line.Trim().Equals("}"));
            stringEditor.InsertLine($"            {options.EntityNameTo}Test.AssertDefault2({options.EntityNameLowerFrom}Detail.{options.PropertyNameTo}.ToArray()[0]);");
            fileData = stringEditor.GetText();

            return(stringEditor.GetText());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write(@"
using System.Collections.Generic;
using Newtonsoft.Json;
using SweepableEstimator = Microsoft.ML.AutoML.SweepableEstimator;
using Microsoft.ML.AutoML.CodeGen;
using ColorsOrder = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorsOrder;
using ColorBits = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorBits;
using ResizingKind = Microsoft.ML.Transforms.Image.ImageResizingEstimator.ResizingKind;
using Anchor = Microsoft.ML.Transforms.Image.ImageResizingEstimator.Anchor;
using Microsoft.ML.SearchSpace;

namespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(NameSpace));
            this.Write("\r\n{\r\n    internal partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write(" : SweepableEstimator<");
            this.Write(this.ToStringHelper.ToStringWithCulture(TOption));
            this.Write(">\r\n    {\r\n        public ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write("(");
            this.Write(this.ToStringHelper.ToStringWithCulture(TOption));
            this.Write(" defaultOption, SearchSpace<");
            this.Write(this.ToStringHelper.ToStringWithCulture(TOption));
            this.Write("> searchSpace = null)\r\n        {\r\n            this.TParameter = defaultOption;\r\n " +
                       "           this.SearchSpace = searchSpace;\r\n            this.EstimatorType = Est" +
                       "imatorType.");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write(";\r\n        }\r\n\r\n        internal ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write("()\r\n        {\r\n            this.EstimatorType = EstimatorType.");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write(";\r\n            this.TParameter = new ");
            this.Write(this.ToStringHelper.ToStringWithCulture(TOption));
            this.Write("();\r\n        }\r\n    \r\n        internal override IEnumerable<string> CSharpUsingSt" +
                       "atements \r\n        {\r\n            get => new string[] {");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.PrettyPrintListOfString(UsingStatements.Select(x => $"using {x};"))));
            this.Write("};\r\n        }\r\n\r\n        internal override IEnumerable<string> NugetDependencies\r" +
                       "\n        {\r\n            get => new string[] {");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.PrettyPrintListOfString(NugetDependencies)));
            this.Write("};\r\n        }\r\n\r\n        internal override string FunctionName \r\n        {\r\n     " +
                       "       get => \"");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.GetPrefix(Type)));
            this.Write(".");
            this.Write(this.ToStringHelper.ToStringWithCulture(FunctionName));
            this.Write("\";\r\n        }\r\n    }\r\n}\r\n\r\n");
            return(this.GenerationEnvironment.ToString());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write(@"
using System.Collections.Generic;
using Newtonsoft.Json;
using SweepableEstimator = Microsoft.ML.AutoML.SweepableEstimator;
using Microsoft.ML.AutoML.CodeGen;
using ColorsOrder = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorsOrder;
using ColorBits = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorBits;
using ResizingKind = Microsoft.ML.Transforms.Image.ImageResizingEstimator.ResizingKind;
using Anchor = Microsoft.ML.Transforms.Image.ImageResizingEstimator.Anchor;

namespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(NameSpace));
            this.Write("\r\n{\r\n    internal partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write(" : SweepableEstimator\r\n    {\r\n        public ");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write("()\r\n        {\r\n            this.EstimatorType = EstimatorType.");
            this.Write(this.ToStringHelper.ToStringWithCulture(ClassName));
            this.Write(";\r\n        }\r\n    \r\n");
            foreach (var arg in ArgumentsList)
            {
                var typeAttributeName = Utils.CapitalFirstLetter(arg.ArgumentType);
                var propertyName      = Utils.CapitalFirstLetter(arg.ArgumentName);
                this.Write("        [");
                this.Write(this.ToStringHelper.ToStringWithCulture(typeAttributeName));
                this.Write("]\r\n        [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]\r\n        pu" +
                           "blic string ");
                this.Write(this.ToStringHelper.ToStringWithCulture(propertyName));
                this.Write(" { get; set; }\r\n\r\n");
            }
            this.Write("        internal override IEnumerable<string> CSharpUsingStatements \r\n        {\r\n" +
                       "            get => new string[] {");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.PrettyPrintListOfString(UsingStatements.Select(x => $"using {x};"))));
            this.Write("};\r\n        }\r\n\r\n        internal override IEnumerable<string> NugetDependencies\r" +
                       "\n        {\r\n            get => new string[] {");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.PrettyPrintListOfString(NugetDependencies)));
            this.Write("};\r\n        }\r\n\r\n        internal override string FunctionName \r\n        {\r\n     " +
                       "       get => \"");
            this.Write(this.ToStringHelper.ToStringWithCulture(Utils.GetPrefix(Type)));
            this.Write(".");
            this.Write(this.ToStringHelper.ToStringWithCulture(FunctionName));
            this.Write("\";\r\n        }\r\n    }\r\n}\r\n\r\n");
            return(this.GenerationEnvironment.ToString());
        }