Exemplo n.º 1
0
 public static NuGetVersion ToNuGetVersion([NotNull] this SemanticVersion version)
 {
     if (version == null)
     {
         throw new ArgumentNullException(nameof(version));
     }
     return(NuGetVersion.Parse(version.ToFullString()));
 }
        public void ParseSemanticVersionStrict(string versionString)
        {
            // Act
            SemanticVersion semVer = null;

            SemanticVersion.TryParseStrict(versionString, out semVer);

            // Assert
            Assert.Equal <string>(versionString, semVer.ToFullString());
            Assert.Equal <string>(semVer.ToNormalizedString(), semVer.ToString());
        }
Exemplo n.º 3
0
        public void Constructor_DeserializesSemVer2Version()
        {
            // Arrange
            var cacheFile  = "{\"SchemaVersion\":\"3.0.0\",\"Packages\":[{\"Id\":\"" + PackageId + "\",\"Version\":\"" + SemVer2VersionString + "\"}]}";
            var fileSystem = new Mock <IFileSystem>();

            fileSystem
            .Setup(x => x.FileExists(CacheFileName))
            .Returns(true);
            fileSystem
            .Setup(x => x.OpenFile(CacheFileName))
            .Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(cacheFile)));

            // Act
            var actual = new ServerPackageCache(fileSystem.Object, CacheFileName);

            // Assert
            Assert.Equal(1, actual.GetAll().Count());
            var package = actual.GetAll().First();

            Assert.Equal(SemVer2Version.ToOriginalString(), package.Version.ToOriginalString());
            Assert.Equal(SemVer2Version.ToFullString(), package.Version.ToFullString());
            Assert.Equal(SemVer2Version.ToNormalizedString(), package.Version.ToNormalizedString());
        }
        public static async Task <ServiceVersion> FindServiceVersion(this DbSet <ServiceVersion> services, Service service, SemanticVersion serviceVersion,
                                                                     CancellationToken cancellationToken, bool asNoTracking = false)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (serviceVersion == null)
            {
                throw new ArgumentNullException(nameof(serviceVersion));
            }

            if (service == null)
            {
                return(null);
            }

            var versionString = serviceVersion.ToFullString();

            var queryable = asNoTracking ? services.AsNoTracking() : services;

            return(await queryable.Where(v => v.ServiceId == service.Id && v.Version == versionString)
                   .FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false));
        }
