Beispiel #1
0
        public void PackageBuilderWorksWithFileNamesContainingSpecialCharacters()
        {
            // Arrange
            var fileNames = new[] {
                @"lib\regular.file.dll",
                @"lib\name with spaces.dll",
                @"lib\C#\test.dll",
                @"content\images\logo123?#78.png",
                @"content\images\bread&butter.jpg",
            };

            // Act
            var builder = new PackageBuilder {
                Id = "test", Version = new Version("1.0"), Description = "test"
            };

            builder.Authors.Add("test");
            foreach (var name in fileNames)
            {
                builder.Files.Add(CreatePackageFile(name));
            }

            // Assert
            using (MemoryStream stream = new MemoryStream()) {
                builder.Save(stream);

                var zipPackage = new ZipPackage(() => new MemoryStream(stream.ToArray()));
                Assert.AreEqual(@"content\images\bread&butter.jpg", zipPackage.GetFiles().ElementAt(0).Path);
                Assert.AreEqual(@"content\images\logo123?#78.png", zipPackage.GetFiles().ElementAt(1).Path);
                Assert.AreEqual(@"lib\C#\test.dll", zipPackage.GetFiles().ElementAt(2).Path);
                Assert.AreEqual(@"lib\name with spaces.dll", zipPackage.GetFiles().ElementAt(3).Path);
                Assert.AreEqual(@"lib\regular.file.dll", zipPackage.GetFiles().ElementAt(4).Path);
            }
        }
Beispiel #2
0
        public void ApplyDeltaPackageSmokeTest()
        {
            var basePackage         = new ReleasePackage(IntegrationTestHelper.GetPath("fixtures", "Squirrel.Core.1.0.0.0-full.nupkg"));
            var deltaPackage        = new ReleasePackage(IntegrationTestHelper.GetPath("fixtures", "Squirrel.Core.1.1.0.0-delta.nupkg"));
            var expectedPackageFile = IntegrationTestHelper.GetPath("fixtures", "Squirrel.Core.1.1.0.0-full.nupkg");
            var outFile             = Path.GetTempFileName() + ".nupkg";

            try {
                var deltaBuilder = new DeltaPackageBuilder();
                deltaBuilder.ApplyDeltaPackage(basePackage, deltaPackage, outFile);

                var result   = new ZipPackage(outFile);
                var expected = new ZipPackage(expectedPackageFile);

                result.Id.ShouldEqual(expected.Id);
                result.Version.ShouldEqual(expected.Version);

                this.Log().Info("Expected file list:");
                expected.GetFiles().Select(x => x.Path).OrderBy(x => x).ForEach(x => this.Log().Info(x));

                this.Log().Info("Actual file list:");
                result.GetFiles().Select(x => x.Path).OrderBy(x => x).ForEach(x => this.Log().Info(x));

                Enumerable.Zip(
                    expected.GetFiles().Select(x => x.Path).OrderBy(x => x),
                    result.GetFiles().Select(x => x.Path).OrderBy(x => x),
                    (e, a) => e == a
                    ).All(x => x).ShouldBeTrue();
            } finally {
                if (File.Exists(outFile))
                {
                    File.Delete(outFile);
                }
            }
        }
Beispiel #3
0
        public void ReleasePackageIntegrationTest()
        {
            var inputPackage  = IntegrationTestHelper.GetPath("fixtures", "Shimmer.Core.1.0.0.0.nupkg");
            var outputPackage = Path.GetTempFileName() + ".nupkg";
            var sourceDir     = IntegrationTestHelper.GetPath("..", "packages");

            var fixture = new ReleasePackage(inputPackage);

            (new DirectoryInfo(sourceDir)).Exists.ShouldBeTrue();

            try {
                fixture.CreateReleasePackage(outputPackage, sourceDir);

                this.Log().Info("Resulting package is at {0}", outputPackage);
                var pkg = new ZipPackage(outputPackage);

                int refs = pkg.FrameworkAssemblies.Count();
                this.Log().Info("Found {0} refs", refs);
                refs.ShouldEqual(0);

                this.Log().Info("Files in release package:");
                pkg.GetFiles().ForEach(x => this.Log().Info(x.Path));

                pkg.GetFiles().Any(x => x.Path.ToLowerInvariant().Contains(@"lib\sl")).ShouldBeFalse();
                pkg.GetFiles().Any(x => x.Path.ToLowerInvariant().Contains(@".xml")).ShouldBeFalse();
            } finally {
                File.Delete(outputPackage);
            }
        }
Beispiel #4
0
        public void CtorWithStream()
        {
            // Arrange
            var builder = new PackageBuilder();

            builder.Id      = "Package";
            builder.Version = new Version("1.0");
            builder.Authors.Add("David");
            builder.Description = "This is a test package";
            builder.Files.AddRange(PackageUtility.CreateFiles(new[] { @"lib\40\A.dll", @"content\foo" }));

            var ms = new MemoryStream();

            builder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);

            // Act
            var package = new ZipPackage(ms);

            // Assert
            Assert.AreEqual("Package", package.Id);
            Assert.AreEqual(new Version("1.0"), package.Version);
            Assert.AreEqual("David", package.Authors.First());
            var files = package.GetFiles().ToList();

            Assert.AreEqual(2, files.Count);
            Assert.AreEqual(@"content\foo", files[0].Path);
            Assert.AreEqual(@"lib\40\A.dll", files[1].Path);
            var assemblyReferences = package.AssemblyReferences.ToList();

            Assert.AreEqual(1, assemblyReferences.Count);
            Assert.AreEqual("A.dll", assemblyReferences[0].Name);
            Assert.AreEqual(new FrameworkName(".NETFramework", new Version("4.0")), assemblyReferences[0].TargetFramework);
        }
