/// <summary> /// Returns whether a designed class should be generated. /// </summary> private static bool ShouldGenerateForXib(ClassDescriptionCollector visitor, String className) { // Not clean, but needed anyway... if (String.Equals ("FirstResponder", className)) { return false; } // TODO: Skip classes that are standard ones IEnumerable<IBPartialClassDescription> enumerable = visitor [className]; return (enumerable.SelectMany (d => d.Outlets).Count () > 0 || enumerable.SelectMany (d => d.Actions).Count () > 0); }
private static void GenerateXibDesignCode(CodeBehindWriter writer, ProjectFile file, ProjectFile designerFile) { // Create the compilation unit CodeCompileUnit ccu = new CodeCompileUnit (); CodeNamespace ns = new CodeNamespace (((DotNetProject)file.Project).GetDefaultNamespace (designerFile.FilePath)); ccu.Namespaces.Add (ns); // Add namespaces ns.Imports.AddRange(CodeBehindGenerator.GetNamespaces(file.Project)); // Parse the XIB document to collect the class names and their definitions IBDocument document = IBDocument.LoadFromFile (file.FilePath.FullPath); ClassDescriptionCollector collector = new ClassDescriptionCollector (); document.Root.Accept (collector); // Iterate over the extracted class names foreach (String className in collector.ClassNames) { CodeTypeDeclaration ctd = CodeBehindGenerator.GetType(collector, writer, className); if (ctd != null) { ns.Types.Add(ctd); } } // Write the result writer.Write (ccu, designerFile.FilePath); }
public static CodeTypeDeclaration GetType(ClassDescriptionCollector collector, CodeBehindWriter writer, String className) { List<IBPartialClassDescription> descriptions = collector[className]; // Don't generate the partial class if there are neither actions nor outlets int actions = descriptions.Sum (description => description.Actions.Count); int outlets = descriptions.Sum (description => description.Outlets.Count); if (actions == 0 && outlets == 0) { return null; } // Create the partial class CodeTypeDeclaration type = new CodeTypeDeclaration(className); type.IsClass = true; type.IsPartial = true; // Create fields for outlets foreach (IBPartialClassDescription.OutletDescription outlet in descriptions.SelectMany(description => description.Outlets)) { type.Members.Add(GetOutletField(outlet)); } // Create methods for actions foreach (IBPartialClassDescription.ActionDescription action in descriptions.SelectMany(description => description.Actions)) { type.Members.Add(GetPartialMethod(action, writer)); type.Members.Add(GetExposedMethod(action)); } return type; }
private IList<FilePath> GenerateCodeBehindForXib(ProjectTypeCache cache, CodeBehindWriter writer, FilePath file) { IDELogger.Log ("CodeBehindHandler::GenerateCodeBehindForXib -- Parsing {0}", file); // Parse and collect information from the document IBDocument document = IBDocument.LoadFromFile (file); ClassDescriptionCollector visitor = new ClassDescriptionCollector (); document.Root.Accept (visitor); List<FilePath> designerFiles = new List<FilePath> (); foreach (string className in visitor.ClassNames) { // Check if the class should be generated if (!ShouldGenerateForXib (visitor, className)) { IDELogger.Log ("CodeBehindHandler::GenerateCodeBehindForXib -- Skipping {0} (no outlets, no actions or reserved name)", className); continue; } // Generate the designer part FilePath designerFile = this.CodeGenerator.GenerateCodeBehindCode (cache, writer, className, visitor [className]); if (designerFile != FilePath.Null) { designerFiles.Add (designerFile); } } return designerFiles; }
public void TestMainMenuReading012() { String content = ReadResource(Resources.MainMenu_012); IBDocument document = IBDocument.LoadFromXml(content); CheckDocument(document); ClassDescriptionCollector collector = new ClassDescriptionCollector(); document.Root.Accept(collector); Assert.AreEqual(3, collector.ClassNames.Count()); Assert.IsTrue(collector.ClassNames.Contains("MainController")); IEnumerable<IBPartialClassDescription> classDescriptions = collector["MainController"]; IEnumerable<IBOutletDescriptor> outlets = classDescriptions.SelectMany(d => d.Outlets); IEnumerable<IBActionDescriptor> actions = classDescriptions.SelectMany(d => d.Actions); Assert.AreEqual(3, outlets.Count()); Assert.AreEqual(2, actions.Count()); }