Ejemplo n.º 1
0
        public static bool TryParseVersionSpec(string value, out IVersionSpec result)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var versionSpec = new VersionSpec();

            value = value.Trim();

            // First, try to parse it as a plain version string
            SemanticVersion version;

            if (SemanticVersion.TryParse(value, out version))
            {
                // A plain version is treated as an inclusive minimum range
                result = new VersionSpec
                {
                    MinVersion     = version,
                    IsMinInclusive = true
                };

                return(true);
            }

            // It's not a plain version, so it must be using the bracket arithmetic range syntax

            result = null;

            // Fail early if the string is too short to be valid
            if (value.Length < 3)
            {
                return(false);
            }

            // The first character must be [ ot (
            switch (value.First())
            {
            case '[':
                versionSpec.IsMinInclusive = true;
                break;

            case '(':
                versionSpec.IsMinInclusive = false;
                break;

            default:
                return(false);
            }

            // The last character must be ] ot )
            switch (value.Last())
            {
            case ']':
                versionSpec.IsMaxInclusive = true;
                break;

            case ')':
                versionSpec.IsMaxInclusive = false;
                break;

            default:
                return(false);
            }

            // Get rid of the two brackets
            value = value.Substring(1, value.Length - 2);

            // Split by comma, and make sure we don't get more than two pieces
            string[] parts = value.Split(',');
            if (parts.Length > 2)
            {
                return(false);
            }
            else if (parts.All(String.IsNullOrEmpty))
            {
                // If all parts are empty, then neither of upper or lower bounds were specified. Version spec is of the format (,]
                return(false);
            }

            // If there is only one piece, we use it for both min and max
            string minVersionString = parts[0];
            string maxVersionString = (parts.Length == 2) ? parts[1] : parts[0];

            // Only parse the min version if it's non-empty
            if (!String.IsNullOrWhiteSpace(minVersionString))
            {
                if (!TryParseVersion(minVersionString, out version))
                {
                    return(false);
                }
                versionSpec.MinVersion = version;
            }

            // Same deal for max
            if (!String.IsNullOrWhiteSpace(maxVersionString))
            {
                if (!TryParseVersion(maxVersionString, out version))
                {
                    return(false);
                }
                versionSpec.MaxVersion = version;
            }

            // Successful parse!
            result = versionSpec;
            return(true);
        }
Ejemplo n.º 2
0
        public IEnumerable <PackageReference> GetPackageReferences(bool requireVersion)
        {
            XDocument document = GetDocument();

            if (document == null)
            {
                yield break;
            }

            foreach (var e in document.Root.Elements("package"))
            {
                string          id                          = e.GetOptionalAttributeValue("id");
                string          versionString               = e.GetOptionalAttributeValue("version");
                string          versionConstraintString     = e.GetOptionalAttributeValue("allowedVersions");
                string          targetFrameworkString       = e.GetOptionalAttributeValue("targetFramework");
                string          developmentFlagString       = e.GetOptionalAttributeValue("developmentDependency");
                string          requireReinstallationString = e.GetOptionalAttributeValue("requireReinstallation");
                SemanticVersion version                     = null;

                if (String.IsNullOrEmpty(id))
                {
                    // If the id is empty, ignore the record
                    continue;
                }

                // If the version is invalid, raise an error unless it's both empty and not required
                if ((requireVersion || !String.IsNullOrEmpty(versionString)) && !SemanticVersion.TryParse(versionString, out version))
                {
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ReferenceFile_InvalidVersion, versionString, _path));
                }

                IVersionSpec versionConstaint = null;
                if (!String.IsNullOrEmpty(versionConstraintString))
                {
                    if (!VersionUtility.TryParseVersionSpec(versionConstraintString, out versionConstaint))
                    {
                        throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ReferenceFile_InvalidVersion, versionConstraintString, _path));
                    }

                    _constraints[id] = versionConstraintString;
                }

                FrameworkName targetFramework = null;
                if (!String.IsNullOrEmpty(targetFrameworkString))
                {
                    targetFramework = VersionUtility.ParseFrameworkName(targetFrameworkString);
                    if (targetFramework == VersionUtility.UnsupportedFrameworkName)
                    {
                        targetFramework = null;
                    }
                }

                var developmentFlag = false;
                if (!String.IsNullOrEmpty(developmentFlagString))
                {
                    if (!Boolean.TryParse(developmentFlagString, out developmentFlag))
                    {
                        throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ReferenceFile_InvalidDevelopmentFlag, developmentFlagString, _path));
                    }

                    _developmentFlags[id] = developmentFlagString;
                }

                var requireReinstallation = false;
                if (!String.IsNullOrEmpty(requireReinstallationString))
                {
                    if (!Boolean.TryParse(requireReinstallationString, out requireReinstallation))
                    {
                        throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ReferenceFile_InvalidRequireReinstallationFlag, requireReinstallationString, _path));
                    }
                }

                yield return(new PackageReference(id, version, versionConstaint, targetFramework, developmentFlag, requireReinstallation));
            }
        }
