Exemplo n.º 1
0
        private static void AddNickname(ulong gtype, ICollection <string> allNickNames)
        {
            var nickname = Base.NicknameFind(gtype);

            allNickNames.Add(nickname);

            IntPtr TypeMap(ulong type, IntPtr a, IntPtr b)
            {
                AddNickname(type, allNickNames);

                return(IntPtr.Zero);
            }

            Base.TypeMap(gtype, TypeMap);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generate the `Image.Generated.cs` file.
        /// </summary>
        /// <remarks>
        /// This is used to generate the `Image.Generated.cs` file (<see cref="Image"/>).
        /// Use it with something like:
        /// <code language="lang-csharp">
        /// File.WriteAllText("Image.Generated.cs", Operation.GenerateImageClass());
        /// </code>
        /// </remarks>
        /// <returns></returns>
        public static string GenerateImageClass(string indent = "        ")
        {
            // generate list of all nicknames we can generate docstrings for
            var allNickNames = new List <string>();

            IntPtr TypeMap(ulong type, IntPtr a, IntPtr b)
            {
                AddNickname(type, allNickNames);

                return(IntPtr.Zero);
            }

            Base.TypeMap(Base.TypeFromName("VipsOperation"), TypeMap);

            // Sort
            allNickNames.Sort();

            // Filter duplicates
            allNickNames = allNickNames.Distinct().ToList();

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

            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, $"{Base.Version(0)}.{Base.Version(1)}.{Base.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)
                {
                    // ignore
                }
            }

            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 = GValue.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());
        }