Exemplo n.º 5
0
        private static void LoadPackages(IEnumerable <string> packagePaths, Dictionary <string, PackageInfo> packages, string tempDirectoryOpt, VersionTranslation translation)
        {
            bool readOnly = tempDirectoryOpt == null;

            foreach (var packagePath in packagePaths)
            {
                Package package;
                string  tempPathOpt;
                bool    isDotnetTool = false;
                if (readOnly)
                {
                    tempPathOpt = null;
                    package     = Package.Open(packagePath, FileMode.Open, FileAccess.Read);
                }
                else
                {
                    tempPathOpt = Path.Combine(tempDirectoryOpt, Guid.NewGuid().ToString());
                    File.Copy(packagePath, tempPathOpt);
                    package = Package.Open(tempPathOpt, FileMode.Open, FileAccess.ReadWrite);
                }

                string    packageId    = null;
                Stream    nuspecStream = null;
                XDocument nuspecXml    = null;

                PackageInfo packageInfo = null;
                try
                {
                    SemanticVersion packageVersion    = null;
                    SemanticVersion newPackageVersion = null;
                    string          nuspecXmlns       = NuGetUtils.DefaultNuspecXmlns;

                    foreach (var part in package.GetParts())
                    {
                        string relativePath = part.Uri.OriginalString;
                        if (NuGetUtils.IsNuSpec(relativePath))
                        {
                            nuspecStream = part.GetStream(FileMode.Open, readOnly ? FileAccess.Read : FileAccess.ReadWrite);
                            nuspecXml    = XDocument.Load(nuspecStream);

                            if (nuspecXml.Root.HasAttributes)
                            {
                                var xmlNsAttribute = nuspecXml.Root.Attributes("xmlns").SingleOrDefault();
                                if (xmlNsAttribute != null)
                                {
                                    nuspecXmlns = xmlNsAttribute.Value;
                                }
                            }

                            var metadata = nuspecXml.Element(XName.Get("package", nuspecXmlns))?.Element(XName.Get("metadata", nuspecXmlns));
                            if (metadata == null)
                            {
                                throw new InvalidDataException($"'{packagePath}' has invalid nuspec: missing 'metadata' element");
                            }

                            packageId = metadata.Element(XName.Get("id", nuspecXmlns))?.Value;
                            if (packageId == null)
                            {
                                throw new InvalidDataException($"'{packagePath}' has invalid nuspec: missing 'id' element");
                            }

                            var    versionElement    = metadata.Element(XName.Get("version", nuspecXmlns));
                            string packageVersionStr = versionElement?.Value;
                            if (packageVersionStr == null)
                            {
                                throw new InvalidDataException($"'{packagePath}' has invalid nuspec: missing 'version' element");
                            }

                            if (!SemanticVersion.TryParse(packageVersionStr, out packageVersion))
                            {
                                throw new InvalidDataException($"'{packagePath}' has invalid nuspec: invalid 'version' value '{packageVersionStr}'");
                            }

                            if (!packageVersion.IsPrerelease)
                            {
                                throw new InvalidOperationException($"Can only update pre-release packages: '{packagePath}' has release version");
                            }

                            isDotnetTool = IsDotnetTool(nuspecXmlns, metadata);

                            switch (translation)
                            {
                            case VersionTranslation.Release:
                                // "1.2.3-beta-12345-01-abcdef" -> "1.2.3"
                                // "1.2.3-beta.12345.1+abcdef" -> "1.2.3"
                                newPackageVersion = new SemanticVersion(packageVersion.Major, packageVersion.Minor, packageVersion.Patch);
                                break;

                            case VersionTranslation.PreRelease:
                                // To strip build number take the first pre-release label.
                                // "1.2.3-beta-12345-01-abcdef" -> "1.2.3-beta-final-abcdef"
                                // "1.2.3-beta.12345.1+abcdef" -> "1.2.3-beta.final+abcdef"

                                // SemVer1 version has a single label "beta-12345-01-abcdef" and no metadata.
                                // SemVer2 version has multiple labels { "beta", "12345", "1" } and metadata "abcdef".
                                const string finalLabel = "final";
                                bool         isSemVer1  = packageVersion.Release.Contains('-');
                                var          label      = packageVersion.ReleaseLabels.First().Split('-')[0];

                                newPackageVersion = new SemanticVersion(
                                    packageVersion.Major,
                                    packageVersion.Minor,
                                    packageVersion.Patch,
                                    isSemVer1 ? new[] { label + "-" + finalLabel } : new[] { label, finalLabel },
                                    packageVersion.Metadata);
                                break;

                            case VersionTranslation.None:
                                newPackageVersion = packageVersion;
                                break;
                            }

                            if (!readOnly)
                            {
                                // Note: ToFullString = ToNormalizedString + metadata
                                versionElement.SetValue(newPackageVersion.ToFullString());
                            }

                            break;
                        }
                    }

                    if (isDotnetTool)
                    {
                        // skip repack DotnetTool since it has version embedded in executable
                        // and repack does not support it
                        continue;
                    }

                    if (nuspecStream == null)
                    {
                        throw new InvalidDataException($"'{packagePath}' is missing .nuspec file");
                    }

                    if (packages.ContainsKey(packageId))
                    {
                        throw new InvalidDataException($"Multiple packages of name '{packageId}' specified");
                    }

                    if (!readOnly)
                    {
                        package.PackageProperties.Version = newPackageVersion.ToFullString();
                    }

                    packageInfo = new PackageInfo(package, packageId, packageVersion, newPackageVersion, tempPathOpt, nuspecStream, nuspecXml, nuspecXmlns);
                }
                finally
                {
                    if (packageInfo == null)
                    {
                        nuspecStream.Dispose();
                        package.Close();

                        if (tempPathOpt != null)
                        {
                            File.Delete(tempPathOpt);
                        }
                    }
                }

                packages.Add(packageId, packageInfo);
            }
        }