public static void Build(string path)
        {
            Console.WriteLine(ExecuteCommandUtility.Run($"echo ADD LOGIC SERVICE COLLECTION EXTENSION"));

            const string className  = "LogicServiceCollectionExtensions";
            const string methodName = "AddLogic";
            const string nameSpace  = "Logic.Configuration";

            Directory.CreateDirectory(path);

            ExecuteCommandUtility.Run("cd Logic/Configuration");

            var importNameSpaces = new List <NamespaceModel>
            {
                new NamespaceModel("MediatR"),
                new NamespaceModel("Microsoft.Extensions.Configuration"),
                new NamespaceModel("Microsoft.Extensions.DependencyInjection")
            };

            var modifiers = new[]
            {
                SyntaxFactory.Token(SyntaxKind.PublicKeyword)
                , SyntaxFactory.Token(SyntaxKind.StaticKeyword)
            };

            var statements = new List <StatementSyntax>
            {
                SyntaxFactory.ParseStatement("services")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
                SyntaxFactory.ParseStatement(".AddMediatR(typeof(LogicServiceCollectionExtensions).Assembly)")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
                SyntaxFactory.ParseStatement(";")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
                SyntaxFactory.ParseStatement("return services;")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
            };

            var paramArray = new[]
            {
                SyntaxFactory.Parameter(SyntaxFactory.Identifier("services"))
                .WithType(SyntaxFactory.ParseTypeName("this IServiceCollection")),
                SyntaxFactory.Parameter(SyntaxFactory.Identifier("config"))
                .WithType(SyntaxFactory.ParseTypeName("IConfiguration"))
            };

            ClassAssembler
            .Configure()
            .ImportNamespaces(importNameSpaces)
            .CreateNamespace(nameSpace)
            .CreateClass(modifiers, className)
            .AddMethod(
                modifiers
                , SyntaxFactory.ParseTypeName($"IServiceCollection")
                , methodName
                , paramArray
                , statements)
            .Generate(path, className);

            ExecuteCommandUtility.Run("cd ../../");
        }
        public static void Build(string solutionName)
        {
            Console.WriteLine(ExecuteCommandUtility.Run($"echo CREATING SOLUTION"));
            Console.WriteLine(ExecuteCommandUtility.Run($"dotnet new sln -n {solutionName}"));

            _commands.ForEach(cmd => Console.WriteLine(ExecuteCommandUtility.Run(cmd)));
        }
        public static void Build(string path)
        {
            Console.WriteLine(ExecuteCommandUtility.Run($"echo ADD HEALTH CHECK EXTENSION"));
            const string className = "HealthCheckExtensions";

            Directory.CreateDirectory(path);
            ExecuteCommandUtility.Run("cd Extensions");

            var importNameSpaces = new List <NamespaceModel>
            {
                new NamespaceModel("Microsoft.AspNetCore.Builder"),
                new NamespaceModel("Microsoft.Extensions.DependencyInjection"),
            };

            var statementsOne = new List <StatementSyntax>
            {
                SyntaxFactory.ParseStatement("return services;")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
            };
            var statementsTwo = new List <StatementSyntax>
            {
                SyntaxFactory.ParseStatement("return app;")
                .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
            };

            var modifiers = new[]
            {
                SyntaxFactory.Token(SyntaxKind.PublicKeyword)
                , SyntaxFactory.Token(SyntaxKind.StaticKeyword)
            };

            var paramArrayOne = new[] { SyntaxFactory.Parameter(SyntaxFactory.Identifier("services"))
                                        .WithType(SyntaxFactory.ParseTypeName("this IServiceCollection")) };

            var paramArrayTwo = new[] { SyntaxFactory.Parameter(SyntaxFactory.Identifier("app"))
                                        .WithType(SyntaxFactory.ParseTypeName("this IApplicationBuilder")) };

            ClassAssembler
            .Configure()
            .ImportNamespaces(importNameSpaces)
            .CreateNamespace("Api.Extensions")
            .CreateClass(modifiers, className)
            .AddMethod(
                modifiers
                , SyntaxFactory.ParseTypeName($"IServiceCollection")
                , "AddMicroserviceHealthChecks"
                , paramArrayOne
                , statementsOne)
            .AddMethod(
                modifiers
                , SyntaxFactory.ParseTypeName($"IApplicationBuilder")
                , "UseMicroserviceHealthChecks"
                , paramArrayTwo
                , statementsTwo
                )
            .Generate(path, className);
        }