Ejemplo n.º 3
0
        public IEnumerable <PackageInfo> FindPackagesById(string packageId)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentNullException(nameof(packageId));
            }

            // packages\{packageId}\{version}\{packageId}.nuspec
            return(_cache.GetOrAdd(packageId, id =>
            {
                var packages = new List <PackageInfo>();

                foreach (var versionDir in _repositoryRoot.GetDirectories(id))
                {
                    // versionDir = {packageId}\{version}
                    var folders = versionDir.Split(new[] { Path.DirectorySeparatorChar }, 2);

                    // Unknown format
                    if (folders.Length < 2)
                    {
                        continue;
                    }

                    var versionPart = folders[1];

                    // Get the version part and parse it
                    SemanticVersion version;
                    if (!SemanticVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    var manifestFilePath = _repositoryRoot.GetFiles(versionDir, "*" + Constants.ManifestExtension)
                                           .FirstOrDefault();
                    if (string.IsNullOrEmpty(manifestFilePath))
                    {
                        // This is a corrupted packages because {id}.nupsec is missing
                        continue;
                    }

                    if (CheckHashFile && !_repositoryRoot.GetFiles(versionDir, "*" + Constants.HashFileExtension).Any())
                    {
                        // Writing the marker file is the last operation performed by NuGetPackageUtils.InstallFromStream. We'll use the
                        // presence of the file to denote the package was successfully installed.
                        continue;
                    }

                    // If we need to help ensure case-sensitivity, we try to get
                    // the package id in accurate casing by extracting the name of nuspec file
                    // Otherwise we just use the passed in package id for efficiency
                    if (_checkPackageIdCase)
                    {
                        id = Path.GetFileNameWithoutExtension(manifestFilePath);
                    }

                    packages.Add(new PackageInfo(_repositoryRoot, id, version, versionDir));
                }

                return packages;
            }));
        }
Ejemplo n.º 4
0
        private static int GetMaxVersionFromMetadata(ManifestMetadata metadata)
        {
            SemanticVersion version;

            return(!((metadata.ReferenceSets != null) && Enumerable.Any <ManifestReferenceSet>(metadata.ReferenceSets, r => r.TargetFramework != null)) ? (!((metadata.DependencySets != null) && Enumerable.Any <ManifestDependencySet>(metadata.DependencySets, d => d.TargetFramework != null)) ? ((!SemanticVersion.TryParse(metadata.Version, out version) || string.IsNullOrEmpty(version.SpecialVersion)) ? 1 : 3) : 4) : 5);
        }
