Esempio n. 1
0
        private static void ScaffoldBasicFileDomainRegistration(DomainProjectOptions domainProjectOptions)
        {
            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(domainProjectOptions);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.Create("DomainRegistration")
                                   .AddGeneratedCodeAttribute(domainProjectOptions.ToolName, domainProjectOptions.ToolVersion.ToString());

            // Add class to namespace
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(new[] { "System.CodeDom.Compiler" });

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            var codeAsString = compilationUnit
                               .NormalizeWhitespace()
                               .ToFullString();

            var file = new FileInfo(Path.Combine(domainProjectOptions.PathForSrcGenerate.FullName, "DomainRegistration.cs"));

            TextFileHelper.Save(file, codeAsString);
        }
Esempio n. 2
0
 public SyntaxGeneratorHandlers(
     DomainProjectOptions domainProjectOptions,
     string focusOnSegmentName)
 {
     this.DomainProjectOptions = domainProjectOptions ?? throw new ArgumentNullException(nameof(domainProjectOptions));
     this.FocusOnSegmentName   = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Esempio n. 3
0
        private static List <LogKeyValueItem> GenerateTestHandlers(DomainProjectOptions domainProjectOptions, List <SyntaxGeneratorHandler> sgHandlers)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            if (sgHandlers == null)
            {
                throw new ArgumentNullException(nameof(sgHandlers));
            }

            var logItems = new List <LogKeyValueItem>();

            if (domainProjectOptions.PathForTestHandlers != null)
            {
                foreach (var sgHandler in sgHandlers)
                {
                    logItems.Add(GenerateServerDomainXunitTestHelper.GenerateGeneratedTests(domainProjectOptions, sgHandler));
                    logItems.Add(GenerateServerDomainXunitTestHelper.GenerateCustomTests(domainProjectOptions, sgHandler));
                }
            }

            return(logItems);
        }
Esempio n. 4
0
        private static List <LogKeyValueItem> ScaffoldSrc(DomainProjectOptions domainProjectOptions)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            if (!Directory.Exists(domainProjectOptions.PathForSrcGenerate.FullName))
            {
                Directory.CreateDirectory(domainProjectOptions.PathForSrcGenerate.FullName);
            }

            var logItems = new List <LogKeyValueItem>();

            if (domainProjectOptions.PathForSrcGenerate.Exists && domainProjectOptions.ProjectSrcCsProj.Exists)
            {
                var element = XElement.Load(domainProjectOptions.ProjectSrcCsProj.FullName);
                var originalNullableValue = SolutionAndProjectHelper.GetBoolFromNullableString(SolutionAndProjectHelper.GetNullableValueFromProject(element));

                bool hasUpdates = false;
                if (domainProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes != originalNullableValue)
                {
                    var newNullableValue = SolutionAndProjectHelper.GetNullableStringFromBool(domainProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes);
                    SolutionAndProjectHelper.SetNullableValueForProject(element, newNullableValue);
                    element.Save(domainProjectOptions.ProjectSrcCsProj.FullName);
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileUpdate", "#", $"Update domain csproj - Nullable value={newNullableValue}"));
                    hasUpdates = true;
                }

                if (!hasUpdates)
                {
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileSkip", "#", "No updates for domain csproj"));
                }
            }
            else
            {
                var projectReferences = new List <FileInfo>();
                if (domainProjectOptions.ApiProjectSrcCsProj != null)
                {
                    projectReferences.Add(domainProjectOptions.ApiProjectSrcCsProj);
                }

                logItems.Add(SolutionAndProjectHelper.ScaffoldProjFile(
                                 domainProjectOptions.ProjectSrcCsProj,
                                 false,
                                 false,
                                 domainProjectOptions.ProjectName,
                                 domainProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                                 new List <string> {
                    "Microsoft.AspNetCore.App"
                },
                                 null,
                                 projectReferences,
                                 false));
            }

            ScaffoldBasicFileDomainRegistration(domainProjectOptions);

            return(logItems);
        }
 public ServerDomainGenerator(
     ILogger logger,
     DomainProjectOptions projectOptions)
 {
     this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));
     this.projectOptions = projectOptions ?? throw new ArgumentNullException(nameof(projectOptions));
 }
