/// <summary>
        /// A package is deemed to be a satellite package if it has a language property set, the id of the package is
        /// of the format [.*].[Language]
        /// and it has at least one dependency with an id that maps to the runtime package .
        /// </summary>
        private static async Task <SatellitePackageInfo> GetSatellitePackageInfoAsync(
            IAsyncPackageCoreReader packageReader,
            CancellationToken cancellationToken)
        {
            // A satellite package has the following properties:
            //     1) A package suffix that matches the package's language, with a dot preceding it
            //     2) A dependency on the package with the same Id minus the language suffix
            //     3) The dependency can be found by Id in the repository (as its path is needed for installation)
            // Example: foo.ja-jp, with a dependency on foo

            var nuspec = await packageReader.GetNuspecAsync(cancellationToken);

            var             nuspecReader           = new NuspecReader(nuspec);
            var             packageId              = nuspecReader.GetId();
            var             packageLanguage        = nuspecReader.GetLanguage();
            string          localRuntimePackageId  = null;
            PackageIdentity runtimePackageIdentity = null;

            if (!string.IsNullOrEmpty(packageLanguage) &&
                packageId.EndsWith('.' + packageLanguage, StringComparison.OrdinalIgnoreCase))
            {
                // The satellite pack's Id is of the format <Core-Package-Id>.<Language>. Extract the core package id using this.
                // Additionally satellite packages have a strict dependency on the core package
                localRuntimePackageId = packageId.Substring(0, packageId.Length - packageLanguage.Length - 1);

                foreach (var group in nuspecReader.GetDependencyGroups())
                {
                    foreach (var dependencyPackage in group.Packages)
                    {
                        if (dependencyPackage.Id.Equals(localRuntimePackageId, StringComparison.OrdinalIgnoreCase) &&
                            dependencyPackage.VersionRange != null &&
                            dependencyPackage.VersionRange.MaxVersion == dependencyPackage.VersionRange.MinVersion &&
                            dependencyPackage.VersionRange.IsMaxInclusive &&
                            dependencyPackage.VersionRange.IsMinInclusive)
                        {
                            var runtimePackageVersion = new NuGetVersion(dependencyPackage.VersionRange.MinVersion.ToNormalizedString());
                            runtimePackageIdentity = new PackageIdentity(dependencyPackage.Id, runtimePackageVersion);
                        }
                    }
                }
            }

            return(new SatellitePackageInfo(runtimePackageIdentity != null, packageLanguage, runtimePackageIdentity));
        }
Exemple #2
0
        /// <summary>
        /// A package is deemed to be a satellite package if it has a language property set, the id of the package is
        /// of the format [.*].[Language]
        /// and it has at least one dependency with an id that maps to the runtime package .
        /// </summary>
        public static bool IsSatellitePackage(NuspecReader nuspecReader, out PackageIdentity runtimePackageIdentity, out string packageLanguage)
        {
            // A satellite package has the following properties:
            //     1) A package suffix that matches the package's language, with a dot preceding it
            //     2) A dependency on the package with the same Id minus the language suffix
            //     3) The dependency can be found by Id in the repository (as its path is needed for installation)
            // Example: foo.ja-jp, with a dependency on foo

            var packageId = nuspecReader.GetId();

            packageLanguage = nuspecReader.GetLanguage();

            if (!String.IsNullOrEmpty(packageLanguage) &&
                packageId.EndsWith('.' + packageLanguage, StringComparison.OrdinalIgnoreCase))
            {
                // The satellite pack's Id is of the format <Core-Package-Id>.<Language>. Extract the core package id using this.
                // Additionally satellite packages have a strict dependency on the core package
                var localruntimePackageId = packageId.Substring(0, packageId.Length - packageLanguage.Length - 1);

                foreach (var group in nuspecReader.GetDependencyGroups())
                {
                    foreach (var dependencyPackage in group.Packages)
                    {
                        if (dependencyPackage.Id.Equals(localruntimePackageId, StringComparison.OrdinalIgnoreCase) &&
                            dependencyPackage.VersionRange != null &&
                            dependencyPackage.VersionRange.MaxVersion == dependencyPackage.VersionRange.MinVersion &&
                            dependencyPackage.VersionRange.IsMaxInclusive &&
                            dependencyPackage.VersionRange.IsMinInclusive)
                        {
                            var runtimePackageVersion = new NuGetVersion(dependencyPackage.VersionRange.MinVersion.ToString());
                            runtimePackageIdentity = new PackageIdentity(dependencyPackage.Id, runtimePackageVersion);
                            return(true);
                        }
                    }
                }
            }

            runtimePackageIdentity = null;
            return(false);
        }
Exemple #3
0
        /// <summary>
        /// A package is deemed to be a satellite package if it has a language property set, the id of the package is
        /// of the format [.*].[Language]
        /// and it has at least one dependency with an id that maps to the runtime package .
        /// </summary>
        public static bool IsSatellitePackage(NuspecReader nuspecReader, out PackageIdentity runtimePackageIdentity, out string packageLanguage)
        {
            // A satellite package has the following properties:
            //     1) A package suffix that matches the package's language, with a dot preceding it
            //     2) A dependency on the package with the same Id minus the language suffix
            //     3) The dependency can be found by Id in the repository (as its path is needed for installation)
            // Example: foo.ja-jp, with a dependency on foo

            var packageId = nuspecReader.GetId();
            packageLanguage = nuspecReader.GetLanguage();

            if (!String.IsNullOrEmpty(packageLanguage)
                && packageId.EndsWith('.' + packageLanguage, StringComparison.OrdinalIgnoreCase))
            {
                // The satellite pack's Id is of the format <Core-Package-Id>.<Language>. Extract the core package id using this.
                // Additionally satellite packages have a strict dependency on the core package
                var localruntimePackageId = packageId.Substring(0, packageId.Length - packageLanguage.Length - 1);

                foreach (var group in nuspecReader.GetDependencyGroups())
                {
                    foreach (var dependencyPackage in group.Packages)
                    {
                        if (dependencyPackage.Id.Equals(localruntimePackageId, StringComparison.OrdinalIgnoreCase)
                            && dependencyPackage.VersionRange != null
                            && dependencyPackage.VersionRange.MaxVersion == dependencyPackage.VersionRange.MinVersion
                            && dependencyPackage.VersionRange.IsMaxInclusive
                            && dependencyPackage.VersionRange.IsMinInclusive)
                        {
                            var runtimePackageVersion = new NuGetVersion(dependencyPackage.VersionRange.MinVersion.ToString());
                            runtimePackageIdentity = new PackageIdentity(dependencyPackage.Id, runtimePackageVersion);
                            return true;
                        }
                    }
                }
            }

            runtimePackageIdentity = null;
            return false;
        }
