Exemplo n.º 1
0
        public ModuleLoaderContext(IServiceProvider serviceProvider,
                                       string projectDirectory)
        {
            ProjectDirectory = projectDirectory;
            RootDirectory = Microsoft.Dnx.Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory);

            // A new project resolver is required. you cannot reuse the one from the
            // parent service provider as that will be for the parent context.
            ProjectResolver = new ProjectResolver(ProjectDirectory, RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(new FrameworkReferenceResolver());

            // Need to pass through package directory incase you download a package from the gallary, this needs to know about it
            var nuGetDependencyProvider = new NuGetDependencyResolver(new PackageRepository(
                NuGetDependencyResolver.ResolveRepositoryPath(RootDirectory)));
            var gacDependencyResolver = new GacDependencyResolver();
            var projectDepencyProvider = new ProjectReferenceDependencyProvider(ProjectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFileFormat.LockFileName);
            if (File.Exists(projectLockJsonPath)) {
                var lockFileFormat = new LockFileFormat();
                var lockFile = lockFileFormat.Read(projectLockJsonPath);
                nuGetDependencyProvider.ApplyLockFile(lockFile);
            }

            DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                projectDepencyProvider,
                nuGetDependencyProvider,
                referenceAssemblyDependencyResolver,
                gacDependencyResolver,
                unresolvedDependencyProvider
            });

            LibraryExportProvider = new CompositeLibraryExportProvider(new ILibraryExportProvider[] {
                new ModuleProjectLibraryExportProvider(
                    ProjectResolver, serviceProvider),
                referenceAssemblyDependencyResolver,
                gacDependencyResolver,
                nuGetDependencyProvider
            });
        }
Exemplo n.º 2
0
        public static void Initialize(ApplicationHostContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ProjectDirectory = context.Project?.ProjectDirectory ?? context.ProjectDirectory;
            context.RootDirectory    = context.RootDirectory ?? ProjectResolver.ResolveRootDirectory(context.ProjectDirectory);
            var projectResolver            = new ProjectResolver(context.ProjectDirectory, context.RootDirectory);
            var frameworkReferenceResolver = context.FrameworkResolver ?? new FrameworkReferenceResolver();

            context.PackagesDirectory = context.PackagesDirectory ?? PackageDependencyProvider.ResolveRepositoryPath(context.RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            var gacDependencyResolver        = new GacDependencyResolver();
            var projectDependencyProvider    = new ProjectReferenceDependencyProvider(projectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            IList <IDependencyProvider> dependencyProviders = null;
            LockFileLookup lockFileLookup = null;

            if (context.Project == null)
            {
                Project project;
                if (Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    context.Project = project;
                }
                else
                {
                    throw new InvalidOperationException($"Unable to resolve project from {context.ProjectDirectory}");
                }
            }

            var    projectLockJsonPath       = Path.Combine(context.ProjectDirectory, LockFileReader.LockFileName);
            var    lockFileExists            = File.Exists(projectLockJsonPath);
            var    validLockFile             = false;
            var    skipLockFileValidation    = context.SkipLockfileValidation;
            string lockFileValidationMessage = null;

            if (lockFileExists)
            {
                var lockFileReader = new LockFileReader();
                var lockFile       = lockFileReader.Read(projectLockJsonPath);
                validLockFile = lockFile.IsValidForProject(context.Project, out lockFileValidationMessage);

                // 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 = context.SkipLockfileValidation && (lockFile.Version == Constants.LockFileVersion);

                if (validLockFile || skipLockFileValidation)
                {
                    lockFileLookup = new LockFileLookup(lockFile);
                    var packageDependencyProvider = new PackageDependencyProvider(context.PackagesDirectory, lockFileLookup);

                    dependencyProviders = new IDependencyProvider[] {
                        projectDependencyProvider,
                        packageDependencyProvider,
                        referenceAssemblyDependencyResolver,
                        gacDependencyResolver,
                        unresolvedDependencyProvider
                    };
                }
            }

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

            var libraries = DependencyWalker.Walk(dependencyProviders, context.Project.Name, context.Project.Version, context.TargetFramework);

            context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);

            if (!validLockFile)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                                                                $"{lockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
                                                                Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                                                                DiagnosticMessageSeverity.Error));
            }

            if (!lockFileExists)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                                                                $"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
                                                                Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                                                                DiagnosticMessageSeverity.Error));
            }

            // Clear all the temporary memory aggressively here if we don't care about reuse
            // e.g. runtime scenarios
            lockFileLookup?.Clear();
            projectResolver.Clear();
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static void Initialize(ApplicationHostContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ProjectDirectory = context.Project?.ProjectDirectory ?? context.ProjectDirectory;
            context.RootDirectory = context.RootDirectory ?? ProjectResolver.ResolveRootDirectory(context.ProjectDirectory);
            var projectResolver = new ProjectResolver(context.ProjectDirectory, context.RootDirectory);
            var frameworkReferenceResolver = context.FrameworkResolver ?? new FrameworkReferenceResolver();
            context.PackagesDirectory = context.PackagesDirectory ?? PackageDependencyProvider.ResolveRepositoryPath(context.RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            var gacDependencyResolver = new GacDependencyResolver();
            var projectDependencyProvider = new ProjectReferenceDependencyProvider(projectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            IList<IDependencyProvider> dependencyProviders = null;
            LockFileLookup lockFileLookup = null;

            if (context.Project == null)
            {
                Project project;
                if (Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    context.Project = project;
                }
                else
                {
                    throw new InvalidOperationException($"Unable to resolve project from {context.ProjectDirectory}");
                }
            }

            var projectLockJsonPath = Path.Combine(context.ProjectDirectory, LockFileReader.LockFileName);
            var lockFileExists = File.Exists(projectLockJsonPath);
            var validLockFile = false;
            var skipLockFileValidation = context.SkipLockfileValidation;
            string lockFileValidationMessage = null;

            if (lockFileExists)
            {
                var lockFileReader = new LockFileReader();
                var lockFile = lockFileReader.Read(projectLockJsonPath);
                validLockFile = lockFile.IsValidForProject(context.Project, out lockFileValidationMessage);

                // 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 = context.SkipLockfileValidation && (lockFile.Version == Constants.LockFileVersion);

                if (validLockFile || skipLockFileValidation)
                {
                    lockFileLookup = new LockFileLookup(lockFile);
                    var packageDependencyProvider = new PackageDependencyProvider(context.PackagesDirectory, lockFileLookup);

                    dependencyProviders = new IDependencyProvider[] {
                        projectDependencyProvider,
                        packageDependencyProvider,
                        referenceAssemblyDependencyResolver,
                        gacDependencyResolver,
                        unresolvedDependencyProvider
                    };
                }
            }

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

            var libraries = DependencyWalker.Walk(dependencyProviders, context.Project.Name, context.Project.Version, context.TargetFramework);

            context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);

            if (!validLockFile)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                    $"{lockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
                    Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                    DiagnosticMessageSeverity.Error));
            }

            if (!lockFileExists)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                    $"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
                    Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                    DiagnosticMessageSeverity.Error));
            }

            // Clear all the temporary memory aggressively here if we don't care about reuse
            // e.g. runtime scenarios
            lockFileLookup?.Clear();
            projectResolver.Clear();
        }
