static void GenerateCU(XmlParsedDocument doc) { if (doc.XDocument == null || doc.XDocument.RootElement == null) { doc.Add(new Error(ErrorType.Error, 1, 1, "No root node found.")); return; } XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName("x", "Class")]; if (rootClass == null) { doc.Add(new Error(ErrorType.Error, 1, 1, "Root node does not contain an x:Class attribute.")); return; } bool isApplication = doc.XDocument.RootElement.Name.Name == "Application"; string rootNamespace, rootType, rootAssembly; XamlG.ParseXmlns(rootClass.Value, out rootType, out rootNamespace, out rootAssembly); CompilationUnit cu = new CompilationUnit(doc.FileName); doc.CompilationUnit = cu; DomRegion rootRegion = doc.XDocument.RootElement.Region; if (doc.XDocument.RootElement.IsClosed) { rootRegion.End = doc.XDocument.RootElement.ClosingTag.Region.End; } DomType declType = new DomType(cu, ClassType.Class, Modifiers.Partial | Modifiers.Public, rootType, doc.XDocument.RootElement.Region.Start, rootNamespace, rootRegion); cu.Add(declType); DomMethod initcomp = new DomMethod(); initcomp.Name = "InitializeComponent"; initcomp.Modifiers = Modifiers.Public; initcomp.ReturnType = DomReturnType.Void; declType.Add(initcomp); DomField _contentLoaded = new DomField("_contentLoaded"); _contentLoaded.ReturnType = new DomReturnType("System.Boolean"); if (isApplication) { return; } cu.Add(new DomUsing(DomRegion.Empty, "System")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Controls")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Documents")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Input")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Media")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Media.Animation")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Shapes")); cu.Add(new DomUsing(DomRegion.Empty, "System.Windows.Controls.Primitives")); // Dictionary<string,string> namespaceMap = new Dictionary<string, string> (); // namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml"; XName nameAtt = new XName("x", "Name"); foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements) { XAttribute name = el.Attributes [nameAtt]; if (name != null && name.IsComplete) { string type = ResolveType(el); if (type == null || type.Length == 0) { doc.Add(new Error(ErrorType.Error, el.Region.Start, "Could not find namespace for '" + el.Name.FullName + "'.")); } else { declType.Add(new DomField(name.Value, Modifiers.Internal, el.Region.Start, new DomReturnType(type))); } } } }
static void GenerateCU(XmlParsedDocument doc) { if (doc.XDocument == null || doc.XDocument.RootElement == null) { doc.Add(new Error(ErrorType.Error, "No root node found.", 1, 1)); return; } XAttribute rootClass = doc.XDocument.RootElement.Attributes [new XName("x", "Class")]; if (rootClass == null) { doc.Add(new Error(ErrorType.Error, "Root node does not contain an x:Class attribute.", 1, 1)); return; } bool isApplication = doc.XDocument.RootElement.Name.Name == "Application"; string rootNamespace, rootType, rootAssembly; XamlG.ParseXmlns(rootClass.Value, out rootType, out rootNamespace, out rootAssembly); var cu = new DefaultParsedDocument(doc.FileName); DomRegion rootRegion = doc.XDocument.RootElement.Region; if (doc.XDocument.RootElement.IsClosed) { rootRegion = new DomRegion(doc.XDocument.RootElement.Region.FileName, doc.XDocument.RootElement.Region.Begin, doc.XDocument.RootElement.ClosingTag.Region.End); } var declType = new DefaultUnresolvedTypeDefinition(rootNamespace, rootType) { Kind = TypeKind.Class, Accessibility = Accessibility.Public, Region = rootRegion }; cu.TopLevelTypeDefinitions.Add(declType); var initcomp = new DefaultUnresolvedMethod(declType, "InitializeComponent") { ReturnType = KnownTypeReference.Void, Accessibility = Accessibility.Public }; declType.Members.Add(initcomp); var _contentLoaded = new DefaultUnresolvedField(declType, "_contentLoaded") { ReturnType = KnownTypeReference.Boolean }; // was missing in the original code: correct ? // declType.Fields.Add (_contentLoaded); if (isApplication) { return; } // cu.Add (new DomUsing (DomRegion.Empty, "System")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Documents")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Input")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Media.Animation")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Shapes")); // cu.Add (new DomUsing (DomRegion.Empty, "System.Windows.Controls.Primitives")); // Dictionary<string,string> namespaceMap = new Dictionary<string, string> (); // namespaceMap["x"] = "http://schemas.microsoft.com/winfx/2006/xaml"; XName nameAtt = new XName("x", "Name"); foreach (XElement el in doc.XDocument.RootElement.AllDescendentElements) { XAttribute name = el.Attributes [nameAtt]; if (name != null && name.IsComplete) { string type = ResolveType(el); if (type == null || type.Length == 0) { cu.Add(new Error(ErrorType.Error, "Could not find namespace for '" + el.Name.FullName + "'.", el.Region.Begin)); } else { declType.Members.Add(new DefaultUnresolvedField(declType, name.Value) { Accessibility = Accessibility.Internal, Region = el.Region, ReturnType = new DefaultUnresolvedTypeDefinition(type) }); } } } }
protected override BuildResult Compile(IProgressMonitor monitor, SolutionEntityItem item, BuildData buildData) { MoonlightProject proj = item as MoonlightProject; if (proj == null) { return(base.Compile(monitor, item, buildData)); } var objDir = GetObjDir(proj, buildData.Configuration); if (!Directory.Exists(objDir)) { Directory.CreateDirectory(objDir); } var codeDomProvider = proj.LanguageBinding.GetCodeDomProvider(); string appName = proj.Name; var toResGen = new List <FilePath> (); List <BuildResult> results = new List <BuildResult> (); foreach (ProjectFile pf in proj.Files) { if (pf.BuildAction == BuildAction.Resource || pf.BuildAction == BuildAction.Page || pf.BuildAction == BuildAction.ApplicationDefinition) { toResGen.Add(pf.FilePath); } if (pf.FilePath.Extension == ".xaml" && pf.Generator == "MSBuild:MarkupCompilePass1") { var outFile = objDir.Combine(proj.LanguageBinding.GetFileName(pf.FilePath.FileName + ".g")); buildData.Items.Add(new ProjectFile(outFile, BuildAction.Compile)); if (!File.Exists(outFile) || File.GetLastWriteTime(outFile) < File.GetLastWriteTime(pf.FilePath)) { string rel = pf.ProjectVirtualPath; monitor.Log.WriteLine("Generating codebehind accessors for {0}...", rel); BuildResult result = XamlG.GenerateFile(codeDomProvider, appName, pf.FilePath, rel, outFile); if (result.Failed) { return(result); } results.Add(result); } } } string resFile = objDir.Combine(appName + ".g.resources"); if (toResGen.Count > 0) { DateTime lastMod = DateTime.MinValue; if (File.Exists(resFile)) { lastMod = File.GetLastWriteTime(resFile); } foreach (string f in toResGen) { if (File.GetLastWriteTime(f) > lastMod) { BuildResult result = Respack(monitor, proj, toResGen, resFile); if (result.Failed) { return(result); } results.Add(result); break; } } buildData.Items.Add(new ProjectFile(resFile, BuildAction.EmbeddedResource) { ResourceId = appName + ".g.resources" }); } else { if (File.Exists(resFile)) { File.Delete(resFile); } } return(base.Compile(monitor, item, buildData).Append(results)); }