private ContentFileV2 ReadContentElement() { ContentFileV2 data = new ContentFileV2(); string version = reader.GetAttribute("Version"); if (String.IsNullOrEmpty(version)) throw new XmlException("Version attribute not present"); if (version != "2") throw new XmlException("Version attribute must be 2"); reader.ReadStartElement(contentAtom); reader.MoveToContent(); data.Items = new List<ContentFileV2.Item>(); data.Targets = new List<ContentFileV2.Target>(); while (true) { if (String.ReferenceEquals(reader.Name, contentAtom)) { reader.ReadEndElement(); reader.MoveToContent(); break; } if (reader.NodeType == XmlNodeType.Element) { if (String.ReferenceEquals(reader.Name, itemGroupAtom)) { List<ContentFileV2.Item> items = ReadItemGroupElement(); data.Items.AddRange(items); continue; } else if (String.ReferenceEquals(reader.Name, targetAtom)) { ContentFileV2.Target target = ReadTargetElement(); foreach (var otherTarget in data.Targets) { if (String.CompareOrdinal(target.Name, otherTarget.Name) == 0) throw new XmlException("Duplicate target name '{0}'".CultureFormat(target.Name)); } data.Targets.Add(target); continue; } } throw new XmlException("Expected ItemGroup, PropertyGroup or Target element"); } return data; }
private void SafeExecute() { if (!NoLogo) { Console.WriteLine(Parser.LogoBanner); } if (!runningFromCommandLine) { Parser.GetTargetArguments(this); Output.Message(MessageImportance.Normal, Parser.CommandName + Parser.Arguments); } if (ShowHelp) { Console.WriteLine(Parser.Usage); return; } if (String.IsNullOrEmpty(ContentPath)) { Output.Error("A .content file must be specified"); return; } this.ContentPath = this.ContentPath.MakeFullPath(); if (!File.Exists(this.ContentPath)) { Output.Error("Content file '{0}' does not exist", this.ContentPath); return; } // Initialize properties from the environment and command line PropertyGroup globalProps = new PropertyGroup(); globalProps.AddFromEnvironment(); globalProps.AddWellKnownProperties( new ParsedPath(Assembly.GetExecutingAssembly().Location, PathType.File).VolumeAndDirectory, ContentPath.VolumeAndDirectory); globalProps.AddFromPropertyString(this.Properties); BuildContext buildContext = new BuildContext(this.Output, this.ContentPath); ContentFileV2 contentFile = null; try { contentFile = ContentFileReaderV2.ReadFile(this.ContentPath); } catch (Exception e) { throw new ContentFileException(this.ContentPath, (int)e.Data["LineNumber"], "Problem reading content file", e); } Output.Message(MessageImportance.Low, "Read content file '{0}'", this.ContentPath); ItemGroup globalItems = new ItemGroup(); globalItems.ExpandAndAddFromList(contentFile.Items, globalProps); List <CompilerClass> compilerClasses = LoadCompilerClasses(globalItems, globalProps); this.NewestAssemblyWriteTime = FindNewestAssemblyWriteTime(compilerClasses); this.ContentPathWriteTime = File.GetLastWriteTime(this.ContentPath); List <BuildTarget> BuildTargets = PrepareBuildTargets(contentFile.Targets, globalItems, globalProps); foreach (var buildTarget in BuildTargets) { bool compilerFound = false; foreach (var compilerClass in compilerClasses) { if (buildTarget.InputExtensions.SequenceEqual(compilerClass.InputExtensions) && buildTarget.OutputExtensions.SequenceEqual(compilerClass.OutputExtensions)) { compilerFound = true; string msg = String.Format("Building target '{0}' with '{1}' compiler", buildTarget.Name, compilerClass.Name); foreach (var input in buildTarget.InputFiles) { msg += Environment.NewLine + "\t" + input; } msg += Environment.NewLine + "\t->"; foreach (var output in buildTarget.OutputFiles) { msg += Environment.NewLine + "\t" + output; } Output.Message(MessageImportance.Normal, msg); if (TestMode) { continue; } compilerClass.ContextProperty.SetValue(compilerClass.Instance, buildContext, null); compilerClass.TargetProperty.SetValue(compilerClass.Instance, buildTarget, null); try { compilerClass.CompileMethod.Invoke(compilerClass.Instance, null); } catch (TargetInvocationException e) { ContentFileException contentEx = e.InnerException as ContentFileException; if (contentEx != null) { contentEx.EnsureFileNameAndLineNumber(buildContext.ContentFile, buildTarget.LineNumber); throw contentEx; } else { throw new ContentFileException( this.ContentPath, buildTarget.LineNumber, "Unable to compile target '{0}'".CultureFormat(buildTarget.Name), e.InnerException); } } // Ensure that the output files were generated foreach (var outputFile in buildTarget.OutputFiles) { if (!File.Exists(outputFile)) { throw new ContentFileException(this.ContentPath, buildTarget.LineNumber, "Output file '{0}' was not generated".CultureFormat(outputFile)); } } } } if (!compilerFound) { Output.Warning("No compiler found for target '{0}' for extensions '{1}' -> '{2}'", buildTarget.Name, String.Join(Path.PathSeparator.ToString(), buildTarget.InputExtensions), String.Join(Path.PathSeparator.ToString(), buildTarget.OutputExtensions)); } } Output.Message(MessageImportance.Normal, "Done"); }
private ContentFileV2 ReadContentElement() { ContentFileV2 data = new ContentFileV2(); string version = reader.GetAttribute("Version"); if (String.IsNullOrEmpty(version)) { throw new XmlException("Version attribute not present"); } if (version != "2") { throw new XmlException("Version attribute must be 2"); } reader.ReadStartElement(contentAtom); reader.MoveToContent(); data.Items = new List <ContentFileV2.Item>(); data.Targets = new List <ContentFileV2.Target>(); while (true) { if (String.ReferenceEquals(reader.Name, contentAtom)) { reader.ReadEndElement(); reader.MoveToContent(); break; } if (reader.NodeType == XmlNodeType.Element) { if (String.ReferenceEquals(reader.Name, itemGroupAtom)) { List <ContentFileV2.Item> items = ReadItemGroupElement(); data.Items.AddRange(items); continue; } else if (String.ReferenceEquals(reader.Name, targetAtom)) { ContentFileV2.Target target = ReadTargetElement(); foreach (var otherTarget in data.Targets) { if (String.CompareOrdinal(target.Name, otherTarget.Name) == 0) { throw new XmlException("Duplicate target name '{0}'".CultureFormat(target.Name)); } } data.Targets.Add(target); continue; } } throw new XmlException("Expected ItemGroup, PropertyGroup or Target element"); } return(data); }