Пример #1
0
        protected override async Task <bool> ProcessInternal()
        {
            if (this.Plugin == "csharp-simplifier")
            {
                var fs = new MemoryFileSystem();

                // setup filesystem
                var files = await ListInputs();

                foreach (var file in files)
                {
                    fs.WriteAllText(file, await ReadFile(file));
                }

                // simplify
                new AutoRest.Simplify.CSharpSimplifier().Run(fs).ConfigureAwait(false).GetAwaiter().GetResult();
                await WriteFiles(fs);

                return(true);
            }
            else
            {
                var files = await ListInputs();

                if (files.Length != 1)
                {
                    throw new Exception($"Generator received incorrect number of inputs: {files.Length} : {string.Join(",", files)}");
                }
                var modelAsJson = (await ReadFile(files[0])).EnsureYamlIsJson();
                var codeModelT  = new ModelSerializer <CodeModel>().Load(modelAsJson);

                // build settings
                var altNamespace = (await GetValue <string[]>("input-file") ?? new[] { "" }).FirstOrDefault()?.Split('/').Last().Split('\\').Last().Split('.').First();

                new Settings
                {
                    Namespace  = await GetValue("namespace"),
                    ClientName = GetXmsCodeGenSetting <string>(codeModelT, "name") ?? await GetValue("override-client-name"),
                    PayloadFlatteningThreshold = GetXmsCodeGenSetting <int?>(codeModelT, "ft") ?? await GetValue <int?>("payload-flattening-threshold") ?? 0,
                    AddCredentials             = await GetValue <bool?>("add-credentials") ?? false,
                    Host = this
                };
                var header = await GetValue("license-header");

                if (header != null)
                {
                    Settings.Instance.Header = header;
                }
                Settings.Instance.CustomSettings.Add("InternalConstructors", GetXmsCodeGenSetting <bool?>(codeModelT, "internalConstructors") ?? await GetValue <bool?>("use-internal-constructors") ?? false);
                Settings.Instance.CustomSettings.Add("SyncMethods", GetXmsCodeGenSetting <string>(codeModelT, "syncMethods") ?? await GetValue("sync-methods") ?? "essential");
                Settings.Instance.CustomSettings.Add("UseDateTimeOffset", GetXmsCodeGenSetting <bool?>(codeModelT, "useDateTimeOffset") ?? await GetValue <bool?>("use-datetimeoffset") ?? false);
                Settings.Instance.CustomSettings["ClientSideValidation"] = await GetValue <bool?>("client-side-validation") ?? true;

                int defaultMaximumCommentColumns = Settings.DefaultMaximumCommentColumns;
                Settings.Instance.MaximumCommentColumns = await GetValue <int?>("max-comment-columns") ?? defaultMaximumCommentColumns;

                Settings.Instance.OutputFileName = await GetValue <string>("output-file");

                Settings.Instance.Header = $"<auto-generated>\n{Settings.Instance.Header}\n</auto-generated>";

                // process
                var plugin = ExtensionsLoader.GetPlugin(
                    this.Plugin,
                    await GetValue <bool?>("azure-arm") ?? false,
                    await GetValue <bool?>("fluent") ?? false);
                Settings.PopulateSettings(plugin.Settings, Settings.Instance.CustomSettings);

                using (plugin.Activate())
                {
                    Settings.Instance.Namespace = Settings.Instance.Namespace ?? CodeNamer.Instance.GetNamespaceName(altNamespace);
                    var codeModel = plugin.Serializer.Load(modelAsJson);
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);
                    if (await GetValue <bool?>("sample-generation") ?? false)
                    {
                        plugin.CodeGenerator.GenerateSamples(codeModel).GetAwaiter().GetResult();
                    }
                    else
                    {
                        plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();
                    }
                }

                // write out files
                await WriteFiles(Settings.Instance.FileSystemOutput);

                return(true);
            }
        }