Beispiel #5
0
        public void ReleasePackageIntegrationTest()
        {
            var inputPackage  = IntegrationTestHelper.GetPath("fixtures", "Squirrel.Core.1.0.0.0.nupkg");
            var outputPackage = Path.GetTempFileName() + ".nupkg";
            var sourceDir     = IntegrationTestHelper.GetPath("..", "packages");

            var fixture = new ReleasePackage(inputPackage);

            (new DirectoryInfo(sourceDir)).Exists.ShouldBeTrue();

            try {
                fixture.CreateReleasePackage(outputPackage, sourceDir);

                this.Log().Info("Resulting package is at {0}", outputPackage);
                var pkg = new ZipPackage(outputPackage);

                int refs = pkg.FrameworkAssemblies.Count();
                this.Log().Info("Found {0} refs", refs);
                refs.ShouldEqual(0);

                this.Log().Info("Files in release package:");

                List <IPackageFile> files = pkg.GetFiles().ToList();
                files.ForEach(x => this.Log().Info(x.Path));

                List <string> nonDesktopPaths = new[] { "sl", "winrt", "netcore", "win8", "windows8", "MonoAndroid", "MonoTouch", "MonoMac", "wp", }
                .Select(x => @"lib\" + x)
                .ToList();

                files.Any(x => nonDesktopPaths.Any(y => x.Path.ToLowerInvariant().Contains(y.ToLowerInvariant()))).ShouldBeFalse();
                files.Any(x => x.Path.ToLowerInvariant().Contains(@".xml")).ShouldBeFalse();
            } finally {
                File.Delete(outputPackage);
            }
        }
Beispiel #6
0
        public void CtorWithStream()
        {
            // Arrange
            var builder = new PackageBuilder();
            builder.Id = "Package";
            builder.Version = new SemanticVersion("1.0");
            builder.Authors.Add("David");
            builder.Description = "This is a test package";
            builder.ReleaseNotes = "This is a release note.";
            builder.Copyright = "Copyright";
            builder.Files.AddRange(PackageUtility.CreateFiles(new[] { @"lib\40\A.dll", @"content\foo" }));

            var ms = new MemoryStream();
            builder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);

            // Act
            var package = new ZipPackage(ms);

            // Assert
            Assert.Equal("Package", package.Id);
            Assert.Equal(new SemanticVersion("1.0"), package.Version);
            Assert.Equal("David", package.Authors.First());
            Assert.Equal("Copyright", package.Copyright);
            var files = package.GetFiles().ToList();
            Assert.Equal(2, files.Count);
            Assert.Equal(@"content\foo", files[0].Path);
            Assert.Equal(@"lib\40\A.dll", files[1].Path);
            var assemblyReferences = package.AssemblyReferences.ToList();
            Assert.Equal(1, assemblyReferences.Count);
            Assert.Equal("A.dll", assemblyReferences[0].Name);
            Assert.Equal(new FrameworkName(".NETFramework", new Version("4.0")), assemblyReferences[0].TargetFramework);
            Assert.Equal("This is a release note.", package.ReleaseNotes);
        }
Beispiel #7
0
        public IPackage ExplodeTo(string directory)
        {
            var explodedDirectory = ExplodedDirectory(directory);

            RippleLog.Info("Exploding to " + explodedDirectory);

            var fileSystem = new FileSystem();

            fileSystem.CreateDirectory(explodedDirectory);
            fileSystem.ForceClean(explodedDirectory);

            var package = new ZipPackage(_path);

            package.GetFiles().Each(file =>
            {
                var target = explodedDirectory.AppendPath(file.Path);
                fileSystem.CreateDirectory(target.ParentDirectory());

                using (var stream = new FileStream(target, FileMode.Create, FileAccess.Write))
                {
                    file.GetStream().CopyTo(stream);
                }
            });

            fileSystem.CopyToDirectory(_path, explodedDirectory);

            fileSystem.DeleteFile(_path);

            var newFile = Path.Combine(explodedDirectory, Path.GetFileName(_path));

            return(new ZipPackage(newFile));
        }
        public override SolutionPackageBase Unpack(Stream package, SolutionPackageOptions options)
        {
            package.Position = 0;

            var zipPackage          = new ZipPackage(package);
            var solutionPackageFile = FindSolutionPackageFile(zipPackage);

            SerializationService.RegisterKnownType(typeof(SolutionPackageBase));
            //SerializationService.RegisterKnownType(typeof(SPMeta2SolutionPackage));

            using (var streamReader = new StreamReader(solutionPackageFile.GetStream()))
            {
                var solutionFileContent = streamReader.ReadToEnd();

                var solutionPackage     = SerializationService.Deserialize(typeof(SolutionPackageBase), solutionFileContent) as SolutionPackageBase;
                var modelContainerFiles = zipPackage.GetFiles(MetaPackConsts.ModelsContainerFolder);

                foreach (var modelContainerFile in modelContainerFiles)
                {
                    using (var modelContainerStream = modelContainerFile.GetStream())
                    {
                        var modelContainerBinary = modelContainerStream.ReadToEnd();

                        var modelContainer = SerializationService.Deserialize(typeof(ModelContainerBase), modelContainerBinary) as ModelContainerBase;
                        solutionPackage.AddModel(modelContainer);
                    }
                }

                return(solutionPackage);
            }
        }
Beispiel #9
0
        public void GetSource(RequestContext context)
        {
            var packageId = context.RouteData.Values["id"].ToString();
            var version   = context.RouteData.Values["version"].ToString();
            var path      = Path.Combine("src", context.RouteData.Values["path"].ToString().Replace('/', '\\'));

            var response = context.HttpContext.Response;

            ZipPackage package;

            try
            {
                package = new ZipPackage(symbolPackagePathResolver.GetSymbolSourcePath(packageId, version));
            }
            catch (FileNotFoundException)
            {
                response.StatusCode = 404;
                return;
            }

            string directory = Path.GetDirectoryName(path);
            var    file      =
                package.GetFiles(directory)
                .FirstOrDefault(f => string.Equals(f.Path, path, StringComparison.InvariantCultureIgnoreCase));

            if (file == null)
            {
                response.StatusCode = 404;
                return;
            }
            response.ContentType = "binary/octet-stream";
            file.GetStream().CopyTo(response.OutputStream);
        }
Beispiel #10
0
        private ZipPackage VerifyPackageContents(string packageFile, IEnumerable <string> expectedFiles)
        {
            var package = new ZipPackage(packageFile);
            var files   = package.GetFiles().Select(f => f.Path).OrderBy(f => f).ToList();

            CollectionAssert.AreEqual(expectedFiles.OrderBy(f => f).ToList(), files);
            return(package);
        }
        public static T GetPackageOptions <T>(this ZipPackage package) where T : class
        {
            var descriptor = package.GetFiles().FirstOrDefault(f => f.Path == "powerdeploy.xml");

            if (descriptor == null)
            {
                throw new InvalidOperationException("The package {0} has no powerdeploy.xml. Please create a valid package first!".Fmt(package.Id));
            }

            return(descriptor.GetStream().FromXml <T>());
        }
Beispiel #12
0
        public void CanResolveMultipleLevelsOfDependencies()
        {
            var inputPackage  = IntegrationTestHelper.GetPath("fixtures", "SampleUpdatingApp.1.0.0.0.nupkg");
            var outputPackage = Path.GetTempFileName() + ".nupkg";
            var sourceDir     = IntegrationTestHelper.GetPath("..", "packages");

            var fixture = new ReleasePackage(inputPackage);

            (new DirectoryInfo(sourceDir)).Exists.ShouldBeTrue();

            try {
                fixture.CreateReleasePackage(outputPackage, sourceDir);

                this.Log().Info("Resulting package is at {0}", outputPackage);
                var pkg = new ZipPackage(outputPackage);

                int refs = pkg.FrameworkAssemblies.Count();
                this.Log().Info("Found {0} refs", refs);
                refs.ShouldEqual(0);

                this.Log().Info("Files in release package:");
                pkg.GetFiles().ForEach(x => this.Log().Info(x.Path));

                var filesToLookFor = new[] {
                    "System.Reactive.Core.dll",
                    "ReactiveUI.dll",
                    "MarkdownSharp.dll",
                    "SampleUpdatingApp.exe",
                };

                filesToLookFor.ForEach(name => {
                    this.Log().Info("Looking for {0}", name);
                    pkg.GetFiles().Any(y => y.Path.ToLowerInvariant().Contains(name.ToLowerInvariant())).ShouldBeTrue();
                });
            } finally {
                File.Delete(outputPackage);
            }
        }
