Пример #1
0
        /// <summary>
        /// Generates client using provided settings.
        /// </summary>
        /// <param name="settings">Code generator settings.</param>
        public static void Generate(Settings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            Logger.Entries.Clear();
            Logger.LogInfo(Resources.AutoRestCore, Version);
            Modeler modeler = ExtensionsLoader.GetModeler(settings);
            ServiceClient serviceClient = null;

            IEnumerable<ValidationMessage> messages = new List<ValidationMessage>();
            try
            {
                serviceClient = modeler.Build(out messages);
            }
            catch (Exception exception)
            {
                throw ErrorManager.CreateError(exception, Resources.ErrorGeneratingClientModel, exception.Message);
            }
            finally
            {
                // Make sure to log any validation messages
                foreach (var message in messages)
                {
                    Logger.Entries.Add(new LogEntry(message.Severity, message.ToString()));
                }
                if (messages.Any(entry => entry.Severity >= settings.ValidationLevel))
                {
                    throw ErrorManager.CreateError(null, Resources.ErrorGeneratingClientModel, Resources.CodeGenerationError);
                }
            }

            CodeGenerator codeGenerator = ExtensionsLoader.GetCodeGenerator(settings);
            Logger.WriteOutput(codeGenerator.UsageInstructions);

            settings.Validate();
            try
            {
                codeGenerator.NormalizeClientModel(serviceClient);
                codeGenerator.Generate(serviceClient).GetAwaiter().GetResult();
            }
            catch (Exception exception)
            {
                throw ErrorManager.CreateError(exception, Resources.ErrorSavingGeneratedCode, exception.Message);
            }
        }
Пример #2
0
        public void TestSettingsFromSwagger()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Modeler = "Swagger",
                    CodeGenerator = "CSharp",
                    Input = Path.Combine("Swagger", "swagger-x-ms-code-generation-settings.json"),
                    Header = "NONE"
                };
                var modeler = ExtensionsLoader.GetModeler();
                var client = modeler.Build();
                var plugin = ExtensionsLoader.GetPlugin() as PluginCs;
                Assert.NotNull(plugin);
                settings.Validate();

                Assert.Equal("MIT", settings.Header);
                Assert.Equal(true, plugin.Settings.InternalConstructors);
            }
        }
Пример #3
0
        public void TestSettingsFromSwagger()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Modeler = "Swagger",
                CodeGenerator = "CSharp",
                Input = Path.Combine("Swagger", "swagger-x-ms-code-generation-settings.json"),
                Header = "NONE"
            };
            var modeler = ExtensionsLoader.GetModeler(settings);
            var client = modeler.Build();
            var codeGenerator = ExtensionsLoader.GetCodeGenerator(settings) as CSharpCodeGenerator;
            settings.Validate();

            Assert.Equal("MIT", settings.Header);
            Assert.Equal(true, codeGenerator.InternalConstructors);
        }