Пример #2
0
        protected override async Task <bool> ProcessInternal()
        {
            if (this.Plugin == "csharp-simplifier")
            {
                var fs = new MemoryFileSystem();

                // setup filesystem
                var files = await ListInputs();

                foreach (var file in files)
                {
                    fs.WriteAllText(file, await ReadFile(file));
                }

                new Settings
                {
                    Host = this
                };

                // simplify
                new AutoRest.Simplify.CSharpSimplifier().Run(fs).ConfigureAwait(false).GetAwaiter().GetResult();
                await WriteFiles(fs);

                return(true);
            }
            else
            {
                var files = await ListInputs();

                if (files.Length != 1)
                {
                    throw new Exception($"Generator received incorrect number of inputs: {files.Length} : {string.Join(",", files)}");
                }

                Console.Error.WriteLine("Loading code model as JSON.");

                var modelAsJson = (await ReadFile(files[0])).EnsureYamlIsJson();
                var codeModelT  = new ModelSerializer <CodeModel>().Load(modelAsJson);

                Console.Error.WriteLine("Process settings.");

                // build settings
                var inputFile    = (await GetValue <string[]>("input-file") ?? new[] { "" }).FirstOrDefault();
                var altNamespace = inputFile?.Split('/').Last().Split('\\').Last().Split('.').First();

                new Settings
                {
                    Namespace  = await GetValue("namespace"),
                    ClientName = GetXmsCodeGenSetting <string>(codeModelT, "name") ?? await GetValue("override-client-name"),
                    PayloadFlatteningThreshold = GetXmsCodeGenSetting <int?>(codeModelT, "ft") ?? await GetValue <int?>("payload-flattening-threshold") ?? 0,
                    AddCredentials             = await GetValue <bool?>("add-credentials") ?? false,
                    Host = this
                };
                var header = await GetValue("license-header");

                if (header != null)
                {
                    Settings.Instance.Header = header;
                }
                Settings.Instance.CustomSettings.Add("InternalConstructors", GetXmsCodeGenSetting <bool?>(codeModelT, "internalConstructors") ?? await GetValue <bool?>("use-internal-constructors") ?? false);
                Settings.Instance.CustomSettings.Add("SyncMethods", GetXmsCodeGenSetting <string>(codeModelT, "syncMethods") ?? await GetValue("sync-methods") ?? "essential");
                Settings.Instance.CustomSettings.Add("UseDateTimeOffset", GetXmsCodeGenSetting <bool?>(codeModelT, "useDateTimeOffset") ?? await GetValue <bool?>("use-datetimeoffset") ?? false);
                Settings.Instance.CustomSettings["ClientSideValidation"] = await GetValue <bool?>("client-side-validation") ?? true;

                Settings.Instance.CustomSettings["HandleInternalServerError"] = await GetValue <bool?>("handle-internal-server-error") ?? true;

                Settings.Instance.CustomSettings["HttpClientTimeoutMinutes"] = await GetValue <int?>("http-client-timeout-minutes") ?? 10;

                int defaultMaximumCommentColumns = 160;
                Settings.Instance.MaximumCommentColumns = await GetValue <int?>("max-comment-columns") ?? defaultMaximumCommentColumns;

                Settings.Instance.OutputFileName = await GetValue <string>("output-file");

                Settings.Instance.CodeGenerationMode = (await GetValue <bool?>("rest-server") ?? false) ? "rest-server" : "rest-client";
                Settings.Instance.Header             =
                    $"<auto-generated>\n" +
                    $"Code generated by Flexi REST Code Generator {Settings.Version} based on Microsoft (R) AutoRest Code Generator.\n" +
                    $"Changes may cause incorrect behavior and will be lost if the code is regenerated.\n" +
                    $"Flexi REST Code Generator doc: FlexiRESTClientCodeGenerator.docx\n" +
                    $"URL: {@"http://vs-shpt/specs/_layouts/15/DocIdRedir.aspx?ID=KPWR5ZWTMEY4-20-3488"}\n" +
                    $"Source swagger file: {inputFile}\n" +
                    $"</auto-generated>";

                // process
                var plugin = ExtensionsLoader.GetPlugin(
                    this.Plugin,
                    await GetValue <bool?>("azure-arm") ?? false,
                    await GetValue <bool?>("fluent") ?? false);
                Settings.PopulateSettings(plugin.Settings, Settings.Instance.CustomSettings);

                using (plugin.Activate())
                {
                    Settings.Instance.Namespace = Settings.Instance.Namespace ?? CodeNamer.Instance.GetNamespaceName(altNamespace);

                    Console.Error.WriteLine("Deserializing code model.");

                    var codeModel = plugin.Serializer.Load(modelAsJson);
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);
                    if (await GetValue <bool?>("sample-generation") ?? false)
                    {
                        Console.Error.WriteLine("Generating sample code.");
                        plugin.CodeGenerator.GenerateSamples(codeModel).GetAwaiter().GetResult();
                    }
                    else
                    {
                        Console.Error.WriteLine("Generating code.");
                        plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult();
                    }
                }

                // write out files
                Console.Error.WriteLine("Writing files.");
                await WriteFiles(Settings.Instance.FileSystemOutput);

                Console.Error.WriteLine("Done.");
                return(true);
            }
        }