Beispiel #13
0
        public void Build_And_Packaged_XCopy_Has_Correct_Metadata()
        {
            System.Environment.CurrentDirectory = "samples".MapVcsRoot();

            MsBuild("PowerDeploy.Sample.XCopy\\PowerDeploy.Sample.XCopy.csproj /t:clean,build /p:RunOctoPack=true /p:OctoPackPackageVersion=1.3.3.7 /p:Configuration=Release /v:m");

            Assert.IsTrue(File.Exists(@"PowerDeploy.Sample.XCopy\obj\octopacked\PowerDeploy.Sample.XCopy.1.3.3.7.nupkg"));

            var nupkg = new ZipPackage(@"PowerDeploy.Sample.XCopy\obj\octopacked\PowerDeploy.Sample.XCopy.1.3.3.7.nupkg");

            Assert.AreEqual("1.3.3.7", nupkg.Version.ToString());
            Assert.AreEqual("PowerDeploy.Sample.XCopy", nupkg.Id);
            Assert.IsTrue(nupkg.GetFiles().Any(f => f.Path.Contains("powerdeploy.template.xml")));
        }
Beispiel #14
0
        public void Configure_XCopy_Sample_Project_With_Mocked_Environment()
        {
            System.Environment.CurrentDirectory = "samples".MapVcsRoot();

            MsBuild("PowerDeploy.Sample.XCopy\\PowerDeploy.Sample.XCopy.csproj /t:clean,build /p:RunOctoPack=true /p:OctoPackPackageVersion=1.3.3.7 /p:Configuration=Release /v:m");

            var environmentMock = new Mock <IEnvironmentProvider>();

            environmentMock.Setup(prov => prov.GetEnvironment(It.IsAny <string>())).Returns(GetUnitEnvironment);

            var outputPath = FileSystem.CreateTempWorkingDir();

            var target = new PackageManager(environmentMock.Object);

            target.ConfigurePackageByEnvironment(@"PowerDeploy.Sample.XCopy\obj\octopacked\PowerDeploy.Sample.XCopy.1.3.3.7.nupkg", "Unit", outputPath);

            // check if all template files are parsed
            var nupkg = new ZipPackage(Path.Combine(outputPath, "PowerDeploy.Sample.XCopy_v1.3.3.7_UNIT.nupkg"));

            Assert.IsFalse(nupkg.GetFiles().Any(f => f.Path.Contains(".template.")));

            AssertPackage(Path.Combine(outputPath, "PowerDeploy.Sample.XCopy_v1.3.3.7_UNIT.nupkg"),
                          pkg => pkg.AssertFileContent(new Dictionary <string, string>()
            {
                {
                    "powerdeploy.xml",
                    @"<?xml version=""1.0"" encoding=""utf-8"" ?>
                          <package type=""xcopy"" environment=""UNIT"">
                              <destination>c:\temp</destination>
                          </package>".ToXmlOneLine()
                },
                {
                    "App.config",
                    @"<?xml version=""1.0"" encoding=""utf-8"" ?>
                          <configuration>
                            <appSettings>
                              <add key=""variable1"" value=""Val1"" />
                              <add key=""variable2"" value=""Val2"" />
                              <add key=""default.variable"" value=""defaultvalue"" />
                              <add key=""env"" value=""UNIT"" />
                            </appSettings>
                          </configuration>".ToXmlOneLine()
                }
            }));
        }
Beispiel #15
0
        public void CtorWithStream()
        {
            // Arrange
            var builder = new PackageBuilder();

            builder.Id      = "Package";
            builder.Version = new SemanticVersion("1.0");
            builder.Authors.Add("David");
            builder.Description  = "This is a test package";
            builder.ReleaseNotes = "This is a release note.";
            builder.Copyright    = "Copyright";
            builder.Files.AddRange(PackageUtility.CreateFiles(new[] { PathFixUtility.FixPath(@"lib\40\A.dll"), PathFixUtility.FixPath(@"content\foo") }));

            var ms = new MemoryStream();

            builder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);

            // Act
            var package = new ZipPackage(ms);

            // Assert
            Assert.Equal("Package", package.Id);
            Assert.Equal(new SemanticVersion("1.0"), package.Version);
            Assert.Equal("David", package.Authors.First());
            Assert.Equal("Copyright", package.Copyright);

            //Order is not gauranteed (or required) from GetFiles(),
            //but we rely on the order for a few of the asserts,
            //and it appears to not behave the same way on Mono,
            //so we call "order by" here to force a specific order.
            var files = package.GetFiles().OrderBy(k => k.Path).ToList();

            Assert.Equal(2, files.Count);
            Assert.Equal(PathFixUtility.FixPath(@"content\foo"), files[0].Path);
            Assert.Equal(PathFixUtility.FixPath(@"lib\40\A.dll"), files[1].Path);
            var assemblyReferences = package.AssemblyReferences.ToList();

            Assert.Equal(1, assemblyReferences.Count);
            Assert.Equal("A.dll", assemblyReferences[0].Name);
            Assert.Equal(new FrameworkName(".NETFramework", new Version("4.0")), assemblyReferences[0].TargetFramework);
            Assert.Equal("This is a release note.", package.ReleaseNotes);
        }
Beispiel #16
0
        //
        // ApplyReleases methods
        //

        List <string> installPackageToAppDir(UpdateInfo updateInfo, ReleaseEntry release)
        {
            var pkg    = new ZipPackage(Path.Combine(rootAppDirectory, "packages", release.Filename));
            var target = getDirectoryForRelease(release.Version);

            // NB: This might happen if we got killed partially through applying the release
            if (target.Exists)
            {
                Utility.DeleteDirectory(target.FullName);
            }
            target.Create();

            // Copy all of the files out of the lib/ dirs in the NuGet package
            // into our target App directory.
            //
            // NB: We sort this list in order to guarantee that if a Net20
            // and a Net40 version of a DLL get shipped, we always end up
            // with the 4.0 version.
            pkg.GetFiles().Where(x => pathIsInFrameworkProfile(x, appFrameworkVersion)).OrderBy(x => x.Path)
            .ForEach(x => {
                var targetPath = Path.Combine(target.FullName, Path.GetFileName(x.Path));

                var fi = fileSystem.GetFileInfo(targetPath);
                if (fi.Exists)
                {
                    fi.Delete();
                }

                using (var inf = x.GetStream())
                    using (var of = fi.Open(FileMode.CreateNew, FileAccess.Write)) {
                        log.Info("Writing {0} to app directory", targetPath);
                        inf.CopyTo(of);
                    }
            });

            var newCurrentVersion = updateInfo.FutureReleaseEntry.Version;

            // Perform post-install; clean up the previous version by asking it
            // which shortcuts to install, and nuking them. Then, run the app's
            // post install and set up shortcuts.
            return(runPostInstallAndCleanup(newCurrentVersion, updateInfo.IsBootstrapping));
        }
