Exemple #1
0
        private void WriteLockFile(string projectLockFilePath, Runtime.Project project, List <GraphItem> graphItems,
                                   PackageRepository repository, IEnumerable <FrameworkName> frameworks)
        {
            var lockFile = new LockFile();

            lockFile.Islocked = Lock;

            using (var sha512 = SHA512.Create())
            {
                foreach (var item in graphItems.OrderBy(x => x.Match.Library, new LibraryComparer()))
                {
                    var library     = item.Match.Library;
                    var packageInfo = repository.FindPackagesById(library.Name)
                                      .FirstOrDefault(p => p.Version == library.Version);

                    if (packageInfo == null)
                    {
                        continue;
                    }

                    var package     = packageInfo.Package;
                    var lockFileLib = LockFileUtils.CreateLockFileLibraryForProject(
                        project,
                        package,
                        sha512,
                        frameworks,
                        new DefaultPackagePathResolver(repository.RepositoryRoot),
                        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.GetTargetFrameworks())
            {
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                             frameworkInfo.FrameworkName.ToString(),
                                                             frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
            }

            var lockFileFormat = new LockFileFormat();

            lockFileFormat.Write(projectLockFilePath, lockFile);
        }
Exemple #2
0
        private void UpdateLockFile(PublishRoot root)
        {
            var    lockFileFormat = new LockFileFormat();
            string lockFilePath;

            if (root.NoSource)
            {
                lockFilePath = Path.Combine(TargetPath, "root", LockFileFormat.LockFileName);
            }
            else
            {
                lockFilePath = Path.Combine(TargetPath, LockFileFormat.LockFileName);
            }

            LockFile lockFile;

            if (File.Exists(lockFilePath))
            {
                lockFile = lockFileFormat.Read(lockFilePath);
            }
            else
            {
                lockFile = new LockFile
                {
                    Islocked = false
                };

                var project = GetCurrentProject();

                // Restore dependency groups for future lockfile validation
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                             string.Empty,
                                                             project.Dependencies.Select(x => x.LibraryRange.ToString())));
                foreach (var frameworkInfo in project.GetTargetFrameworks())
                {
                    lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                                 frameworkInfo.FrameworkName.ToString(),
                                                                 frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
                }
            }

            if (root.NoSource)
            {
                // The dependency group shared by all frameworks should only contain the main nupkg (i.e. entrypoint)
                lockFile.ProjectFileDependencyGroups.RemoveAll(g => string.IsNullOrEmpty(g.FrameworkName));
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                             string.Empty,
                                                             new[] { _libraryDescription.LibraryRange.ToString() }));
            }

            var repository = new PackageRepository(root.TargetPackagesPath);
            var resolver   = new DefaultPackagePathResolver(root.TargetPackagesPath);

            // For dependency projects that were published to nupkgs
            // Add them to lockfile to ensure the contents of lockfile are still valid
            using (var sha512 = SHA512.Create())
            {
                foreach (var publishProject in root.Projects.Where(p => p.IsPackage))
                {
                    var packageInfo = repository.FindPackagesById(publishProject.Name)
                                      .SingleOrDefault();
                    if (packageInfo == null)
                    {
                        root.Reports.Information.WriteLine("Unable to locate published package {0} in {1}",
                                                           publishProject.Name.Yellow(),
                                                           repository.RepositoryRoot);
                        continue;
                    }

                    var package   = packageInfo.Package;
                    var nupkgPath = resolver.GetPackageFilePath(package.Id, package.Version);

                    var project = publishProject.GetCurrentProject();
                    lockFile.Libraries.Add(LockFileUtils.CreateLockFileLibraryForProject(
                                               project,
                                               package,
                                               sha512,
                                               project.GetTargetFrameworks().Select(f => f.FrameworkName),
                                               resolver));
                }
            }

            lockFileFormat.Write(lockFilePath, lockFile);
        }