/// <summary> /// Modifies experiment to add reference to new package. /// </summary> /// <param name="pPkg">The package being created.</param> /// <param name="pExperimentFile">The experiment file.</param> /// <returns>True is there was no error during the operation, false otherwise.</returns> private bool AddPkgRefToExperiment(TraceLab.Core.PackageSystem.Package pPkg, string pExperimentFile) { bool noError = true; if (System.IO.File.Exists(pExperimentFile)) { try { XmlDocument xmlExperiment = new XmlDocument(); xmlExperiment.Load(pExperimentFile); XmlNode nodeReferences = xmlExperiment.SelectSingleNode("//References"); XmlElement newPkgReference = xmlExperiment.CreateElement("PackageReference"); newPkgReference.SetAttribute("ID", pPkg.ID); newPkgReference.SetAttribute("Name", pPkg.Name); nodeReferences.AppendChild(newPkgReference); xmlExperiment.Save(pExperimentFile); } catch (Exception) { MessageBox.Show("Unable to modify experiment - reference to new package could not be added.", "Package Creation Error", MessageBoxButton.OK, MessageBoxImage.Error); noError = false; } } else { noError = false; } return(noError); }
/// <summary> /// Adds item and its content (file or folder) to package. /// </summary> /// <param name="pPkg">Package being created.</param> /// <param name="item">Item to be added.</param> /// <returns>True is there was no error during the operation, false otherwise.</returns> private bool AddItemToPackage(TraceLab.Core.PackageSystem.Package pkg, PackageFileSourceInfo item) { bool noError = true; string targetPath = System.IO.Path.Combine(pkg.Location, item.GetPath()); PackageHeirarchyItem dir = item as PackageHeirarchyItem; if (dir != null) { if (item.Parent != null) { System.IO.Directory.CreateDirectory(targetPath); } foreach (PackageFileSourceInfo child in dir.Children) { noError = noError && AddItemToPackage(pkg, child); } if (dir.HasComponents) { pkg.SetDirectoryHasComponents(dir.GetPath(), true); } if (dir.HasTypes) { pkg.SetDirectoryHasTypes(dir.GetPath(), true); } } else { System.IO.File.Copy(item.SourceFilePath, targetPath); //Add reference to this created package to all experiments and composite components if (this.isExperimentPackage && targetPath.EndsWith(".teml") || targetPath.EndsWith(".tcml")) { noError = noError && AddPkgRefToExperiment(pkg, targetPath); } System.IO.File.SetAttributes(targetPath, System.IO.File.GetAttributes(targetPath) & ~System.IO.FileAttributes.ReadOnly); pkg.AddFile(targetPath); } return(noError); }
/// <summary> /// Handles the Click event of the Build Package Button /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void BuildPkgButton_Click(object sender, RoutedEventArgs e) { MainBorder.Visibility = System.Windows.Visibility.Visible; var viewModel = (PackageBuilderViewModel)DataContext; PackageSourceInfo info = viewModel.PackageSourceInfo; bool noError = true; TraceLab.Core.PackageSystem.Package pkg = null; string pkgFilePath = GetFilePath(info.Name); if (pkgFilePath != null) { string pkgFileName = System.IO.Path.GetFileNameWithoutExtension(pkgFilePath); string pkgTempDirectory = pkgFilePath + "~temp"; try { System.IO.Directory.CreateDirectory(pkgTempDirectory); try { pkg = new TraceLab.Core.PackageSystem.Package(info.Name, pkgTempDirectory, false); } catch (TraceLab.Core.PackageSystem.PackageAlreadyExistsException) { MessageBox.Show("Package already exists in: " + pkgTempDirectory, "Package Creation Error", MessageBoxButton.OK, MessageBoxImage.Error); noError = false; } catch (TraceLab.Core.PackageSystem.PackageException ex) { MessageBox.Show("Error creating package: " + ex.Message, "Package Creation Error", MessageBoxButton.OK, MessageBoxImage.Error); noError = false; } if (pkg != null && noError) { foreach (PackageFileSourceInfo item in info.Files) { noError = noError && AddItemToPackage(pkg, item); } pkg.SaveManifest(); using (System.IO.FileStream stream = new System.IO.FileStream(pkgFilePath, System.IO.FileMode.Create)) { pkg.Pack(stream); } } } catch (System.IO.IOException error) { MessageBox.Show("Unable to create package. Error: " + error.Message, "Package Creation Error", MessageBoxButton.OK, MessageBoxImage.Error); noError = false; } catch (System.UnauthorizedAccessException error) { MessageBox.Show("Unable to create package - Unauthorized access: " + error.Message, "Package Creation Error", MessageBoxButton.OK, MessageBoxImage.Error); noError = false; } try { if (System.IO.Directory.Exists(pkgTempDirectory)) { System.IO.Directory.Delete(pkgTempDirectory, true); } } catch (System.IO.IOException error) { MessageBox.Show("Unable to cleanup after package creation. Error: " + error.Message, "After Package Cleanup Failure", MessageBoxButton.OK, MessageBoxImage.Exclamation); } catch (System.UnauthorizedAccessException error) { MessageBox.Show("Unable to cleanup after package creation. Unauthorized access: " + error.Message, "After Package Cleanup Failure", MessageBoxButton.OK, MessageBoxImage.Warning); } } if (noError && pkg != null) { MessageBox.Show("Package \"" + info.Name + "\" was built successfully.", "Package Created", MessageBoxButton.OK, MessageBoxImage.Information); } MainBorder.Visibility = System.Windows.Visibility.Hidden; }
public void PackRoundTripTest() { // TODO: Create new package based on a directory (See the Load test) var packageRoot = WriteTestPackage("RoundTripPackage"); Package target = new Package(packageRoot, true); Package unpackedTarget = null; using (System.IO.MemoryStream packedStream = new System.IO.MemoryStream()) { target.Pack(packedStream); packedStream.Seek(0, System.IO.SeekOrigin.Begin); System.IO.Directory.Delete(System.IO.Path.Combine(PackageTestRoot, "RoundTripPackage"), true); unpackedTarget = new Package(packedStream); unpackedTarget.Unpack(PackageTestRoot); } Assert.IsNotNull(unpackedTarget); Assert.AreEqual(target.ID, unpackedTarget.ID); Assert.AreEqual(target.Name, unpackedTarget.Name); Assert.AreEqual(target.Files.Count(), unpackedTarget.Files.Count()); Assert.AreEqual(target.References.Count(), unpackedTarget.References.Count()); Assert.AreEqual(target.ComponentLocations.Count(), unpackedTarget.ComponentLocations.Count()); Assert.AreEqual(target.TypeLocations.Count(), unpackedTarget.TypeLocations.Count()); for (int i = 0; i < target.Files.Count(); ++i) { var targetFile = target.Files.ElementAt(i); var unpackedFile = unpackedTarget.Files.ElementAt(i); Assert.AreEqual(targetFile.ID, unpackedFile.ID); } for (int i = 0; i < target.References.Count(); ++i) { Assert.AreEqual(target.References.ElementAt(i), unpackedTarget.References.ElementAt(i)); } for (int i = 0; i < target.ComponentLocations.Count(); ++i) { Assert.AreEqual(target.ComponentLocations.ElementAt(i), unpackedTarget.ComponentLocations.ElementAt(i)); } for (int i = 0; i < target.TypeLocations.Count(); ++i) { Assert.AreEqual(target.TypeLocations.ElementAt(i), unpackedTarget.TypeLocations.ElementAt(i)); } // and lets make sure the files came through too. Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "RoundTripPackage.manifest"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "coest.xml"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "coest1.xml"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "randomfile.something"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Components", "Importer.dll"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Types", "DictionaryTermWeights.dll"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "somerandomfile.xml"))); }
public void SaveManifestTest() { var name = "TestPackage"; var packageRoot = WriteTestPackage(name); // Load the package Package target = new Package(packageRoot, true); var manifestFile = System.IO.Path.Combine(PackageTestRoot, name, "TestPackage.manifest"); var newManifestFile = System.IO.Path.Combine(PackageTestRoot, name, name + ".manifest"); // Move the manifest to a backup System.IO.File.Move(manifestFile, manifestFile + ".bak"); // Compare the written manifest with the m anifest that is saved in the test resources Assert.IsFalse(System.IO.File.Exists(newManifestFile)); // Force the save of the manifest target.SaveManifest(); // Compare the written manifest with the m anifest that is saved in the test resources Assert.IsTrue(System.IO.File.Exists(newManifestFile)); var backup = System.IO.File.ReadAllText(manifestFile + ".bak", Encoding.UTF8); var manifest = System.IO.File.ReadAllText(newManifestFile, Encoding.UTF8); Assert.IsTrue(backup.SequenceEqual(manifest)); }
public void SetDirectoryHasTypes() { string packageName = "SetDirectoryHasTypesPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); string components = System.IO.Path.Combine(target.Location, "Components"); string types = System.IO.Path.Combine(target.Location, "Types"); string data = System.IO.Path.Combine(target.Location, "Files"); System.IO.Directory.CreateDirectory(components); System.IO.Directory.CreateDirectory(types); System.IO.Directory.CreateDirectory(data); WriteFile(System.IO.Path.Combine(components, "Importer.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Components.Importer.dll"); WriteFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Types.DictionaryTermWeights.dll"); target.AddFile(System.IO.Path.Combine(components, "Importer.dll")); target.AddFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll")); Assert.AreEqual(0, target.ComponentLocations.Count()); Assert.AreEqual(0, target.TypeLocations.Count()); target.SetDirectoryHasTypes(types, true); Assert.AreEqual(1, target.TypeLocations.Count()); Assert.AreEqual(types, target.TypeLocations.ElementAtOrDefault(0)); target.SetDirectoryHasTypes(types, false); Assert.AreEqual(0, target.TypeLocations.Count()); Assert.AreEqual(0, target.ComponentLocations.Count()); }
public void AddFileTest() { string packageName = "AddFileTestPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); string components = System.IO.Path.Combine(target.Location, "Components"); string types = System.IO.Path.Combine(target.Location, "Types"); string data = System.IO.Path.Combine(target.Location, "Files"); System.IO.Directory.CreateDirectory(components); System.IO.Directory.CreateDirectory(types); System.IO.Directory.CreateDirectory(data); WriteFile(System.IO.Path.Combine(data, "coest.xml"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Data.coest.xml"); WriteFile(System.IO.Path.Combine(target.Location, "randomfile.something"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Data.randomfile.something"); WriteFile(System.IO.Path.Combine(components, "Importer.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Components.Importer.dll"); WriteFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Types.DictionaryTermWeights.dll"); target.AddFile(System.IO.Path.Combine(data, "coest.xml")); target.AddFile(System.IO.Path.Combine(target.Location, "randomfile.something")); target.AddFile(System.IO.Path.Combine(components, "Importer.dll")); target.AddFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll")); // TODO: Verify that files are added correctly Assert.AreEqual(4, target.Files.Count()); }
public void PackageLoadTest() { string packageName = "TestPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); Package target = new Package(packageDirectory, true); Assert.AreEqual("A939607B-7159-4466-88C4-A869044F81C6", target.ID); Assert.AreEqual("TestPackage", target.Name); Assert.AreEqual(2, target.References.Count()); Assert.AreEqual("1886E5F6-27CA-4D42-AB51-7B7BBD9E97C1", target.References.ElementAt(0).ID); Assert.AreEqual("E6D01E22-7C9E-4B75-B7C3-6FC8B33A9593", target.References.ElementAt(1).ID); Assert.AreEqual(1, target.ComponentLocations.Count()); Assert.AreEqual("./Components", target.ComponentLocations.ElementAt(0)); Assert.AreEqual(1, target.TypeLocations.Count()); Assert.AreEqual("./Types", target.TypeLocations.ElementAt(0)); Assert.AreEqual(6, target.Files.Count()); foreach (PackageFile file in target.Files) { Assert.AreEqual(target, file.Owner); string filePath = target.GetAbsolutePath(file); using (System.IO.FileStream reader = System.IO.File.OpenRead(filePath)) { Assert.AreEqual(reader.Length, file.UncompressedSize, "File does not match expected uncompressed size: " + file.Path); Assert.AreEqual(Package.ComputeHash(reader), file.UncompressedHash, "File does not match expected uncompressed hash: " + file.Path); reader.Seek(0, System.IO.SeekOrigin.Begin); // We can't validate the specific size of the compressed data or the hash of the compressed data // because the compression algorithm can change based on the runtime. And we don't need to validate the round-trip since // PackageFile already tests that. } } }
public void PackageConstructorTest() { string packageName = "AnEmptyPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); Guid id; Assert.IsTrue(Guid.TryParse(target.ID, out id)); Assert.AreEqual(packageName, target.Name); Assert.AreEqual(System.IO.Path.Combine(PackageTestRoot, packageName), target.Location); Assert.AreEqual(0, target.Files.Count()); Assert.AreEqual(0, target.References.Count()); Assert.AreEqual(0, target.TypeLocations.Count()); Assert.AreEqual(0, target.ComponentLocations.Count()); }
/// <summary> /// Unpacks this package into the specified directory. /// </summary> /// <param name="baseDirectory">The directory to use.</param> public static Package Unpack(string rootLocation, Stream packageStream) { XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; XmlReader reader = XmlReader.Create(packageStream, settings); XPathDocument doc = new XPathDocument(reader); // Read the name so we can know where to create it. var nav = doc.CreateNavigator(); var nameNode = nav.SelectSingleNode("/Package/@Name"); string name = nameNode.Value; string packageDir = System.IO.Path.Combine(rootLocation, name); System.IO.Directory.CreateDirectory(packageDir); Package newPackage = new Package(packageDir, false); newPackage.ReadPackage(nav); newPackage.SaveManifest(); return newPackage; }