Beispiel #17
0
        public void Merge(string secondNupkg, bool replaceMetadata = false)
        {
            var secondPackage = new ZipPackage(secondNupkg);

            if (replaceMetadata)
            {
                ReplaceMetadata(_Builder, secondPackage);
            }
            else
            {
                MergeMetadata(_Builder, secondPackage);
            }

            foreach (var packageFile in secondPackage.GetFiles())
            {
                if (!_Builder.Files.Any(e => e.Path.Equals(packageFile.Path, StringComparison.OrdinalIgnoreCase)))
                {
                    _Builder.Files.Add(packageFile);
                }
            }
        }
Beispiel #18
0
        public void CtorWithStream()
        {
            // Arrange
            var builder = new PackageBuilder();
            builder.Id = "Package";
            builder.Version = new SemanticVersion("1.0");
            builder.Authors.Add("David");
            builder.Description = "This is a test package";
            builder.ReleaseNotes = "This is a release note.";
            builder.Copyright = "Copyright";
            builder.Files.AddRange(PackageUtility.CreateFiles(new[] { PathFixUtility.FixPath(@"lib\40\A.dll"), PathFixUtility.FixPath(@"content\foo") }));

            var ms = new MemoryStream();
            builder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);

            // Act
            var package = new ZipPackage(ms);

            // Assert
            Assert.Equal("Package", package.Id);
            Assert.Equal(new SemanticVersion("1.0"), package.Version);
            Assert.Equal("David", package.Authors.First());
            Assert.Equal("Copyright", package.Copyright);

            //Order is not gauranteed (or required) from GetFiles(), 
            //but we rely on the order for a few of the asserts, 
            //and it appears to not behave the same way on Mono,
            //so we call "order by" here to force a specific order.
            var files = package.GetFiles().OrderBy(k => k.Path).ToList();

            Assert.Equal(2, files.Count);
            Assert.Equal(PathFixUtility.FixPath(@"content\foo"), files[0].Path);
            Assert.Equal(PathFixUtility.FixPath(@"lib\40\A.dll"), files[1].Path);
            var assemblyReferences = package.AssemblyReferences.ToList();
            Assert.Equal(1, assemblyReferences.Count);
            Assert.Equal("A.dll", assemblyReferences[0].Name);
            Assert.Equal(new FrameworkName(".NETFramework", new Version("4.0")), assemblyReferences[0].TargetFramework);
            Assert.Equal("This is a release note.", package.ReleaseNotes);
        }
Beispiel #19
0
        private void PrintVerbose(string outputPath)
        {
            Console.WriteLine();
            var package = new ZipPackage(outputPath);

            Console.WriteLine("Id: {0}", package.Id);
            Console.WriteLine("Version: {0}", package.Version);
            Console.WriteLine("Authors: {0}", String.Join(", ", package.Authors));
            Console.WriteLine("Description: {0}", package.Description);
            if (package.LicenseUrl != null)
            {
                Console.WriteLine("License Url: {0}", package.LicenseUrl);
            }
            if (package.ProjectUrl != null)
            {
                Console.WriteLine("Project Url: {0}", package.ProjectUrl);
            }
            if (!String.IsNullOrEmpty(package.Tags))
            {
                Console.WriteLine("Tags: {0}", package.Tags.Trim());
            }
            if (package.Dependencies.Any())
            {
                Console.WriteLine("Dependencies: {0}", String.Join(", ", package.Dependencies.Select(d => d.ToString())));
            }
            else
            {
                Console.WriteLine("Dependencies: None");
            }

            Console.WriteLine();

            foreach (var file in package.GetFiles().OrderBy(p => p.Path))
            {
                Console.WriteLine(NuGetResources.PackageCommandAddedFile, file.Path);
            }

            Console.WriteLine();
        }
Beispiel #20
0
        public static void AssertContents(this ZipPackage package, params string[] files)
        {
            var actualFiles   = package.GetFiles().Select(f => f.Path).ToDictionary(f => f, f => false, StringComparer.OrdinalIgnoreCase);
            var expectedFiles = files.ToDictionary(f => f, f => false, StringComparer.OrdinalIgnoreCase);

            foreach (var actualFile in actualFiles.Keys.ToList())
            {
                if (expectedFiles.ContainsKey(actualFile))
                {
                    actualFiles[actualFile]   = true;
                    expectedFiles[actualFile] = true;
                }

                foreach (var expected in expectedFiles.Keys.ToList().Where(k => k.Contains("*")))
                {
                    var regex = Regex.Escape(expected).Replace("\\*", ".*");

                    if (Regex.IsMatch(actualFile, regex, RegexOptions.IgnoreCase))
                    {
                        actualFiles[actualFile] = true;
                        expectedFiles[expected] = true;
                    }
                }
            }

            var unexpectedInPackage = actualFiles.Where(kvp => kvp.Value == false).Select(kvp => kvp.Key).ToList();
            var missingInPackage    = expectedFiles.Where(kvp => kvp.Value == false).Select(kvp => kvp.Key).ToList();

            if (unexpectedInPackage.Any())
            {
                Assert.Fail("These files were NOT expected to be in the package: " + Environment.NewLine + string.Join("," + Environment.NewLine, unexpectedInPackage.Select(u => "@\"" + u + "\"")));
            }

            if (missingInPackage.Any())
            {
                Assert.Fail("These files were expected to be in the package: " + Environment.NewLine + string.Join("," + Environment.NewLine, missingInPackage.Select(u => "@\"" + u + "\"")));
            }
        }
Beispiel #21
0
        public void CreatePackage(HttpContextBase context)
        {
            var request      = context.Request;
            var stream       = request.Files.Count > 0 ? request.Files[0].InputStream : request.InputStream;
            var clonedStream = CloneStream(stream);
            var package      = new ZipPackage(clonedStream);        // provide a cloned stream because ZipPackage now dispose it

            var symbolFiles = package.GetFiles("lib").Where(f => Path.GetExtension(f.Path) == ".pdb").ToList();

            foreach (var symbolFile in symbolFiles)
            {
                ProcessSymbolFile(symbolFile, package);
            }

            if (IsSymbolPackage(package, symbolFiles))
            {
                symbolPackageService.CreatePackage(context);
            }
            else
            {
                packageService.CreatePackage(context);
            }
        }
Beispiel #22
0
        private void ProcessSymbolFile(IPackageFile symbolFile, ZipPackage package)
        {
            var tempSymbolFilePath = ExtractToSymbols(package, symbolFile);

            try
            {
                var referencedSources = symbolTools.GetSources(tempSymbolFilePath).ToList();

                var sourceFiles = new HashSet <string>(package.GetFiles("src").Select(f => f.Path.ToLowerInvariant()).ToList());
                if (referencedSources.Any() && sourceFiles.Any())
                {
                    var basePath = FindSourceBasePath(sourceFiles, referencedSources);

                    if (basePath.Length != 0)
                    {
                        symbolTools.MapSources(package, tempSymbolFilePath, referencedSources, r => r.Substring(basePath.Length));
                    }
                }
            }
            finally
            {
                File.Delete(tempSymbolFilePath);
            }
        }
