private void GenerateManifests() { // Read in extra xml; make sure it's valid if (this.extraXmlStream == null) { this.extraXmlDoc = null; } else { try { this.extraXmlDoc = new XmlDocument(); this.extraXmlDoc.Load(this.extraXmlStream); } catch (XmlException xmle) { throw new XmlException("Problem with extra input XML", xmle); } } // Start doing work XmlTextReader manReader = null; XmlValidatingReader manValidatingReader = null; XmlSchemaCollection appXmlSchemaCollection = null, subXmlSchemaCollection = null; // Now scan manifest dependencies MGDepTracker mgDT = new MGDepTracker(); DirScanner.BeginScan((IFileOperator)mgDT, sourceDir); try { mgDT.VerifyDependencies(); } catch (MGDependencyException mgde) { throw mgde; } mgDT.CalculateSizes(); // Write application manifest XmlTextWriter appGen = new XmlTextWriter(appManStream, null); //XmlTextWriter appGen = new XmlTextWriter(Console.Out); appGen.Formatting = Formatting.Indented; appGen.WriteStartDocument(); appGen.WriteComment("Generated by mg v0.1 (managed)"); appGen.WriteStartElement("assembly"); appGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1"); appGen.WriteAttributeString("manifestVersion", "1.0"); //MGTemplateWriter.XmlOutput(appGen, mgPR.AppKeyValPairs, mgPR.DescriptionStr); mgPP.WriteAppParams(appGen); mgDT.FileXmlOutput(appGen); appGen.WriteStartElement("dependency"); mgDT.AssmXmlOutput(appGen); //MGPlatformWriter.XmlOutput(appGen); mgPP.WritePlatParams(appGen); appGen.WriteEndElement(); // dependency appGen.WriteEndElement(); // assembly appGen.WriteEndDocument(); appGen.Flush(); appManStream.Position = 0; // Verify application manifest appXmlSchemaCollection = new XmlSchemaCollection(); appXmlSchemaCollection.Add(null, new XmlTextReader(appManSchema)); manReader = new XmlTextReader(appManStream); manValidatingReader = new XmlValidatingReader(manReader); manValidatingReader.Schemas.Add(appXmlSchemaCollection); manValidatingReader.ValidationType = ValidationType.Schema; XmlDocument appManifest = new XmlDocument(); try { appManifest.Load(manValidatingReader); } catch (XmlSchemaException xmlse) { // Validation error throw new XmlException("Error validating final application manifest; check parameters", xmlse); } appManStream.Close(); if (extraXmlDoc != null) { XmlNode extraCopy = appManifest.ImportNode(extraXmlDoc.DocumentElement, true); appManifest.DocumentElement.AppendChild(extraCopy); } appManStream = new BufferedStream(new MemoryStream()); appManifest.Save(appManStream); appManStream.Position = 0; // Write subscription manifest XmlTextWriter subGen = new XmlTextWriter(subManStream, null); subGen.Formatting = Formatting.Indented; subGen.WriteStartDocument(); subGen.WriteComment("Generated by mg v0.1 (managed)"); subGen.WriteStartElement("assembly"); subGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1"); subGen.WriteAttributeString("manifestVersion", "1.0"); //MGSubManifestWriter.XmlOutput(subGen, mgPR.AppKeyValPairs, mgPR.SubKeyValPairs, mgPR.DescriptionStr); mgPP.WriteSubParams(subGen); subGen.WriteEndElement(); subGen.WriteEndDocument(); subGen.Flush(); subManStream.Position = 0; // Verify subscription manifest subXmlSchemaCollection = new XmlSchemaCollection(); subXmlSchemaCollection.Add(null, new XmlTextReader(subManSchema)); manReader = new XmlTextReader(subManStream); manValidatingReader = new XmlValidatingReader(manReader); manValidatingReader.Schemas.Add(subXmlSchemaCollection); manValidatingReader.ValidationType = ValidationType.Schema; try { while (manValidatingReader.Read()) { ; } } catch (XmlSchemaException xmlse) { // Validation error throw new XmlException("Error validating final subscription manifest; check parameters", xmlse); } subManStream.Position = 0; }
public static void Main(string[] args) { string sourceDir, targetDir, paramFile, extraXmlFile; ManifestGenerator mg = null; MGParamParser mgPP = null; Stream paramFileStream = null, extraXmlStream = null; if (args.Length < 2 || args.Length > 3) { Console.WriteLine("usage: mg <source_dir> <param_file> [extra_xml]"); Console.WriteLine("<> indicates a parameter that is required."); Console.WriteLine("[] indicates a parameter that is optional."); return; } if (args.Length == 2) { sourceDir = args[0]; paramFile = args[1]; targetDir = args[0]; extraXmlFile = null; } else { sourceDir = args[0]; paramFile = args[1]; targetDir = args[0]; extraXmlFile = args[2]; } try { paramFileStream = File.Open(paramFile, FileMode.Open); } catch (FileNotFoundException fnfe) { Console.WriteLine(fnfe.ToString()); } try { if (extraXmlFile != null) { extraXmlStream = File.Open(extraXmlFile, FileMode.Open); } } catch (FileNotFoundException fnfe) { Console.WriteLine(fnfe.ToString()); } try { mgPP = new MGParamParser(paramFileStream); } catch (MGParseErrorException mgpee) { Console.WriteLine(mgpee.ToString()); } string appManFilePath = ""; string subManFilePath = ""; if (mgPP != null) { appManFilePath = Path.Combine(targetDir, String.Concat(mgPP.AppName, ".manifest")); subManFilePath = Path.Combine(targetDir, String.Concat(mgPP.AppName, ".subscription.manifest")); // DEAL WITH THIS MORE ELEGANTLY ... EITHER SAVE THESE FILES IN MEMORY OR MOVE TO TEMP DIRECTORY? if (File.Exists(appManFilePath)) { File.Delete(appManFilePath); } if (File.Exists(subManFilePath)) { File.Delete(subManFilePath); } try { mg = new ManifestGenerator(sourceDir, mgPP, extraXmlStream); } catch (ArgumentNullException ane) { Console.WriteLine(ane.ToString()); } catch (ArgumentException ae) { Console.WriteLine(ae.ToString()); } catch (MGParseErrorException mgpee) { Console.WriteLine(mgpee.ToString()); } catch (MGDependencyException mgde) { Console.WriteLine(mgde.ToString()); } catch (XmlException xmle) { Console.WriteLine(xmle.ToString()); } } if (mg != null) { // Take the streams and make files out of them FileStream appManFile = File.Create(appManFilePath); FileStream subManFile = File.Create(subManFilePath); // Set up copy buffer int copyBufferSize = 32768; byte[] copyBuffer = new byte[copyBufferSize]; int bytesRead; // Write the streams out Stream appManContents = mg.AppManStream; Stream subManContents = mg.SubManStream; while ((bytesRead = appManContents.Read(copyBuffer, 0, copyBufferSize)) > 0) { appManFile.Write(copyBuffer, 0, bytesRead); } while ((bytesRead = subManContents.Read(copyBuffer, 0, copyBufferSize)) > 0) { subManFile.Write(copyBuffer, 0, bytesRead); } // Close streams appManFile.Flush(); appManFile.Close(); Console.WriteLine("Created " + appManFilePath + " successfully"); subManFile.Flush(); subManFile.Close(); Console.WriteLine("Created " + subManFilePath + " successfully"); mg.CloseStreams(); // Convert sourceDir and targetDir to absolute paths so that we can check if they are the same // Then check and store value bool dirsEqual = false; if (targetDir != null) { string absSourceDir = Path.GetFullPath(sourceDir).ToLower(); string absTargetDir = Path.GetFullPath(targetDir).ToLower(); dirsEqual = absSourceDir.Equals(absTargetDir); } // Now do the file copying if necessary if (!dirsEqual && targetDir != null) { if (!Directory.Exists(targetDir)) { try { Directory.CreateDirectory(targetDir); } catch (IOException ioe) { throw new ArgumentException("Target directory " + targetDir + " could not be created.", ioe); } catch (ArgumentException ae) { throw new ArgumentException("Target directory " + targetDir + " could not be created.", ae); } } MGFileCopier mgFC = new MGFileCopier(targetDir); DirScanner.BeginScan((IFileOperator)mgFC, sourceDir); } } }