Esempio n. 6
0
        private static SyntaxNode ReadCsFile(DomainProjectOptions domainProjectOptions, string area, SyntaxGeneratorHandler sgHandler)
        {
            var csSrcFile = Util.GetCsFileNameForHandler(domainProjectOptions.PathForSrcHandlers !, area, sgHandler.HandlerTypeName);
            var csSrcCode = File.ReadAllText(csSrcFile);
            var tree      = CSharpSyntaxTree.ParseText(csSrcCode);

            return(tree.GetRoot());
        }
Esempio n. 7
0
        public static LogKeyValueItem GenerateGeneratedTests(
            DomainProjectOptions domainProjectOptions,
            SyntaxGeneratorHandler sgHandler)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            if (sgHandler == null)
            {
                throw new ArgumentNullException(nameof(sgHandler));
            }

            var area   = sgHandler.FocusOnSegmentName.EnsureFirstCharacterToUpper();
            var nsSrc  = $"{domainProjectOptions.ProjectName}.{NameConstants.Handlers}.{area}";
            var nsTest = $"{domainProjectOptions.ProjectName}.Tests.{NameConstants.Handlers}.{area}.Generated";

            var srcSyntaxNodeRoot           = ReadCsFile(domainProjectOptions, sgHandler.FocusOnSegmentName, sgHandler);
            var usedInterfacesInConstructor = GetUsedInterfacesInConstructor(srcSyntaxNodeRoot);

            var usingStatements = GetUsedUsingStatements(
                srcSyntaxNodeRoot,
                nsSrc,
                usedInterfacesInConstructor.Count > 0);

            var sb = new StringBuilder();

            foreach (var item in usingStatements)
            {
                sb.AppendLine($"using {item};");
            }

            sb.AppendLine();
            GenerateCodeHelper.AppendNamespaceComment(sb, domainProjectOptions.ToolNameAndVersion);
            sb.AppendLine($"namespace {nsTest}");
            sb.AppendLine("{");
            GenerateCodeHelper.AppendGeneratedCodeAttribute(sb, domainProjectOptions.ToolName, domainProjectOptions.ToolVersion);
            sb.AppendLine(4, $"public class {sgHandler.HandlerTypeName}GeneratedTests");
            sb.AppendLine(4, "{");
            AppendInstantiateConstructor(sb, sgHandler, usedInterfacesInConstructor);
            if (sgHandler.HasParametersOrRequestBody)
            {
                sb.AppendLine();
                AppendParameterArgumentNullCheck(sb, sgHandler, usedInterfacesInConstructor);
            }

            sb.AppendLine(4, "}");
            sb.AppendLine("}");

            var pathGenerated = Path.Combine(Path.Combine(domainProjectOptions.PathForTestHandlers !.FullName, area), "Generated");
            var fileGenerated = new FileInfo(Path.Combine(pathGenerated, $"{sgHandler.HandlerTypeName}GeneratedTests.cs"));

            return(TextFileHelper.Save(fileGenerated, sb.ToString()));
        }