Beispiel #23
0
        public static void AssertFileContent(this ZipPackage package, Dictionary <string, string> fileEntries)
        {
            var missmatches = new List <string>();

            foreach (var fileEntry in fileEntries)
            {
                var file = package.GetFiles().FirstOrDefault(f => f.Path == fileEntry.Key);

                var content = new StreamReader(file.GetStream(), Encoding.UTF8).ReadToEnd();

                if (fileEntry.Key.EndsWith(".xml") || fileEntry.Key.EndsWith(".config"))
                {
                    content = content.ToXmlOneLine();
                }

                if (content != fileEntry.Value)
                {
                    missmatches.Add(file.Path);

                    Trace.WriteLine("Missmatch in " + fileEntry.Key);
                    Trace.WriteLine("IS:");
                    Trace.WriteLine(content + Environment.NewLine);
                    Trace.WriteLine("SHOULD:");
                    Trace.WriteLine(fileEntry.Value);
                }
                else
                {
                    Trace.WriteLine(fileEntry.Key + " OK");
                }
            }

            if (missmatches.Any())
            {
                Assert.Fail("These files have a missmatch: " + Environment.NewLine + string.Join("," + Environment.NewLine, missmatches));
            }
        }
Beispiel #24
0
        static int Main(params string[] args)
        {
            var cBase    = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\\");
            var fileName = cBase.Substring(cBase.LastIndexOf('\\') + 1);

            fileName = fileName.Substring(0, fileName.LastIndexOf('.'));
            Console.WriteLine("Package: " + fileName);
            bool   uninstall   = false;
            bool   upgrade     = false;
            string packageName = fileName;

            if (args.Length > 0)
            {
                switch (args[0])
                {
                case "uninstall":
                    uninstall = true;
                    break;

                case "upgrade":
                    upgrade = false;
                    break;

                default:
                    upgrade = true;
                    break;
                }
            }
            if (string.IsNullOrWhiteSpace(packageName))
            {
                return(-1);
            }

            IPackageRepository pr = PackageRepositoryFactory.Default.CreateRepository("http://192.168.0.17/Pietastic.WebUpdateService/nuget");

            var latestVersion = pr.FindPackagesById(packageName).LastOrDefault(p => p.IsLatestVersion && p.Listed);

            if (latestVersion != null)
            {
                // Check if package is installed

                var packagePath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    packageName
                    );

                if (uninstall)
                {
                    Uninstall(packagePath);
                    Process.GetCurrentProcess().Kill();
                    return(0);
                }

                Console.WriteLine("Creating App directory");
                Console.WriteLine(packagePath);
                Directory.CreateDirectory(packagePath);

                var launcherFile = Path.Combine(packagePath, packageName + ".exe");
                if (!System.IO.File.Exists(launcherFile))
                {
                    System.IO.File.Copy(cBase, launcherFile);
                }

                var programPackages = Path.Combine(packagePath, "packages");

                Console.WriteLine("Creating Packages directory");
                Console.WriteLine(programPackages);
                Directory.CreateDirectory(programPackages);

                var latestPackage = Path.Combine(
                    programPackages,
                    string.Format("{0}-{1}.nupkg", latestVersion.Id, latestVersion.Version.ToFullString())
                    );

                var latestInstalled = Path.Combine(
                    packagePath,
                    string.Format("app-{0}", latestVersion.Version.ToFullString())
                    );

                Console.WriteLine(latestInstalled);

                if (!System.IO.File.Exists(latestPackage))
                {
                    Console.WriteLine("Latest version not downloaded, removing old versions and downloading latest");
                    DirectoryInfo di = new DirectoryInfo(programPackages);

                    var allPackages = di.EnumerateFiles("*.nupkg").ToList();

                    if (allPackages.Count > 0)
                    {
                        // Enumerate all packages, remove old packages, keep 3 latest
                        var skipTheThreeLatest = allPackages.OrderByDescending(f => f.CreationTimeUtc).Skip(3);
                        foreach (var pack in skipTheThreeLatest)
                        {
                            var appFolder = string.Format("app-{0}", pack.Name.Substring(pack.Name.LastIndexOf('-') + 1).Replace(".nupkg", ""));
                            if (Directory.Exists(appFolder))
                            {
                                Directory.Delete(Path.Combine(packagePath, appFolder), true);
                            }
                        }
                    }

                    using (var fs = latestVersion.GetStream())
                    {
                        System.IO.File.WriteAllBytes(latestPackage, fs.ReadAllBytes());
                    }
                }

                string symLinkPath = string.Empty;

                Console.WriteLine("Installing latest version");
                Directory.CreateDirectory(latestInstalled);

                var fz = new ZipPackage(latestPackage);
                foreach (var f in fz.GetFiles())
                {
                    var lFile = Path.Combine(latestInstalled, f.Path);

                    if (System.IO.File.Exists(lFile))
                    {
                        continue;
                    }

                    using (var fs = f.GetStream())
                    {
                        System.IO.File.WriteAllBytes(lFile, fs.ReadAllBytes());
                    }

                    if (f.Path == latestVersion.Id + ".exe")
                    {
                        Console.WriteLine("Modifying/Creating symlink");

                        symLinkPath = Path.Combine(packagePath, "app-" + f.Path);
                        if (SymbolicLink.Exists(symLinkPath))
                        {
                            System.IO.File.Delete(symLinkPath);
                        }
                        SymbolicLink.CreateFileLink(symLinkPath, lFile);
                        Console.WriteLine(symLinkPath);
                    }
                }

                var desktopShortcut = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                    (latestVersion.Title ?? packageName) + ".lnk"
                    );

                if (System.IO.File.Exists(desktopShortcut))
                {
                    System.IO.File.Delete(desktopShortcut);
                }
                CreateShortcut(desktopShortcut, launcherFile);
                Console.WriteLine(desktopShortcut);

                if (!upgrade)
                {
                    ProcessStartInfo launch = new ProcessStartInfo(symLinkPath);
                    var p = Process.Start(launch);
                    p.WaitForExit();
                    Main("upgrade " + packageName);
                }

                return(0);
            }
            Console.WriteLine("Package: " + packageName + " was not found, please rename file");
            return(1);
        }
Beispiel #25
0
 private static bool IsSymbolPackage(ZipPackage package, IEnumerable <IPackageFile> symbolFiles)
 {
     return(symbolFiles.Any() && package.GetFiles("src").Any());
 }
