public void BuildPackageAndCheckServiceability(string projectName, bool expectedServiceability) { var rootDir = ProjectResolver.ResolveRootDirectory(Directory.GetCurrentDirectory()); var projectSrcDir = Path.Combine(rootDir, "misc", "ServicingTestProjects", projectName); const string configuration = "Debug"; var components = TestUtils.GetRuntimeComponentsCombinations().First(); var flavor = (string)components[0]; var os = (string)components[1]; var architecture = (string)components[2]; using (var runtimeHomeDir = TestUtils.GetRuntimeHomeDir(flavor, os, architecture)) using (var tempDir = new DisposableDir()) { var projectDir = Path.Combine(tempDir, projectName); var buildOutpuDir = Path.Combine(tempDir, "output"); TestUtils.CopyFolder(projectSrcDir, projectDir); var exitCode = DnuTestUtils.ExecDnu(runtimeHomeDir, "restore", projectDir); Assert.Equal(0, exitCode); exitCode = DnuTestUtils.ExecDnu( runtimeHomeDir, "pack", $"{projectDir} --out {buildOutpuDir} --configuration {configuration}"); Assert.Equal(0, exitCode); var assemblyPath = Path.Combine(buildOutpuDir, configuration, "dnx451", $"{projectName}.dll"); Assert.Equal(expectedServiceability, LockFileUtils.IsAssemblyServiceable(assemblyPath)); } }
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); }
public static void UpdatePackageReferenceMetadata( LockFile lockFile, FallbackPackagePathResolver pathResolver, PackageIdentity package) { var info = pathResolver.GetPackageInfo(package.Id, package.Version); if (info == null) { // don't do anything if package was not resolved on disk return; } var nuspecFilePath = info.PathResolver.GetManifestFilePath(package.Id, package.Version); var nuspecReader = new NuspecReader(nuspecFilePath); var developmentDependency = nuspecReader.GetDevelopmentDependency(); if (developmentDependency) { foreach (var frameworkInfo in lockFile.PackageSpec.TargetFrameworks .OrderBy(framework => framework.FrameworkName.ToString(), StringComparer.Ordinal)) { var dependency = frameworkInfo.Dependencies.First(dep => dep.Name.Equals(package.Id, StringComparison.OrdinalIgnoreCase)); if (dependency?.SuppressParent == LibraryIncludeFlagUtils.DefaultSuppressParent && dependency?.IncludeType == LibraryIncludeFlags.All) { var includeType = LibraryIncludeFlags.All & ~LibraryIncludeFlags.Compile; dependency.SuppressParent = LibraryIncludeFlags.All; dependency.IncludeType = includeType; // update lock file target libraries foreach (var target in lockFile.Targets .Where(t => EqualityUtility.EqualsWithNullCheck(t.TargetFramework, frameworkInfo.FrameworkName))) { var targetLibrary = target.GetTargetLibrary(package.Id); if (targetLibrary != null) { LockFileUtils.ExcludeItems(targetLibrary, includeType); } } } } } }
public void BuildPackageAndCheckServiceability(string projectName, bool expectedServiceability) { var rootDir = ProjectResolver.ResolveRootDirectory(Directory.GetCurrentDirectory()); var projectDir = Path.Combine(rootDir, "misc", "ServicingTestProjects", projectName); const string configuration = "Debug"; using (var runtimeHomeDir = TestUtils.GetRuntimeHomeDir(flavor: "clr", os: "win", architecture: "x86")) using (var tempDir = new DisposableDir()) { var buildOutpuDir = Path.Combine(tempDir, "output"); DnuTestUtils.ExecDnu( runtimeHomeDir, "pack", $"{projectDir} --out {buildOutpuDir} --configuration {configuration}"); var assemblyPath = Path.Combine(buildOutpuDir, configuration, "dnx451", $"{projectName}.dll"); Assert.Equal(expectedServiceability, LockFileUtils.IsAssemblyServiceable(assemblyPath)); } }
private void WriteLockFile(LockFile previousLockFile, string projectLockFilePath, Runtime.Project project, List <GraphItem> graphItems, PackageRepository repository, IProjectResolver projectResolver, IEnumerable <TargetContext> contexts) { var resolver = new DefaultPackagePathResolver(repository.RepositoryRoot.Root); var previousPackageLibraries = previousLockFile?.PackageLibraries.ToDictionary(l => Tuple.Create(l.Name, l.Version)); var lockFile = new LockFile(); lockFile.Islocked = Lock; // 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()))); } // Record all libraries used foreach (var item in graphItems.OrderBy(x => x.Match.Library, new LibraryComparer())) { if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Implicit)) { continue; } var library = item.Match.Library; if (library.Name == project.Name) { continue; } if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Project)) { var projectDependency = projectResolver.FindProject(library.Name); var projectLibrary = LockFileUtils.CreateLockFileProjectLibrary(project, projectDependency); lockFile.ProjectLibraries.Add(projectLibrary); } else if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Package)) { var packageInfo = repository.FindPackagesById(library.Name) .FirstOrDefault(p => p.Version == library.Version); if (packageInfo == null) { throw new InvalidOperationException($"Unresolved package: {library.Name}"); } LockFilePackageLibrary previousLibrary = null; previousPackageLibraries?.TryGetValue(Tuple.Create(library.Name, library.Version), out previousLibrary); var package = packageInfo.Package; // The previousLibrary can't be a project, otherwise exception has been thrown. lockFile.PackageLibraries.Add(LockFileUtils.CreateLockFilePackageLibrary( previousLibrary, resolver, package, correctedPackageName: library.Name)); } else { throw new InvalidOperationException($"Unresolved library: {library.Name}"); } } var packageLibraries = lockFile.PackageLibraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version)); // Add the contexts foreach (var context in contexts) { var target = new LockFileTarget(); target.TargetFramework = context.RestoreContext.FrameworkName; target.RuntimeIdentifier = context.RestoreContext.RuntimeName; foreach (var match in context.Matches.OrderBy(x => x.Library, new LibraryComparer())) { if (match.Library.Name == project.Name) { continue; } if (match.LibraryType.Equals(Runtime.LibraryTypes.Project)) { var projectDependency = projectResolver.FindProject(match.Library.Name); var projectTargetLibrary = LockFileUtils.CreateLockFileTargetLibrary(projectDependency, context.RestoreContext); target.Libraries.Add(projectTargetLibrary); } else if (match.LibraryType.Equals(Runtime.LibraryTypes.Package)) { var packageInfo = repository.FindPackagesById(match.Library.Name) .FirstOrDefault(p => p.Version == match.Library.Version); var package = packageInfo.Package; target.Libraries.Add(LockFileUtils.CreateLockFileTargetLibrary( packageLibraries[Tuple.Create(match.Library.Name, match.Library.Version)], package, context.RestoreContext, correctedPackageName: match.Library.Name)); } } lockFile.Targets.Add(target); } var lockFileFormat = new LockFileFormat(); lockFileFormat.Write(projectLockFilePath, lockFile); }
private void WriteLockFile(LockFile previousLockFile, string projectLockFilePath, Runtime.Project project, List <GraphItem> graphItems, PackageRepository repository, IEnumerable <TargetContext> contexts) { var resolver = new DefaultPackagePathResolver(repository.RepositoryRoot); var previousLibraries = previousLockFile?.Libraries.ToDictionary(l => Tuple.Create(l.Name, l.Version)); var lockFile = new LockFile(); lockFile.Islocked = Lock; // 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()))); } // Record all libraries used 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; LockFileLibrary previousLibrary = null; previousLibraries?.TryGetValue(Tuple.Create(library.Name, library.Version), out previousLibrary); var lockFileLib = LockFileUtils.CreateLockFileLibrary( previousLibrary, resolver, package, correctedPackageName: library.Name); lockFile.Libraries.Add(lockFileLib); } var libraries = lockFile.Libraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version)); // Add the contexts foreach (var context in contexts) { var target = new LockFileTarget(); target.TargetFramework = context.RestoreContext.FrameworkName; target.RuntimeIdentifier = context.RestoreContext.RuntimeName; foreach (var library in context.Libraries.OrderBy(x => x, new LibraryComparer())) { var packageInfo = repository.FindPackagesById(library.Name) .FirstOrDefault(p => p.Version == library.Version); if (packageInfo == null) { continue; } var package = packageInfo.Package; var targetLibrary = LockFileUtils.CreateLockFileTargetLibrary( libraries[Tuple.Create(library.Name, library.Version)], package, context.RestoreContext, correctedPackageName: library.Name); target.Libraries.Add(targetLibrary); } lockFile.Targets.Add(target); } var lockFileFormat = new LockFileFormat(); lockFileFormat.Write(projectLockFilePath, lockFile); }
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); }