Exemple #4
0
        private void LoadNuSpecFile(string filePath)
        {
            NuSpecFilePath = Directory.GetFiles(System.IO.Path.GetDirectoryName(filePath), "*.nuspec", SearchOption.AllDirectories).FirstOrDefault();

            if (NuSpecFilePath != null)
            {
                using (var stream = File.Open(NuSpecFilePath, FileMode.Open))
                {
                    var reader = new NuspecReader(stream);

                    var packageId = reader.GetId();
                    if (packageId != null && packageId.ToLower() == "$id$")
                        packageId = Name;

                    NuGetPackageId = packageId;
                    NuGetPackageTitle = Name; 

                    var metadata = reader.GetMetadata().ToArray();
                    if (metadata.Any(p => p.Key == "title"))
                    {
                        var titlePair = metadata.SingleOrDefault(p => p.Key == "title");
                        NuGetPackageTitle = titlePair.Value;
                    }
                }
            }
        }
        private static NuGetPackageId CreateIdentity(NuspecReader reader, string folderPath)
        {
            NuGetPackageId package = null;
            NuGetVersion version = null;

            if (NuGetVersion.TryParse(reader.GetVersion(), out version))
            {
                package = new NuGetPackageId(reader.GetId(), version, folderPath);
            }
            else
            {
                throw new InvalidDataException("invalid version");
            }

            return package;
        }
Exemple #6
0
        internal static LibraryIdentity CreateLibraryFromNupkg(string nupkgPath)
        {
            using (var fileStream = File.OpenRead(nupkgPath))
            {
                using (var archive = new ZipArchive(fileStream))
                {
                    foreach (var entry in archive.Entries)
                    {
                        if (!entry.Name.EndsWith(ManifestExtension, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        using (var entryStream = entry.Open())
                        {
                            var reader = new NuspecReader(entryStream);
                            return new LibraryIdentity()
                                {
                                    Name = reader.GetId(),
                                    Version = reader.GetVersion(),
                                    Type = LibraryTypes.Package
                                };
                        }
                    }

                    throw new FormatException(
                        string.Format("{0} doesn't contain {1} entry", nupkgPath, ManifestExtension));
                }
            }
        }
        private List<CachedPackageInfo> GetPackageInfos(string id)
        {
            List<CachedPackageInfo> cachedPackageInfos;

            if (_packageInfoCache.TryGetValue(id, out cachedPackageInfos))
            {
                cachedPackageInfos = cachedPackageInfos.ToList();
            }

            var result = new List<CachedPackageInfo>();

            // packages\{packageId}.{version}.nupkg
            foreach (var nupkgInfo in GetNupkgFiles(_source, id))
            {
                var cachedPackageInfo = cachedPackageInfos?.FirstOrDefault(package => string.Equals(package.Path, nupkgInfo.FullName, StringComparison.OrdinalIgnoreCase));
                if (cachedPackageInfo != null
                    && cachedPackageInfo.LastWriteTimeUtc == nupkgInfo.LastWriteTimeUtc)
                {
                    result.Add(cachedPackageInfo);
                }

                using (var stream = nupkgInfo.OpenRead())
                {
                    var packageReader = new PackageReader(stream);
                    NuspecReader reader;
                    try
                    {
                        reader = new NuspecReader(packageReader.GetNuspec());
                    }
                    catch (XmlException ex)
                    {
                        var message = string.Format(CultureInfo.CurrentCulture, Strings.Protocol_PackageMetadataError, nupkgInfo.Name, _source);
                        throw new NuGetProtocolException(message, ex);
                    }
                    catch (PackagingException ex)
                    {
                        var message = string.Format(CultureInfo.CurrentCulture, Strings.Protocol_PackageMetadataError, nupkgInfo.Name, _source);
                        throw new NuGetProtocolException(message, ex);
                    }

                    if (string.Equals(reader.GetId(), id, StringComparison.Ordinal))
                    {
                        result.Add(new CachedPackageInfo { Path = nupkgInfo.FullName, Reader = reader });
                    }
                }
            }

            _packageInfoCache.TryAdd(id, result);

            return result;
        }
Exemple #8
0
        private void LoadNuSpecFile(string filePath)
        {
            NuSpecFilePath = Directory.GetFiles(System.IO.Path.GetDirectoryName(filePath), "*.nuspec", SearchOption.AllDirectories).FirstOrDefault();

            if (NuSpecFilePath != null)
            {
                using (var stream = File.Open(NuSpecFilePath, FileMode.Open))
                {
                    var reader = new NuspecReader(stream);

                    var packageId = reader.GetId();
                    if (packageId != null && packageId.ToLower() == "$id$")
                        packageId = Name;

                    NuGetPackageId = packageId; 
                }
            }
        }