public void DisplayAssemblyTree(AssemblyFacet AssemblyFacet) { foreach (NamespaceFacet @namespace in AssemblyFacet.Namespaces) { // namespace node var namespaceNode = new TreeNode(@namespace.Name) { ImageIndex = 0 }; if (AssemblyFacet.Namespaces.Count == 1) { namespaceNode.Expand(); } TreeView.Nodes.Add(namespaceNode); // class nodes DisplayFacet(@namespace.Classes, namespaceNode, "Classes"); // enumeration nodes DisplayFacet(@namespace.Enumerations, namespaceNode, "Enumerations"); // interface nodes DisplayFacet(@namespace.Interfaces, namespaceNode, "Interfaces"); // struct nodes DisplayFacet(@namespace.Structs, namespaceNode, "Structs"); } }
public void Execute() { // build the assembly Facet from the xml doc root AssemblyFacet assemblyFacet = new AssemblyFacet(codeDoc.Root); // generate assembly code from template GenerateAssemblyCodeFromTemplate(assemblyFacet); }
public void Execute() { // build the assembly Facet from the xml doc root AssemblyFacet assemblyFacet = new AssemblyFacet(codeDoc.Root); // generate assembly code from template GenerateAssemblyCodeFromTemplate(assemblyFacet); }
/// <summary> /// Returns additional required ObjC import directives. /// </summary> /// <param name="facet"></param> /// <returns></returns> public List <string> ObjCImportDirectives(CodeFacet facet, bool filter = true) { List <string> imports = new List <string>(); if (!Config.GenerateFacetBinding(facet)) { return(imports); } // objC type // note that a constructor has no return type string objCType = facet.ObjCFacet.Type; string importStr = null; if (!string.IsNullOrEmpty(objCType)) { // we only need to import types defined in the current assembly. // other types will be imported via their own assembly's header if (AssemblyFacet.DefinesFacetType(facet.Type)) { // if the type is an enum if (facet.IsEnum) { objCType = facet.ObjCFacet.Type; importStr = $"#import \"{objCType}.h\""; imports.Add(importStr); } } } // forward declare objC facet types for all children List <CodeFacet> children = facet.Children(); foreach (CodeFacet child in children) { imports.AddRange(ObjCImportDirectives(child, false)); } // remove duplicates imports = imports.Distinct().ToList(); imports.Sort(); // the filter ensures that the import list doesn't include // the header that the import directives will be inserted into if (filter && importStr != null) { imports.Remove(importStr); } return(imports); }
public void GenerateAssemblyCodeFromTemplate(AssemblyFacet assemblyFacet) { string templateFileName = null; // Create the template host TemplatingEngineHost templateHost = new TemplatingEngineHost(); templateHost.TemplateFileValue = "Net2ObjC.tt"; // utilise the runtime template N2ObjC = new Net2ObjC(); N2ObjC.Host = templateHost; N2ObjC.AssemblyFacet = assemblyFacet; N2ObjC.XMLFilePath = XMLFile; // run the template LogText = N2ObjC.TransformText(); bool saveLog = false; // save the logOutput if required if (saveLog) { string outputFileName = Path.GetFileNameWithoutExtension(templateFileName); outputFileName = Path.Combine(Path.GetDirectoryName(templateFileName), outputFileName); outputFileName = outputFileName + "1" + templateHost.FileExtension; File.WriteAllText(outputFileName, LogText); } // format errors if (templateHost.Errors != null) { StringBuilder errors = new StringBuilder(); foreach (CompilerError error in templateHost.Errors) { errors.AppendLine(error.ToString()); errors.AppendLine(" "); } ErrorText = errors.ToString(); } }
public void GenerateAssemblyCodeFromTemplate(AssemblyFacet assemblyFacet) { string templateFileName = null; // Create the template host TemplatingEngineHost templateHost = new TemplatingEngineHost(); templateHost.TemplateFileValue = "Net2ObjC.tt"; // utilise the runtime template N2ObjC = new Net2ObjC(); N2ObjC.Host = templateHost; N2ObjC.AssemblyFacet = assemblyFacet; N2ObjC.XMLFilePath = XMLFile; // run the template LogText = N2ObjC.TransformText(); bool saveLog = false; // save the logOutput if required if (saveLog) { string outputFileName = Path.GetFileNameWithoutExtension(templateFileName); outputFileName = Path.Combine(Path.GetDirectoryName(templateFileName), outputFileName); outputFileName = outputFileName + "1" + templateHost.FileExtension; File.WriteAllText(outputFileName, LogText, templateHost.FileEncoding); } // format errors if (templateHost.Errors != null) { StringBuilder errors = new StringBuilder(); foreach (CompilerError error in templateHost.Errors) { errors.AppendLine(error.ToString()); errors.AppendLine(" "); } ErrorText = errors.ToString(); } }
private void DoReflection() { TreeView.Nodes.Clear(); // get the binding flags var bindingFlags = ShowInheritiedMembersCheckBox.Checked ? AssemblyParser.BindingFlagsWithHierarchy : AssemblyParser.BindingFlagsDeclaredOnly; // parse the assembly via reflection into XML _assemblyParser = new AssemblyParser(); _xml = _assemblyParser.ParseAssembly(_assembly, _assemblyFileName, bindingFlags); // load xml into xdoc XDocument codeDoc = XDocument.Parse(_xml); // build the assembly Facet from the xml doc root AssemblyFacet assemblyFacet = new AssemblyFacet(codeDoc.Root); // iterate over the assemblyFacet to populate the tree DisplayAssemblyTree(assemblyFacet); // enable export ExportButton.Enabled = true; }
// // WriteInterfaceFilePreliminaries // public void WriteInterfaceFilePreliminaries() { string objCAssemblyHeaderName = ObjCIdentifierFromManagedIdentifier(AssemblyFacet.Name); string objCAssemblyExtraHeader = objCAssemblyHeaderName + ".__Extra__.h"; #line default #line hidden #line 170 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("#import <Dubrovnik/Dubrovnik.h>\r\n\r\n//\r\n// Extra include\r\n//\r\n#if __has_include(\""); #line default #line hidden #line 176 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write(this.ToStringHelper.ToStringWithCulture(objCAssemblyExtraHeader)); #line default #line hidden #line 176 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("\")\r\n#import \""); #line default #line hidden #line 177 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write(this.ToStringHelper.ToStringWithCulture(objCAssemblyExtraHeader)); #line default #line hidden #line 177 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("\"\t// Not auto generated. Add manually to project.\r\n#endif\r\n\r\n"); #line default #line hidden #line 180 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" if (AssemblyFacet.References.Count() > 0) { #line default #line hidden #line 183 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("\r\n//\r\n// Referenced assemblies\r\n//\r\n"); #line default #line hidden #line 188 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" } foreach (CodeFacet reference in AssemblyFacet.References) { string includeFile = reference.Name; string defineName = (ObjCIdentifierFromManagedIdentifier(includeFile) + "_Included").ToUpper(); #line default #line hidden #line 195 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("#ifndef "); #line default #line hidden #line 196 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write(this.ToStringHelper.ToStringWithCulture(defineName)); #line default #line hidden #line 196 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("\r\n//#import \""); #line default #line hidden #line 197 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write(this.ToStringHelper.ToStringWithCulture(includeFile)); #line default #line hidden #line 197 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write(".h\"\r\n#endif\r\n\r\n"); #line default #line hidden #line 200 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" } #line default #line hidden #line 203 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" this.Write("\r\n//\r\n// Class aliases\r\n//\r\n"); #line default #line hidden #line 208 "C:\Users\Jonathan Mitchell\Documents\Dubrovnik\dotNET\Dubrovnik.Tools\Dubrovnik.Tools\Net2ObjC.tt" foreach (CodeFacet facet in AssemblyFacet.AllFacets().OrderBy(f => f.Type)) { WriteClassPredeclaration(facet); } }
/// <summary> /// Returns all ObjC import directives required to fully derive an ObjC class from its subclass and its adopted protocols. /// These directives constitute the minimum required to define a class in an ObjC interface header file. /// </summary> /// <param name="facet">Facet</param> /// <returns>List of ObjC import directives.</returns> public List <string> ObjCDerivationImportDirectives(CodeFacet facet) { List <string> imports = new List <string>(); // iterate over the sub facets required to derive the native representation List <CodeFacet> derivation = facet.Derivation(); foreach (CodeFacet cursor in derivation) { string objCType = null; // Managed interfaces don't derive from a base type but from other interfaces (see GetInterfaces) // https://msdn.microsoft.com/en-us/library/system.type.basetype(v=vs.110).aspx // However, our native implementation of the managed interface is a class of System.Object. if (cursor.GetType() == typeof(InterfaceFacet)) { if (AssemblyFacet.DefinesFacetType("System.Object")) { objCType = "System_Object"; imports.Add($"#import \"{objCType}.h\""); } } // for all interfaces we require to import a protocol if (cursor.GetType() == typeof(ImplementedInterfaceFacet) || cursor.GetType() == typeof(InterfaceFacet)) { if (!AssemblyFacet.DefinesFacetType(cursor.Type)) { continue; } // if this interface type is not required then just skip it. // this means that some of the interface methods etc may be represented // and others may not depending on the type exclusion settings if (!Config.GenerateFacetBinding(cursor)) { continue; } objCType = cursor.ObjCFacet.Type; imports.Add($"#import \"{objCType}_Protocol.h\""); } // use base type else { string baseType = cursor.BaseType; // System.Object has no base type if (baseType == null) { continue; } // Derived import directives will only be required for types // defined in the target assembly. If this is not the case // then the required import will have to be defined elsewhere, // most likely in a framework header. if (!AssemblyFacet.DefinesFacetType(baseType)) { continue; } // if we are not generating bindings for the given type then // a header file won't be available string importPrefix = ""; string importSuffix = ""; if (!Config.GenerateTypeBinding(baseType)) { importPrefix = "//"; importSuffix = " // class base defaults to System.Object"; } objCType = cursor.ObjCFacet.BaseType; imports.Add($"{importPrefix}#import \"{objCType}.h\"{importSuffix}"); } } // return a distinct list to remove duplicates imports = imports.Distinct().ToList(); imports.Sort(); return(imports); }