Ejemplo n.º 5
0
        public IEnumerable <PackageInfo> FindPackagesById(string packageId)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentNullException(nameof(packageId));
            }

            // packages\{packageId}\{version}\{packageId}.nuspec
            return(_cache.GetOrAdd(packageId, id =>
            {
                var packages = new List <PackageInfo>();

                if (_lockFileLibraries != null)
                {
                    foreach (var lockFileLibrary in _lockFileLibraries[packageId])
                    {
                        packages.Add(new PackageInfo(
                                         _repositoryRoot,
                                         lockFileLibrary.Name,
                                         lockFileLibrary.Version,
                                         Path.Combine(
                                             lockFileLibrary.Name,
                                             lockFileLibrary.Version.ToString()),
                                         lockFileLibrary));
                    }

                    return packages;
                }

                foreach (var versionDir in _repositoryRoot.GetDirectories(id))
                {
                    // versionDir = {packageId}\{version}
                    var folders = versionDir.Split(new[] { Path.DirectorySeparatorChar }, 2);

                    // Unknown format
                    if (folders.Length < 2)
                    {
                        continue;
                    }

                    var versionPart = folders[1];

                    // Get the version part and parse it
                    SemanticVersion version;
                    if (!SemanticVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    if (!version.IsOriginalStringNormalized())
                    {
                        // For a non-http match, if the OriginalVersion string is not normalized that means name of the folder which contains
                        // the package is not a normalized string. It will cause trouble for file searching in later stage. By invalidating this
                        // match, it ensures the package will be reinstalled under a correct folder. This change ensures a package installed
                        // by older version of DNX won't prevent new DNX to install correct package.
                        continue;
                    }

                    var manifestFilePath = _repositoryRoot.GetFiles(versionDir, "*" + Constants.ManifestExtension)
                                           .FirstOrDefault();
                    if (string.IsNullOrEmpty(manifestFilePath))
                    {
                        // This is a corrupted packages because {id}.nupsec is missing
                        continue;
                    }

                    if (CheckHashFile && !_repositoryRoot.GetFiles(versionDir, "*" + Constants.HashFileExtension).Any())
                    {
                        // Writing the marker file is the last operation performed by NuGetPackageUtils.InstallFromStream. We'll use the
                        // presence of the file to denote the package was successfully installed.
                        continue;
                    }

                    // If we need to help ensure case-sensitivity, we try to get
                    // the package id in accurate casing by extracting the name of nuspec file
                    // Otherwise we just use the passed in package id for efficiency
                    if (_checkPackageIdCase)
                    {
                        id = Path.GetFileNameWithoutExtension(manifestFilePath);
                    }

                    packages.Add(new PackageInfo(_repositoryRoot, id, version, versionDir));
                }

                return packages;
            }));
        }
Ejemplo n.º 6
0
        public IEnumerable <PackageInfo> FindPackagesById(string packageId)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentNullException("packageId");
            }

            // packages\{packageId}\{version}\{packageId}.nuspec
            return(_cache.GetOrAdd(packageId, id =>
            {
                var packages = new List <PackageInfo>();

                if (_lockFileLibraries != null)
                {
                    foreach (var lockFileLibrary in _lockFileLibraries[packageId])
                    {
                        packages.Add(new PackageInfo(
                                         _repositoryRoot,
                                         lockFileLibrary.Name,
                                         lockFileLibrary.Version,
                                         Path.Combine(
                                             lockFileLibrary.Name,
                                             lockFileLibrary.Version.ToString()),
                                         lockFileLibrary));
                    }

                    return packages;
                }

                foreach (var versionDir in _repositoryRoot.GetDirectories(id))
                {
                    // versionDir = {packageId}\{version}
                    var folders = versionDir.Split(new[] { Path.DirectorySeparatorChar }, 2);

                    // Unknown format
                    if (folders.Length < 2)
                    {
                        continue;
                    }

                    var versionPart = folders[1];

                    // Get the version part and parse it
                    SemanticVersion version;
                    if (!SemanticVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    // If we need to help ensure case-sensitivity, we try to get
                    // the package id in accurate casing by extracting the name of nuspec file
                    // Otherwise we just use the passed in package id for efficiency
                    if (_checkPackageIdCase)
                    {
                        var manifestFileName = Path.GetFileName(
                            _repositoryRoot.GetFiles(versionDir, "*" + Constants.ManifestExtension).FirstOrDefault());
                        if (string.IsNullOrEmpty(manifestFileName))
                        {
                            continue;
                        }
                        id = Path.GetFileNameWithoutExtension(manifestFileName);
                    }

                    packages.Add(new PackageInfo(_repositoryRoot, id, version, versionDir));
                }

                return packages;
            }));
        }