예제 #1
0
        public LockFileLookup(LockFile lockFile)
        {
            _packages = new Dictionary<Tuple<string, SemanticVersion>, LockFilePackageLibrary>();
            _projects = new Dictionary<string, LockFileProjectLibrary>();

            foreach (var library in lockFile.PackageLibraries)
            {
                _packages[Tuple.Create(library.Name, library.Version)] = library;
            }

            foreach (var libary in lockFile.ProjectLibraries)
            {
                _projects[libary.Name] = libary;
            }
        }
예제 #2
0
        public LockFileLookup(LockFile lockFile)
        {
            _lookup = new Dictionary<Tuple<string, FrameworkName, string>, LockFileTargetLibrary>();
            _packages = new Dictionary<Tuple<string, SemanticVersion>, LockFilePackageLibrary>();

            foreach (var t in lockFile.Targets)
            {
                foreach (var library in t.Libraries)
                {
                    // Each target has a single package version per id
                    _lookup[Tuple.Create(t.RuntimeIdentifier, t.TargetFramework, library.Name)] = library;
                }
            }

            foreach (var library in lockFile.PackageLibraries)
            {
                _packages[Tuple.Create(library.Name, library.Version)] = library;
            }
        }
예제 #3
0
        private void AssertLockFileTarget(LockFile lockFile, string searchRid, string assemblyRid)
        {
            var target = lockFile.Targets.SingleOrDefault(t => t.TargetFramework == Dnx451 && string.Equals(t.RuntimeIdentifier, searchRid, StringComparison.Ordinal));
            Assert.NotNull(target);
            var library = target.Libraries.SingleOrDefault(l => l.Name.Equals("RuntimeRestoreTest"));
            Assert.NotNull(library);

            if (string.IsNullOrEmpty(assemblyRid))
            {
                AssertLockFileItemPath("lib/dnx451/RuntimeRestoreTest.dll", library.CompileTimeAssemblies.Single());
                AssertLockFileItemPath("lib/dnx451/RuntimeRestoreTest.dll", library.RuntimeAssemblies.Single());
            }
            else
            {
                AssertLockFileItemPath($"runtimes/{assemblyRid}/lib/dnx451/RuntimeRestoreTest.dll", library.CompileTimeAssemblies.Single());
                AssertLockFileItemPath($"runtimes/{assemblyRid}/lib/dnx451/RuntimeRestoreTest.dll", library.RuntimeAssemblies.Single());
            }
        }
예제 #4
0
 private void WriteLockFile(string dir, string libName, string version)
 {
     var lockFile = new LockFile
     {
         Islocked = false,
         PackageLibraries = new List<LockFilePackageLibrary>
         {
             new LockFilePackageLibrary
             {
                 Name = libName,
                 Version = new SemanticVersion(version),
                 Sha512 = "TestSha"
             }
         }
     };
     var lockFormat = new LockFileFormat();
     lockFormat.Write($"{dir}/project.lock.json", lockFile);
 }
