public BuildContext(CompilationEngine compilationEngine, Runtime.Project project, FrameworkName targetFramework, string configuration, string outputPath) { _project = project; _targetFramework = targetFramework; _configuration = configuration; _targetFrameworkFolder = VersionUtility.GetShortFrameworkName(_targetFramework); _outputPath = Path.Combine(outputPath, _targetFrameworkFolder); _libraryExporter = compilationEngine.CreateProjectExporter( _project, _targetFramework, _configuration); _libraryManager = _libraryExporter.LibraryManager; }
public Assembly Load(AssemblyName assemblyName) { var projectPath = Path.Combine(_path, assemblyName.Name); if (!Project.HasProjectFile(projectPath)) { return(null); } return(_cache.Get <Assembly>(assemblyName.Name, cacheContext => { var moduleContext = new ModuleLoaderContext( projectPath, _applicationEnvironment.RuntimeFramework); foreach (var lib in moduleContext.LibraryManager.GetLibraries()) { _libraryManager.AddLibrary(lib); } var engine = new CompilationEngine(new CompilationEngineContext( _applicationEnvironment, _assemblyLoadContextAccessor.Default, new CompilationCache())); var exporter = engine.CreateProjectExporter( moduleContext.Project, moduleContext.TargetFramework, _applicationEnvironment.Configuration); var exports = exporter.GetAllExports(moduleContext.Project.Name); foreach (var metadataReference in exports.MetadataReferences) { _libraryManager.AddMetadataReference(metadataReference); } return engine.LoadProject(moduleContext.Project, null, _assemblyLoadContextAccessor.Default); })); }
private DependencyInfo ResolveProjectDependencies(Project project, string configuration, FrameworkName frameworkName, int protocolVersion, List <string> updatedSearchPath) { var cacheKey = Tuple.Create("DependencyInfo", project.Name, configuration, frameworkName); return(_compilationEngine.CompilationCache.Cache.Get <DependencyInfo>(cacheKey, ctx => { var applicationHostContext = _applicationHostContextCreator(ctx, project, frameworkName); var libraryManager = applicationHostContext.LibraryManager; var libraryExporter = _compilationEngine.CreateProjectExporter(project, frameworkName, configuration); var info = new DependencyInfo { Dependencies = new Dictionary <string, DependencyDescription>(), ProjectReferences = new List <ProjectReference>(), References = new List <string>(), RawReferences = new Dictionary <string, byte[]>(), ExportedSourcesFiles = new List <string>(), Diagnostics = libraryManager.GetAllDiagnostics().ToList() }; var diagnosticSources = info.Diagnostics.ToLookup(diagnostic => diagnostic.Source); var projectCandiates = GetProjectCandidates(updatedSearchPath); foreach (var library in applicationHostContext.LibraryManager.GetLibraryDescriptions()) { var diagnostics = diagnosticSources[library].ToList(); var newDiagnostic = ValidateDependency(library, projectCandiates); if (newDiagnostic != null) { info.Diagnostics.Add(newDiagnostic); diagnostics.Add(newDiagnostic); } var description = CreateDependencyDescription(library, diagnostics, protocolVersion); info.Dependencies[description.Name] = description; if (string.Equals(library.Type, LibraryTypes.Project) && !string.Equals(library.Identity.Name, project.Name)) { var referencedProject = (ProjectDescription)library; var targetFrameworkInformation = referencedProject.TargetFrameworkInfo; // If this is an assembly reference then treat it like a file reference if (!string.IsNullOrEmpty(targetFrameworkInformation?.AssemblyPath) && string.IsNullOrEmpty(targetFrameworkInformation?.WrappedProject)) { string assemblyPath = GetProjectRelativeFullPath(referencedProject.Project, targetFrameworkInformation.AssemblyPath); info.References.Add(assemblyPath); description.Path = assemblyPath; description.Type = "Assembly"; } else { string wrappedProjectPath = null; if (!string.IsNullOrEmpty(targetFrameworkInformation?.WrappedProject) && referencedProject.Project != null) { wrappedProjectPath = GetProjectRelativeFullPath(referencedProject.Project, targetFrameworkInformation.WrappedProject); } info.ProjectReferences.Add(new ProjectReference { Name = referencedProject.Identity.Name, Framework = new FrameworkData { ShortName = VersionUtility.GetShortFrameworkName(library.Framework), FrameworkName = library.Framework.ToString(), FriendlyName = _frameworkReferenceResolver.GetFriendlyFrameworkName(library.Framework) }, Path = library.Path, WrappedProjectPath = wrappedProjectPath, Project = referencedProject.Project }); } } } var exportWithoutProjects = libraryExporter.GetNonProjectExports(project.Name); foreach (var reference in exportWithoutProjects.MetadataReferences) { var fileReference = reference as IMetadataFileReference; if (fileReference != null) { info.References.Add(fileReference.Path); } var embedded = reference as IMetadataEmbeddedReference; if (embedded != null) { info.RawReferences[embedded.Name] = embedded.Contents; } } foreach (var sourceFileReference in exportWithoutProjects.SourceReferences.OfType <ISourceFileReference>()) { info.ExportedSourcesFiles.Add(sourceFileReference.Path); } return info; })); }
private void Initialize(DefaultHostOptions options, IAssemblyLoadContextAccessor loadContextAccessor) { var applicationHostContext = new ApplicationHostContext { ProjectDirectory = _projectDirectory, RuntimeIdentifiers = _runtimeEnvironment.GetAllRuntimeIdentifiers(), TargetFramework = _targetFramework }; var libraries = ApplicationHostContext.GetRuntimeLibraries(applicationHostContext, throwOnInvalidLockFile: true); Logger.TraceInformation("[{0}]: Project path: {1}", GetType().Name, applicationHostContext.ProjectDirectory); Logger.TraceInformation("[{0}]: Project root: {1}", GetType().Name, applicationHostContext.RootDirectory); Logger.TraceInformation("[{0}]: Project configuration: {1}", GetType().Name, options.Configuration); Logger.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, applicationHostContext.PackagesDirectory); _applicationHostContext = applicationHostContext; _project = applicationHostContext.Project; #if FEATURE_DNX_MIN_VERSION_CHECK ValidateMinRuntimeVersion(libraries); #endif // 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. var hostEnvironment = PlatformServices.Default.Application; var applicationEnvironment = new ApplicationEnvironment(Project, _targetFramework, hostEnvironment); var compilationContext = new CompilationEngineContext( applicationEnvironment, _runtimeEnvironment, loadContextAccessor.Default, new CompilationCache()); var compilationEngine = new CompilationEngine(compilationContext); var runtimeLibraryExporter = new RuntimeLibraryExporter(() => compilationEngine.CreateProjectExporter(Project, _targetFramework, options.Configuration)); var runtimeLibraryManager = new RuntimeLibraryManager(applicationHostContext); // Default services _serviceProvider.Add(typeof(ILibraryExporter), runtimeLibraryExporter); _serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment); _serviceProvider.Add(typeof(IRuntimeEnvironment), PlatformServices.Default.Runtime); _serviceProvider.Add(typeof(ILibraryManager), runtimeLibraryManager); _serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), PlatformServices.Default.AssemblyLoadContextAccessor); _serviceProvider.Add(typeof(IAssemblyLoaderContainer), PlatformServices.Default.AssemblyLoaderContainer); PlatformServices.SetDefault(new ApplicationHostPlatformServices(PlatformServices.Default, applicationEnvironment, runtimeLibraryManager)); if (options.CompilationServerPort.HasValue) { // Change the project reference provider Project.DefaultCompiler = Project.DefaultDesignTimeCompiler; Project.DesignTimeCompilerPort = options.CompilationServerPort.Value; } // TODO: Dedupe this logic in the RuntimeLoadContext var projects = libraries.Where(p => p.Type == Runtime.LibraryTypes.Project) .ToDictionary(p => p.Identity.Name, p => (ProjectDescription)p); var assemblies = PackageDependencyProvider.ResolvePackageAssemblyPaths(libraries); // Configure Assembly loaders _loaders.Add(new ProjectAssemblyLoader(loadContextAccessor, compilationEngine, projects.Values, options.Configuration)); _loaders.Add(new PackageAssemblyLoader(loadContextAccessor, assemblies, libraries)); var compilerOptionsProvider = new CompilerOptionsProvider(projects); _serviceProvider.Add(typeof(ICompilerOptionsProvider), compilerOptionsProvider); CompilationServices.SetDefault( CompilationServices.Create( libraryExporter: runtimeLibraryExporter, compilerOptionsProvider: compilerOptionsProvider ) ); #if DNX451 PackageDependencyProvider.EnableLoadingNativeLibraries(libraries); #endif AddBreadcrumbs(libraries); }
private void Initialize(RuntimeOptions options, IServiceProvider hostServices, IAssemblyLoadContextAccessor loadContextAccessor, IFileWatcher fileWatcher) { var applicationHostContext = new ApplicationHostContext { ProjectDirectory = _projectDirectory, TargetFramework = _targetFramework }; ApplicationHostContext.Initialize(applicationHostContext); Logger.TraceInformation("[{0}]: Project path: {1}", GetType().Name, applicationHostContext.ProjectDirectory); Logger.TraceInformation("[{0}]: Project root: {1}", GetType().Name, applicationHostContext.RootDirectory); Logger.TraceInformation("[{0}]: Project configuration: {1}", GetType().Name, options.Configuration); Logger.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, applicationHostContext.PackagesDirectory); _libraryManager = applicationHostContext.LibraryManager; _project = applicationHostContext.Project; if (options.WatchFiles) { fileWatcher.OnChanged += _ => { _shutdown.RequestShutdownWaitForDebugger(); }; } // 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. var hostEnvironment = (IApplicationEnvironment)hostServices.GetService(typeof(IApplicationEnvironment)); var applicationEnvironment = new ApplicationEnvironment(Project, _targetFramework, options.Configuration, hostEnvironment); var compilationContext = new CompilationEngineContext(applicationEnvironment, loadContextAccessor.Default, new CompilationCache(), fileWatcher, new ProjectGraphProvider()); // Compilation services available only for runtime compilation compilationContext.AddCompilationService(typeof(RuntimeOptions), options); compilationContext.AddCompilationService(typeof(IApplicationShutdown), _shutdown); var compilationEngine = new CompilationEngine(compilationContext); // Default services _serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment); _serviceProvider.Add(typeof(ILibraryManager), _libraryManager); // TODO: Make this lazy _serviceProvider.Add(typeof(ILibraryExporter), compilationEngine.CreateProjectExporter(Project, _targetFramework, options.Configuration)); _serviceProvider.Add(typeof(IApplicationShutdown), _shutdown); _serviceProvider.Add(typeof(ICompilerOptionsProvider), new CompilerOptionsProvider(_libraryManager)); if (options.CompilationServerPort.HasValue) { // Change the project reference provider Project.DefaultCompiler = Project.DefaultDesignTimeCompiler; } CallContextServiceLocator.Locator.ServiceProvider = ServiceProvider; // Configure Assembly loaders _loaders.Add(new ProjectAssemblyLoader( loadContextAccessor, compilationEngine, _libraryManager)); _loaders.Add(new PackageAssemblyLoader(loadContextAccessor, _libraryManager)); }
public Assembly Load(AssemblyName assemblyName) { var reference = _libraryManager.GetMetadataReference(assemblyName.Name); if (reference != null && reference is MetadataFileReference) { var fileReference = (MetadataFileReference)reference; var assembly = _assemblyLoadContextAccessor .Default .LoadFile(fileReference.Path); return(assembly); } var projectPath = Path.Combine(_path, assemblyName.Name); if (!Project.HasProjectFile(projectPath)) { return(null); } var moduleContext = new ModuleLoaderContext( projectPath, _applicationEnvironment.RuntimeFramework); foreach (var lib in moduleContext.LibraryManager.GetLibraries()) { _libraryManager.AddLibrary(lib); } var engine = new CompilationEngine(new CompilationEngineContext( _applicationEnvironment, _runtimeEnvironment, _assemblyLoadContextAccessor.Default, _compilationCache)); var exporter = engine.CreateProjectExporter( moduleContext.Project, moduleContext.TargetFramework, _applicationEnvironment.Configuration); var exports = exporter.GetAllExports(moduleContext.Project.Name); foreach (var metadataReference in exports.MetadataReferences) { _libraryManager.AddMetadataReference(metadataReference); } var loadedProjectAssembly = engine.LoadProject( moduleContext.Project, _applicationEnvironment.RuntimeFramework, null, _assemblyLoadContextAccessor.Default, assemblyName); IList <LibraryDependency> flattenedList = moduleContext .Project .Dependencies .SelectMany(x => Flatten(x)) .Where(x => x.Library.Type == LibraryTypes.Package) .Distinct() .ToList(); foreach (var dependency in flattenedList) { foreach (var assemblyToLoad in dependency.Library.Assemblies) { Assembly.Load(new AssemblyName(assemblyToLoad)); } } return(loadedProjectAssembly); }
private void Initialize(RuntimeOptions options, IServiceProvider hostServices, IAssemblyLoadContextAccessor loadContextAccessor, IFileWatcher fileWatcher) { var applicationHostContext = new ApplicationHostContext { ProjectDirectory = _projectDirectory, RuntimeIdentifiers = _runtimeEnvironment.GetAllRuntimeIdentifiers(), TargetFramework = _targetFramework }; var libraries = ApplicationHostContext.GetRuntimeLibraries(applicationHostContext, throwOnInvalidLockFile: true); Logger.TraceInformation("[{0}]: Project path: {1}", GetType().Name, applicationHostContext.ProjectDirectory); Logger.TraceInformation("[{0}]: Project root: {1}", GetType().Name, applicationHostContext.RootDirectory); Logger.TraceInformation("[{0}]: Project configuration: {1}", GetType().Name, options.Configuration); Logger.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, applicationHostContext.PackagesDirectory); _applicationHostContext = applicationHostContext; _project = applicationHostContext.Project; ValidateMinRuntimeVersion(libraries); if (options.WatchFiles) { fileWatcher.OnChanged += _ => { _shutdown.RequestShutdownWaitForDebugger(); }; } // 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. var hostEnvironment = (IApplicationEnvironment)hostServices.GetService(typeof(IApplicationEnvironment)); var applicationEnvironment = new ApplicationEnvironment(Project, _targetFramework, options.Configuration, hostEnvironment); var compilationContext = new CompilationEngineContext( applicationEnvironment, _runtimeEnvironment, loadContextAccessor.Default, new CompilationCache(), fileWatcher); // Compilation services available only for runtime compilation compilationContext.AddCompilationService(typeof(RuntimeOptions), options); compilationContext.AddCompilationService(typeof(IApplicationShutdown), _shutdown); var compilationEngine = new CompilationEngine(compilationContext); // Default services _serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment); _serviceProvider.Add(typeof(ILibraryManager), new RuntimeLibraryManager(applicationHostContext)); _serviceProvider.Add(typeof(ILibraryExporter), new RuntimeLibraryExporter(() => compilationEngine.CreateProjectExporter(Project, _targetFramework, options.Configuration))); _serviceProvider.Add(typeof(IApplicationShutdown), _shutdown); if (options.CompilationServerPort.HasValue) { // Change the project reference provider Project.DefaultCompiler = Project.DefaultDesignTimeCompiler; } CallContextServiceLocator.Locator.ServiceProvider = ServiceProvider; // TODO: Dedupe this logic in the RuntimeLoadContext var projects = libraries.Where(p => p.Type == LibraryTypes.Project) .ToDictionary(p => p.Identity.Name, p => (ProjectDescription)p); var assemblies = PackageDependencyProvider.ResolvePackageAssemblyPaths(libraries); // Configure Assembly loaders _loaders.Add(new ProjectAssemblyLoader(loadContextAccessor, compilationEngine, projects.Values)); _loaders.Add(new PackageAssemblyLoader(loadContextAccessor, assemblies)); _serviceProvider.Add(typeof(ICompilerOptionsProvider), new CompilerOptionsProvider(projects)); AddBreadcrumbs(libraries); }
private List <MetadataReference> GetApplicationReferences() { var references = new List <MetadataReference>(); ILibraryExporter libraryExporter; if (_hostingEnvironment.IsDevelopment()) { Project project; if (!Project.TryGetProject(_environment.ApplicationBasePath, out project)) { return(references); } var engine = new CompilationEngine( new CompilationEngineContext( _environment, _runtimeEnvironment, _loader, new CompilationCache())); libraryExporter = engine.CreateProjectExporter( project, _environment.RuntimeFramework, _environment.Configuration); } else { libraryExporter = _libraryExporter; } // Get the MetadataReference for the executing application. If it's a Roslyn reference, // we can copy the references created when compiling the application to the Razor page being compiled. // This avoids performing expensive calls to MetadataReference.CreateFromImage. var libraryExport = libraryExporter.GetExport(_environment.ApplicationName); if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0) { Debug.Assert(libraryExport.MetadataReferences.Count == 1, "Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count); var roslynReference = libraryExport.MetadataReferences[0] as IRoslynMetadataReference; var compilationReference = roslynReference?.MetadataReference as CompilationReference; if (compilationReference != null) { references.AddRange(compilationReference.Compilation.References); references.Add(roslynReference.MetadataReference); references.AddRange(_libraryManager .GetAllMetadataReferences() .OfType <IRoslynMetadataReference>() .Select(x => x.MetadataReference)); return(references); } } var export = libraryExporter.GetAllExports(_environment.ApplicationName); foreach (var metadataReference in export.MetadataReferences) { // Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/... // Microsoft.Framework.Runtime.Roslyn/RoslynCompiler.cs#L164 // We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies // than the view engine needs (Microsoft.Framework.Runtime) for example references.Add(ConvertMetadataReference(metadataReference)); } return(references); }