Esempio n. 8
0
    public static void GenerateGeneratedTests(
        ILogger logger,
        DomainProjectOptions domainProjectOptions,
        SyntaxGeneratorHandler sgHandler)
    {
        ArgumentNullException.ThrowIfNull(logger);
        ArgumentNullException.ThrowIfNull(domainProjectOptions);
        ArgumentNullException.ThrowIfNull(sgHandler);

        var area   = sgHandler.FocusOnSegmentName.EnsureFirstCharacterToUpper();
        var nsSrc  = $"{domainProjectOptions.ProjectName}.{NameConstants.Handlers}.{area}";
        var nsTest = $"{domainProjectOptions.ProjectName}.Tests.{NameConstants.Handlers}.{area}.Generated";

        var srcSyntaxNodeRoot           = ReadCsFile(domainProjectOptions, sgHandler.FocusOnSegmentName, sgHandler);
        var usedInterfacesInConstructor = GetUsedInterfacesInConstructor(srcSyntaxNodeRoot);

        var usingStatements = GetUsedUsingStatements(
            srcSyntaxNodeRoot,
            nsSrc,
            usedInterfacesInConstructor.Count > 0);

        var sb = new StringBuilder();

        foreach (var item in usingStatements)
        {
            sb.AppendLine($"using {item};");
        }

        sb.AppendLine();
        GenerateCodeHelper.AppendGeneratedCodeWarningComment(sb, domainProjectOptions.ToolNameAndVersion);
        sb.AppendLine($"namespace {nsTest}");
        sb.AppendLine("{");
        GenerateCodeHelper.AppendGeneratedCodeAttribute(sb, domainProjectOptions.ToolName, domainProjectOptions.ToolVersion);
        sb.AppendLine(4, $"public class {sgHandler.HandlerTypeName}GeneratedTests");
        sb.AppendLine(4, "{");
        AppendInstantiateConstructor(sb, sgHandler, usedInterfacesInConstructor);
        if (sgHandler.HasParametersOrRequestBody)
        {
            sb.AppendLine();
            AppendParameterArgumentNullCheck(sb, sgHandler, usedInterfacesInConstructor);
        }

        sb.AppendLine(4, "}");
        sb.AppendLine("}");

        var pathGenerated = Path.Combine(Path.Combine(domainProjectOptions.PathForTestHandlers !.FullName, area), "Generated");
        var fileGenerated = new FileInfo(Path.Combine(pathGenerated, $"{sgHandler.HandlerTypeName}GeneratedTests.cs"));

        var fileDisplayLocation = fileGenerated.FullName.Replace(domainProjectOptions.PathForTestGenerate !.FullName, "test: ", StringComparison.Ordinal);

        TextFileHelper.Save(logger, fileGenerated, fileDisplayLocation, sb.ToString());
    }
Esempio n. 9
0
 public SyntaxGeneratorHandler(
     DomainProjectOptions domainProjectOptions,
     OperationType apiOperationType,
     OpenApiOperation apiOperation,
     string focusOnSegmentName,
     bool hasParametersOrRequestBody)
 {
     this.DomainProjectOptions       = domainProjectOptions ?? throw new ArgumentNullException(nameof(domainProjectOptions));
     this.ApiOperationType           = apiOperationType;
     this.ApiOperation               = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
     this.FocusOnSegmentName         = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
     this.HasParametersOrRequestBody = hasParametersOrRequestBody;
 }
Esempio n. 10
0
        public static string[] CreateUsingList(
            DomainProjectOptions domainProjectOptions,
            string focusOnSegmentName)
        {
            var list = new List <string>
            {
                "System.Threading",
                "System.Threading.Tasks",
                $"{domainProjectOptions.ProjectName.Replace(".Domain", ".Api", StringComparison.Ordinal)}.Generated.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}",
            };

            return(list.ToArray());
        }
    private void GenerateTestHandlers(
        DomainProjectOptions domainProjectOptions,
        List <SyntaxGeneratorHandler> sgHandlers)
    {
        ArgumentNullException.ThrowIfNull(domainProjectOptions);
        ArgumentNullException.ThrowIfNull(sgHandlers);

        if (domainProjectOptions.PathForTestHandlers is not null)
        {
            foreach (var sgHandler in sgHandlers)
            {
                GenerateServerDomainXunitTestHelper.GenerateGeneratedTests(logger, domainProjectOptions, sgHandler);
                GenerateServerDomainXunitTestHelper.GenerateCustomTests(logger, domainProjectOptions, sgHandler);
            }
        }
    }