예제 #5
0
        public ApplicationHostContext(IServiceProvider hostServices,
                                      string projectDirectory,
                                      string packagesDirectory,
                                      string configuration,
                                      FrameworkName targetFramework,
                                      IAssemblyLoadContextFactory loadContextFactory = null,
                                      bool skipLockFileValidation = false)
        {
            ProjectDirectory           = projectDirectory;
            Configuration              = configuration;
            RootDirectory              = Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory);
            ProjectResolver            = new ProjectResolver(ProjectDirectory, RootDirectory);
            FrameworkReferenceResolver = new FrameworkReferenceResolver();
            ProjectGraphProvider       = new ProjectGraphProvider(hostServices);
            _serviceProvider           = new ServiceProvider(hostServices);

            PackagesDirectory = packagesDirectory ?? NuGetDependencyResolver.ResolveRepositoryPath(RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(FrameworkReferenceResolver);

            NuGetDependencyProvider = new NuGetDependencyResolver(new PackageRepository(PackagesDirectory));
            var gacDependencyResolver = new GacDependencyResolver();

            ProjectDependencyProvider = new ProjectReferenceDependencyProvider(ProjectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            var projectName = PathUtility.GetDirectoryName(ProjectDirectory);

            Project project;

            if (ProjectResolver.TryResolveProject(projectName, out project))
            {
                Project = project;
            }
            else
            {
                throw new InvalidOperationException(
                          string.Format("Unable to resolve project '{0}' from {1}", projectName, ProjectDirectory));
            }

            var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFileReader.LockFileName);
            var lockFileExists      = File.Exists(projectLockJsonPath);
            var validLockFile       = false;

            if (lockFileExists)
            {
                var lockFileReader = new LockFileReader();
                _lockFile     = lockFileReader.Read(projectLockJsonPath);
                validLockFile = _lockFile.IsValidForProject(project);

                // When the only invalid part of a lock file is version number,
                // we shouldn't skip lock file validation because we want to leave all dependencies unresolved, so that
                // VS can be aware of this version mismatch error and automatically do restore
                skipLockFileValidation = skipLockFileValidation && (_lockFile.Version == Constants.LockFileVersion);

                if (validLockFile || skipLockFileValidation)
                {
                    NuGetDependencyProvider.ApplyLockFile(_lockFile);

                    DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                        ProjectDependencyProvider,
                        NuGetDependencyProvider,
                        referenceAssemblyDependencyResolver,
                        gacDependencyResolver,
                        unresolvedDependencyProvider
                    });
                }
            }

            if ((!validLockFile && !skipLockFileValidation) || !lockFileExists)
            {
                // We don't add NuGetDependencyProvider to DependencyWalker
                // It will leave all NuGet packages unresolved and give error message asking users to run "dnu restore"
                DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                    ProjectDependencyProvider,
                    referenceAssemblyDependencyResolver,
                    gacDependencyResolver,
                    unresolvedDependencyProvider
                });
            }

            LibraryManager = new LibraryManager(() => DependencyWalker.Libraries);

            AssemblyLoadContextFactory = loadContextFactory ?? new RuntimeLoadContextFactory(ServiceProvider);

            // Create a new Application Environment for running the app. It needs a reference to the Host's application environment
            // (if any), which we can get from the service provider we were given.
            // If this is null (i.e. there is no Host Application Environment), that's OK, the Application Environment we are creating
            // will just have it's own independent set of global data.
            IApplicationEnvironment hostEnvironment = null;

            if (hostServices != null)
            {
                hostEnvironment = (IApplicationEnvironment)hostServices.GetService(typeof(IApplicationEnvironment));
            }
            ApplicationEnvironment = new ApplicationEnvironment(Project, targetFramework, configuration, hostEnvironment);

            // Default services
            _serviceProvider.Add(typeof(IApplicationEnvironment), ApplicationEnvironment);
            _serviceProvider.Add(typeof(ILibraryManager), LibraryManager);
            _serviceProvider.Add(typeof(ICompilerOptionsProvider), new CompilerOptionsProvider(ProjectResolver));

            // Not exposed to the application layer
            _serviceProvider.Add(typeof(IProjectResolver), ProjectResolver, includeInManifest: false);
            _serviceProvider.Add(typeof(NuGetDependencyResolver), NuGetDependencyProvider, includeInManifest: false);
            _serviceProvider.Add(typeof(ProjectReferenceDependencyProvider), ProjectDependencyProvider, includeInManifest: false);
            _serviceProvider.Add(typeof(IAssemblyLoadContextFactory), AssemblyLoadContextFactory, includeInManifest: false);
        }
예제 #6
0
 public static bool HasTarget(this LockFile self, FrameworkName framework, string runtimeIdentifier)
 {
     return(self.Targets.Any(t => t.TargetFramework == framework && string.Equals(t.RuntimeIdentifier, runtimeIdentifier, StringComparison.Ordinal)));
 }
예제 #7
0
 public static bool HasTarget(this LockFile self, FrameworkName framework)
 {
     return(self.Targets.Any(t => t.TargetFramework == framework && string.IsNullOrEmpty(t.RuntimeIdentifier)));
 }
예제 #8
0
        private static LockFileTarget SelectTarget(ApplicationHostContext context, LockFile lockFile)
        {
            foreach (var runtimeIdentifier in context.RuntimeIdentifiers)
            {
                foreach (var scanTarget in lockFile.Targets)
                {
                    if (scanTarget.TargetFramework == context.TargetFramework && string.Equals(scanTarget.RuntimeIdentifier, runtimeIdentifier, StringComparison.Ordinal))
                    {
                        return scanTarget;
                    }
                }
            }

            foreach (var scanTarget in lockFile.Targets)
            {
                if (scanTarget.TargetFramework == context.TargetFramework && string.IsNullOrEmpty(scanTarget.RuntimeIdentifier))
                {
                    return scanTarget;
                }
            }

            return null;
        }
예제 #9
0
 public void ApplyLockFile(LockFile lockFile)
 {
     var stringComparer = _checkPackageIdCase ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
     _lockFileLibraries = lockFile.PackageLibraries
                                  .ToLookup(l => l.Name, stringComparer);
 }