public void SearchesLibraryInstancesForMethod() { var processor = new Service(); processor.PushLibraryInstance(new TypedValue(new SampleClass())); var runtime = new RuntimeLibrary { Processor = processor }; SampleClass.MethodCount = 0; runtime.Invoke(new TypedValue("stuff"), "samplemethod", new TreeList<string>()); Assert.AreEqual(1, SampleClass.MethodCount); }
private void ProcessRuntimeAssemblies(RuntimeLibrary runtimeLibrary, string pathToGlobalPackagesFolder, HashSet <RuntimeDependency> runtimeDepedencies) { foreach (var runtimeAssemblyGroup in runtimeLibrary.RuntimeAssemblyGroups.Where(rag => IsRelevantForCurrentRuntime(rag.Runtime))) { foreach (var assetPath in runtimeAssemblyGroup.AssetPaths) { var path = Path.Combine(runtimeLibrary.Path, assetPath); if (!path.EndsWith("_._")) { var fullPath = Path.Combine(pathToGlobalPackagesFolder, path); _logger.LogInformation(fullPath); runtimeDepedencies.Add(new RuntimeDependency(runtimeLibrary.Name, fullPath)); } } } }
private void AssemblyDependencyResolver(string path) { //AssemblyLoadContext.Default.LoadFromAssemblyName(assembly); var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path); var dependencyContext = DependencyContext.Load(assembly); var assemblyResolver = new CompositeCompilationAssemblyResolver (new ICompilationAssemblyResolver[] { new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(path)), new ReferenceAssemblyPathResolver(), new PackageCompilationAssemblyResolver() }); var loadContext = AssemblyLoadContext.GetLoadContext(assembly); dependencyContext?.RuntimeLibraries.ToList().ForEach(x => { var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray(); foreach (var compilationLibrary in dependencyContext.CompileLibraries.Where(y => y.Name.StartsWith("Jimu"))) { if (!loadedPaths.Select(z => Path.GetFileNameWithoutExtension(z)).ToList().Contains(compilationLibrary.Name, StringComparer.InvariantCultureIgnoreCase)) { Console.WriteLine($"dynamic load {compilationLibrary.Name}"); RuntimeLibrary library = dependencyContext.RuntimeLibraries.FirstOrDefault(runtime => runtime.Name == compilationLibrary.Name); var wrapper = new CompilationLibrary( library.Type, library.Name, library.Version, library.Hash, library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), library.Dependencies, library.Serviceable); var assemblies = new List <string>(); assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies); if (assemblies.Count > 0) { loadContext.LoadFromAssemblyPath(assemblies[0]); } } //DependencyDLL[library.Name] = cb; } }); }
/// <summary> /// Find and store native driver dependencies for later loading. /// </summary> /// <param name="lib"> The driver runtime library. </param> protected void StoreDriverNativeDependencies(RuntimeLibrary lib) { foreach (var dependency in lib.Dependencies) { RuntimeLibrary depLib = GetRuntimeLibrary(dependency.Name); if (depLib == null) { continue; // it's a "compileOnly" assembly } if (IsLibraryNative(depLib) && !NativeDependencies.Contains(dependency.Name)) { NativeDependencies.Add(GetNativeLibraryPath(depLib)); } StoreDriverNativeDependencies(depLib); // rec } }
private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name) { bool NamesMatch(RuntimeLibrary runtime) { return(string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase)); } var assembly = Assemblies.FirstOrDefault(i => string.Equals(i.GetName().Name, name.Name, StringComparison.OrdinalIgnoreCase)); if (assembly == null) { assembly = dependencyContexts.Select(i => { RuntimeLibrary library = i.RuntimeLibraries.FirstOrDefault(NamesMatch); if (library != null) { var wrapper = new CompilationLibrary( library.Type, library.Name, library.Version, library.Hash, library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), library.Dependencies, library.Serviceable); var assemblies = new List <string>(); this.assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies); if (assemblies.Count > 0) { return(AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblies[0])); } else { return(null); } } else { return(null); } }).FirstOrDefault(i => i != null); } return(assembly); }
public DependencyContext Create( string target = null, string runtime = null, bool? isPortable = null, CompilationOptions compilationOptions = null, CompilationLibrary[] compileLibraries = null, RuntimeLibrary[] runtimeLibraries = null, IReadOnlyList<KeyValuePair<string, string[]>> runtimeGraph = null) { return new DependencyContext( target ?? string.Empty, runtime ?? string.Empty, isPortable ?? false, compilationOptions ?? CompilationOptions.Default, compileLibraries ?? new CompilationLibrary[0], runtimeLibraries ?? new RuntimeLibrary[0], runtimeGraph ?? new KeyValuePair<string, string[]>[0] ); }
private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name) { bool NamesMatch(Library library) { return(string.Equals(library.Name, name.Name, StringComparison.OrdinalIgnoreCase)); } CompilationLibrary compilationLibrary = _dependencyContext .CompileLibraries.FirstOrDefault(NamesMatch); if (compilationLibrary == null) { RuntimeLibrary runtimeLibrary = _dependencyContext .RuntimeLibraries.FirstOrDefault(NamesMatch); if (runtimeLibrary != null) { compilationLibrary = new CompilationLibrary( runtimeLibrary.Type, runtimeLibrary.Name, runtimeLibrary.Version, runtimeLibrary.Hash, runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), runtimeLibrary.Dependencies, runtimeLibrary.Serviceable ); } } if (compilationLibrary != null) { var assemblies = new List <string>(); _assemblyResolver.TryResolveAssemblyPaths(compilationLibrary, assemblies); if (assemblies.Count > 0) { return(_loadContext.LoadFromAssemblyPath(assemblies[0])); } } return(null); }
private void Prepare(string source, IRuntimeLibrary runtimeLibrary = null, bool expectErrors = false, bool MinimizeNames = false) { IProjectContent project = new CSharpProjectContent(); var parser = new CSharpParser(); using (var rdr = new StringReader(source)) { var pf = new CSharpUnresolvedFile() { FileName = "File.cs" }; var syntaxTree = parser.Parse(rdr, pf.FileName); syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf)); project = project.AddOrUpdateFiles(pf); } project = project.AddAssemblyReferences(new[] { Files.Mscorlib, Files.Web, Files.Knockout }); _compilation = project.CreateCompilation(); var options = new CompilerOptions { MinimizeScript = MinimizeNames }; _errorReporter = new MockErrorReporter(!expectErrors); var s = new AttributeStore(_compilation, _errorReporter); s.RunAttributeCode(); _metadata = new MetadataImporter(_errorReporter, _compilation, s, options); _metadata.Prepare(_compilation.GetAllTypeDefinitions()); _allErrors = _errorReporter.AllMessages.ToList().AsReadOnly(); if (expectErrors) { Assert.That(_allErrors, Is.Not.Empty, "Compile should have generated errors"); } else { Assert.That(_allErrors, Is.Empty, "Compile should not generate errors"); } var rtl = new RuntimeLibrary(_metadata, _errorReporter, _compilation, new Namer(), s); }
protected Tuple <ICompilation, IOOPEmulator, List <JsType> > Compile(string source, IEnumerable <IAssemblyResource> resources = null, IErrorReporter errorReporter = null) { errorReporter = errorReporter ?? new MockErrorReporter(true); var sourceFile = new MockSourceFile("file.cs", source); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null, resources); var s = new AttributeStore(compilation.Compilation, errorReporter); RunAutomaticMetadataAttributeAppliers(s, compilation.Compilation); s.RunAttributeCode(); var md = new MetadataImporter(errorReporter, compilation.Compilation, s, new CompilerOptions()); var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n, s); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, errorReporter); var compiledTypes = compiler.Compile(compilation).ToList(); return(Tuple.Create(compilation.Compilation, (IOOPEmulator) new OOPEmulator(compilation.Compilation, md, rtl, n, new MockLinker(), s, errorReporter), compiledTypes)); }
protected override Assembly Load(AssemblyName assemblyName) { Assembly assembly = _onResolve(assemblyName); if (assembly != null) { return(assembly); } RuntimeLibrary runtimeLibrary = _dependencyContext.RuntimeLibraries.FirstOrDefault( lib => string.Equals(lib.Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase)); CompilationLibrary library; if (runtimeLibrary != null) { library = new CompilationLibrary( runtimeLibrary.Type, runtimeLibrary.Name, runtimeLibrary.Version, runtimeLibrary.Hash, runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), runtimeLibrary.Dependencies, runtimeLibrary.Serviceable); } else { library = _dependencyContext.CompileLibraries.FirstOrDefault(lib => string.Equals(lib.Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase)); } if (library == null) { return(null); } var assemblies = new List <string>(); _assemblyResolver.TryResolveAssemblyPaths(library, assemblies); return(assemblies.Count > 0 ? LoadFromAssemblyPath(assemblies[0]) : null); }
public DependencyContext Create( string target = null, string runtime = null, bool? isPortable = null, CompilationOptions compilationOptions = null, CompilationLibrary[] compileLibraries = null, RuntimeLibrary[] runtimeLibraries = null, IReadOnlyList<RuntimeFallbacks> runtimeGraph = null, string runtimeSignature = null) { return new DependencyContext(new TargetInfo( target ?? "DefaultTarget", runtime ?? string.Empty, runtimeSignature ?? string.Empty, isPortable ?? false), compilationOptions ?? CompilationOptions.Default, compileLibraries ?? new CompilationLibrary[0], runtimeLibraries ?? new RuntimeLibrary[0], runtimeGraph ?? new RuntimeFallbacks[0] ); }
private void ProcessNativeLibraries(RuntimeLibrary runtimeLibrary, string pathToGlobalPackagesFolder) { foreach (var nativeLibraryGroup in runtimeLibrary.NativeLibraryGroups.Where(nlg => IsRelevantForCurrentRuntime(nlg.Runtime))) { foreach (var assetPath in nativeLibraryGroup.AssetPaths) { var fullPath = Path.Combine(pathToGlobalPackagesFolder, runtimeLibrary.Path, assetPath); _logger.Verbose($"Loading native library from {fullPath}"); if (RuntimeHelper.GetPlatformIdentifier() == "win") { LoadLibrary(fullPath); } else { // Maybe something like this? // https://stackoverflow.com/questions/13461989/p-invoke-to-dynamically-loaded-library-on-mono } } } }
private void ProcessNativeLibraries(RuntimeLibrary runtimeLibrary, string[] nugetPackageFolders) { foreach (var nativeLibraryGroup in runtimeLibrary.NativeLibraryGroups.Where( nlg => RuntimeHelper.AppliesToCurrentRuntime(nlg.Runtime))) { foreach (var assetPath in nativeLibraryGroup.AssetPaths) { var fullPath = GetFullPath(Path.Combine(runtimeLibrary.Path, assetPath), nugetPackageFolders); _logger.Debug($"Loading native library from {fullPath}"); if (RuntimeHelper.IsWindows()) { LoadLibrary(fullPath); } else { // Maybe something like this? // https://stackoverflow.com/questions/13461989/p-invoke-to-dynamically-loaded-library-on-mono } } } }
/// <summary> /// Load the driver <see cref="Type"/> from a .deps file definition. /// </summary> /// <returns> The driver type. </returns> protected override Type TypeFromAssembly() { RuntimeLibrary lib = GetRuntimeLibrary(DriverTypeName.Assembly); StoreDriverNativeDependencies(lib); if (lib.Name == "Microsoft.Data.Sqlite" && lib.Version[0] == '2') { string extraPath2 = GetAssemblyPath(GetRuntimeLibrary("SQLitePCLRaw.provider.e_sqlite3.netstandard11")); _assemblyLoader.LoadFromAssemblyPath(extraPath2); _assemblyLoader.LoadFromAssemblyPath(@"C:/Program Files/dotnet/sdk/NuGetFallbackFolder/SQLitePCLRaw.bundle_green/1.1.7/lib/netcoreapp/SQLitePCLRaw.batteries_green.dll"); _assemblyLoader.LoadFromAssemblyPath(@"C:/Program Files/dotnet/sdk/NuGetFallbackFolder/SQLitePCLRaw.bundle_green/1.1.7/lib/netcoreapp/SQLitePCLRaw.batteries_v2.dll"); lib = GetRuntimeLibrary("Microsoft.Data.Sqlite.Core"); } string driverPath = GetAssemblyPath(lib); var driverAssembly = _assemblyLoader.LoadFromAssemblyPath(driverPath); return(driverAssembly.GetType(DriverTypeName.Type)); }
private static Assembly Context_Resolving(AssemblyLoadContext context, AssemblyName assemblyName) { // avoid loading *.resources dlls, because of: https://github.com/dotnet/coreclr/issues/8416 if (assemblyName.Name.EndsWith("resources")) { return(null); } // check cached assemblies first RuntimeLibrary library = DependencyContext.Default.RuntimeLibraries.FirstOrDefault(runtimeLibrary => runtimeLibrary.Name == assemblyName.Name); if (library != null) { return(context.LoadFromAssemblyName(new AssemblyName(library.Name))); } // try gac string[] foundDlls = Directory.GetFileSystemEntries(Environment.ExpandEnvironmentVariables("%windir%\\Microsoft.NET\\assembly"), $"{assemblyName.Name}.dll", SearchOption.AllDirectories); return(foundDlls.Any() ? context.LoadFromAssemblyPath(foundDlls[0]) : null); }
/// <summary> /// Return a list of dependencies, as well as compilation context data and compilation dependencies /// found in the deps file for a given assembly. /// </summary> /// <param name="assemblyName"> The name of the assembly to find in the deps file. </param> /// <returns> The runtime library or null if its a "compileOnly" assembly. </returns> /// <exception cref="EvolveException"> Throws an EvolveException when data of the given assembly name is not found in deps file. </exception> protected RuntimeLibrary GetRuntimeLibrary(string assemblyName) { RuntimeLibrary lib = ProjectDependencyContext.RuntimeLibraries.SingleOrDefault(x => x.Name.Equals(assemblyName, StringComparison.OrdinalIgnoreCase)); if (lib != null) { return(lib); } else { try { ProjectDependencyContext.CompileLibraries.Single(x => x.Name.Equals(assemblyName, StringComparison.OrdinalIgnoreCase)); return(null); } catch (Exception ex) { throw new EvolveException(string.Format(RuntimeLibraryLoadingError, assemblyName, _depsFile), ex); } } }
private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name) { bool NamesMatch(RuntimeLibrary runtime) { return(string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase)); } RuntimeLibrary library = this.dependencyContext.RuntimeLibraries.FirstOrDefault(NamesMatch); if (library != null) { var wrapper = new CompilationLibrary( library.Type, library.Name, library.Version, library.Hash, library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), library.Dependencies, library.Serviceable); var assemblies = new List <string>(); this.assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies); if (assemblies.Count > 0) { return(this.loadContext.LoadFromAssemblyPath(assemblies[0])); } } var file = Path.Combine(this.path, name.Name + ".dll"); if (File.Exists(file)) { return(this.loadContext.LoadFromAssemblyPath(file)); } return(null); }
private static Assembly Context_Resolving(AssemblyLoadContext context, AssemblyName assemblyName) { // avoid loading *.resources dlls, because of: https://github.com/dotnet/coreclr/issues/8416 if (assemblyName.Name.EndsWith("resources")) { return(null); } // check cached assemblies first RuntimeLibrary library = DependencyContext.Default.RuntimeLibraries.FirstOrDefault(runtimeLibrary => runtimeLibrary.Name == assemblyName.Name); if (library != null) { return(context.LoadFromAssemblyName(new AssemblyName(library.Name))); } // try known directories string found = context.Assemblies.Select(x => Path.Combine(Path.GetDirectoryName(x.Location), $"{assemblyName.Name}.dll")).Distinct().FirstOrDefault(File.Exists); if (found != null) { return(context.LoadFromAssemblyPath(found)); } // try the current directory string pathInCurrentDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{assemblyName.Name}.dll"); if (File.Exists(pathInCurrentDirectory)) { return(context.LoadFromAssemblyPath(found)); } // try gac found = Directory.GetFileSystemEntries(Environment.ExpandEnvironmentVariables("%windir%\\Microsoft.NET\\assembly"), $"{assemblyName.Name}.dll", SearchOption.AllDirectories).FirstOrDefault(); return(found == null ? null : context.LoadFromAssemblyPath(found)); }
public static Assembly OnResolving(AssemblyLoadContext context, AssemblyName dependency) { bool NamesMatch(RuntimeLibrary runtime) { return(string.Equals(runtime.Name, dependency.Name, StringComparison.OrdinalIgnoreCase)); } // avoid loading *.resources dlls, because of: https://github.com/dotnet/coreclr/issues/8416 if (dependency.Name.EndsWith("resources")) { return(null); } RuntimeLibrary library = allLibraries.FirstOrDefault(NamesMatch); if (library != null) { var wrapper = new CompilationLibrary( library.Type, library.Name, library.Version, library.Hash, library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), library.Dependencies, library.Serviceable); var assemblies = new List <string>(); assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies); if (assemblies.Count > 0) { return(AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblies[0])); } } return(null); }
protected string Process(string source, string[] typeNames = null, string entryPoint = null, IErrorReporter errorReporter = null) { bool assertNoErrors = errorReporter == null; errorReporter = errorReporter ?? new MockErrorReporter(true); var sourceFile = new MockSourceFile("file.cs", source); var n = new Namer(); var references = new[] { Files.Mscorlib }; var compilation = PreparedCompilation.CreateCompilation(new[] { sourceFile }, references, null);; var md = new MetadataImporter(errorReporter, compilation.Compilation, new CompilerOptions()); var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, errorReporter); var compiledTypes = compiler.Compile(compilation).ToList(); var obj = new OOPEmulator(compilation.Compilation, md, rtl, n, errorReporter); IMethod ep; if (entryPoint != null) { var type = compiledTypes.Single(c => c.CSharpTypeDefinition.FullName == entryPoint.Substring(0, entryPoint.IndexOf('.'))); ep = type.CSharpTypeDefinition.Methods.Single(m => m.FullName == entryPoint); } else { ep = null; } var rewritten = obj.Process(compiledTypes.Where(t => typeNames == null || typeNames.Contains(t.CSharpTypeDefinition.FullName)), ep); if (assertNoErrors) { Assert.That(((MockErrorReporter)errorReporter).AllMessages, Is.Empty, "Should not have errors"); } return(string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true)))); }
private Tuple <JsClass, MockErrorReporter> Compile(string source, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var er = new MockErrorReporter(!expectErrors); var n = new Namer(); var references = new[] { Mscorlib, QUnit }; var compilation = PreparedCompilation.CreateCompilation("Test", new[] { sourceFile }, references, null); var s = new AttributeStore(compilation.Compilation, er); s.RunAttributeCode(); var md = new MetadataImporter(er, compilation.Compilation, s, new CompilerOptions()); var rtl = new RuntimeLibrary(md, er, compilation.Compilation, n, s); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Compiler(md, n, rtl, er); var result = compiler.Compile(compilation).ToList(); Assert.That(result, Has.Count.EqualTo(1), "Should compile exactly one type"); Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors"); result = new TestRewriter(er, rtl, s).Rewrite(result).ToList(); Assert.That(result, Has.Count.EqualTo(1), "Should have one type after rewrite"); Assert.That(result[0], Is.InstanceOf <JsClass>(), "Compiled type should be a class after rewrite"); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty); } else { Assert.That(er.AllMessages, Is.Empty); } return(Tuple.Create((JsClass)result[0], er)); }
public Dependency(RuntimeLibrary library, DependencyClassification classification) { Library = library; Classification = classification; }
private static bool IsCandidateLibrary(RuntimeLibrary library, string assemblyName) { return(library.Name == (assemblyName) || library.Dependencies.Any(d => d.Name.StartsWith(assemblyName))); }
static bool IsCandidateLibrary(RuntimeLibrary library, string assemblyName) { return(string.Compare(assemblyName, library.Name, ignoreCase: true) == 0 || library.Dependencies.Any(d => d.Name.StartsWith(assemblyName))); }
private static bool IsCandidateCompilationLibrary(RuntimeLibrary compilationLibrary) { return(compilationLibrary.Name == "Specify" || compilationLibrary.Dependencies.Any(d => d.Name.StartsWith("Specify"))); }
private static bool LocaleInSeparatePackage(RuntimeLibrary localeLibrary) { return(localeLibrary != null); }
private void ExecuteCore() { var target = new TargetInfo(TargetFramework, RuntimeIdentifier, string.Empty, isPortable: false); var runtimeFiles = new List <RuntimeFile>(); var nativeFiles = new List <RuntimeFile>(); var resourceAssemblies = new List <ResourceAssembly>(); var platformManifest = new List <string>(); foreach (var reference in References) { var filePath = reference.ItemSpec; var fileName = Path.GetFileName(filePath); var fileVersion = FileUtilities.GetFileVersion(filePath)?.ToString() ?? string.Empty; var assemblyVersion = FileUtilities.TryGetAssemblyVersion(filePath); if (assemblyVersion == null) { var nativeFile = new RuntimeFile(fileName, null, fileVersion); nativeFiles.Add(nativeFile); platformManifest.Add($"{fileName}|{FrameworkName}||{fileVersion}"); } else { var runtimeFile = new RuntimeFile(fileName, fileVersion: fileVersion, assemblyVersion: assemblyVersion.ToString()); runtimeFiles.Add(runtimeFile); platformManifest.Add($"{fileName}|{FrameworkName}|{assemblyVersion}|{fileVersion}"); } } var runtimeLibrary = new RuntimeLibrary("package", RuntimePackageName, FrameworkVersion, hash: string.Empty, runtimeAssemblyGroups: new[] { new RuntimeAssetGroup(string.Empty, runtimeFiles) }, nativeLibraryGroups: new[] { new RuntimeAssetGroup(string.Empty, nativeFiles) }, Enumerable.Empty <ResourceAssembly>(), Array.Empty <Dependency>(), hashPath: null, path: $"{RuntimePackageName.ToLowerInvariant()}/{FrameworkVersion}", serviceable: true); var context = new DependencyContext(target, CompilationOptions.Default, Enumerable.Empty <CompilationLibrary>(), new[] { runtimeLibrary }, Enumerable.Empty <RuntimeFallbacks>()); Directory.CreateDirectory(Path.GetDirectoryName(DepsFilePath)); Directory.CreateDirectory(Path.GetDirectoryName(PlatformManifestOutputPath)); File.WriteAllText( PlatformManifestOutputPath, string.Join("\n", platformManifest.OrderBy(n => n)), Encoding.UTF8); try { using (var depsStream = File.Create(DepsFilePath)) { new DependencyContextWriter().Write(context, depsStream); } } catch (Exception ex) { // If there is a problem, ensure we don't write a partially complete version to disk. if (File.Exists(DepsFilePath)) { File.Delete(DepsFilePath); } Log.LogErrorFromException(ex); } }
public DependencyContext Build() { bool includeCompilationLibraries = _compilationOptions != null; IEnumerable <LockFileTargetLibrary> runtimeExports = _projectContext.GetRuntimeLibraries(_privateAssetPackageIds); IEnumerable <LockFileTargetLibrary> compilationExports = includeCompilationLibraries ? _projectContext.GetCompileLibraries(_privateAssetPackageIds) : Enumerable.Empty <LockFileTargetLibrary>(); var dependencyLookup = compilationExports .Concat(runtimeExports) .Distinct() .Select(library => new Dependency(library.Name, library.Version.ToString())) .ToDictionary(dependency => dependency.Name, StringComparer.OrdinalIgnoreCase); var libraryLookup = new LockFileLookup(_projectContext.LockFile); var runtimeSignature = GenerateRuntimeSignature(runtimeExports); RuntimeLibrary projectRuntimeLibrary = GetProjectRuntimeLibrary( _mainProjectInfo, _projectContext, dependencyLookup); IEnumerable <RuntimeLibrary> runtimeLibraries = new[] { projectRuntimeLibrary } .Concat(GetLibraries(runtimeExports, libraryLookup, dependencyLookup, runtime: true).Cast <RuntimeLibrary>()) .Concat(GetDirectReferenceRuntimeLibraries()); IEnumerable <CompilationLibrary> compilationLibraries; if (includeCompilationLibraries) { CompilationLibrary projectCompilationLibrary = GetProjectCompilationLibrary( _mainProjectInfo, _projectContext, dependencyLookup); compilationLibraries = new[] { projectCompilationLibrary } .Concat(GetFrameworkLibraries()) .Concat(GetLibraries(compilationExports, libraryLookup, dependencyLookup, runtime: false).Cast <CompilationLibrary>()) .Concat(GetDirectReferenceCompilationLibraries()); } else { compilationLibraries = Enumerable.Empty <CompilationLibrary>(); } var targetInfo = new TargetInfo( _projectContext.LockFileTarget.TargetFramework.DotNetFrameworkName, _projectContext.LockFileTarget.RuntimeIdentifier, runtimeSignature, _projectContext.IsPortable); return(new DependencyContext( targetInfo, _compilationOptions ?? CompilationOptions.Default, compilationLibraries, runtimeLibraries, new RuntimeFallbacks[] { })); }
protected virtual IEnumerable <Assembly> GetLibraryAssemblies(DependencyContext dependencyContext, RuntimeLibrary runtimeLibrary) { foreach (var assemblyName in runtimeLibrary.GetDefaultAssemblyNames(dependencyContext)) { var assembly = Assembly.Load(assemblyName); yield return(assembly); } }
private bool IsLibraryNative(RuntimeLibrary lib) => lib?.NativeLibraryGroups.Count > 0;
private string GetNativeAssemblyRelativePath(RuntimeLibrary lib) => GetRuntimeAssemblyAssetGroup(lib.Name, lib.NativeLibraryGroups)?.AssetPaths[0];
/// <summary> /// Define the path of a native library given the os and the process architecture the application is running. /// </summary> /// <param name="lib"> A resource library in the <see cref="_depsFile"/> </param> /// <returns> The library full path, or null if not found. </returns> private string GetNativeLibraryPath(RuntimeLibrary lib) { string path = GetNativeAssemblyRelativePath(lib); return(path == null ? null : Path.Combine(GetLibraryPackageFolderPath(lib), path)); }
/// <summary> /// Define the path of a managed assembly given the os and the process architecture the application is running. /// </summary> /// <param name="lib"> A resource assembly in the <see cref="_depsFile"/> </param> /// <returns> The assembly full path, or null if not found. </returns> protected virtual string GetAssemblyPath(RuntimeLibrary lib) { string path = GetAssemblyRelativePath(lib); return(path == null ? null : Path.Combine(GetLibraryPackageFolderPath(lib), path)); }