Esempio n. 12
0
        private static List <LogKeyValueItem> ScaffoldTest(DomainProjectOptions domainProjectOptions)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            var logItems = new List <LogKeyValueItem>();

            if (domainProjectOptions.PathForTestGenerate == null || domainProjectOptions.ProjectTestCsProj == null)
            {
                return(logItems);
            }

            if (domainProjectOptions.PathForTestGenerate.Exists && domainProjectOptions.ProjectTestCsProj.Exists)
            {
                // Update
            }
            else
            {
                if (!Directory.Exists(domainProjectOptions.PathForTestGenerate.FullName))
                {
                    Directory.CreateDirectory(domainProjectOptions.PathForTestGenerate.FullName);
                }

                var projectReferences = new List <FileInfo>();
                if (domainProjectOptions.ApiProjectSrcCsProj != null)
                {
                    projectReferences.Add(domainProjectOptions.ApiProjectSrcCsProj);
                    projectReferences.Add(domainProjectOptions.ProjectSrcCsProj);
                }

                logItems.Add(SolutionAndProjectHelper.ScaffoldProjFile(
                                 domainProjectOptions.ProjectTestCsProj,
                                 false,
                                 true,
                                 $"{domainProjectOptions.ProjectName}.Tests",
                                 domainProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                                 null,
                                 NugetPackageReferenceHelper.CreateForTestProject(false),
                                 projectReferences,
                                 true));
            }

            return(logItems);
        }
Esempio n. 13
0
        public static LogKeyValueItem GenerateCustomTests(
            DomainProjectOptions domainProjectOptions,
            SyntaxGeneratorHandler sgHandler)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            if (sgHandler == null)
            {
                throw new ArgumentNullException(nameof(sgHandler));
            }

            var area   = sgHandler.FocusOnSegmentName.EnsureFirstCharacterToUpper();
            var nsSrc  = $"{domainProjectOptions.ProjectName}.{NameConstants.Handlers}.{area}";
            var nsTest = $"{domainProjectOptions.ProjectName}.Tests.{NameConstants.Handlers}.{area}.Generated";

            var sb = new StringBuilder();

            sb.AppendLine($"using {nsSrc};");
            sb.AppendLine("using Xunit;");
            sb.AppendLine();
            sb.AppendLine($"namespace {nsTest}");
            sb.AppendLine("{");
            sb.AppendLine(4, $"public class {sgHandler.HandlerTypeName}Tests");
            sb.AppendLine(4, "{");
            sb.AppendLine(8, "[Fact(Skip=\"Change this to a real test\")]");
            sb.AppendLine(8, "public void Sample()");
            sb.AppendLine(8, "{");
            sb.AppendLine(12, "// Arrange");
            sb.AppendLine();
            sb.AppendLine(12, "// Act");
            sb.AppendLine();
            sb.AppendLine(12, "// Assert");
            sb.AppendLine(8, "}");
            sb.AppendLine(4, "}");
            sb.AppendLine("}");

            var pathCustom = Path.Combine(domainProjectOptions.PathForTestHandlers !.FullName, area);
            var fileCustom = new FileInfo(Path.Combine(pathCustom, $"{sgHandler.HandlerTypeName}Tests.cs"));

            return(TextFileHelper.Save(fileCustom, sb.ToString(), false));
        }
    private void GenerateSrcHandlers(
        DomainProjectOptions domainProjectOptions,
        out List <SyntaxGeneratorHandler> sgHandlers)
    {
        ArgumentNullException.ThrowIfNull(domainProjectOptions);

        sgHandlers = new List <SyntaxGeneratorHandler>();
        foreach (var basePathSegmentName in domainProjectOptions.BasePathSegmentNames)
        {
            var generatorHandlers = new SyntaxGeneratorHandlers(logger, domainProjectOptions, basePathSegmentName);
            var generatedHandlers = generatorHandlers.GenerateSyntaxTrees();
            sgHandlers.AddRange(generatedHandlers);
        }

        foreach (var sg in sgHandlers)
        {
            if (!sg.GetFilePath().Exists)
            {
                sg.ToFile();
            }
        }
    }
