示例#1
0
        public static string Property(ScrapedProperty property, ScrapedClass parent, bool isInterface)
        {
            var output = "";

            //TODO: static/private properties shouldn't be here for interfaces

            output += property.GetTrivia();

            if (property.Type.Optional)
            {
                output += "[Optional]" + NewLine;
            }
            if (property.Type.Unwrapped)
            {
                output += "[Unwrapped]" + NewLine;
            }

            if (!isInterface) //todo: comment out private/static properties in interfaces
            {
                output += property.Public ? "public " : "private ";
                output += property.Static ? "static " : "";
            }
            if (property.Type.CSharpType == "Self")
            {
                property.Type.CSharpType = parent.Name;
            }
            output += property.Type.CSharpType + " ";
            output += property.CSharpName + " { ";
            output += property.PublicGetter ? "get; " : "private get; ";
            output += property.PublicSetter ? "set; " : "private set; ";
            output += "}" + NewLine + NewLine;

            return(output);
        }
示例#2
0
        public static string Notification(ScrapedNotification notification, ScrapedClass parent, bool isInterface)
        {
            var output = "";

            output += notification.GetTrivia();

            output += "public class " + notification.CSharpName + " : NSNotification" + NewLine + "{" + NewLine;
            output += "public " + notification.CSharpName + "() : base(\"\", null) { }" + NewLine + "}" + NewLine;
            return(output);
        }
示例#3
0
        public static string Typedef(ScrapedTypedef typeAlias, ScrapedClass parent, bool isInterface)
        {
            var output = "";

            output += typeAlias.GetTrivia();

            output += "public struct " + typeAlias.Alias + NewLine + "{" + NewLine;
            output +=
                "static public implicit operator " + typeAlias.Alias + "(" + typeAlias.RealType.CSharpType +
                " value)" +
                NewLine + "{" + NewLine +
                "return default(" + typeAlias.Alias + ");" + NewLine +
                "}" + NewLine;

            output += "static public implicit operator " + typeAlias.RealType.CSharpType + "(" + typeAlias.Alias + " value)" + NewLine +
                      "{" + NewLine + "return default(" + typeAlias.RealType.CSharpType + ");" + NewLine + "}" + NewLine + "}" + NewLine;
            return(output);
        }
示例#4
0
        public static string Struct(ScrapedStruct scrapedEnum, ScrapedClass parent, bool isInterface)
        {
            var output = "";

            output += scrapedEnum.GetTrivia();

            output += "public struct " + scrapedEnum.CSharpName + NewLine + "{" + NewLine;
            if (scrapedEnum.Members != null)
            {
                foreach (var member in scrapedEnum.Members)
                {
                    output += member.GetTrivia();

                    if (member.Type.Optional)
                    {
                        output += "[Optional]" + NewLine;
                    }
                    if (member.Type.Unwrapped)
                    {
                        output += "[Unwrapped]" + NewLine;
                    }

                    if (!isInterface) //todo: comment out private/static properties in interfaces
                    {
                        output += member.Public ? "public " : "";
                        output += member.Static ? "static " : "";
                    }
                    if (member.Type.CSharpType == "Self")
                    {
                        member.Type.CSharpType = scrapedEnum.CSharpName;
                    }

                    output += member.Type.CSharpType + " ";
                    output += member.CSharpName + " { get; ";
                    output += member.ReadOnly ? "private set; " : "set; ";
                    output += "}" + NewLine + NewLine;
                }
            }

            output += "}" + NewLine;
            return(output);
        }
示例#5
0
        public static string Enum(ScrapedEnum scrapedEnum, ScrapedClass parent, bool isInterface)
        {
            var output = "";

            output += scrapedEnum.GetTrivia();

            output += "public enum " + scrapedEnum.CSharpName + NewLine + "{" + NewLine;
            if (scrapedEnum.Members != null)
            {
                foreach (var member in scrapedEnum.Members)
                {
                    output += member.GetTrivia();

                    output += member.CSharpName + "," + NewLine;
                }
            }

            output += "}" + NewLine;
            return(output);
        }
