Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeGenConsoleWrapper"/> class.
        /// </summary>
        /// <param name="company">The company name.</param>
        /// <param name="appName">The application/domain name.</param>
        /// <param name="apiName">The Web API name.</param>
        /// <param name="outDir">The output path/directory.</param>
        /// <param name="assemblies">Optional list of assemblies to probe for resources.</param>
        private CodeGenConsoleWrapper(Assembly[] assemblies, string company, string appName, string apiName = "Api", string?outDir = null)
        {
            Company = Check.NotEmpty(company, nameof(company));
            AppName = Check.NotEmpty(appName, nameof(appName));
            ApiName = Check.NotEmpty(apiName, nameof(apiName));

            _exeDir    = CodeGenFileManager.GetExeDirectory();
            OutDir     = string.IsNullOrEmpty(outDir) ? new DirectoryInfo(_exeDir).Parent.FullName : outDir;
            Assemblies = new List <Assembly>(assemblies ?? Array.Empty <Assembly>());
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override async Task <CodeGenStatistics> OnCodeGenerationAsync()
        {
            OnWriteMasthead();
            OnWriteHeader();

            var cmd    = _cmdArg !.ParsedValue;
            var exedir = GetBaseExeDirectory();

            var company = Args.GetCompany(false);
            var appName = Args.GetAppName(false);

            if (company == null || appName == null)
            {
                throw new CodeGenException($"Parameters '{CompanyParamName}' and {AppNameParamName}  must be specified.");
            }

            // Where XML to YAML requested do so, then exit.
            if (_x2yOpt !.HasValue())
            {
                if (await CodeGenFileManager.ConvertXmlToYamlAsync(cmd, Args.ConfigFileName ?? CodeGenFileManager.GetConfigFilename(exedir, cmd, company, appName)).ConfigureAwait(false))
                {
                    return(new CodeGenStatistics());
                }
                else
                {
                    throw new CodeGenException("An error occured whilst converting XML to YAML.");
                }
            }

            var count = 0;
            var stats = new CodeGenStatistics();

            if (IsDatabaseSupported && cmd.HasFlag(CommandType.Database))
            {
                stats.Add(await ExecuteCodeGenerationAsync(_databaseScript, CodeGenFileManager.GetConfigFilename(exedir, CommandType.Database, company, appName), count++).ConfigureAwait(false));
            }

            if (IsRefDataSupported && cmd.HasFlag(CommandType.RefData))
            {
                stats.Add(await ExecuteCodeGenerationAsync(_refDataScript, CodeGenFileManager.GetConfigFilename(exedir, CommandType.RefData, company, appName), count++).ConfigureAwait(false));
            }

            if (IsEntitySupported && cmd.HasFlag(CommandType.Entity))
            {
                stats.Add(await ExecuteCodeGenerationAsync(_entityScript, CodeGenFileManager.GetConfigFilename(exedir, CommandType.Entity, company, appName), count++).ConfigureAwait(false));
            }

            if (IsDataModelSupported && cmd.HasFlag(CommandType.DataModel))
            {
                stats.Add(await ExecuteCodeGenerationAsync(_dataModelScript, CodeGenFileManager.GetConfigFilename(exedir, CommandType.DataModel, company, appName), count++).ConfigureAwait(false));
            }

            if (count > 1)
            {
                Args.Logger?.LogInformation(new string('-', 80));
                Args.Logger?.LogInformation("");
                Args.Logger?.LogInformation($"{AppName} OVERALL. {stats.ToSummaryString()}");
                Args.Logger?.LogInformation("");
            }

            return(stats);
        }
Esempio n. 3
0
        /// <summary>
        /// Executes the underlying <see cref="CodeGenConsole"/> using the code generation arguments.
        /// </summary>
        /// <param name="args">The code generation arguments.</param>
        /// <returns><b>Zero</b> indicates success; otherwise, unsucessful.</returns>
        public async Task <int> RunAsync(string[] args)
        {
            using var app = new CommandLineApplication()
                  {
                      Name        = "beef.codegen",
                      Description = "Business Entity Execution Framework (Beef) Code Generator."
                  };

            var cmd = app.Argument <CommandType>("command", "Execution command type: Entity, Database, RefData or All.", false).IsRequired();
            var cs  = app.Option("-cs|--connectionString", "Override the connection string for Database.", CommandOptionType.SingleValue);
            var cf  = app.Option("-cf|--configFile", "Override the filename for the configuration.", CommandOptionType.SingleValue).Accepts(v => v.ExistingFile());
            var sf  = app.Option("-s|--scriptFile", "Override the filename for the script orchestration.", CommandOptionType.SingleValue).Accepts(v => v.ExistingFile());
            var enc = app.Option("-enc|--expectNoChanges", "Expect no changes in the artefact output and error where changes are detected (e.g. within build pipeline).", CommandOptionType.NoValue);
            var x2y = app.Option("-x2y|--xmlToYaml", "Convert the XML configuration into YAML equivalent (will not codegen).", CommandOptionType.NoValue);

            app.OnExecuteAsync(async(_) =>
            {
                var ct     = cmd.Value == null ? CommandType.All : Enum.Parse <CommandType>(cmd.Value, true);
                string?cfn = null;
                if (cf.HasValue())
                {
                    if (ct == CommandType.All)
                    {
                        throw new CommandParsingException(app, "Command 'All' is not compatible with --configFile; the command must be more specific when using a specified configuration file.");
                    }

                    cfn = cf.Value() !;
                }

                string?sfn = null;
                if (sf.HasValue())
                {
                    if (ct == CommandType.All)
                    {
                        throw new CommandParsingException(app, "Command 'All' is not compatible with --scriptFile; the command must be more specific when using a specified script file.");
                    }

                    sfn = sf.Value() !;
                }

                if (x2y.HasValue())
                {
                    if (ct == CommandType.All)
                    {
                        throw new CommandParsingException(app, "Command 'All' is not compatible with --xmlToYaml; the command must be more specific when converting XML configuration to YAML.");
                    }

                    CodeGenConsole.WriteMasthead();
                    return(await CodeGenFileManager.ConvertXmlToYamlAsync(ct, cfn ?? CodeGenFileManager.GetConfigFilename(_exeDir, ct, Company, AppName)).ConfigureAwait(false));
                }

                var encArg = enc.HasValue() ? " --expectNoChanges" : string.Empty;

                var rc = 0;
                if (IsDatabaseSupported && ct.HasFlag(CommandType.Database))
                {
                    rc = await CodeGenConsole.Create().RunAsync(AppendAssemblies(ReplaceMoustache($"\"{cfn ?? CodeGenFileManager.GetConfigFilename(_exeDir, CommandType.Database, Company, AppName)}\"" + " " + DatabaseCommandLineTemplate, sfn ?? _databaseScript) + (cs.HasValue() ? $" -p \"ConnectionString={cs.Value()}\""
                        : (string.IsNullOrEmpty(ConnectionString) ? "" : $" -p \"ConnectionString={ConnectionString}\"")) + encArg)).ConfigureAwait(false);
                }

                if (rc == 0 && IsRefDataSupported && ct.HasFlag(CommandType.RefData))
                {
                    rc = await CodeGenConsole.Create().RunAsync(AppendAssemblies(ReplaceMoustache($"\"{cfn ?? CodeGenFileManager.GetConfigFilename(_exeDir, CommandType.RefData, Company, AppName)}\"" + " " + RefDataCommandLineTemplate, sfn ?? _refDataScript) + encArg)).ConfigureAwait(false);
                }

                if (rc == 0 && IsEntitySupported && ct.HasFlag(CommandType.Entity))
                {
                    rc = await CodeGenConsole.Create().RunAsync(AppendAssemblies(ReplaceMoustache($"\"{cfn ?? CodeGenFileManager.GetConfigFilename(_exeDir, CommandType.Entity, Company, AppName)}\"" + " " + EntityCommandLineTemplate, sfn ?? _entityScript) + encArg)).ConfigureAwait(false);
                }

                if (rc == 0 && IsDataModelSupported && ct.HasFlag(CommandType.DataModel))
                {
                    rc = await CodeGenConsole.Create().RunAsync(AppendAssemblies(ReplaceMoustache($"\"{cfn ?? CodeGenFileManager.GetConfigFilename(_exeDir, CommandType.DataModel, Company, AppName)}\"" + " " + DataModelCommandLineTemplate, sfn ?? _dataModelScript) + encArg)).ConfigureAwait(false);
                }

                return(rc);
            });

            try
            {
                return(await app.ExecuteAsync(args).ConfigureAwait(false));
            }
            catch (CommandParsingException cpex)
            {
                Console.Error.WriteLine(cpex.Message);
                return(-1);
            }
        }