Esempio n. 1
0
        public ExecuteResultEnum Build(ICommandOptions options)
        {
            if (!_configFile.Exists())
            {
                _reportService.Error($"Config file not found: {Configuration.ConfigurationFile}");   // why do we use Configuration here?
                _reportService.Output($"\tPlease make sure you are in the right working directory");
                return(ExecuteResultEnum.Error);
            }

            RunAutoUpdate(options);

            if (RunConfigVersionCheck(options) == ExecuteResultEnum.Aborted)
            {
                return(ExecuteResultEnum.Aborted);
            }

            _reportService.PrintTitle("Build DataContext from spocr.json");

            var config           = _configFile.Config;
            var project          = config?.Project;
            var schemas          = config?.Schema;
            var connectionString = project?.DataBase?.ConnectionString;

            var hasSchemas          = schemas?.Any() ?? false;
            var hasConnectionString = string.IsNullOrWhiteSpace(connectionString);

            if (!hasConnectionString)
            {
                _dbContext.SetConnectionString(connectionString);
            }

            if (!hasSchemas)
            {
                _reportService.Error($"Schema is empty: {Configuration.ConfigurationFile}"); // why do we use Configuration here?
                _reportService.Output($"\tPlease run pull to get the DB-Schema.");
                return(ExecuteResultEnum.Error);
            }

            var stopwatch = new Stopwatch();
            var elapsed   = new Dictionary <string, long>();

            var codeBaseAlreadyExists = project.Role.Kind == ERoleKind.Extension;

            if (!codeBaseAlreadyExists)
            {
                stopwatch.Start();
                _reportService.PrintSubTitle("Generating CodeBase");
                _output.GenerateCodeBase(project.Output, options.DryRun);
                elapsed.Add("CodeBase", stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating TableTypes");
            _engine.GenerateDataContextTableTypes(options.DryRun);
            elapsed.Add("TableTypes", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Inputs");
            _engine.GenerateDataContextInputs(options.DryRun);
            elapsed.Add("Inputs", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Outputs");
            _engine.GenerateDataContextOutputs(options.DryRun);
            elapsed.Add("Outputs", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating Models");
            _engine.GenerateDataContextModels(options.DryRun);
            elapsed.Add("Models", stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            _reportService.PrintSubTitle("Generating StoredProcedures");
            _engine.GenerateDataContextStoredProcedures(options.DryRun);
            elapsed.Add("StoredProcedures", stopwatch.ElapsedMilliseconds);

            var summary = elapsed.Select(_ => $"{_.Key} generated in {_.Value} ms.");

            _reportService.PrintSummary(summary, $"SpocR v{_spocr.Version.ToVersionString()}");
            _reportService.PrintTotal($"Total elapsed time: {elapsed.Sum(_ => _.Value)} ms.");

            if (options.DryRun)
            {
                _reportService.PrintDryRunMessage();
            }

            return(ExecuteResultEnum.Succeeded);
        }