예제 #1
0
        protected override async Task <bool> ProcessInternal()
        {
            var codeGenerator = this.plugin;

            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);

            // get internal name
            var language = new[] {
                "CSharp",
                "Ruby",
                "NodeJS",
                "TypeScript",
                "Python",
                "Go",
                "Php",
                "Java",
                "AzureResourceSchema",
                "JsonRpcClient"
            }
            .Where(x => x.ToLowerInvariant() == codeGenerator)
            .First();

            // 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") ?? false;

            int defaultMaximumCommentColumns = codeGenerator == "go" ? 120 : Settings.DefaultMaximumCommentColumns;

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

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

            foreach (PropertyInfo propertyInfo in typeof(GeneratorSettingsTS).GetProperties())
            {
                string propertyName          = propertyInfo.Name;
                string kebabCasePropertyName = propertyName.ToKebabCase();
                Type   propertyType          = propertyInfo.PropertyType;
                if (propertyType == typeof(bool))
                {
                    bool?propertyValue = await GetValue <bool?>(kebabCasePropertyName);

                    if (propertyValue.HasValue)
                    {
                        Settings.Instance.CustomSettings[propertyName] = propertyValue.Value;
                    }
                }
                else if (propertyType == typeof(string))
                {
                    string propertyValue = await GetValue <string>(kebabCasePropertyName);

                    if (propertyValue != null)
                    {
                        Settings.Instance.CustomSettings[propertyName] = propertyValue;
                    }
                }
                else
                {
                    throw new NotSupportedException($"Cannot convert command line argument --{kebabCasePropertyName} to {nameof(GeneratorSettingsTS)}.{propertyName} because type {propertyType} is not supported.");
                }
            }

            // process
            var plugin = ExtensionsLoader.GetPlugin(
                (await GetValue <bool?>("azure-arm") ?? false ? "Azure." : "") +
                language +
                (await GetValue <bool?>("fluent") ?? false ? ".Fluent" : "") +
                (await GetValue <bool?>("testgen") ?? false ? ".TestGen" : ""));

            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
            var outFS    = Settings.Instance.FileSystemOutput;
            var outFiles = outFS.GetFiles("", "*", System.IO.SearchOption.AllDirectories);

            foreach (var outFile in outFiles)
            {
                WriteFile(outFile, outFS.ReadAllText(outFile), null);
            }

            return(true);
        }
예제 #2
0
        protected override async Task <bool> ProcessInternal()
        {
            var codeGenerator = this.plugin;

            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);

            // get internal name
            var language = new[] {
                "CSharp",
                "Ruby",
                "NodeJS",
                "TypeScript",
                "Python",
                "Go",
                "Php",
                "Java",
                "AzureResourceSchema",
                "JsonRpcClient"
            }
            .Where(x => x.ToLowerInvariant() == codeGenerator)
            .First();

            // 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") ?? false;

            int defaultMaximumCommentColumns = codeGenerator == "go" ? 120 : Settings.DefaultMaximumCommentColumns;

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

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

            Settings.Instance.CustomSettings.Add("GenerateMetadata", await GetValue("generate-metadata"));
            if (codeGenerator == "csharp")
            {
                Settings.Instance.Header = $"<auto-generated>\n{Settings.Instance.Header}\n</auto-generated>";
            }
            if (codeGenerator == "ruby" || codeGenerator == "python")
            {
                // TODO: sort out matters here entirely instead of relying on Input being read somewhere...
                var inputFile = await GetValue <string[]>("input-file");

                Settings.Instance.Input       = inputFile.FirstOrDefault();
                Settings.Instance.PackageName = await GetValue("package-name");

                Settings.Instance.PackageVersion = await GetValue("package-version");
            }
            if (codeGenerator == "go")
            {
                Settings.Instance.PackageVersion = await GetValue("package-version");
            }
            if (codeGenerator == "typescript")
            {
                Settings.Instance.PackageName = await GetValue("package-name");

                Settings.Instance.PackageVersion = await GetValue("package-version");
            }

            // process
            var plugin = ExtensionsLoader.GetPlugin(
                (await GetValue <bool?>("azure-arm") ?? false ? "Azure." : "") +
                language +
                (await GetValue <bool?>("fluent") ?? false ? ".Fluent" : "") +
                (await GetValue <bool?>("testgen") ?? false ? ".TestGen" : ""));

            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
            var outFS    = Settings.Instance.FileSystemOutput;
            var outFiles = outFS.GetFiles("", "*", System.IO.SearchOption.AllDirectories);

            foreach (var outFile in outFiles)
            {
                WriteFile(outFile, outFS.ReadAllText(outFile), null);
            }

            return(true);
        }