コード例 #1
0
        /// <summary>
        /// Generate the `Image.Generated.cs` file.
        /// </summary>
        /// <remarks>
        /// This is used to generate the `Image.Generated.cs` file (<see cref="Image"/>).
        /// </remarks>
        /// <param name="indent">Indentation level.</param>
        /// <returns>The `Image.Generated.cs` as string.</returns>
        private string Generate(string indent = "        ")
        {
            // get the list of all nicknames we can generate docstrings for.
            var allNickNames = NetVips.GetOperations();

            // remove operations we have to wrap by hand
            var exclude = new[]
            {
                "scale",
                "ifthenelse",
                "bandjoin",
                "bandrank",
                "composite",
                "case"
            };

            allNickNames = allNickNames.Where(x => !exclude.Contains(x)).ToList();

            const string preamble = @"//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     libvips version: {0}
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------";

            var stringBuilder =
                new StringBuilder(string.Format(preamble,
                                                $"{NetVips.Version(0)}.{NetVips.Version(1)}.{NetVips.Version(2)}"));

            stringBuilder.AppendLine()
            .AppendLine()
            .AppendLine("namespace NetVips")
            .AppendLine("{")
            .AppendLine("    public sealed partial class Image")
            .AppendLine("    {")
            .AppendLine($"{indent}#region auto-generated functions")
            .AppendLine();
            foreach (var nickname in allNickNames)
            {
                try
                {
                    stringBuilder.AppendLine(GenerateFunction(nickname, indent));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            stringBuilder.AppendLine($"{indent}#endregion")
            .AppendLine()
            .AppendLine($"{indent}#region auto-generated properties")
            .AppendLine();

            var tmpFile       = Image.NewTempFile("%s.v");
            var allProperties = tmpFile.GetFields();

            foreach (var property in allProperties)
            {
                var type = GTypeToCSharp(tmpFile.GetTypeOf(property));
                stringBuilder.AppendLine($"{indent}/// <summary>")
                .AppendLine($"{indent}/// {tmpFile.GetBlurb(property)}")
                .AppendLine($"{indent}/// </summary>")
                .AppendLine($"{indent}public {type} {property.ToPascalCase()} => ({type})Get(\"{property}\");")
                .AppendLine();
            }

            stringBuilder.AppendLine($"{indent}#endregion")
            .AppendLine("    }")
            .AppendLine("}");

            return(stringBuilder.ToString());
        }