Beispiel #1
0
        private PackageInfo FindCandidate(string name, SemanticVersionRange versionRange)
        {
            var packages = _repository.FindPackagesById(name);

            if (versionRange == null)
            {
                // TODO: Disallow null versions for nuget packages
                var packageInfo = packages.FirstOrDefault();
                if (packageInfo != null)
                {
                    return(packageInfo);
                }

                return(null);
            }

            PackageInfo bestMatch = null;

            foreach (var packageInfo in packages)
            {
                if (VersionUtility.ShouldUseConsidering(
                        current: bestMatch?.Version,
                        considering: packageInfo.Version,
                        ideal: versionRange))
                {
                    bestMatch = packageInfo;
                }
            }

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

            return(bestMatch);
        }
Beispiel #2
0
        private static void PopulateDependencies(
            string projectPath,
            IList <LibraryDependency> results,
            JsonObject settings,
            string propertyName,
            bool isGacOrFrameworkReference)
        {
            var dependencies = settings.ValueAsJsonObject(propertyName);

            if (dependencies != null)
            {
                foreach (var dependencyKey in dependencies.Keys)
                {
                    if (string.IsNullOrEmpty(dependencyKey))
                    {
                        throw FileFormatException.Create(
                                  "Unable to resolve dependency ''.",
                                  dependencies.Value(dependencyKey),
                                  projectPath);
                    }

                    var        dependencyValue           = dependencies.Value(dependencyKey);
                    var        dependencyTypeValue       = LibraryDependencyType.Default;
                    JsonString dependencyVersionAsString = null;

                    if (dependencyValue is JsonObject)
                    {
                        // "dependencies" : { "Name" : { "version": "1.0", "type": "build" } }
                        var dependencyValueAsObject = (JsonObject)dependencyValue;
                        dependencyVersionAsString = dependencyValueAsObject.ValueAsString("version");

                        IEnumerable <string> strings;
                        if (TryGetStringEnumerable(dependencyValueAsObject, "type", out strings))
                        {
                            dependencyTypeValue = LibraryDependencyType.Parse(strings);
                        }
                    }
                    else if (dependencyValue is JsonString)
                    {
                        // "dependencies" : { "Name" : "1.0" }
                        dependencyVersionAsString = (JsonString)dependencyValue;
                    }
                    else
                    {
                        throw FileFormatException.Create(
                                  string.Format("Invalid dependency version: {0}. The format is not recognizable.", dependencyKey),
                                  dependencyValue,
                                  projectPath);
                    }

                    SemanticVersionRange dependencyVersionRange = null;
                    if (!string.IsNullOrEmpty(dependencyVersionAsString?.Value))
                    {
                        try
                        {
                            dependencyVersionRange = VersionUtility.ParseVersionRange(dependencyVersionAsString.Value);
                        }
                        catch (Exception ex)
                        {
                            throw FileFormatException.Create(
                                      ex,
                                      dependencyValue,
                                      projectPath);
                        }
                    }

                    results.Add(new LibraryDependency
                    {
                        LibraryRange = new LibraryRange(dependencyKey, isGacOrFrameworkReference)
                        {
                            VersionRange = dependencyVersionRange,
                            FileName     = projectPath,
                            Line         = dependencyValue.Line,
                            Column       = dependencyValue.Column
                        },
                        Type = dependencyTypeValue
                    });
                }
            }
        }
