Exemplo n.º 1
0
        /// <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;
        }
Exemplo n.º 2
0
        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")));
        }