Exemplo n.º 4
0
        public static void Build(string path)
        {
            Console.WriteLine(ExecuteCommandUtility.Run($"echo ADD STARTUP"));
            ExecuteCommandUtility.Run("rm Startup.cs");

            ClassAssembler
            .Configure()
            .ImportNamespaces(_namespaceModels)
            .CreateNamespace("Api")
            .CreateClass(new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword) }, "Startup")
            .AddGetProperty("IConfiguration", "Configuration", SyntaxKind.PrivateKeyword)
            .AddStartupConstructor()
            .AddStartupConfigureServices()
            .AddStartupConfigure()
            .Generate(path, "Startup")
            ;
        }
        private static int RunScaffolding(string[] args)
        {
            var app = new CommandLineApplication
            {
                Name                   = "Scaffolding CLI",
                Description            = "A CQRS and Mediator scaffolding CLI",
                AllowArgumentSeparator = true,
            };

            app.HelpOption(true);
            //Top level command which can split into two directions
            app.Command("new", configCmd =>
            {
                configCmd.OnExecute(() =>
                {
                    Console.WriteLine("Scaffold a new solution or extend the domain");
                    configCmd.ShowHelp();
                    return(1);
                });

                //Split 1: sln
                configCmd.Command("sln", setCmd =>
                {
                    setCmd.Description =
                        "Scaffold a new solution including the API, Logic, DB, Unit and Integration test projects";
                    var nameArgument = setCmd.Option("-n| --name <NAME>", "Name of the solution", CommandOptionType.SingleValue).IsRequired();

                    setCmd.OnExecute(() =>
                    {
                        var path = Directory.GetCurrentDirectory();
                        BuilderSolution.Build(nameArgument.Value());
                        BuildStartup.Build($"{path}/API");
                        BuildCorsExtension.Build($"{path}/API/Extensions");
                        BuildHealthCheckExtensions.Build($"{path}/API/Extensions");
                        BuildDatabaseServiceCollectionExtensions.Build($"{path}/DB/Configuration");
                        BuildLogicServiceCollectionExtensions.Build($"{path}/Logic/Configuration");

                        Console.WriteLine(ExecuteCommandUtility.Run($"echo DONE"));
                    });
                });

                //Split 2: domain
                configCmd.Command("domain", setCmd =>
                {
                    setCmd.Description = "Extent the domain with new handlers";

                    var operationType = setCmd.Option(
                        "-ot|--operationType <TYPE>",
                        "Can either be [command] or [query]",
                        CommandOptionType.SingleValue)
                                        .IsRequired(false,
                                                    "Must specify an operation type: Can either be [command] or [query] i.e -ot|--operationType <TYPE>");

                    var concern = setCmd.Option(
                        "-c|--concern <NAME>",
                        "Name of the concern",
                        CommandOptionType.SingleValue)
                                  .IsRequired(false, "Name of the concern: -c|--concern <NAME>");

                    var operation = setCmd.Option(
                        "-o|--operation <NAME>",
                        "Name of the operation",
                        CommandOptionType.SingleValue)
                                    .IsRequired(false, "Name of the operation: -o|--operation <NAME>");

                    var groupBy = setCmd.Option(
                        "-g|--groupBy <TYPE>",
                        "Group domain objects by [C] for concerns or [O] for operations, defaults to concerns",
                        CommandOptionType.SingleValue);

                    setCmd.OnExecute(() =>
                    {
                        var groupByType = GroupByType.Concern;
                        if (groupBy.HasValue())
                        {
                            groupByType = (groupBy.Value()?.ToLower()) switch
                            {
                                "c" => GroupByType.Concern,
                                "o" => GroupByType.Operation,
                                _ => GroupByType.Concern
                            };
                        }

                        var operationTypeBuilderResult = OperationTypeResolver.Resolve(operationType.Value());
                        if (operationTypeBuilderResult == OperationType.UNSUPPORTED)
                        {
                            LogUtility.Error("Invalid operation type parameter: must specify [c] for command or [q] for query");
                            return(0);
                        }

                        BuildResponse.Build(concern.Value(), operation.Value(), groupByType);

                        switch (operationTypeBuilderResult)
                        {
                        case OperationType.COMMAND:
                            BuildCommand.Build(concern.Value(), operation.Value(), groupByType);
                            break;

                        case OperationType.QUERY:
                            BuildQuery.Build(concern.Value(), operation.Value(), groupByType);
                            break;

                        case OperationType.UNSUPPORTED:
                            LogUtility.Error(
                                "Invalid operation type parameter: must specify [c] for command or [q] for query");
                            break;

                        default:
                            LogUtility.Error(
                                "Invalid operation type parameter: must specify [c] for command or [q] for query");
                            break;
                        }
                        ;

                        BuildHandler.Build(concern.Value(), operation.Value(), operationTypeBuilderResult, groupByType);

                        return(0);
                    });