コード例 #1
0
        public async Task <int> OnExecute(CommandLineApplication app)
        {
            if (string.IsNullOrEmpty(ConfigPath))
            {
                app.Out.WriteLine("Please enter the path to build configuration file:");
                return(1);
            }
            try
            {
                var build = new JsonBuilder(ConfigPath);
                if (build.Config.Tables.Count == 0)
                {
                    app.Out.WriteLine($"Process error.Detail message:no table in appsettings.json.");
                    return(1);
                }
                foreach (var p in build.Config.Tables)
                {
                    var tableName    = p.Name.ToLower();
                    var columns      = GetColumns(build.Config.Project.ConnectionString, tableName);
                    var context      = new ContextBuild(build.Config.Project, tableName, columns, build.Config.DbTypeMaps);
                    var bulidContent = await build.TemplateEngine.Render(context, viewPath);

                    await new FileOutput().Output(bulidContent, tableName, build.Config.Output);
                }
            }
            catch (Exception e)
            {
                app.Out.WriteLine($"Process error.Detail message:{e.Message}");
                return(1);
            }
            return(0);
        }
コード例 #2
0
        public async Task <string> Render(ContextBuild model, string viewPath)
        {
            using (var serviceScope = _scopeFactory.CreateScope())
            {
                var helper = serviceScope.ServiceProvider.GetRequiredService <OfficialRazorViewToStringRenderer>();
                if (Path.IsPathRooted(viewPath))
                {
                    var tempFileName = $"{Path.GetFileNameWithoutExtension(viewPath)}-{Guid.NewGuid():N}{Path.GetExtension(viewPath)}";
                    var destFileName = Path.Combine(_temp, tempFileName);
                    File.Copy(viewPath, destFileName);
                    viewPath = Path.Combine(TEMP, tempFileName);
                    var result = await helper.RenderViewToStringAsync(viewPath, model);

                    File.Delete(destFileName);
                    return(result);
                }
                else
                {
                    return(await helper.RenderViewToStringAsync(viewPath, model));
                }
            }
        }