コード例 #1
0
        public int Generate(
            string wszInputFilePath,
            string bstrInputFileContents,
            string wszDefaultNamespace,
            IntPtr[] rgbOutputFileContents,
            out uint pcbOutput,
            IVsGeneratorProgress pGenerateProgress)
        {
            try
            {
                pGenerateProgress.Progress(5);

                var options = GetOptionsFromOptionsPage();

                string baseDirectory = Path.GetDirectoryName(wszInputFilePath);
                string apiName       = Path.GetFileNameWithoutExtension(wszInputFilePath);

                if (options.UseUserOptions)
                {
                    string optionsPath = Path.Combine(baseDirectory, $"{apiName}.RestEaseOptions");

                    if (!File.Exists(optionsPath))
                    {
                        try
                        {
                            string json = _optionsFactory.Serialize(options);
                            File.WriteAllText(optionsPath, json);
                        }
                        catch
                        {
                            Trace.WriteLine($"Unable to write custom settings file '{optionsPath}'.");
                        }
                    }
                    else
                    {
                        try
                        {
                            var restEaseUserOptions = _optionsFactory.Deserialize(File.ReadAllText(optionsPath));
                            options.MergeWith(restEaseUserOptions);
                        }
                        catch
                        {
                            Trace.WriteLine($"Unable to read custom settings file '{optionsPath}'.");
                        }
                    }
                }

                Trace.WriteLine("Generating Interface and Models");
                var settings = AutoMapperUtils.Instance.Mapper.Map <GeneratorSettings>(options);
                settings.SingleFile = true;
                settings.Namespace  = wszDefaultNamespace;
                settings.ApiName    = apiName;

                //var settings = new GeneratorSettings
                //{
                //    SingleFile = true,
                //    Namespace = wszDefaultNamespace,
                //    ApiName = apiName,
                //    ArrayType = options.ArrayType,
                //    UseDateTimeOffset = options.UseDateTimeOffset,
                //    MethodReturnType = options.MethodReturnType,
                //    AppendAsync = options.AppendAsync,
                //    GenerateFormUrlEncodedExtensionMethods = options.GenerateFormUrlEncodedExtensionMethods,
                //    GenerateApplicationOctetStreamExtensionMethods = options.GenerateApplicationOctetStreamExtensionMethods,
                //    GenerateMultipartFormDataExtensionMethods = options.GenerateMultipartFormDataExtensionMethods,
                //    MultipartFormDataFileType = options.MultipartFormDataFileType,
                //    ApplicationOctetStreamType = options.ApplicationOctetStreamType,
                //    ApiNamespace = options.ApiNamespace,
                //    ModelsNamespace = options.ModelsNamespace,
                //    ReturnObjectFromMethodWhenResponseIsDefinedButNoModelIsSpecified = options.ReturnObjectFromMethodWhenResponseIsDefinedButNoModelIsSpecified,
                //    PreferredContentType = options.PreferredContentType,
                //    ForceContentTypeToApplicationJson = options.ForceContentTypeToApplicationJson,
                //    UseOperationIdAsMethodName = options.UseOperationIdAsMethodName,
                //    PreferredSecurityDefinitionType = options.PreferredSecurityDefinitionType,
                //    GeneratePrimitivePropertiesAsNullableForOpenApi20 = options.GeneratePrimitivePropertiesAsNullableForOpenApi20
                //};
                var result = _generator.FromStream(File.OpenRead(wszInputFilePath), settings, out var diagnostic);

                if (options.FailOnOpenApiErrors && diagnostic.Errors.Any())
                {
                    var errorMessages = string.Join(" | ", diagnostic.Errors.Select(e => JsonSerializer.Serialize(e)));
                    Trace.WriteLine($"OpenApi Errors: {errorMessages}");

                    pcbOutput = 0;
                    return(1);
                }

                pGenerateProgress.Progress(90);
                string code = result.First().Content;

                rgbOutputFileContents[0] = code.ConvertToIntPtr(out pcbOutput);
                pGenerateProgress.Progress(100);

                Trace.WriteLine("All done");
            }
            catch (Exception e)
            {
                pGenerateProgress.GeneratorError(e);
                Trace.WriteLine("Unable to generate code");
                Trace.WriteLine(e);
                throw;
            }

            return(0);
        }