Exemplo n.º 5
0
        public static void Initialize(ApplicationHostContext context)
        {
            var result = InitializeCore(context);

            // Map dependencies
            var lookup = result.Libraries.ToDictionary(l => l.Identity.Name);

            var frameworkReferenceResolver = context.FrameworkResolver ?? new FrameworkReferenceResolver();
            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            var gacDependencyResolver = new GacDependencyResolver();
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            // Fix up dependencies
            foreach (var library in lookup.Values.ToList())
            {
                if (string.Equals(library.Type, LibraryTypes.Package) &&
                    !Directory.Exists(library.Path))
                {
                    // If the package path doesn't exist then mark this dependency as unresolved
                    library.Resolved = false;
                }

                library.Framework = library.Framework ?? context.TargetFramework;
                foreach (var dependency in library.Dependencies)
                {
                    LibraryDescription dep;
                    if (!lookup.TryGetValue(dependency.Name, out dep))
                    {
                        if (dependency.LibraryRange.IsGacOrFrameworkReference)
                        {
                            dep = referenceAssemblyDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
                                  gacDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
                                  unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);

                            dep.Framework = context.TargetFramework;
                            lookup[dependency.Name] = dep;
                        }
                        else
                        {
                            dep = unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);
                            lookup[dependency.Name] = dep;
                        }
                    }

                    // REVIEW: This isn't quite correct but there's a many to one relationship here.
                    // Different ranges can resolve to this dependency but only one wins
                    dep.RequestedRange = dependency.LibraryRange;
                    dependency.Library = dep;
                }
            }

            var libraries = lookup.Values.ToList();

            context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);

            AddLockFileDiagnostics(context, result);

            // Clear all the temporary memory aggressively here if we don't care about reuse
            // e.g. runtime scenarios
            lookup.Clear();
        }
Exemplo n.º 6
0
        public static void Initialize(ApplicationHostContext context)
        {
            var result = InitializeCore(context);

            // Map dependencies
            var lookup = result.Libraries.ToDictionary(l => l.Identity.Name);

            var frameworkReferenceResolver          = context.FrameworkResolver ?? new FrameworkReferenceResolver();
            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            var gacDependencyResolver        = new GacDependencyResolver();
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            // Fix up dependencies
            foreach (var library in lookup.Values.ToList())
            {
                if (string.Equals(library.Type, LibraryTypes.Package) &&
                    !Directory.Exists(library.Path))
                {
                    // If the package path doesn't exist then mark this dependency as unresolved
                    library.Resolved = false;
                }

                library.Framework = library.Framework ?? context.TargetFramework;
                foreach (var dependency in library.Dependencies)
                {
                    LibraryDescription dep;
                    if (!lookup.TryGetValue(dependency.Name, out dep))
                    {
                        if (dependency.LibraryRange.IsGacOrFrameworkReference)
                        {
                            dep = referenceAssemblyDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
                                  gacDependencyResolver.GetDescription(dependency.LibraryRange, context.TargetFramework) ??
                                  unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);

                            dep.Framework           = context.TargetFramework;
                            lookup[dependency.Name] = dep;
                        }
                        else
                        {
                            dep = unresolvedDependencyProvider.GetDescription(dependency.LibraryRange, context.TargetFramework);
                            lookup[dependency.Name] = dep;
                        }
                    }

                    // REVIEW: This isn't quite correct but there's a many to one relationship here.
                    // Different ranges can resolve to this dependency but only one wins
                    dep.RequestedRange = dependency.LibraryRange;
                    dependency.Library = dep;
                }
            }

            var libraries = lookup.Values.ToList();

            context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);

            AddLockFileDiagnostics(context, result);

            // Clear all the temporary memory aggressively here if we don't care about reuse
            // e.g. runtime scenarios
            lookup.Clear();
        }