Beispiel #26
0
        public override void ExecuteCommand()
        {
            Log.Trace("Getting latest packages...");
            var packages = GetLatestStablePackages();

            Log.Trace("Getting previously curated packages...");
            var alreadyCuratedPackageIds = GetAlreadyCuratedPackageIds();

            Log.Trace("Calculating minimum difference set...");
            var packageIdsToCurate = packages.Keys.Except(alreadyCuratedPackageIds).ToList();

            var totalCount     = packageIdsToCurate.Count;
            var processedCount = 0;

            Log.Trace(
                "Curating {0} packages for the WebMatrix curated on '{1}',",
                totalCount,
                ConnectionString);

            Parallel.ForEach(packageIdsToCurate, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, packageIdToCurate =>
            {
                var package = packages[packageIdToCurate];

                try
                {
                    var downloadPath = DownloadPackage(package);
                    var nugetPackage = new ZipPackage(downloadPath);

                    var shouldBeIncluded = nugetPackage.Tags != null && nugetPackage.Tags.ToLowerInvariant().Contains("aspnetwebpages");

                    if (!shouldBeIncluded)
                    {
                        shouldBeIncluded = true;
                        foreach (var file in nugetPackage.GetFiles())
                        {
                            var fi = new FileInfo(file.Path);
                            if (fi.Extension == ".ps1" || fi.Extension == ".t4")
                            {
                                shouldBeIncluded = false;
                                break;
                            }
                        }
                    }

                    if (shouldBeIncluded)
                    {
                        AddPackageToCuratedFeed(package);
                    }

                    File.Delete(downloadPath);

                    Interlocked.Increment(ref processedCount);
                    Log.Info(
                        "{2} package '{0}.{1}' ({3} of {4}).",
                        package.Id,
                        package.Version,
                        shouldBeIncluded ? "Curated" : "Ignored",
                        processedCount,
                        totalCount);
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref processedCount);
                    Log.Error(
                        "Error curating package '{0}.{1}' ({2} of {3}): {4}.",
                        package.Id,
                        package.Version,
                        processedCount,
                        totalCount,
                        ex.Message);
                }
            });
        }
        public void PackageBuilderWorksWithFileNamesContainingSpecialCharacters()
        {
            // Arrange
            var fileNames = new[] {
                @"lib\regular.file.dll",
                @"lib\name with spaces.dll",
                @"lib\C#\test.dll",
                @"content\images\logo123?#78.png",
                @"content\images\bread&butter.jpg",
            };

            // Act
            var builder = new PackageBuilder { Id = "test", Version = new SemanticVersion("1.0"), Description = "test" };
            builder.Authors.Add("test");
            foreach (var name in fileNames)
            {
                builder.Files.Add(CreatePackageFile(name));
            }

            // Assert
            using (MemoryStream stream = new MemoryStream())
            {
                builder.Save(stream);

                var zipPackage = new ZipPackage(() => new MemoryStream(stream.ToArray()), enableCaching: false);
                Assert.Equal(@"content\images\bread&butter.jpg", zipPackage.GetFiles().ElementAt(0).Path);
                Assert.Equal(@"content\images\logo123?#78.png", zipPackage.GetFiles().ElementAt(1).Path);
                Assert.Equal(@"lib\C#\test.dll", zipPackage.GetFiles().ElementAt(2).Path);
                Assert.Equal(@"lib\name with spaces.dll", zipPackage.GetFiles().ElementAt(3).Path);
                Assert.Equal(@"lib\regular.file.dll", zipPackage.GetFiles().ElementAt(4).Path);
            }
        }
        private async Task CalculateValidity(IReadOnlyList <PackageFile> files)
        {
            var filesWithPdb = (from pf in files
                                let ext = Path.GetExtension(pf.Path)
                                          where ".dll".Equals(ext, StringComparison.OrdinalIgnoreCase) ||
                                          ".exe".Equals(ext, StringComparison.OrdinalIgnoreCase) ||
                                          ".winmd".Equals(ext, StringComparison.OrdinalIgnoreCase)
                                          select new FileWithPdb
            {
                Primary = pf,
                Pdb = pf.GetAssociatedFiles().FirstOrDefault(af => ".pdb".Equals(Path.GetExtension(af.Path), StringComparison.OrdinalIgnoreCase))
            })
                               .ToList();


            var sourceLinkErrors = new List <(PackageFile file, string errors)>();
            var noSourceLink     = new List <PackageFile>();
            var noSymbols        = new List <FileWithDebugData>();
            var untrackedSources = new List <FileWithDebugData>();
            var nonDeterministic = new List <PackageFile>();

            var allFilePaths = filesWithPdb.ToDictionary(pf => pf.Primary.Path);

            var pdbChecksumValid = true;

            foreach (var file in filesWithPdb.ToArray()) // work on array as we'll remove items that are satellite assemblies as we go
            {
                // Skip satellite assemblies
                if (IsSatelliteAssembly(file.Primary.Path))
                {
                    filesWithPdb.Remove(allFilePaths[file.Primary.Path]);
                    continue;
                }

                // If we have a PDB, try loading that first. If not, may be embedded.
                // Local checks first
                if (file.Pdb != null)
                {
                    var filePair = new FileWithDebugData(file.Primary, null);
                    if (!ValidatePdb(filePair, file.Pdb.GetStream(), noSourceLink, sourceLinkErrors, untrackedSources, nonDeterministic))
                    {
                        pdbChecksumValid = false;
                        noSymbols.Add(filePair);
                    }
                }
                else // No PDB, see if it's embedded
                {
                    try
                    {
                        using var str      = file.Primary.GetStream();
                        using var tempFile = new TemporaryFile(str);

                        var assemblyMetadata = AssemblyMetadataReader.ReadMetaData(tempFile.FileName);

                        if (assemblyMetadata?.DebugData.HasDebugInfo == true)
                        {
                            // we have an embedded pdb
                            if (!assemblyMetadata.DebugData.HasSourceLink)
                            {
                                noSourceLink.Add(file.Primary);
                            }

                            if (assemblyMetadata.DebugData.SourceLinkErrors.Count > 0)
                            {
                                // Has source link errors
                                sourceLinkErrors.Add((file.Primary, string.Join("\n", assemblyMetadata.DebugData.SourceLinkErrors)));
                            }

                            // Check for non-embedded sources
                            if (assemblyMetadata.DebugData.UntrackedSources.Count > 0)
                            {
                                var filePair = new FileWithDebugData(file.Primary, assemblyMetadata.DebugData);
                                untrackedSources.Add(filePair);
                            }

                            // Check for deterministic sources
                            if (!assemblyMetadata.DebugData.SourcesAreDeterministic)
                            {
                                nonDeterministic.Add(file.Primary);
                            }
                        }
                        else // no embedded pdb, try to look for it elsewhere
                        {
                            noSymbols.Add(new FileWithDebugData(file.Primary, assemblyMetadata?.DebugData));
                        }
                    }
                    catch // an error occured, no symbols
                    {
                        noSymbols.Add(new FileWithDebugData(file.Primary, null));
                    }
                }
            }


            var requireExternal = false;

            // See if any pdb's are missing and check for a snupkg on NuGet.org.
            if (noSymbols.Count > 0 && _publishedOnNuGetOrg)
            {
                // try to get on NuGet.org
                // https://www.nuget.org/api/v2/symbolpackage/Newtonsoft.Json/12.0.3 -- Will redirect


                try
                {
#pragma warning disable CA2234 // Pass system uri objects instead of strings
                    var response = await _httpClient.GetAsync($"https://www.nuget.org/api/v2/symbolpackage/{_package.Id}/{_package.Version.ToNormalizedString()}").ConfigureAwait(false);

#pragma warning restore CA2234                        // Pass system uri objects instead of strings

                    if (response.IsSuccessStatusCode) // we'll get a 404 if none
                    {
                        requireExternal = true;

                        using var getStream = await response.Content.ReadAsStreamAsync();

                        using var tempFile = new TemporaryFile(getStream, ".snupkg");
                        using var package  = new ZipPackage(tempFile.FileName);

                        // Look for pdb's for the missing files
                        var dict = package.GetFiles().ToDictionary(k => k.Path);

                        foreach (var file in noSymbols.ToArray()) // from a copy so we can remove as we go
                        {
                            // file to look for
                            var pdbpath = Path.ChangeExtension(file.File.Path, ".pdb");

                            if (dict.TryGetValue(pdbpath, out var pdbfile))
                            {
                                // Validate
                                if (ValidatePdb(file, pdbfile.GetStream(), noSourceLink, sourceLinkErrors, untrackedSources, nonDeterministic))
                                {
                                    noSymbols.Remove(file);
                                }
                                else
                                {
                                    pdbChecksumValid = false;
                                }
                            }
                        }
                    }
                }
                catch // Could not check, leave status as-is
                {
                }
            }

            // Check for Microsoft assemblies on the Microsoft symbol server
            if (noSymbols.Count > 0)
            {
                var microsoftFiles = noSymbols.Where(f => f.DebugData != null && IsMicrosoftFile(f.File)).ToList();

                foreach (var file in microsoftFiles)
                {
                    var pdbStream = await GetSymbolsAsync(file.DebugData !.SymbolKeys);

                    if (pdbStream != null)
                    {
                        requireExternal = true;

                        // Found a PDB for it
                        if (ValidatePdb(file, pdbStream, noSourceLink, sourceLinkErrors, untrackedSources, nonDeterministic))
                        {
                            noSymbols.Remove(file);
                        }
                        else
                        {
                            pdbChecksumValid = false;
                        }
                    }
                }
            }

            if (noSymbols.Count == 0 && noSourceLink.Count == 0 && sourceLinkErrors.Count == 0)
            {
                if (untrackedSources.Count > 0)
                {
                    SourceLinkResult = SymbolValidationResult.HasUntrackedSources;

                    var sb = new StringBuilder("Contains untracked sources:\n");
                    sb.AppendLine("To Fix:");
                    sb.AppendLine("<EmbedUntrackedSources>true</EmbedUntrackedSources>");

                    foreach (var untracked in untrackedSources)
                    {
                        sb.AppendLine($"Assembly: {untracked.File.Path}");

                        foreach (var source in untracked.DebugData !.UntrackedSources)
                        {
                            sb.AppendLine($"  {source}");
                        }

                        sb.AppendLine();
                    }

                    SourceLinkErrorMessage = sb.ToString();
                }
                else if (filesWithPdb.Count == 0)
                {
                    SourceLinkResult       = SymbolValidationResult.NothingToValidate;
                    SourceLinkErrorMessage = "No files found to validate";
                }
                else if (requireExternal)
                {
                    SourceLinkResult       = SymbolValidationResult.ValidExternal;
                    SourceLinkErrorMessage = null;
                }
                else
                {
                    SourceLinkResult       = SymbolValidationResult.Valid;
                    SourceLinkErrorMessage = null;
                }
            }
            else
            {
                var found = false;
                var sb    = new StringBuilder();
                if (noSourceLink.Count > 0)
                {
                    SourceLinkResult = SymbolValidationResult.NoSourceLink;

                    sb.AppendLine($"Missing Source Link for:\n{string.Join("\n", noSourceLink.Select(p => p.Path)) }");
                    found = true;
                }

                if (sourceLinkErrors.Count > 0)
                {
                    SourceLinkResult = SymbolValidationResult.InvalidSourceLink;

                    if (found)
                    {
                        sb.AppendLine();
                    }

                    foreach (var(file, errors) in sourceLinkErrors)
                    {
                        sb.AppendLine($"Source Link errors for {file.Path}:\n{string.Join("\n", errors) }");
                    }

                    found = true;
                }

                if (noSymbols.Count > 0) // No symbols "wins" as it's more severe
                {
                    SourceLinkResult = SymbolValidationResult.NoSymbols;

                    if (found)
                    {
                        sb.AppendLine();
                    }

                    if (!pdbChecksumValid)
                    {
                        sb.AppendLine("Some PDB's checksums do not match their PE files and are shown as missing.");
                    }

                    sb.AppendLine($"Missing Symbols for:\n{string.Join("\n", noSymbols.Select(p => p.File.Path)) }");
                }

                SourceLinkErrorMessage = sb.ToString();
            }

            if (SourceLinkResult == SymbolValidationResult.NothingToValidate)
            {
                DeterministicResult       = DeterministicResult.NothingToValidate;
                DeterministicErrorMessage = null;
            }
            else if (SourceLinkResult == SymbolValidationResult.NoSymbols)
            {
                DeterministicResult       = DeterministicResult.NonDeterministic;
                DeterministicErrorMessage = "Missing Symbols";
            }
            else if (nonDeterministic.Count > 0)
            {
                DeterministicResult = DeterministicResult.NonDeterministic;

                var sb = new StringBuilder();
                sb.AppendLine("Ensure that the following properties are enabled for CI builds\nand you're using at least the 2.1.300 SDK:");
                sb.AppendLine("<Deterministic>true</Deterministic>");
                sb.AppendLine("<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>");
                sb.AppendLine();
                sb.AppendLine("The following assemblies have not been compiled with deterministic settings:");

                foreach (var file in nonDeterministic)
                {
                    sb.AppendLine(file.Path);
                }

                DeterministicErrorMessage = sb.ToString();
            }
            else if (SourceLinkResult == SymbolValidationResult.HasUntrackedSources)
            {
                DeterministicResult       = DeterministicResult.HasUntrackedSources;
                DeterministicErrorMessage = null;
            }
            else
            {
                DeterministicResult       = DeterministicResult.Valid;
                DeterministicErrorMessage = null;
            }
        }