示例#6
0
        public static string Method(ScrapedMethod method, ScrapedClass parent, bool isInterface)
        {
            var output   = "";
            var toOutput = method.GetTrivia();

            var toAddAfterSummary = NewLine;

            foreach (var param in method.Parameters)
            {
                if (param.Description == null)
                {
                    param.Description = "";
                }
                toAddAfterSummary += "/// <param name=\"" + param.Name + "\">" + ParseAsDescription(param.Description) +
                                     "</param>" + NewLine;
            }

            var differentiator = Differentiate(method);

            if (differentiator != "")
            {
                toAddAfterSummary +=
                    "/// <param name=\"NAME_YOUR_PARAMS\">DO NOT USE THIS PARAMETER - Instead make sure to name the parameters you're using.</param>" +
                    NewLine;
            }
            if (method.ReturnDescription != null)
            {
                toAddAfterSummary += "/// <returns>" + ParseAsDescription(method.ReturnDescription) + "</returns>" +
                                     NewLine;
            }

            if (method.IsOptional)
            {
                toOutput += "[InheritOptional]" + NewLine;
            }

            //Deal with optional/unwrapped return values using attributes
            if (method.ReturnType.Optional)
            {
                toOutput += "[return:Optional]" + NewLine;
            }
            if (method.ReturnType.Unwrapped)
            {
                toOutput += "[return:Unwrapped]" + NewLine;
            }

            if (differentiator != "")
            {
                toOutput += "[IgnoreParameter(\"NAME_YOUR_PARAMS\")]" + NewLine;
            }

            if (isInterface && (method.Static || method.IsConstructor || method.IsOptional))
            {
                //csharp doesn't allow static things in interfaces
                toOutput = toOutput.Replace("[", "//[");
            }
            output += toOutput.Replace("/// </summary>" + NewLine, "/// </summary>" + toAddAfterSummary);

            if (isInterface && (method.Static || method.IsConstructor || method.IsOptional))
            {
                output += "//";
            }

            if (!method.IsConstructor)
            {
                if (!isInterface) //TODO: comment out private/static methods
                {
                    output += method.Public ? "public " : "private ";
                    output += method.Static ? "static " : "virtual ";
                }

                if (method.ReturnType.CSharpType == "Self")
                {
                    method.ReturnType.CSharpType = parent.CSharpName;
                }

                output += method.ReturnType.CSharpType + " ";
                output += method.CSharpName + "(";

                output += string.Join(", ",
                                      method.Parameters.Select(param =>
                                                               (param.Type.Optional ? "[Optional] " : "") + (param.Type.Unwrapped ? "[Unwrapped] " : "") +
                                                               (param.Type.CSharpType == "Self" ? parent.CSharpName : param.Type.CSharpType) + " " + param.Name));

                output += differentiator + ")";
                if (!isInterface)
                {
                    output += " { ";
                    if (method.ReturnType.CSharpType != "void")
                    {
                        output += "return default(" + method.ReturnType.CSharpType + ");";
                    }
                    output += " }";
                }
                else
                {
                    output += ";";
                }
                output += NewLine + NewLine;
            }
            else if (method.IsConstructor)
            {
                output += "public " + parent.Name + "(";
                output += string.Join(", ",
                                      method.Parameters.Select(param =>
                                                               (param.Type.Optional ? "[Optional] " : "") + (param.Type.Unwrapped ? "[Unwrapped] " : "") +
                                                               (param.Type.CSharpType == "Self" ? parent.CSharpName : param.Type.CSharpType) + " " + param.Name));
                output += differentiator + ") { }" + NewLine + NewLine;
            }
            return(output);
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Class(string url, string defaultNamespace, bool isInterface = false, string[] usings = null)
        {
            Signatures = new List <string>();
            var parsed = new ScrapedClass(url);

            if (usings == null)
            {
                usings = new[]
                {
                    "ObjectiveC",
                    "System",
                    "SharpSwift.Attributes",
                    "Foundation",
                    "CoreGraphics",
                    "UIKit"
                };
            }

            //add all the usings
            var output = "using " + string.Join(";" + NewLine + "using ", usings) + ";";


            //namespace xyz {
            output += NewLine + NewLine + "namespace " + (!string.IsNullOrWhiteSpace(parsed.Namespace) ? parsed.Namespace : defaultNamespace) + NewLine + "{" + NewLine;


            //trivia, add the <see cref> tag
            output += parsed.GetTrivia().Replace("/// </summary>" + NewLine,
                                                 "/// </summary>" + NewLine +
                                                 "/// <see cref=\"" + url + "\"/>" + NewLine);


            //public class Name
            output += "public " + (isInterface ? "interface" : "class") + " " + parsed.Name;
            if (isInterface)
            {
                var m = 1;
            }

            if (!string.IsNullOrEmpty(parsed.Inherits))
            {
                output += " : " + parsed.Inherits + "//, ";
            }
            else
            {
                output += "//: ";
            }
            if (parsed.ConformsTo != null && parsed.ConformsTo.Any())
            {
                //All the interfaces are commented out, since they don't all work right
                output += string.Join(", ", parsed.ConformsTo);
            }

            output  = output.TrimEnd(' ', ',');
            output += NewLine + "{" + NewLine;

            if (!isInterface)
            {
                if (
                    !parsed.Members.OfType <ScrapedMethod>()
                    .Any(method => method.IsConstructor && method.Parameters.Count == 0))
                {
                    //Add a parameterless constructor for everything
                    output += "public " + parsed.Name + "() { }" + NewLine;
                }
            }

            //Remove repeated properties
            var allProperties = parsed.Members.OfType <ScrapedProperty>().ToList();

            for (var i = 0; i < allProperties.Count(); i++)
            {
                var currProperty = allProperties[i];
                var allWithSame  = allProperties.Where(
                    prop =>
                    prop != currProperty && prop.RawName == currProperty.RawName &&
                    prop.Type.RawSwift == currProperty.Type.RawSwift).ToList();

                for (var w = 0; w < allWithSame.Count(); w++)
                {
                    var withSame = allWithSame[w];
                    allProperties.Remove(withSame);
                    parsed.Members.Remove(withSame);
                }
            }


            foreach (var method in parsed.Members.OfType <ScrapedMethod>())
            {
                output += Finals.Method(method, parsed, isInterface);
            }

            foreach (var property in parsed.Members.OfType <ScrapedProperty>())
            {
                output += Finals.Property(property, parsed, isInterface);
            }

            output = output.TrimEnd() + NewLine + "}" + NewLine;

            //Enums and Typedefs don't need to be in the actual class

            foreach (var scrapedEnum in parsed.Members.OfType <ScrapedEnum>())
            {
                output += Finals.Enum(scrapedEnum, parsed, isInterface);
            }

            foreach (var typeAlias in parsed.Members.OfType <ScrapedTypedef>())
            {
                output += Finals.Typedef(typeAlias, parsed, isInterface);
            }

            foreach (var notification in parsed.Members.OfType <ScrapedNotification>())
            {
                output += Finals.Notification(notification, parsed, isInterface);
            }

            output = output.TrimEnd() + NewLine + NewLine;

            foreach (var strct in parsed.Members.OfType <ScrapedStruct>())
            {
                output += Finals.Struct(strct, parsed, isInterface);
            }

            return(output.TrimEnd() + NewLine + "}" + NewLine);
        }