/// <summary> /// Write an object back to the caller. /// </summary> /// <param name="obj"></param> public void Write(object obj) { try { if (obj == null) { _context.Response.StatusCode = StatusCode; _context.Response.OutputStream.Close(); } else { _context.Response.StatusCode = StatusCode; _context.Response.ContentEncoding = Encoding.UTF8; _context.Response.ContentType = "application/json"; var result = Encoding.UTF8.GetBytes(WebApiConverter.ObjectToJson(obj)); _context.Response.ContentLength64 = result.Length; _context.Response.OutputStream.Write(result, 0, result.Length); _context.Response.OutputStream.Close(); } } catch (Exception ex) { Logger.Write($"Write object back to client error:{ex}"); } }
public Result Handle() { var analyzer = new WebApiAnalyzer(new WebApiAnalysisContext()); var analysisResult = analyzer.Analyze(_configuration.InputPath); if (!analysisResult.Success) { return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}")); } log.Debug("Source analyzed"); var converter = new WebApiConverter(); var generator = new WebClientGenerator(); var emitter = new TypeScriptEmitter(); var serviceModels = converter.ConvertControllers(analysisResult.Value); log.Debug("Models converted"); foreach (var tsModel in serviceModels) { var contents = generator.GenerateService(tsModel); emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Service, contents); log.Debug($"Service {tsModel.Name} emitted"); new TsGenerator().GenerateServiceAST(tsModel, _configuration.OutputPath); } return(Result.CreateSuccess()); }
public Result Handle() { var syncer = new Synchronizer(); var modelConverter = new ModelConverter(); var webApiConverter = new WebApiConverter(); var modelGenerator = new ModelGenerator(); var webClientGenerator = new WebClientGenerator(); var tsGenerator = new TsGenerator(); var emitter = new TypeScriptEmitter(); Generator generatorEnum; if (!Enum.TryParse(_configuration.Generator, true, out generatorEnum)) { return(Result.CreateError("Unknown generator was specified")); } var analysisResult = syncer.Synchronize(_configuration.InputPath); if (!analysisResult.Success) { return(Result.CreateError($"Source analysis error: {analysisResult.ErrorMessage}")); } log.Debug("Source analyzed"); var tsClassModels = modelConverter.ConvertClasses(analysisResult.Value.DataModels.Classes); var tsEnumModels = modelConverter.ConvertEnums(analysisResult.Value.DataModels.Enums); var serviceModels = webApiConverter.ConvertControllers(analysisResult.Value.Controllers); log.Debug("Models converted"); foreach (var tsModel in tsClassModels) { if (generatorEnum == Generator.Template) { var contents = modelGenerator.GenerateClass(tsModel); emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Model, contents); } else if (generatorEnum == Generator.Compiler) { tsGenerator.GenerateDataModelAST(tsModel, _configuration.OutputPath); } log.Debug($"Class {tsModel.Name} emitted"); } foreach (var tsModel in tsEnumModels) { if (generatorEnum == Generator.Template) { var contents = modelGenerator.GenerateEnum(tsModel); emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Enum, contents); } else if (generatorEnum == Generator.Compiler) { tsGenerator.GenerateEnumAST(tsModel, _configuration.OutputPath); } log.Debug($"Enum {tsModel.Name} emitted"); } foreach (var tsModel in serviceModels) { if (generatorEnum == Generator.Template) { var contents = webClientGenerator.GenerateService(tsModel); emitter.Emit(_configuration.OutputPath, tsModel.Name, EmittedFileType.Service, contents); } else if (generatorEnum == Generator.Compiler) { tsGenerator.GenerateServiceAST(tsModel, _configuration.OutputPath); } log.Debug($"Service {tsModel.Name} emitted"); } return(Result.CreateSuccess()); }