Beispiel #29
0
        private void PrintVerbose(string outputPath)
        {
            Console.WriteLine();
            var package = new ZipPackage(outputPath);

            Console.WriteLine("Id: {0}", package.Id);
            Console.WriteLine("Version: {0}", package.Version);
            Console.WriteLine("Authors: {0}", String.Join(", ", package.Authors));
            Console.WriteLine("Description: {0}", package.Description);
            if (package.LicenseUrl != null)
            {
                Console.WriteLine("License Url: {0}", package.LicenseUrl);
            }
            if (package.ProjectUrl != null)
            {
                Console.WriteLine("Project Url: {0}", package.ProjectUrl);
            }
            if (!String.IsNullOrEmpty(package.Tags))
            {
                Console.WriteLine("Tags: {0}", package.Tags.Trim());
            }
            if (package.DependencySets.Any())
            {
                Console.WriteLine("Dependencies: {0}", String.Join(", ", package.DependencySets.SelectMany(d => d.Dependencies).Select(d => d.ToString())));
            }
            else
            {
                Console.WriteLine("Dependencies: None");
            }

            Console.WriteLine();

            foreach (var file in package.GetFiles().OrderBy(p => p.Path))
            {
                Console.WriteLine(NuGetResources.PackageCommandAddedFile, file.Path);
            }

            Console.WriteLine();
        }
