Exemplo n.º 1
0
        private int LibraryDetail(PackageSpec project, LockFile lockfile, string targetName, string library)
        {
            var lib = lockfile.Libraries.FirstOrDefault(l => l.Name.Equals(library, StringComparison.OrdinalIgnoreCase));
            if (lib == null)
            {
                _log.LogError($"Library not found: {library}");
                return -1;
            }

            _log.LogInformation($"{lib.Name} {lib.Version}");
            _log.LogInformation($"Servicable: {lib.IsServiceable}");
            _log.LogInformation($"SHA512 Hash: {lib.Sha512}");
            _log.LogInformation($"Files:");
            foreach (var file in lib.Files)
            {
                _log.LogInformation($" * {file}");
            }

            IEnumerable<LockFileTarget> targets = lockfile.Targets;
            if (!string.IsNullOrEmpty(targetName))
            {
                var parts = targetName.Split('/');
                var tfm = NuGetFramework.Parse(parts[0]);
                var rid = parts[1];
                targets = targets.Where(t => string.Equals(rid, t.RuntimeIdentifier, StringComparison.Ordinal) && tfm.Equals(t.TargetFramework));
            }
            var libraryTargets = targets.Select(t => new { Target = t, Library = t.Libraries.FirstOrDefault(l => string.Equals(l.Name, library, StringComparison.OrdinalIgnoreCase)) });
            foreach (var libraryTarget in libraryTargets)
            {
                _log.LogInformation($"Target: {libraryTarget.Target.TargetFramework}/{libraryTarget.Target.RuntimeIdentifier}");
                if (libraryTarget.Library == null)
                {
                    _log.LogInformation(" Not supported");
                }
                else
                {
                    WriteList(Compile, libraryTarget.Library.CompileTimeAssemblies.Select(f => f.Path));
                    WriteList(Runtime, libraryTarget.Library.RuntimeAssemblies.Select(f => f.Path));
                    WriteList(Native, libraryTarget.Library.NativeLibraries.Select(f => f.Path));
                    WriteList(Framework, libraryTarget.Library.FrameworkAssemblies);
                }
            }

            return 0;
        }