Beispiel #3
0
        public LibraryDescription GetDescription(LibraryRange libraryRange, FrameworkName targetFramework)
        {
            if (libraryRange.IsGacOrFrameworkReference)
            {
                return(null);
            }

            LockFileTargetLibrary targetLibrary = null;
            var versionRange = libraryRange.VersionRange;

            // REVIEW: This is a little messy because we have the lock file logic and non lock file logic in the same class
            // The runtime rewrite separates the 2 things.
            if (_lookup != null)
            {
                // This means we have a lock file and the target should have
                var lookupKey = Tuple.Create((string)null, targetFramework, libraryRange.Name);

                if (_lookup.TryGetValue(lookupKey, out targetLibrary))
                {
                    // Adjust the target version so we find the right one when looking at the
                    // lock file libraries
                    versionRange = new SemanticVersionRange(targetLibrary.Version);
                }
            }

            var package = FindCandidate(libraryRange.Name, versionRange);

            if (package != null)
            {
                IEnumerable <LibraryDependency> dependencies;
                var resolved   = true;
                var compatible = true;
                if (package.LockFileLibrary != null)
                {
                    if (targetLibrary?.Version == package.LockFileLibrary.Version)
                    {
                        dependencies = GetDependencies(package, targetFramework, targetLibrary);
                    }
                    else
                    {
                        resolved     = false;
                        dependencies = Enumerable.Empty <LibraryDependency>();
                    }

                    // If a NuGet dependency is supposed to provide assemblies but there is no assembly compatible with
                    // current target framework, we should mark this dependency as unresolved
                    if (targetLibrary != null)
                    {
                        var containsAssembly = package.LockFileLibrary.Files
                                               .Any(x => x.StartsWith($"ref{Path.DirectorySeparatorChar}") ||
                                                    x.StartsWith($"lib{Path.DirectorySeparatorChar}"));
                        compatible = targetLibrary.FrameworkAssemblies.Any() ||
                                     targetLibrary.CompileTimeAssemblies.Any() ||
                                     targetLibrary.RuntimeAssemblies.Any() ||
                                     !containsAssembly;
                        resolved = compatible;
                    }
                }
                else
                {
                    dependencies = GetDependencies(package, targetFramework, targetLibrary: null);
                }

                return(new LibraryDescription
                {
                    LibraryRange = libraryRange,
                    Identity = new Library
                    {
                        Name = package.Id,
                        Version = package.Version
                    },
                    Type = "Package",
                    Dependencies = dependencies,
                    Resolved = resolved,
                    Compatible = compatible
                });
            }

            return(null);
        }
Beispiel #4
0
        private static void PopulateDependencies(
            string projectPath,
            IList <LibraryDependency> results,
            JObject settings,
            string propertyName,
            bool isGacOrFrameworkReference)
        {
            var dependencies = settings[propertyName] as JObject;

            if (dependencies != null)
            {
                foreach (var dependency in dependencies.Properties())
                {
                    if (string.IsNullOrEmpty(dependency.Name))
                    {
                        throw FileFormatException.Create(
                                  "Unable to resolve dependency ''.",
                                  dependency,
                                  projectPath);
                    }

                    // Support
                    // "dependencies" : {
                    //    "Name" : "1.0"
                    // }

                    var dependencyValue     = dependency.Value;
                    var dependencyTypeValue = LibraryDependencyType.Default;

                    string dependencyVersionValue = null;
                    JToken dependencyVersionToken = dependencyValue;

                    if (dependencyValue.Type == JTokenType.String)
                    {
                        dependencyVersionValue = dependencyValue.Value <string>();
                    }
                    else
                    {
                        if (dependencyValue.Type == JTokenType.Object)
                        {
                            dependencyVersionToken = dependencyValue["version"];
                            if (dependencyVersionToken != null && dependencyVersionToken.Type == JTokenType.String)
                            {
                                dependencyVersionValue = dependencyVersionToken.Value <string>();
                            }
                        }

                        IEnumerable <string> strings;
                        if (TryGetStringEnumerable(dependencyValue["type"], out strings))
                        {
                            dependencyTypeValue = LibraryDependencyType.Parse(strings);
                        }
                    }

                    SemanticVersionRange dependencyVersionRange = null;

                    if (!string.IsNullOrEmpty(dependencyVersionValue))
                    {
                        try
                        {
                            dependencyVersionRange = VersionUtility.ParseVersionRange(dependencyVersionValue);
                        }
                        catch (Exception ex)
                        {
                            throw FileFormatException.Create(
                                      ex,
                                      dependencyVersionToken,
                                      projectPath);
                        }
                    }

                    var dependencyLineInfo = (IJsonLineInfo)dependency;

                    results.Add(new LibraryDependency
                    {
                        LibraryRange = new LibraryRange
                        {
                            Name         = dependency.Name,
                            VersionRange = dependencyVersionRange,
                            IsGacOrFrameworkReference = isGacOrFrameworkReference,
                            FileName = projectPath,
                            Line     = dependencyLineInfo.LineNumber,
                            Column   = dependencyLineInfo.LinePosition
                        },
                        Type = dependencyTypeValue
                    });
                }
            }
        }