Beispiel #30
0
        public Stream OpenPackageSourceFile(IPackageName package, string relativePath)
        {
            relativePath = relativePath.Replace('/', Path.DirectorySeparatorChar);

            // Try to return the source file from its uncompressed location
            var parts = new[] {
                package.Id,
                package.Version.ToString(),
                "src",
                relativePath
            };

            var stream = OpenFile(Path.Combine(parts));

            if (stream != null) return stream;

            // If the file wasn't found uncompressed look for it in symbol package zip file
            var packagePath = GetNupkgPath(package);
            if (!File.Exists(packagePath)) return null;

            var srcPath = Path.Combine("src", relativePath);
            var packageFile = new ZipPackage(packagePath);
            var file = packageFile.GetFiles().SingleOrDefault(f => f.Path.Equals(srcPath, StringComparison.InvariantCultureIgnoreCase));
            return file != null ? file.GetStream() : null;
        }
Beispiel #31
0
        public bool ImportPage(byte[] uploadedPackage, string fileName, int pageId, int siteId)
        {
            // Write .nupkg file to the PackageStaging folder...
            var path = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/PackageStaging"), fileName);

            using (var file = new FileStream(path, FileMode.Create))
            {
                file.Write(uploadedPackage, 0, uploadedPackage.Length);
            }

            var  package      = new ZipPackage(path);
            var  packageFiles = package.GetFiles().ToList();
            var  exportFile   = packageFiles.FirstOrDefault(f => f.Path.Contains("export.json"));
            Page page         = null;

            // If export.json is present, deserialize data
            // * Are there any new BlockTypes to register? If so, save them first.
            // * Scrub out any `Id` and `Guid` fields that came over from export
            // * Save page data via PageService

            if (exportFile != null)
            {
                string json;

                using (var stream = exportFile.GetStream())
                {
                    json = stream.ReadToEnd();
                }

                page = Page.FromJson(json);
            }

            // Validate package...
            // + Does it have any executable .dll files? Should those go to the bin folder, or into a plugins directory to be loaded via MEF?
            // - Does it have code or asset files that need to go on the file system? (Done)
            // - Does it have an export.json file? Should that be a requirement? (Done)
            // + Does it have any corresponding SQL, migrations, seed methods to run, etc.

            if (page != null)
            {
                var rockContext = new RockContext();

                // Find new block types and save them prior to scrubbing data...
                var newBlockTypes = FindNewBlockTypes(page, new BlockTypeService(rockContext).Queryable()).ToList();
                rockContext.WrapTransaction(() =>
                {
                    try
                    {
                        var blockTypeService = new BlockTypeService(rockContext);

                        foreach (var blockType in newBlockTypes)
                        {
                            blockTypeService.Add(blockType);
                        }
                        rockContext.SaveChanges();

                        ValidateImportData(page, newBlockTypes);
                        SavePages(rockContext, page, newBlockTypes, pageId, siteId);
                        ExpandFiles(packageFiles);
                    }
                    catch (Exception e)
                    {
                        ErrorMessages.Add(e.Message);
                    }
                });

                // Clean up PackageStaging folder on successful import.
                var file = new FileInfo(path);
                file.Delete();
                return(ErrorMessages.Count <= 0);
            }

            ErrorMessages.Add("The export package uploaded does not appear to have any data associated with it.");
            return(false);
        }
        public void CreateDeltaPackageIntegrationTest()
        {
            var basePackage = IntegrationTestHelper.GetPath("fixtures", "Squirrel.Tests.0.1.0-pre.nupkg");
            var newPackage  = IntegrationTestHelper.GetPath("fixtures", "Squirrel.Tests.0.2.0-pre.nupkg");

            var sourceDir = IntegrationTestHelper.GetPath("fixtures", "packages");

            (new DirectoryInfo(sourceDir)).Exists.ShouldBeTrue();

            var baseFixture = new ReleasePackage(basePackage);
            var fixture     = new ReleasePackage(newPackage);

            var tempFiles = Enumerable.Range(0, 3)
                            .Select(_ => Path.GetTempPath() + Guid.NewGuid().ToString() + ".nupkg")
                            .ToArray();

            try {
                baseFixture.CreateReleasePackage(tempFiles[0], sourceDir);
                fixture.CreateReleasePackage(tempFiles[1], sourceDir);

                (new FileInfo(baseFixture.ReleasePackageFile)).Exists.ShouldBeTrue();
                (new FileInfo(fixture.ReleasePackageFile)).Exists.ShouldBeTrue();

                var deltaBuilder = new DeltaPackageBuilder();
                deltaBuilder.CreateDeltaPackage(baseFixture, fixture, tempFiles[2]);

                var fullPkg  = new ZipPackage(tempFiles[1]);
                var deltaPkg = new ZipPackage(tempFiles[2]);

                //
                // Package Checks
                //

                fullPkg.Id.ShouldEqual(deltaPkg.Id);
                fullPkg.Version.CompareTo(deltaPkg.Version).ShouldEqual(0);

                // Delta packages should be smaller than the original!
                var fileInfos = tempFiles.Select(x => new FileInfo(x)).ToArray();
                this.Log().Info("Base Size: {0}, Current Size: {1}, Delta Size: {2}",
                                fileInfos[0].Length, fileInfos[1].Length, fileInfos[2].Length);

                (fileInfos[2].Length - fileInfos[1].Length).ShouldBeLessThan(0);

                //
                // File Checks
                ///

                var deltaPkgFiles = deltaPkg.GetFiles().ToList();
                deltaPkgFiles.Count.ShouldBeGreaterThan(0);

                this.Log().Info("Files in delta package:");
                deltaPkgFiles.ForEach(x => this.Log().Info(x.Path));

                var newFilesAdded = new[] {
                    "Newtonsoft.Json.dll",
                    "Refit.dll",
                    "Refit-Portable.dll",
                    "Castle.Core.dll",
                }.Select(x => x.ToLowerInvariant());

                // vNext adds a dependency on Refit
                newFilesAdded
                .All(x => deltaPkgFiles.Any(y => y.Path.ToLowerInvariant().Contains(x)))
                .ShouldBeTrue();

                // All the other files should be diffs and shasums
                deltaPkgFiles
                .Where(x => !newFilesAdded.Any(y => x.Path.ToLowerInvariant().Contains(y)))
                .All(x => x.Path.ToLowerInvariant().EndsWith("diff") || x.Path.ToLowerInvariant().EndsWith("shasum"))
                .ShouldBeTrue();

                // Every .diff file should have a shasum file
                deltaPkg.GetFiles().Any(x => x.Path.ToLowerInvariant().EndsWith(".diff")).ShouldBeTrue();
                deltaPkg.GetFiles()
                .Where(x => x.Path.ToLowerInvariant().EndsWith(".diff"))
                .ForEach(x => {
                    var lookingFor = x.Path.Replace(".diff", ".shasum");
                    this.Log().Info("Looking for corresponding shasum file: {0}", lookingFor);
                    deltaPkg.GetFiles().Any(y => y.Path == lookingFor).ShouldBeTrue();
                });
            } finally {
                tempFiles.ForEach(File.Delete);
            }
        }