Exemplo n.º 2
0
        public LockFileDependencyProvider(LockFile lockFile)
        {
            // List of all the libraries in the lock file, there can be multiple versions per id
            _libraries = lockFile.Libraries.ToDictionary(l => Tuple.Create(l.Name, l.Version));

            // Dependencies can only vary by target framework so only look at that target
            _targetLibraries = new Dictionary<Tuple<NuGetFramework, string>, LockFileTargetLibrary>();

            foreach (var target in lockFile.Targets)
            {
                if (!string.IsNullOrEmpty(target.RuntimeIdentifier))
                {
                    continue;
                }

                foreach (var library in target.Libraries)
                {
                    _targetLibraries[Tuple.Create(target.TargetFramework, library.Name)] = library;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the library from each target graph it exists in.
        /// </summary>
        public static IEnumerable <LockFileTargetLibrary> GetTargetLibraries(this IAssetsLogMessage message, LockFile assetsFile)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (assetsFile == null)
            {
                throw new ArgumentNullException(nameof(assetsFile));
            }

            return(message.GetTargetGraphs(assetsFile).Select(target => target.GetTargetLibrary(message.LibraryId)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get target graphs for the current log message.
        /// </summary>
        public static IEnumerable <LockFileTarget> GetTargetGraphs(this IAssetsLogMessage message, LockFile assetsFile)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (assetsFile == null)
            {
                throw new ArgumentNullException(nameof(assetsFile));
            }

            return(assetsFile.Targets.Where(target => message.TargetGraphs.Contains(target.Name, StringComparer.OrdinalIgnoreCase)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get target graphs for the current log message.
        /// </summary>
        /// <remarks>If the message does not contain target graphs all graphs in the file
        /// will be returned.</remarks>
        public static IEnumerable <LockFileTarget> GetTargetGraphs(this IAssetsLogMessage message, LockFile assetsFile)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (assetsFile == null)
            {
                throw new ArgumentNullException(nameof(assetsFile));
            }

            // If the message does not contain any target graph it should apply to all graphs.
            if (message.TargetGraphs == null || message.TargetGraphs.Count == 0)
            {
                return(assetsFile.Targets);
            }

            return(assetsFile.Targets.Where(target => message.TargetGraphs.Contains(target.Name, StringComparer.OrdinalIgnoreCase)));
        }
Exemplo n.º 6
0
        private LockFile CreateLockFile(PackageSpec project, List<RestoreTargetGraph> targetGraphs, NuGetv3LocalRepository repository)
        {
            var lockFile = new LockFile();

            using (var sha512 = SHA512.Create())
            {
                foreach (var item in targetGraphs.SelectMany(g => g.Flattened).Distinct().OrderBy(x => x.Data.Match.Library))
                {
                    var library = item.Data.Match.Library;
                    var packageInfo = repository.FindPackagesById(library.Name)
                        .FirstOrDefault(p => p.Version == library.Version);

                    if (packageInfo == null)
                    {
                        continue;
                    }

                    var lockFileLib = CreateLockFileLibrary(
                        packageInfo,
                        sha512,
                        correctedPackageName: library.Name);

                    lockFile.Libraries.Add(lockFileLib);
                }
            }

            // Use empty string as the key of dependencies shared by all frameworks
            lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                string.Empty,
                project.Dependencies.Select(x => x.LibraryRange.ToString())));

            foreach (var frameworkInfo in project.TargetFrameworks)
            {
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                    frameworkInfo.FrameworkName.ToString(),
                    frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
            }

            // Add the targets
            foreach (var targetGraph in targetGraphs)
            {
                var target = new LockFileTarget();
                target.TargetFramework = targetGraph.Framework;
                target.RuntimeIdentifier = targetGraph.RuntimeIdentifier;

                foreach (var library in targetGraph.Flattened.Select(g => g.Key).OrderBy(x => x))
                {
                    var packageInfo = repository.FindPackagesById(library.Name)
                        .FirstOrDefault(p => p.Version == library.Version);

                    if (packageInfo == null)
                    {
                        continue;
                    }

                    var targetLibrary = CreateLockFileTargetLibrary(
                        packageInfo,
                        targetGraph,
                        new DefaultPackagePathResolver(repository.RepositoryRoot),
                        correctedPackageName: library.Name);

                    target.Libraries.Add(targetLibrary);
                }

                lockFile.Targets.Add(target);
            }

            return lockFile;
        }
Exemplo n.º 7
0
        private int SummarizeLockfile(PackageSpec project, LockFile lockfile, string targetName)
        {
            _log.LogInformation($"Locked: {lockfile.IsLocked}");

            if (project == null)
            {
                _log.LogInformation($"Up-to-date: Unknown");
            }
            else
            {
                _log.LogInformation($"Up-to-date: {lockfile.IsValidForPackageSpec(project)}");
            }

            _log.LogInformation("Project Dependencies:");
            foreach (var group in lockfile.ProjectFileDependencyGroups)
            {
                var fxName = string.IsNullOrEmpty(group.FrameworkName) ? "All Frameworks" : group.FrameworkName;
                if (group.Dependencies.Any())
                {
                    _log.LogInformation($" {fxName}");
                    foreach (var dep in group.Dependencies)
                    {
                        _log.LogInformation($"  * {dep}");
                    }
                }
                else
                {
                    _log.LogInformation($" {fxName}: none");
                }
            }

            _log.LogInformation("All Libraries:");
            foreach (var lib in lockfile.Libraries)
            {
                _log.LogInformation($"* {lib.Name} {lib.Version}");
            }

            IEnumerable<LockFileTarget> targets = lockfile.Targets;
            if (!string.IsNullOrEmpty(targetName))
            {
                var parts = targetName.Split('/');
                var tfm = NuGetFramework.Parse(parts[0]);
                var rid = parts[1];
                targets = targets.Where(t => string.Equals(rid, t.RuntimeIdentifier, StringComparison.Ordinal) && tfm.Equals(t.TargetFramework));
            }

            foreach (var target in targets)
            {
                _log.LogInformation($"Target: {target.TargetFramework} {target.RuntimeIdentifier}");
                foreach (var lib in target.Libraries)
                {
                    var provides = string.Empty;
                    if (lib.NativeLibraries.Any())
                    {
                        provides += Native + ",";
                    }
                    if (lib.RuntimeAssemblies.Any())
                    {
                        provides += Runtime + ",";
                    }
                    if (lib.CompileTimeAssemblies.Any())
                    {
                        provides += Compile + ",";
                    }
                    if (lib.FrameworkAssemblies.Any())
                    {
                        provides += Framework + ",";
                    }
                    provides = provides.TrimEnd(',');
                    if (string.IsNullOrEmpty(provides))
                    {
                        provides = Nothing;
                    }
                    _log.LogInformation($" * [{provides}] {lib.Name} {lib.Version}");
                }
            }
            return 0;
        }
Exemplo n.º 8
0
        private static void WriteLockFile(JsonWriter writer, IObjectWriter jsonObjectWriter, LockFile lockFile)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(VersionProperty);
            writer.WriteValue(lockFile.Version);

            writer.WritePropertyName(TargetsProperty);
            JsonUtility.WriteObject(writer, lockFile.Targets, WriteTarget);

            writer.WritePropertyName(LibrariesProperty);
            JsonUtility.WriteObject(writer, lockFile.Libraries, WriteLibrary);

            writer.WritePropertyName(ProjectFileDependencyGroupsProperty);
            JsonUtility.WriteObject(writer, lockFile.ProjectFileDependencyGroups, WriteProjectFileDependencyGroup);

            if (lockFile.PackageFolders?.Any() == true)
            {
                writer.WritePropertyName(PackageFoldersProperty);
                JsonUtility.WriteObject(writer, lockFile.PackageFolders, WriteFileItem);
            }

            if (lockFile.Version >= 2)
            {
                if (lockFile.PackageSpec != null)
                {
                    writer.WritePropertyName(PackageSpecProperty);

                    jsonObjectWriter.WriteObjectStart();

                    PackageSpecWriter.Write(lockFile.PackageSpec, jsonObjectWriter);

                    jsonObjectWriter.WriteObjectEnd();
                }
            }

            if (lockFile.Version >= 3)
            {
                if (lockFile.LogMessages.Count > 0)
                {
                    var projectPath = lockFile.PackageSpec?.RestoreMetadata?.ProjectPath;
                    writer.WritePropertyName(LogsProperty);
                    WriteLogMessages(writer, lockFile.LogMessages, projectPath);
                }
            }

            if (lockFile.CentralTransitiveDependencyGroups.Any())
            {
                writer.WritePropertyName(CentralTransitiveDependencyGroupsProperty);
                WriteCentralTransitiveDependencyGroup(jsonObjectWriter, lockFile.CentralTransitiveDependencyGroups);
            }

            writer.WriteEndObject();
        }
Exemplo n.º 9
0
 public RestoreResult(bool success, IEnumerable<RestoreTargetGraph> restoreGraphs, LockFile lockfile)
 {
     Success = success;
     RestoreGraphs = restoreGraphs;
     LockFile = lockfile;
 }
Exemplo n.º 10
0
 private static bool TryReadLockFile(string directory, out LockFile lockFile)
 {
     lockFile = null;
     string file = Path.Combine(directory, LockFileFormat.LockFileName);
     if (File.Exists(file))
     {
         using (var stream = File.OpenRead(file))
         {
             lockFile = LockFileFormat.Read(stream);
         }
         return true;
     }
     return false;
 }