Exemple #1
0
    protected override async Task <bool> ProcessInternal()
    {
        new Settings
        {
            Namespace = await GetValue("namespace")
        };

        var files = await ListInputs();

        if (files.Length != 1)
        {
            return(false);
        }

        var content = await ReadFile(files[0]);

        var fs = new MemoryFileSystem();

        fs.WriteAllText(files[0], content);

        var serviceDefinition = SwaggerParser.Load(files[0], fs);
        var modeler           = new SwaggerModeler();
        var codeModel         = modeler.Build(serviceDefinition);

        var genericSerializer = new ModelSerializer <CodeModel>();
        var modelAsJson       = genericSerializer.ToJson(codeModel);

        WriteFile("codeMode.yaml", modelAsJson, null);

        return(true);
    }
        /// <summary>
        /// Generates client using provided settings.
        /// </summary>
        public static void Generate()
        {
            if (Settings.Instance == null)
            {
                throw new ArgumentNullException("settings");
            }
            Logger.Entries.Clear();
            Logger.LogInfo(Resources.AutoRestCore, Version);
            
            CodeModel codeModel = null;
            
            var modeler = ExtensionsLoader.GetModeler();

            try
            {
                IEnumerable<ValidationMessage> messages = new List<ValidationMessage>();

                // generate model from swagger 
                codeModel = modeler.Build(out messages);

                // After swagger Parser
                codeModel = RunExtensions(Trigger.AfterModelCreation, codeModel);

                // After swagger Parser
                codeModel = RunExtensions(Trigger.BeforeLoadingLanguageSpecificModel, codeModel);

                foreach (var message in messages)
                {
                    Logger.Entries.Add(new LogEntry(message.Severity, message.ToString()));
                }

                if (messages.Any(entry => entry.Severity >= Settings.Instance.ValidationLevel))
                {
                    throw ErrorManager.CreateError(null, Resources.ErrorGeneratingClientModel, "Errors found during Swagger validation");
                }
            }
            catch (Exception exception)
            {
                throw ErrorManager.CreateError(exception, Resources.ErrorGeneratingClientModel, exception.Message);
            }

            var plugin = ExtensionsLoader.GetPlugin();

            Logger.WriteOutput(plugin.CodeGenerator.UsageInstructions);

            Settings.Instance.Validate();
            try
            {
                var genericSerializer = new ModelSerializer<CodeModel>();
                var modelAsJson = genericSerializer.ToJson(codeModel);

                // ensure once we're doing language-specific work, that we're working
                // in context provided by the language-specific transformer. 
                using (plugin.Activate())
                {
                    // load model into language-specific code model
                    codeModel = plugin.Serializer.Load(modelAsJson);

                    // we've loaded the model, run the extensions for after it's loaded
                    codeModel = RunExtensions(Trigger.AfterLoadingLanguageSpecificModel, codeModel);
     
                    // apply language-specific tranformation (more than just language-specific types)
                    // used to be called "NormalizeClientModel" . 
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                    // next set of extensions
                    codeModel = RunExtensions(Trigger.AfterLanguageSpecificTransform, codeModel);


                    // next set of extensions
                    codeModel = RunExtensions(Trigger.BeforeGeneratingCode, codeModel);

                    // Generate code from CodeModel.
                    plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();
                }
            }
            catch (Exception exception)
            {
                throw ErrorManager.CreateError(exception, Resources.ErrorSavingGeneratedCode, exception.Message);
            }
        }