Esempio n. 15
0
    public static void GenerateCustomTests(
        ILogger logger,
        DomainProjectOptions domainProjectOptions,
        SyntaxGeneratorHandler sgHandler)
    {
        ArgumentNullException.ThrowIfNull(logger);
        ArgumentNullException.ThrowIfNull(domainProjectOptions);
        ArgumentNullException.ThrowIfNull(sgHandler);

        var area   = sgHandler.FocusOnSegmentName.EnsureFirstCharacterToUpper();
        var nsTest = $"{domainProjectOptions.ProjectName}.Tests.{NameConstants.Handlers}.{area}";

        var sb = new StringBuilder();

        sb.AppendLine("using Xunit;");
        sb.AppendLine();
        sb.AppendLine($"namespace {nsTest}");
        sb.AppendLine("{");
        sb.AppendLine(4, $"public class {sgHandler.HandlerTypeName}Tests");
        sb.AppendLine(4, "{");
        sb.AppendLine(8, "[Fact(Skip=\"Change this to a real test\")]");
        sb.AppendLine(8, "public void Sample()");
        sb.AppendLine(8, "{");
        sb.AppendLine(12, "// Arrange");
        sb.AppendLine();
        sb.AppendLine(12, "// Act");
        sb.AppendLine();
        sb.AppendLine(12, "// Assert");
        sb.AppendLine(8, "}");
        sb.AppendLine(4, "}");
        sb.AppendLine("}");

        var pathCustom = Path.Combine(domainProjectOptions.PathForTestHandlers !.FullName, area);
        var fileCustom = new FileInfo(Path.Combine(pathCustom, $"{sgHandler.HandlerTypeName}Tests.cs"));

        var fileDisplayLocation = fileCustom.FullName.Replace(domainProjectOptions.PathForTestGenerate !.FullName, "test: ", StringComparison.Ordinal);

        TextFileHelper.Save(logger, fileCustom, fileDisplayLocation, sb.ToString(), overrideIfExist: false);
    }
Esempio n. 16
0
        public static List <LogKeyValueItem> GenerateServerDomain(
            string projectPrefixName,
            DirectoryInfo outputPath,
            DirectoryInfo?outputTestPath,
            Tuple <OpenApiDocument, OpenApiDiagnostic, FileInfo> apiDocument,
            ApiOptions apiOptions,
            DirectoryInfo apiPath)
        {
            if (projectPrefixName == null)
            {
                throw new ArgumentNullException(nameof(projectPrefixName));
            }

            if (outputPath == null)
            {
                throw new ArgumentNullException(nameof(outputPath));
            }

            if (apiDocument == null)
            {
                throw new ArgumentNullException(nameof(apiDocument));
            }

            if (apiOptions == null)
            {
                throw new ArgumentNullException(nameof(apiOptions));
            }

            if (apiPath == null)
            {
                throw new ArgumentNullException(nameof(apiPath));
            }

            var domainProjectOptions  = new DomainProjectOptions(outputPath, outputTestPath, apiDocument.Item1, apiDocument.Item3, projectPrefixName, apiOptions, apiPath);
            var serverDomainGenerator = new ServerDomainGenerator(domainProjectOptions);

            return(serverDomainGenerator.Generate());
        }
Esempio n. 17
0
        private static List <LogKeyValueItem> GenerateSrcHandlers(DomainProjectOptions domainProjectOptions, out List <SyntaxGeneratorHandler> sgHandlers)
        {
            if (domainProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(domainProjectOptions));
            }

            var logItems = new List <LogKeyValueItem>();

            sgHandlers = new List <SyntaxGeneratorHandler>();
            foreach (var basePathSegmentName in domainProjectOptions.BasePathSegmentNames)
            {
                var generatorHandlers = new SyntaxGeneratorHandlers(domainProjectOptions, basePathSegmentName);
                var generatedHandlers = generatorHandlers.GenerateSyntaxTrees();
                sgHandlers.AddRange(generatedHandlers);
            }

            foreach (var sg in sgHandlers)
            {
                logItems.Add(sg.ToFile());
            }

            return(logItems);
        }
Esempio n. 18
0
 public ServerDomainGenerator(DomainProjectOptions projectOptions)
 {
     this.projectOptions = projectOptions ?? throw new ArgumentNullException(nameof(projectOptions));
 }